blob: ddb419b3ffc3a059ca3c329afb81bb3d3cee4f36 [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Garfield Tan0fc2fa72019-08-29 17:22:15 -070017#include "../dispatcher/InputDispatcher.h"
Michael Wrightd02c5b62014-02-10 15:10:22 -080018
Siarhei Vishniakou1c494c52021-08-11 20:25:01 -070019#include <android-base/properties.h>
Prabir Pradhana3ab87a2022-01-27 10:00:21 -080020#include <android-base/silent_death_test.h>
Garfield Tan1c7bc862020-01-28 13:24:04 -080021#include <android-base/stringprintf.h>
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -070022#include <android-base/thread_annotations.h>
Robert Carr803535b2018-08-02 16:38:15 -070023#include <binder/Binder.h>
Michael Wright8e9a8562022-02-09 13:44:29 +000024#include <fcntl.h>
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -080025#include <gmock/gmock.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080026#include <gtest/gtest.h>
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100027#include <input/Input.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080028#include <linux/input.h>
Prabir Pradhan07e05b62021-11-19 03:57:24 -080029#include <sys/epoll.h>
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100030
Garfield Tan1c7bc862020-01-28 13:24:04 -080031#include <cinttypes>
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -080032#include <compare>
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -070033#include <thread>
Garfield Tan1c7bc862020-01-28 13:24:04 -080034#include <unordered_set>
chaviwd1c23182019-12-20 18:44:56 -080035#include <vector>
Michael Wrightd02c5b62014-02-10 15:10:22 -080036
Garfield Tan1c7bc862020-01-28 13:24:04 -080037using android::base::StringPrintf;
chaviw3277faf2021-05-19 16:45:23 -050038using android::gui::FocusRequest;
39using android::gui::TouchOcclusionMode;
40using android::gui::WindowInfo;
41using android::gui::WindowInfoHandle;
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080042using android::os::InputEventInjectionResult;
43using android::os::InputEventInjectionSync;
Garfield Tan1c7bc862020-01-28 13:24:04 -080044
Garfield Tane84e6f92019-08-29 17:28:41 -070045namespace android::inputdispatcher {
Michael Wrightd02c5b62014-02-10 15:10:22 -080046
Dominik Laskowski2f01d772022-03-23 16:01:29 -070047using namespace ftl::flag_operators;
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -080048using testing::AllOf;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070049
Michael Wrightd02c5b62014-02-10 15:10:22 -080050// An arbitrary time value.
Prabir Pradhan5735a322022-04-11 17:23:34 +000051static constexpr nsecs_t ARBITRARY_TIME = 1234;
Michael Wrightd02c5b62014-02-10 15:10:22 -080052
53// An arbitrary device id.
Prabir Pradhan5735a322022-04-11 17:23:34 +000054static constexpr int32_t DEVICE_ID = 1;
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -080055static constexpr int32_t SECOND_DEVICE_ID = 2;
Michael Wrightd02c5b62014-02-10 15:10:22 -080056
Jeff Brownf086ddb2014-02-11 14:28:48 -080057// An arbitrary display id.
Arthur Hungabbb9d82021-09-01 14:52:30 +000058static constexpr int32_t DISPLAY_ID = ADISPLAY_ID_DEFAULT;
59static constexpr int32_t SECOND_DISPLAY_ID = 1;
Jeff Brownf086ddb2014-02-11 14:28:48 -080060
Siarhei Vishniakouf372b812023-02-14 18:06:51 -080061static constexpr int32_t ACTION_DOWN = AMOTION_EVENT_ACTION_DOWN;
62static constexpr int32_t ACTION_MOVE = AMOTION_EVENT_ACTION_MOVE;
63static constexpr int32_t ACTION_UP = AMOTION_EVENT_ACTION_UP;
64static constexpr int32_t ACTION_HOVER_ENTER = AMOTION_EVENT_ACTION_HOVER_ENTER;
Siarhei Vishniakoua235c042023-05-02 09:59:09 -070065static constexpr int32_t ACTION_HOVER_MOVE = AMOTION_EVENT_ACTION_HOVER_MOVE;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -080066static constexpr int32_t ACTION_HOVER_EXIT = AMOTION_EVENT_ACTION_HOVER_EXIT;
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -080067static constexpr int32_t ACTION_OUTSIDE = AMOTION_EVENT_ACTION_OUTSIDE;
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -080068static constexpr int32_t ACTION_CANCEL = AMOTION_EVENT_ACTION_CANCEL;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -080069/**
70 * The POINTER_DOWN(0) is an unusual, but valid, action. It just means that the new pointer in the
71 * MotionEvent is at the index 0 rather than 1 (or later). That is, the pointer id=0 (which is at
72 * index 0) is the new pointer going down. The same pointer could have been placed at a different
73 * index, and the action would become POINTER_1_DOWN, 2, etc..; these would all be valid. In
74 * general, we try to place pointer id = 0 at the index 0. Of course, this is not possible if
75 * pointer id=0 leaves but the pointer id=1 remains.
76 */
77static constexpr int32_t POINTER_0_DOWN =
78 AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -080079static constexpr int32_t POINTER_1_DOWN =
80 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +000081static constexpr int32_t POINTER_2_DOWN =
82 AMOTION_EVENT_ACTION_POINTER_DOWN | (2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +000083static constexpr int32_t POINTER_3_DOWN =
84 AMOTION_EVENT_ACTION_POINTER_DOWN | (3 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Arthur Hungc539dbb2022-12-08 07:45:36 +000085static constexpr int32_t POINTER_0_UP =
86 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -080087static constexpr int32_t POINTER_1_UP =
88 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Harry Cuttsb166c002023-05-09 13:06:05 +000089static constexpr int32_t POINTER_2_UP =
90 AMOTION_EVENT_ACTION_POINTER_UP | (2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -080091
Antonio Kantek15beb512022-06-13 22:35:41 +000092// The default pid and uid for windows created on the primary display by the test.
Prabir Pradhan5735a322022-04-11 17:23:34 +000093static constexpr int32_t WINDOW_PID = 999;
94static constexpr int32_t WINDOW_UID = 1001;
95
Antonio Kantek15beb512022-06-13 22:35:41 +000096// The default pid and uid for the windows created on the secondary display by the test.
97static constexpr int32_t SECONDARY_WINDOW_PID = 1010;
98static constexpr int32_t SECONDARY_WINDOW_UID = 1012;
99
Prabir Pradhan5735a322022-04-11 17:23:34 +0000100// The default policy flags to use for event injection by tests.
101static constexpr uint32_t DEFAULT_POLICY_FLAGS = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800102
Siarhei Vishniakou58cfc602020-12-14 23:21:30 +0000103// An arbitrary pid of the gesture monitor window
104static constexpr int32_t MONITOR_PID = 2001;
105
Siarhei Vishniakou289e9242022-02-15 14:50:16 -0800106static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 1000ms;
107
Arthur Hungc539dbb2022-12-08 07:45:36 +0000108static constexpr int expectedWallpaperFlags =
109 AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED;
110
Siarhei Vishniakou56e79092023-02-21 19:13:16 -0800111using ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID;
112
chaviwd1c23182019-12-20 18:44:56 -0800113struct PointF {
114 float x;
115 float y;
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -0800116 auto operator<=>(const PointF&) const = default;
chaviwd1c23182019-12-20 18:44:56 -0800117};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800118
Gang Wang342c9272020-01-13 13:15:04 -0500119/**
120 * Return a DOWN key event with KEYCODE_A.
121 */
122static KeyEvent getTestKeyEvent() {
123 KeyEvent event;
124
Garfield Tanfbe732e2020-01-24 11:26:14 -0800125 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
126 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
127 ARBITRARY_TIME, ARBITRARY_TIME);
Gang Wang342c9272020-01-13 13:15:04 -0500128 return event;
129}
130
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000131static void assertMotionAction(int32_t expectedAction, int32_t receivedAction) {
132 ASSERT_EQ(expectedAction, receivedAction)
133 << "expected " << MotionEvent::actionToString(expectedAction) << ", got "
134 << MotionEvent::actionToString(receivedAction);
135}
136
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -0800137MATCHER_P(WithMotionAction, action, "MotionEvent with specified action") {
138 bool matches = action == arg.getAction();
139 if (!matches) {
140 *result_listener << "expected action " << MotionEvent::actionToString(action)
141 << ", but got " << MotionEvent::actionToString(arg.getAction());
142 }
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -0800143 if (action == AMOTION_EVENT_ACTION_DOWN) {
144 if (!matches) {
145 *result_listener << "; ";
146 }
147 *result_listener << "downTime should match eventTime for ACTION_DOWN events";
148 matches &= arg.getDownTime() == arg.getEventTime();
149 }
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -0800150 if (action == AMOTION_EVENT_ACTION_CANCEL) {
151 if (!matches) {
152 *result_listener << "; ";
153 }
154 *result_listener << "expected FLAG_CANCELED to be set with ACTION_CANCEL, but was not set";
155 matches &= (arg.getFlags() & AMOTION_EVENT_FLAG_CANCELED) != 0;
156 }
157 return matches;
158}
159
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -0800160MATCHER_P(WithDownTime, downTime, "InputEvent with specified downTime") {
161 return arg.getDownTime() == downTime;
162}
163
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -0800164MATCHER_P(WithDisplayId, displayId, "InputEvent with specified displayId") {
165 return arg.getDisplayId() == displayId;
166}
167
Siarhei Vishniakouf372b812023-02-14 18:06:51 -0800168MATCHER_P(WithDeviceId, deviceId, "InputEvent with specified deviceId") {
169 return arg.getDeviceId() == deviceId;
170}
171
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -0800172MATCHER_P(WithSource, source, "InputEvent with specified source") {
173 *result_listener << "expected source " << inputEventSourceToString(source) << ", but got "
174 << inputEventSourceToString(arg.getSource());
175 return arg.getSource() == source;
176}
177
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -0800178MATCHER_P(WithFlags, flags, "InputEvent with specified flags") {
179 return arg.getFlags() == flags;
180}
181
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -0800182MATCHER_P2(WithCoords, x, y, "MotionEvent with specified coordinates") {
183 if (arg.getPointerCount() != 1) {
184 *result_listener << "Expected 1 pointer, got " << arg.getPointerCount();
185 return false;
186 }
Harry Cutts33476232023-01-30 19:57:29 +0000187 return arg.getX(/*pointerIndex=*/0) == x && arg.getY(/*pointerIndex=*/0) == y;
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -0800188}
189
Siarhei Vishniakouf372b812023-02-14 18:06:51 -0800190MATCHER_P(WithPointerCount, pointerCount, "MotionEvent with specified number of pointers") {
191 return arg.getPointerCount() == pointerCount;
192}
193
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -0800194MATCHER_P(WithPointers, pointers, "MotionEvent with specified pointers") {
195 // Build a map for the received pointers, by pointer id
196 std::map<int32_t /*pointerId*/, PointF> actualPointers;
197 for (size_t pointerIndex = 0; pointerIndex < arg.getPointerCount(); pointerIndex++) {
198 const int32_t pointerId = arg.getPointerId(pointerIndex);
199 actualPointers[pointerId] = {arg.getX(pointerIndex), arg.getY(pointerIndex)};
200 }
201 return pointers == actualPointers;
202}
203
Michael Wrightd02c5b62014-02-10 15:10:22 -0800204// --- FakeInputDispatcherPolicy ---
205
206class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface {
207 InputDispatcherConfiguration mConfig;
208
Prabir Pradhanedd96402022-02-15 01:46:16 -0800209 using AnrResult = std::pair<sp<IBinder>, int32_t /*pid*/>;
210
Michael Wrightd02c5b62014-02-10 15:10:22 -0800211public:
Prabir Pradhana41d2442023-04-20 21:30:40 +0000212 FakeInputDispatcherPolicy() = default;
213 virtual ~FakeInputDispatcherPolicy() = default;
Jackal Guof9696682018-10-05 12:23:23 +0800214
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800215 void assertFilterInputEventWasCalled(const NotifyKeyArgs& args) {
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700216 assertFilterInputEventWasCalledInternal([&args](const InputEvent& event) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700217 ASSERT_EQ(event.getType(), InputEventType::KEY);
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700218 EXPECT_EQ(event.getDisplayId(), args.displayId);
219
220 const auto& keyEvent = static_cast<const KeyEvent&>(event);
221 EXPECT_EQ(keyEvent.getEventTime(), args.eventTime);
222 EXPECT_EQ(keyEvent.getAction(), args.action);
223 });
Jackal Guof9696682018-10-05 12:23:23 +0800224 }
225
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700226 void assertFilterInputEventWasCalled(const NotifyMotionArgs& args, vec2 point) {
227 assertFilterInputEventWasCalledInternal([&](const InputEvent& event) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700228 ASSERT_EQ(event.getType(), InputEventType::MOTION);
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700229 EXPECT_EQ(event.getDisplayId(), args.displayId);
230
231 const auto& motionEvent = static_cast<const MotionEvent&>(event);
232 EXPECT_EQ(motionEvent.getEventTime(), args.eventTime);
233 EXPECT_EQ(motionEvent.getAction(), args.action);
Prabir Pradhan00e029d2023-03-09 20:11:09 +0000234 EXPECT_NEAR(motionEvent.getX(0), point.x, MotionEvent::ROUNDING_PRECISION);
235 EXPECT_NEAR(motionEvent.getY(0), point.y, MotionEvent::ROUNDING_PRECISION);
236 EXPECT_NEAR(motionEvent.getRawX(0), point.x, MotionEvent::ROUNDING_PRECISION);
237 EXPECT_NEAR(motionEvent.getRawY(0), point.y, MotionEvent::ROUNDING_PRECISION);
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700238 });
Jackal Guof9696682018-10-05 12:23:23 +0800239 }
240
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700241 void assertFilterInputEventWasNotCalled() {
242 std::scoped_lock lock(mLock);
243 ASSERT_EQ(nullptr, mFilteredEvent);
244 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800245
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800246 void assertNotifyConfigurationChangedWasCalled(nsecs_t when) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700247 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800248 ASSERT_TRUE(mConfigurationChangedTime)
249 << "Timed out waiting for configuration changed call";
250 ASSERT_EQ(*mConfigurationChangedTime, when);
251 mConfigurationChangedTime = std::nullopt;
252 }
253
254 void assertNotifySwitchWasCalled(const NotifySwitchArgs& args) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700255 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800256 ASSERT_TRUE(mLastNotifySwitch);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800257 // We do not check id because it is not exposed to the policy
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800258 EXPECT_EQ(args.eventTime, mLastNotifySwitch->eventTime);
259 EXPECT_EQ(args.policyFlags, mLastNotifySwitch->policyFlags);
260 EXPECT_EQ(args.switchValues, mLastNotifySwitch->switchValues);
261 EXPECT_EQ(args.switchMask, mLastNotifySwitch->switchMask);
262 mLastNotifySwitch = std::nullopt;
263 }
264
chaviwfd6d3512019-03-25 13:23:49 -0700265 void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700266 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800267 ASSERT_EQ(touchedToken, mOnPointerDownToken);
268 mOnPointerDownToken.clear();
269 }
270
271 void assertOnPointerDownWasNotCalled() {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700272 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800273 ASSERT_TRUE(mOnPointerDownToken == nullptr)
274 << "Expected onPointerDownOutsideFocus to not have been called";
chaviwfd6d3512019-03-25 13:23:49 -0700275 }
276
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700277 // This function must be called soon after the expected ANR timer starts,
278 // because we are also checking how much time has passed.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500279 void assertNotifyNoFocusedWindowAnrWasCalled(
Chris Yea209fde2020-07-22 13:54:51 -0700280 std::chrono::nanoseconds timeout,
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500281 const std::shared_ptr<InputApplicationHandle>& expectedApplication) {
Prabir Pradhanedd96402022-02-15 01:46:16 -0800282 std::unique_lock lock(mLock);
283 android::base::ScopedLockAssertion assumeLocked(mLock);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500284 std::shared_ptr<InputApplicationHandle> application;
Prabir Pradhanedd96402022-02-15 01:46:16 -0800285 ASSERT_NO_FATAL_FAILURE(
286 application = getAnrTokenLockedInterruptible(timeout, mAnrApplications, lock));
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500287 ASSERT_EQ(expectedApplication, application);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700288 }
289
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000290 void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout,
Prabir Pradhanedd96402022-02-15 01:46:16 -0800291 const sp<WindowInfoHandle>& window) {
292 LOG_ALWAYS_FATAL_IF(window == nullptr, "window should not be null");
293 assertNotifyWindowUnresponsiveWasCalled(timeout, window->getToken(),
294 window->getInfo()->ownerPid);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500295 }
296
Prabir Pradhanedd96402022-02-15 01:46:16 -0800297 void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout,
298 const sp<IBinder>& expectedToken,
299 int32_t expectedPid) {
300 std::unique_lock lock(mLock);
301 android::base::ScopedLockAssertion assumeLocked(mLock);
302 AnrResult result;
303 ASSERT_NO_FATAL_FAILURE(result =
304 getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock));
305 const auto& [token, pid] = result;
306 ASSERT_EQ(expectedToken, token);
307 ASSERT_EQ(expectedPid, pid);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500308 }
309
Prabir Pradhanedd96402022-02-15 01:46:16 -0800310 /** Wrap call with ASSERT_NO_FATAL_FAILURE() to ensure the return value is valid. */
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000311 sp<IBinder> getUnresponsiveWindowToken(std::chrono::nanoseconds timeout) {
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500312 std::unique_lock lock(mLock);
313 android::base::ScopedLockAssertion assumeLocked(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800314 AnrResult result = getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock);
315 const auto& [token, _] = result;
316 return token;
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000317 }
318
Prabir Pradhanedd96402022-02-15 01:46:16 -0800319 void assertNotifyWindowResponsiveWasCalled(const sp<IBinder>& expectedToken,
320 int32_t expectedPid) {
321 std::unique_lock lock(mLock);
322 android::base::ScopedLockAssertion assumeLocked(mLock);
323 AnrResult result;
324 ASSERT_NO_FATAL_FAILURE(
325 result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock));
326 const auto& [token, pid] = result;
327 ASSERT_EQ(expectedToken, token);
328 ASSERT_EQ(expectedPid, pid);
329 }
330
331 /** Wrap call with ASSERT_NO_FATAL_FAILURE() to ensure the return value is valid. */
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000332 sp<IBinder> getResponsiveWindowToken() {
333 std::unique_lock lock(mLock);
334 android::base::ScopedLockAssertion assumeLocked(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800335 AnrResult result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock);
336 const auto& [token, _] = result;
337 return token;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700338 }
339
340 void assertNotifyAnrWasNotCalled() {
341 std::scoped_lock lock(mLock);
342 ASSERT_TRUE(mAnrApplications.empty());
Prabir Pradhanedd96402022-02-15 01:46:16 -0800343 ASSERT_TRUE(mAnrWindows.empty());
344 ASSERT_TRUE(mResponsiveWindows.empty())
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500345 << "ANR was not called, but please also consume the 'connection is responsive' "
346 "signal";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700347 }
348
Garfield Tan1c7bc862020-01-28 13:24:04 -0800349 void setKeyRepeatConfiguration(nsecs_t timeout, nsecs_t delay) {
350 mConfig.keyRepeatTimeout = timeout;
351 mConfig.keyRepeatDelay = delay;
352 }
353
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000354 PointerCaptureRequest assertSetPointerCaptureCalled(bool enabled) {
Prabir Pradhan99987712020-11-10 18:43:05 -0800355 std::unique_lock lock(mLock);
356 base::ScopedLockAssertion assumeLocked(mLock);
357
358 if (!mPointerCaptureChangedCondition.wait_for(lock, 100ms,
359 [this, enabled]() REQUIRES(mLock) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000360 return mPointerCaptureRequest->enable ==
Prabir Pradhan99987712020-11-10 18:43:05 -0800361 enabled;
362 })) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000363 ADD_FAILURE() << "Timed out waiting for setPointerCapture(" << enabled
364 << ") to be called.";
365 return {};
Prabir Pradhan99987712020-11-10 18:43:05 -0800366 }
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000367 auto request = *mPointerCaptureRequest;
368 mPointerCaptureRequest.reset();
369 return request;
Prabir Pradhan99987712020-11-10 18:43:05 -0800370 }
371
372 void assertSetPointerCaptureNotCalled() {
373 std::unique_lock lock(mLock);
374 base::ScopedLockAssertion assumeLocked(mLock);
375
376 if (mPointerCaptureChangedCondition.wait_for(lock, 100ms) != std::cv_status::timeout) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000377 FAIL() << "Expected setPointerCapture(request) to not be called, but was called. "
Prabir Pradhan99987712020-11-10 18:43:05 -0800378 "enabled = "
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000379 << std::to_string(mPointerCaptureRequest->enable);
Prabir Pradhan99987712020-11-10 18:43:05 -0800380 }
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000381 mPointerCaptureRequest.reset();
Prabir Pradhan99987712020-11-10 18:43:05 -0800382 }
383
arthurhungf452d0b2021-01-06 00:19:52 +0800384 void assertDropTargetEquals(const sp<IBinder>& targetToken) {
385 std::scoped_lock lock(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800386 ASSERT_TRUE(mNotifyDropWindowWasCalled);
arthurhungf452d0b2021-01-06 00:19:52 +0800387 ASSERT_EQ(targetToken, mDropTargetWindowToken);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800388 mNotifyDropWindowWasCalled = false;
arthurhungf452d0b2021-01-06 00:19:52 +0800389 }
390
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800391 void assertNotifyInputChannelBrokenWasCalled(const sp<IBinder>& token) {
392 std::unique_lock lock(mLock);
393 base::ScopedLockAssertion assumeLocked(mLock);
394 std::optional<sp<IBinder>> receivedToken =
395 getItemFromStorageLockedInterruptible(100ms, mBrokenInputChannels, lock,
396 mNotifyInputChannelBroken);
397 ASSERT_TRUE(receivedToken.has_value());
398 ASSERT_EQ(token, *receivedToken);
399 }
400
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800401 /**
402 * Set policy timeout. A value of zero means next key will not be intercepted.
403 */
404 void setInterceptKeyTimeout(std::chrono::milliseconds timeout) {
405 mInterceptKeyTimeout = timeout;
406 }
407
Josep del Riob3981622023-04-18 15:49:45 +0000408 void assertUserActivityPoked() {
409 std::scoped_lock lock(mLock);
410 ASSERT_TRUE(mPokedUserActivity) << "Expected user activity to have been poked";
411 }
412
413 void assertUserActivityNotPoked() {
414 std::scoped_lock lock(mLock);
415 ASSERT_FALSE(mPokedUserActivity) << "Expected user activity not to have been poked";
416 }
417
Michael Wrightd02c5b62014-02-10 15:10:22 -0800418private:
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700419 std::mutex mLock;
420 std::unique_ptr<InputEvent> mFilteredEvent GUARDED_BY(mLock);
421 std::optional<nsecs_t> mConfigurationChangedTime GUARDED_BY(mLock);
422 sp<IBinder> mOnPointerDownToken GUARDED_BY(mLock);
423 std::optional<NotifySwitchArgs> mLastNotifySwitch GUARDED_BY(mLock);
Jackal Guof9696682018-10-05 12:23:23 +0800424
Prabir Pradhan99987712020-11-10 18:43:05 -0800425 std::condition_variable mPointerCaptureChangedCondition;
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000426
427 std::optional<PointerCaptureRequest> mPointerCaptureRequest GUARDED_BY(mLock);
Prabir Pradhan99987712020-11-10 18:43:05 -0800428
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700429 // ANR handling
Chris Yea209fde2020-07-22 13:54:51 -0700430 std::queue<std::shared_ptr<InputApplicationHandle>> mAnrApplications GUARDED_BY(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800431 std::queue<AnrResult> mAnrWindows GUARDED_BY(mLock);
432 std::queue<AnrResult> mResponsiveWindows GUARDED_BY(mLock);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700433 std::condition_variable mNotifyAnr;
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800434 std::queue<sp<IBinder>> mBrokenInputChannels GUARDED_BY(mLock);
435 std::condition_variable mNotifyInputChannelBroken;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700436
arthurhungf452d0b2021-01-06 00:19:52 +0800437 sp<IBinder> mDropTargetWindowToken GUARDED_BY(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800438 bool mNotifyDropWindowWasCalled GUARDED_BY(mLock) = false;
Josep del Riob3981622023-04-18 15:49:45 +0000439 bool mPokedUserActivity GUARDED_BY(mLock) = false;
arthurhungf452d0b2021-01-06 00:19:52 +0800440
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800441 std::chrono::milliseconds mInterceptKeyTimeout = 0ms;
442
Prabir Pradhanedd96402022-02-15 01:46:16 -0800443 // All three ANR-related callbacks behave the same way, so we use this generic function to wait
444 // for a specific container to become non-empty. When the container is non-empty, return the
445 // first entry from the container and erase it.
446 template <class T>
447 T getAnrTokenLockedInterruptible(std::chrono::nanoseconds timeout, std::queue<T>& storage,
448 std::unique_lock<std::mutex>& lock) REQUIRES(mLock) {
449 // If there is an ANR, Dispatcher won't be idle because there are still events
450 // in the waitQueue that we need to check on. So we can't wait for dispatcher to be idle
451 // before checking if ANR was called.
452 // Since dispatcher is not guaranteed to call notifyNoFocusedWindowAnr right away, we need
453 // to provide it some time to act. 100ms seems reasonable.
454 std::chrono::duration timeToWait = timeout + 100ms; // provide some slack
455 const std::chrono::time_point start = std::chrono::steady_clock::now();
456 std::optional<T> token =
457 getItemFromStorageLockedInterruptible(timeToWait, storage, lock, mNotifyAnr);
458 if (!token.has_value()) {
459 ADD_FAILURE() << "Did not receive the ANR callback";
460 return {};
461 }
462
463 const std::chrono::duration waited = std::chrono::steady_clock::now() - start;
464 // Ensure that the ANR didn't get raised too early. We can't be too strict here because
465 // the dispatcher started counting before this function was called
466 if (std::chrono::abs(timeout - waited) > 100ms) {
467 ADD_FAILURE() << "ANR was raised too early or too late. Expected "
468 << std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count()
469 << "ms, but waited "
470 << std::chrono::duration_cast<std::chrono::milliseconds>(waited).count()
471 << "ms instead";
472 }
473 return *token;
474 }
475
476 template <class T>
477 std::optional<T> getItemFromStorageLockedInterruptible(std::chrono::nanoseconds timeout,
478 std::queue<T>& storage,
479 std::unique_lock<std::mutex>& lock,
480 std::condition_variable& condition)
481 REQUIRES(mLock) {
482 condition.wait_for(lock, timeout,
483 [&storage]() REQUIRES(mLock) { return !storage.empty(); });
484 if (storage.empty()) {
485 ADD_FAILURE() << "Did not receive the expected callback";
486 return std::nullopt;
487 }
488 T item = storage.front();
489 storage.pop();
490 return std::make_optional(item);
491 }
492
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600493 void notifyConfigurationChanged(nsecs_t when) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700494 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800495 mConfigurationChangedTime = when;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800496 }
497
Prabir Pradhanedd96402022-02-15 01:46:16 -0800498 void notifyWindowUnresponsive(const sp<IBinder>& connectionToken, std::optional<int32_t> pid,
499 const std::string&) override {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700500 std::scoped_lock lock(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800501 ASSERT_TRUE(pid.has_value());
502 mAnrWindows.push({connectionToken, *pid});
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700503 mNotifyAnr.notify_all();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500504 }
505
Prabir Pradhanedd96402022-02-15 01:46:16 -0800506 void notifyWindowResponsive(const sp<IBinder>& connectionToken,
507 std::optional<int32_t> pid) override {
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500508 std::scoped_lock lock(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800509 ASSERT_TRUE(pid.has_value());
510 mResponsiveWindows.push({connectionToken, *pid});
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500511 mNotifyAnr.notify_all();
512 }
513
514 void notifyNoFocusedWindowAnr(
515 const std::shared_ptr<InputApplicationHandle>& applicationHandle) override {
516 std::scoped_lock lock(mLock);
517 mAnrApplications.push(applicationHandle);
518 mNotifyAnr.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800519 }
520
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800521 void notifyInputChannelBroken(const sp<IBinder>& connectionToken) override {
522 std::scoped_lock lock(mLock);
523 mBrokenInputChannels.push(connectionToken);
524 mNotifyInputChannelBroken.notify_all();
525 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800526
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600527 void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {}
Robert Carr740167f2018-10-11 19:03:41 -0700528
Chris Yef59a2f42020-10-16 12:55:26 -0700529 void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType,
530 InputDeviceSensorAccuracy accuracy, nsecs_t timestamp,
531 const std::vector<float>& values) override {}
532
533 void notifySensorAccuracy(int deviceId, InputDeviceSensorType sensorType,
534 InputDeviceSensorAccuracy accuracy) override {}
Bernardo Rufino2e1f6512020-10-08 13:42:07 +0000535
Chris Yefb552902021-02-03 17:18:37 -0800536 void notifyVibratorState(int32_t deviceId, bool isOn) override {}
537
Prabir Pradhana41d2442023-04-20 21:30:40 +0000538 InputDispatcherConfiguration getDispatcherConfiguration() override { return mConfig; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800539
Prabir Pradhana41d2442023-04-20 21:30:40 +0000540 bool filterInputEvent(const InputEvent& inputEvent, uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700541 std::scoped_lock lock(mLock);
Prabir Pradhana41d2442023-04-20 21:30:40 +0000542 switch (inputEvent.getType()) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700543 case InputEventType::KEY: {
Prabir Pradhana41d2442023-04-20 21:30:40 +0000544 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(inputEvent);
545 mFilteredEvent = std::make_unique<KeyEvent>(keyEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800546 break;
547 }
548
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700549 case InputEventType::MOTION: {
Prabir Pradhana41d2442023-04-20 21:30:40 +0000550 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(inputEvent);
551 mFilteredEvent = std::make_unique<MotionEvent>(motionEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800552 break;
553 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700554 default: {
555 ADD_FAILURE() << "Should only filter keys or motions";
556 break;
557 }
Jackal Guof9696682018-10-05 12:23:23 +0800558 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800559 return true;
560 }
561
Prabir Pradhana41d2442023-04-20 21:30:40 +0000562 void interceptKeyBeforeQueueing(const KeyEvent& inputEvent, uint32_t&) override {
563 if (inputEvent.getAction() == AKEY_EVENT_ACTION_UP) {
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800564 // Clear intercept state when we handled the event.
565 mInterceptKeyTimeout = 0ms;
566 }
567 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800568
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600569 void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800570
Prabir Pradhana41d2442023-04-20 21:30:40 +0000571 nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent&, uint32_t) override {
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800572 nsecs_t delay = std::chrono::nanoseconds(mInterceptKeyTimeout).count();
573 // Clear intercept state so we could dispatch the event in next wake.
574 mInterceptKeyTimeout = 0ms;
575 return delay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800576 }
577
Prabir Pradhana41d2442023-04-20 21:30:40 +0000578 std::optional<KeyEvent> dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent&,
579 uint32_t) override {
580 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800581 }
582
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600583 void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask,
584 uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700585 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800586 /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is
587 * essentially a passthrough for notifySwitch.
588 */
Harry Cutts33476232023-01-30 19:57:29 +0000589 mLastNotifySwitch = NotifySwitchArgs(/*id=*/1, when, policyFlags, switchValues, switchMask);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800590 }
591
Josep del Riob3981622023-04-18 15:49:45 +0000592 void pokeUserActivity(nsecs_t, int32_t, int32_t) override {
593 std::scoped_lock lock(mLock);
594 mPokedUserActivity = true;
595 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800596
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600597 void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700598 std::scoped_lock lock(mLock);
chaviwfd6d3512019-03-25 13:23:49 -0700599 mOnPointerDownToken = newToken;
600 }
601
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000602 void setPointerCapture(const PointerCaptureRequest& request) override {
Prabir Pradhan99987712020-11-10 18:43:05 -0800603 std::scoped_lock lock(mLock);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000604 mPointerCaptureRequest = {request};
Prabir Pradhan99987712020-11-10 18:43:05 -0800605 mPointerCaptureChangedCondition.notify_all();
606 }
607
arthurhungf452d0b2021-01-06 00:19:52 +0800608 void notifyDropWindow(const sp<IBinder>& token, float x, float y) override {
609 std::scoped_lock lock(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800610 mNotifyDropWindowWasCalled = true;
arthurhungf452d0b2021-01-06 00:19:52 +0800611 mDropTargetWindowToken = token;
612 }
613
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700614 void assertFilterInputEventWasCalledInternal(
615 const std::function<void(const InputEvent&)>& verify) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700616 std::scoped_lock lock(mLock);
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -0800617 ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called.";
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700618 verify(*mFilteredEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800619 mFilteredEvent = nullptr;
Jackal Guof9696682018-10-05 12:23:23 +0800620 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800621};
622
Michael Wrightd02c5b62014-02-10 15:10:22 -0800623// --- InputDispatcherTest ---
624
625class InputDispatcherTest : public testing::Test {
626protected:
Prabir Pradhana41d2442023-04-20 21:30:40 +0000627 std::unique_ptr<FakeInputDispatcherPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700628 std::unique_ptr<InputDispatcher> mDispatcher;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800629
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000630 void SetUp() override {
Prabir Pradhana41d2442023-04-20 21:30:40 +0000631 mFakePolicy = std::make_unique<FakeInputDispatcherPolicy>();
632 mDispatcher = std::make_unique<InputDispatcher>(*mFakePolicy, STALE_EVENT_TIMEOUT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800633 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000634 // Start InputDispatcher thread
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700635 ASSERT_EQ(OK, mDispatcher->start());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800636 }
637
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000638 void TearDown() override {
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700639 ASSERT_EQ(OK, mDispatcher->stop());
Prabir Pradhana41d2442023-04-20 21:30:40 +0000640 mFakePolicy.reset();
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700641 mDispatcher.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800642 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700643
644 /**
645 * Used for debugging when writing the test
646 */
647 void dumpDispatcherState() {
648 std::string dump;
649 mDispatcher->dump(dump);
650 std::stringstream ss(dump);
651 std::string to;
652
653 while (std::getline(ss, to, '\n')) {
654 ALOGE("%s", to.c_str());
655 }
656 }
Vishnu Nair958da932020-08-21 17:12:37 -0700657
Chavi Weingarten847e8512023-03-29 00:26:09 +0000658 void setFocusedWindow(const sp<WindowInfoHandle>& window) {
Vishnu Nair958da932020-08-21 17:12:37 -0700659 FocusRequest request;
660 request.token = window->getToken();
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +0000661 request.windowName = window->getName();
Vishnu Nair958da932020-08-21 17:12:37 -0700662 request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
663 request.displayId = window->getInfo()->displayId;
664 mDispatcher->setFocusedWindow(request);
665 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800666};
667
Michael Wrightd02c5b62014-02-10 15:10:22 -0800668TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) {
669 KeyEvent event;
670
671 // Rejects undefined key actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800672 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
673 INVALID_HMAC,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600674 /*action*/ -1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME,
675 ARBITRARY_TIME);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800676 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000677 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000678 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800679 << "Should reject key events with undefined action.";
680
681 // Rejects ACTION_MULTIPLE since it is not supported despite being defined in the API.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800682 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
683 INVALID_HMAC, AKEY_EVENT_ACTION_MULTIPLE, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600684 ARBITRARY_TIME, ARBITRARY_TIME);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800685 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000686 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000687 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800688 << "Should reject key events with ACTION_MULTIPLE.";
689}
690
691TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) {
692 MotionEvent event;
693 PointerProperties pointerProperties[MAX_POINTERS + 1];
694 PointerCoords pointerCoords[MAX_POINTERS + 1];
Siarhei Vishniakou01747382022-01-20 13:23:27 -0800695 for (size_t i = 0; i <= MAX_POINTERS; i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800696 pointerProperties[i].clear();
697 pointerProperties[i].id = i;
698 pointerCoords[i].clear();
699 }
700
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800701 // Some constants commonly used below
702 constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN;
703 constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE;
704 constexpr int32_t metaState = AMETA_NONE;
705 constexpr MotionClassification classification = MotionClassification::NONE;
706
chaviw9eaa22c2020-07-01 16:21:27 -0700707 ui::Transform identityTransform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800708 // Rejects undefined motion actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800709 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
chaviw9eaa22c2020-07-01 16:21:27 -0700710 /*action*/ -1, 0, 0, edgeFlags, metaState, 0, classification,
711 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700712 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
713 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700714 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800715 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000716 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000717 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800718 << "Should reject motion events with undefined action.";
719
720 // Rejects pointer down with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800721 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -0800722 POINTER_1_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
723 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
724 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
725 ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500726 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800727 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000728 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000729 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800730 << "Should reject motion events with pointer down index too large.";
731
Garfield Tanfbe732e2020-01-24 11:26:14 -0800732 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700733 AMOTION_EVENT_ACTION_POINTER_DOWN |
734 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700735 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
736 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700737 identityTransform, ARBITRARY_TIME, ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500738 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800739 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000740 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000741 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800742 << "Should reject motion events with pointer down index too small.";
743
744 // Rejects pointer up with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800745 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -0800746 POINTER_1_UP, 0, 0, edgeFlags, metaState, 0, classification, identityTransform,
747 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
748 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
749 ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500750 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800751 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000752 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000753 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800754 << "Should reject motion events with pointer up index too large.";
755
Garfield Tanfbe732e2020-01-24 11:26:14 -0800756 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700757 AMOTION_EVENT_ACTION_POINTER_UP |
758 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700759 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
760 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700761 identityTransform, ARBITRARY_TIME, ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500762 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800763 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000764 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000765 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800766 << "Should reject motion events with pointer up index too small.";
767
768 // Rejects motion events with invalid number of pointers.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800769 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
770 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700771 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700772 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
773 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700774 /*pointerCount*/ 0, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800775 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000776 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000777 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800778 << "Should reject motion events with 0 pointers.";
779
Garfield Tanfbe732e2020-01-24 11:26:14 -0800780 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
781 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700782 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700783 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
784 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700785 /*pointerCount*/ MAX_POINTERS + 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800786 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000787 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000788 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800789 << "Should reject motion events with more than MAX_POINTERS pointers.";
790
791 // Rejects motion events with invalid pointer ids.
792 pointerProperties[0].id = -1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800793 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
794 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700795 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700796 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
797 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700798 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800799 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000800 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000801 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800802 << "Should reject motion events with pointer ids less than 0.";
803
804 pointerProperties[0].id = MAX_POINTER_ID + 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800805 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
806 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700807 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700808 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
809 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700810 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800811 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000812 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000813 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800814 << "Should reject motion events with pointer ids greater than MAX_POINTER_ID.";
815
816 // Rejects motion events with duplicate pointer ids.
817 pointerProperties[0].id = 1;
818 pointerProperties[1].id = 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800819 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
820 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700821 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700822 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
823 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700824 /*pointerCount*/ 2, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800825 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000826 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000827 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800828 << "Should reject motion events with duplicate pointer ids.";
829}
830
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800831/* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */
832
833TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) {
834 constexpr nsecs_t eventTime = 20;
Prabir Pradhan678438e2023-04-13 19:32:51 +0000835 mDispatcher->notifyConfigurationChanged({/*id=*/10, eventTime});
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800836 ASSERT_TRUE(mDispatcher->waitForIdle());
837
838 mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime);
839}
840
841TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) {
Harry Cutts33476232023-01-30 19:57:29 +0000842 NotifySwitchArgs args(/*id=*/10, /*eventTime=*/20, /*policyFlags=*/0, /*switchValues=*/1,
843 /*switchMask=*/2);
Prabir Pradhan678438e2023-04-13 19:32:51 +0000844 mDispatcher->notifySwitch(args);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800845
846 // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener
847 args.policyFlags |= POLICY_FLAG_TRUSTED;
848 mFakePolicy->assertNotifySwitchWasCalled(args);
849}
850
Arthur Hungb92218b2018-08-14 12:00:21 +0800851// --- InputDispatcherTest SetInputWindowTest ---
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700852static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms;
Siarhei Vishniakou1c494c52021-08-11 20:25:01 -0700853// Default input dispatching timeout if there is no focused application or paused window
854// from which to determine an appropriate dispatching timeout.
855static const std::chrono::duration DISPATCHING_TIMEOUT = std::chrono::milliseconds(
856 android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS *
857 android::base::HwTimeoutMultiplier());
Arthur Hungb92218b2018-08-14 12:00:21 +0800858
859class FakeApplicationHandle : public InputApplicationHandle {
860public:
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700861 FakeApplicationHandle() {
862 mInfo.name = "Fake Application";
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700863 mInfo.token = sp<BBinder>::make();
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500864 mInfo.dispatchingTimeoutMillis =
865 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700866 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800867 virtual ~FakeApplicationHandle() {}
868
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000869 virtual bool updateInfo() override { return true; }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700870
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500871 void setDispatchingTimeout(std::chrono::milliseconds timeout) {
872 mInfo.dispatchingTimeoutMillis = timeout.count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700873 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800874};
875
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800876class FakeInputReceiver {
Arthur Hungb92218b2018-08-14 12:00:21 +0800877public:
Garfield Tan15601662020-09-22 15:32:38 -0700878 explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name)
chaviwd1c23182019-12-20 18:44:56 -0800879 : mName(name) {
Garfield Tan15601662020-09-22 15:32:38 -0700880 mConsumer = std::make_unique<InputConsumer>(std::move(clientChannel));
chaviwd1c23182019-12-20 18:44:56 -0800881 }
882
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800883 InputEvent* consume() {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700884 InputEvent* event;
885 std::optional<uint32_t> consumeSeq = receiveEvent(&event);
886 if (!consumeSeq) {
887 return nullptr;
888 }
889 finishEvent(*consumeSeq);
890 return event;
891 }
892
893 /**
894 * Receive an event without acknowledging it.
895 * Return the sequence number that could later be used to send finished signal.
896 */
897 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Arthur Hungb92218b2018-08-14 12:00:21 +0800898 uint32_t consumeSeq;
899 InputEvent* event;
Arthur Hungb92218b2018-08-14 12:00:21 +0800900
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800901 std::chrono::time_point start = std::chrono::steady_clock::now();
902 status_t status = WOULD_BLOCK;
903 while (status == WOULD_BLOCK) {
Harry Cutts33476232023-01-30 19:57:29 +0000904 status = mConsumer->consume(&mEventFactory, /*consumeBatches=*/true, -1, &consumeSeq,
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800905 &event);
906 std::chrono::duration elapsed = std::chrono::steady_clock::now() - start;
907 if (elapsed > 100ms) {
908 break;
909 }
910 }
911
912 if (status == WOULD_BLOCK) {
913 // Just means there's no event available.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700914 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800915 }
916
917 if (status != OK) {
918 ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK.";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700919 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800920 }
921 if (event == nullptr) {
922 ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700923 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800924 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700925 if (outEvent != nullptr) {
926 *outEvent = event;
927 }
928 return consumeSeq;
929 }
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800930
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700931 /**
932 * To be used together with "receiveEvent" to complete the consumption of an event.
933 */
934 void finishEvent(uint32_t consumeSeq) {
935 const status_t status = mConsumer->sendFinishedSignal(consumeSeq, true);
936 ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK.";
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800937 }
938
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000939 void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) {
940 const status_t status = mConsumer->sendTimeline(inputEventId, timeline);
941 ASSERT_EQ(OK, status);
942 }
943
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700944 void consumeEvent(InputEventType expectedEventType, int32_t expectedAction,
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000945 std::optional<int32_t> expectedDisplayId,
946 std::optional<int32_t> expectedFlags) {
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800947 InputEvent* event = consume();
948
949 ASSERT_NE(nullptr, event) << mName.c_str()
950 << ": consumer should have returned non-NULL event.";
Arthur Hungb92218b2018-08-14 12:00:21 +0800951 ASSERT_EQ(expectedEventType, event->getType())
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700952 << mName.c_str() << " expected " << ftl::enum_string(expectedEventType)
953 << " event, got " << *event;
Arthur Hungb92218b2018-08-14 12:00:21 +0800954
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000955 if (expectedDisplayId.has_value()) {
956 EXPECT_EQ(expectedDisplayId, event->getDisplayId());
957 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800958
Tiger Huang8664f8c2018-10-11 19:14:35 +0800959 switch (expectedEventType) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700960 case InputEventType::KEY: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800961 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event);
962 EXPECT_EQ(expectedAction, keyEvent.getAction());
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000963 if (expectedFlags.has_value()) {
964 EXPECT_EQ(expectedFlags.value(), keyEvent.getFlags());
965 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800966 break;
967 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700968 case InputEventType::MOTION: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800969 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000970 assertMotionAction(expectedAction, motionEvent.getAction());
971
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000972 if (expectedFlags.has_value()) {
973 EXPECT_EQ(expectedFlags.value(), motionEvent.getFlags());
974 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800975 break;
976 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700977 case InputEventType::FOCUS: {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100978 FAIL() << "Use 'consumeFocusEvent' for FOCUS events";
979 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700980 case InputEventType::CAPTURE: {
Prabir Pradhan99987712020-11-10 18:43:05 -0800981 FAIL() << "Use 'consumeCaptureEvent' for CAPTURE events";
982 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700983 case InputEventType::TOUCH_MODE: {
Antonio Kantekf16f2832021-09-28 04:39:20 +0000984 FAIL() << "Use 'consumeTouchModeEvent' for TOUCH_MODE events";
985 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700986 case InputEventType::DRAG: {
arthurhungb89ccb02020-12-30 16:19:01 +0800987 FAIL() << "Use 'consumeDragEvent' for DRAG events";
988 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800989 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800990 }
991
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -0800992 MotionEvent* consumeMotion() {
993 InputEvent* event = consume();
994
995 if (event == nullptr) {
996 ADD_FAILURE() << mName << ": expected a MotionEvent, but didn't get one.";
997 return nullptr;
998 }
999
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001000 if (event->getType() != InputEventType::MOTION) {
1001 ADD_FAILURE() << mName << " expected a MotionEvent, got " << *event;
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08001002 return nullptr;
1003 }
1004 return static_cast<MotionEvent*>(event);
1005 }
1006
1007 void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) {
1008 MotionEvent* motionEvent = consumeMotion();
1009 ASSERT_NE(nullptr, motionEvent) << "Did not get a motion event, but expected " << matcher;
1010 ASSERT_THAT(*motionEvent, matcher);
1011 }
1012
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001013 void consumeFocusEvent(bool hasFocus, bool inTouchMode) {
1014 InputEvent* event = consume();
1015 ASSERT_NE(nullptr, event) << mName.c_str()
1016 << ": consumer should have returned non-NULL event.";
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001017 ASSERT_EQ(InputEventType::FOCUS, event->getType())
1018 << "Instead of FocusEvent, got " << *event;
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001019
1020 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1021 << mName.c_str() << ": event displayId should always be NONE.";
1022
1023 FocusEvent* focusEvent = static_cast<FocusEvent*>(event);
1024 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001025 }
1026
Prabir Pradhan99987712020-11-10 18:43:05 -08001027 void consumeCaptureEvent(bool hasCapture) {
1028 const InputEvent* event = consume();
1029 ASSERT_NE(nullptr, event) << mName.c_str()
1030 << ": consumer should have returned non-NULL event.";
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001031 ASSERT_EQ(InputEventType::CAPTURE, event->getType())
1032 << "Instead of CaptureEvent, got " << *event;
Prabir Pradhan99987712020-11-10 18:43:05 -08001033
1034 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1035 << mName.c_str() << ": event displayId should always be NONE.";
1036
1037 const auto& captureEvent = static_cast<const CaptureEvent&>(*event);
1038 EXPECT_EQ(hasCapture, captureEvent.getPointerCaptureEnabled());
1039 }
1040
arthurhungb89ccb02020-12-30 16:19:01 +08001041 void consumeDragEvent(bool isExiting, float x, float y) {
1042 const InputEvent* event = consume();
1043 ASSERT_NE(nullptr, event) << mName.c_str()
1044 << ": consumer should have returned non-NULL event.";
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001045 ASSERT_EQ(InputEventType::DRAG, event->getType()) << "Instead of DragEvent, got " << *event;
arthurhungb89ccb02020-12-30 16:19:01 +08001046
1047 EXPECT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1048 << mName.c_str() << ": event displayId should always be NONE.";
1049
1050 const auto& dragEvent = static_cast<const DragEvent&>(*event);
1051 EXPECT_EQ(isExiting, dragEvent.isExiting());
1052 EXPECT_EQ(x, dragEvent.getX());
1053 EXPECT_EQ(y, dragEvent.getY());
1054 }
1055
Antonio Kantekf16f2832021-09-28 04:39:20 +00001056 void consumeTouchModeEvent(bool inTouchMode) {
1057 const InputEvent* event = consume();
1058 ASSERT_NE(nullptr, event) << mName.c_str()
1059 << ": consumer should have returned non-NULL event.";
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001060 ASSERT_EQ(InputEventType::TOUCH_MODE, event->getType())
1061 << "Instead of TouchModeEvent, got " << *event;
Antonio Kantekf16f2832021-09-28 04:39:20 +00001062
1063 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1064 << mName.c_str() << ": event displayId should always be NONE.";
1065 const auto& touchModeEvent = static_cast<const TouchModeEvent&>(*event);
1066 EXPECT_EQ(inTouchMode, touchModeEvent.isInTouchMode());
1067 }
1068
chaviwd1c23182019-12-20 18:44:56 -08001069 void assertNoEvents() {
1070 InputEvent* event = consume();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001071 if (event == nullptr) {
1072 return;
1073 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001074 if (event->getType() == InputEventType::KEY) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001075 KeyEvent& keyEvent = static_cast<KeyEvent&>(*event);
1076 ADD_FAILURE() << "Received key event "
1077 << KeyEvent::actionToString(keyEvent.getAction());
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001078 } else if (event->getType() == InputEventType::MOTION) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001079 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
1080 ADD_FAILURE() << "Received motion event "
1081 << MotionEvent::actionToString(motionEvent.getAction());
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001082 } else if (event->getType() == InputEventType::FOCUS) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001083 FocusEvent& focusEvent = static_cast<FocusEvent&>(*event);
1084 ADD_FAILURE() << "Received focus event, hasFocus = "
1085 << (focusEvent.getHasFocus() ? "true" : "false");
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001086 } else if (event->getType() == InputEventType::CAPTURE) {
Prabir Pradhan99987712020-11-10 18:43:05 -08001087 const auto& captureEvent = static_cast<CaptureEvent&>(*event);
1088 ADD_FAILURE() << "Received capture event, pointerCaptureEnabled = "
1089 << (captureEvent.getPointerCaptureEnabled() ? "true" : "false");
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001090 } else if (event->getType() == InputEventType::TOUCH_MODE) {
Antonio Kantekf16f2832021-09-28 04:39:20 +00001091 const auto& touchModeEvent = static_cast<TouchModeEvent&>(*event);
1092 ADD_FAILURE() << "Received touch mode event, inTouchMode = "
1093 << (touchModeEvent.isInTouchMode() ? "true" : "false");
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001094 }
1095 FAIL() << mName.c_str()
1096 << ": should not have received any events, so consume() should return NULL";
chaviwd1c23182019-12-20 18:44:56 -08001097 }
1098
1099 sp<IBinder> getToken() { return mConsumer->getChannel()->getConnectionToken(); }
1100
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001101 int getChannelFd() { return mConsumer->getChannel()->getFd().get(); }
1102
chaviwd1c23182019-12-20 18:44:56 -08001103protected:
1104 std::unique_ptr<InputConsumer> mConsumer;
1105 PreallocatedInputEventFactory mEventFactory;
1106
1107 std::string mName;
1108};
1109
chaviw3277faf2021-05-19 16:45:23 -05001110class FakeWindowHandle : public WindowInfoHandle {
chaviwd1c23182019-12-20 18:44:56 -08001111public:
1112 static const int32_t WIDTH = 600;
1113 static const int32_t HEIGHT = 800;
chaviwd1c23182019-12-20 18:44:56 -08001114
Chris Yea209fde2020-07-22 13:54:51 -07001115 FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001116 const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name,
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001117 int32_t displayId, std::optional<sp<IBinder>> token = std::nullopt)
chaviwd1c23182019-12-20 18:44:56 -08001118 : mName(name) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001119 if (token == std::nullopt) {
Garfield Tan15601662020-09-22 15:32:38 -07001120 base::Result<std::unique_ptr<InputChannel>> channel =
1121 dispatcher->createInputChannel(name);
1122 token = (*channel)->getConnectionToken();
1123 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
chaviwd1c23182019-12-20 18:44:56 -08001124 }
1125
1126 inputApplicationHandle->updateInfo();
1127 mInfo.applicationInfo = *inputApplicationHandle->getInfo();
1128
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001129 mInfo.token = *token;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001130 mInfo.id = sId++;
chaviwd1c23182019-12-20 18:44:56 -08001131 mInfo.name = name;
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -05001132 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001133 mInfo.alpha = 1.0;
chaviwd1c23182019-12-20 18:44:56 -08001134 mInfo.frameLeft = 0;
1135 mInfo.frameTop = 0;
1136 mInfo.frameRight = WIDTH;
1137 mInfo.frameBottom = HEIGHT;
chaviw1ff3d1e2020-07-01 15:53:47 -07001138 mInfo.transform.set(0, 0);
chaviwd1c23182019-12-20 18:44:56 -08001139 mInfo.globalScaleFactor = 1.0;
1140 mInfo.touchableRegion.clear();
1141 mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT));
Prabir Pradhan5735a322022-04-11 17:23:34 +00001142 mInfo.ownerPid = WINDOW_PID;
1143 mInfo.ownerUid = WINDOW_UID;
chaviwd1c23182019-12-20 18:44:56 -08001144 mInfo.displayId = displayId;
Prabir Pradhan51e7db02022-02-07 06:02:57 -08001145 mInfo.inputConfig = WindowInfo::InputConfig::DEFAULT;
chaviwd1c23182019-12-20 18:44:56 -08001146 }
1147
Arthur Hungabbb9d82021-09-01 14:52:30 +00001148 sp<FakeWindowHandle> clone(
1149 const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001150 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t displayId) {
Arthur Hungabbb9d82021-09-01 14:52:30 +00001151 sp<FakeWindowHandle> handle =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001152 sp<FakeWindowHandle>::make(inputApplicationHandle, dispatcher,
1153 mInfo.name + "(Mirror)", displayId, mInfo.token);
Arthur Hungabbb9d82021-09-01 14:52:30 +00001154 return handle;
1155 }
1156
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001157 void setTouchable(bool touchable) {
1158 mInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, !touchable);
1159 }
chaviwd1c23182019-12-20 18:44:56 -08001160
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001161 void setFocusable(bool focusable) {
1162 mInfo.setInputConfig(WindowInfo::InputConfig::NOT_FOCUSABLE, !focusable);
1163 }
1164
1165 void setVisible(bool visible) {
1166 mInfo.setInputConfig(WindowInfo::InputConfig::NOT_VISIBLE, !visible);
1167 }
Vishnu Nair958da932020-08-21 17:12:37 -07001168
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001169 void setDispatchingTimeout(std::chrono::nanoseconds timeout) {
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -05001170 mInfo.dispatchingTimeout = timeout;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001171 }
1172
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001173 void setPaused(bool paused) {
1174 mInfo.setInputConfig(WindowInfo::InputConfig::PAUSE_DISPATCHING, paused);
1175 }
1176
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08001177 void setPreventSplitting(bool preventSplitting) {
1178 mInfo.setInputConfig(WindowInfo::InputConfig::PREVENT_SPLITTING, preventSplitting);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001179 }
1180
1181 void setSlippery(bool slippery) {
1182 mInfo.setInputConfig(WindowInfo::InputConfig::SLIPPERY, slippery);
1183 }
1184
1185 void setWatchOutsideTouch(bool watchOutside) {
1186 mInfo.setInputConfig(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH, watchOutside);
1187 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001188
Prabir Pradhan51e7db02022-02-07 06:02:57 -08001189 void setSpy(bool spy) { mInfo.setInputConfig(WindowInfo::InputConfig::SPY, spy); }
1190
1191 void setInterceptsStylus(bool interceptsStylus) {
1192 mInfo.setInputConfig(WindowInfo::InputConfig::INTERCEPTS_STYLUS, interceptsStylus);
1193 }
1194
1195 void setDropInput(bool dropInput) {
1196 mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT, dropInput);
1197 }
1198
1199 void setDropInputIfObscured(bool dropInputIfObscured) {
1200 mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED, dropInputIfObscured);
1201 }
1202
1203 void setNoInputChannel(bool noInputChannel) {
1204 mInfo.setInputConfig(WindowInfo::InputConfig::NO_INPUT_CHANNEL, noInputChannel);
1205 }
1206
Josep del Riob3981622023-04-18 15:49:45 +00001207 void setDisableUserActivity(bool disableUserActivity) {
1208 mInfo.setInputConfig(WindowInfo::InputConfig::DISABLE_USER_ACTIVITY, disableUserActivity);
1209 }
1210
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001211 void setAlpha(float alpha) { mInfo.alpha = alpha; }
1212
chaviw3277faf2021-05-19 16:45:23 -05001213 void setTouchOcclusionMode(TouchOcclusionMode mode) { mInfo.touchOcclusionMode = mode; }
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001214
Bernardo Rufino7393d172021-02-26 13:56:11 +00001215 void setApplicationToken(sp<IBinder> token) { mInfo.applicationInfo.token = token; }
1216
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001217 void setFrame(const Rect& frame, const ui::Transform& displayTransform = ui::Transform()) {
chaviwd1c23182019-12-20 18:44:56 -08001218 mInfo.frameLeft = frame.left;
1219 mInfo.frameTop = frame.top;
1220 mInfo.frameRight = frame.right;
1221 mInfo.frameBottom = frame.bottom;
1222 mInfo.touchableRegion.clear();
1223 mInfo.addTouchableRegion(frame);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001224
1225 const Rect logicalDisplayFrame = displayTransform.transform(frame);
1226 ui::Transform translate;
1227 translate.set(-logicalDisplayFrame.left, -logicalDisplayFrame.top);
1228 mInfo.transform = translate * displayTransform;
chaviwd1c23182019-12-20 18:44:56 -08001229 }
1230
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001231 void setTouchableRegion(const Region& region) { mInfo.touchableRegion = region; }
1232
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001233 void setIsWallpaper(bool isWallpaper) {
1234 mInfo.setInputConfig(WindowInfo::InputConfig::IS_WALLPAPER, isWallpaper);
1235 }
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001236
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001237 void setDupTouchToWallpaper(bool hasWallpaper) {
1238 mInfo.setInputConfig(WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER, hasWallpaper);
1239 }
chaviwd1c23182019-12-20 18:44:56 -08001240
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001241 void setTrustedOverlay(bool trustedOverlay) {
1242 mInfo.setInputConfig(WindowInfo::InputConfig::TRUSTED_OVERLAY, trustedOverlay);
1243 }
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001244
chaviw9eaa22c2020-07-01 16:21:27 -07001245 void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) {
1246 mInfo.transform.set(dsdx, dtdx, dtdy, dsdy);
1247 }
1248
1249 void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); }
chaviwaf87b3e2019-10-01 16:59:28 -07001250
yunho.shinf4a80b82020-11-16 21:13:57 +09001251 void setWindowOffset(float offsetX, float offsetY) { mInfo.transform.set(offsetX, offsetY); }
1252
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001253 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001254 consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId, expectedFlags);
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001255 }
1256
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001257 void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001258 consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, expectedDisplayId, expectedFlags);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001259 }
1260
Svet Ganov5d3bc372020-01-26 23:11:07 -08001261 void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001262 int32_t expectedFlags = 0) {
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08001263 consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(expectedDisplayId),
1264 WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED)));
Svet Ganov5d3bc372020-01-26 23:11:07 -08001265 }
1266
1267 void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001268 int32_t expectedFlags = 0) {
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08001269 consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
1270 WithDisplayId(expectedDisplayId), WithFlags(expectedFlags)));
Svet Ganov5d3bc372020-01-26 23:11:07 -08001271 }
1272
1273 void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001274 int32_t expectedFlags = 0) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001275 consumeAnyMotionDown(expectedDisplayId, expectedFlags);
1276 }
1277
1278 void consumeAnyMotionDown(std::optional<int32_t> expectedDisplayId = std::nullopt,
1279 std::optional<int32_t> expectedFlags = std::nullopt) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001280 consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId,
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001281 expectedFlags);
1282 }
1283
Svet Ganov5d3bc372020-01-26 23:11:07 -08001284 void consumeMotionPointerDown(int32_t pointerIdx,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001285 int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1286 int32_t expectedFlags = 0) {
1287 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
1288 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001289 consumeEvent(InputEventType::MOTION, action, expectedDisplayId, expectedFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001290 }
1291
1292 void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001293 int32_t expectedFlags = 0) {
1294 int32_t action = AMOTION_EVENT_ACTION_POINTER_UP |
1295 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001296 consumeEvent(InputEventType::MOTION, action, expectedDisplayId, expectedFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001297 }
1298
1299 void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001300 int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001301 consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId,
Michael Wright3a240c42019-12-10 20:53:41 +00001302 expectedFlags);
1303 }
1304
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05001305 void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1306 int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001307 consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE, expectedDisplayId,
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05001308 expectedFlags);
1309 }
1310
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08001311 void consumeMotionOutsideWithZeroedCoords(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1312 int32_t expectedFlags = 0) {
1313 InputEvent* event = consume();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08001314 ASSERT_NE(nullptr, event);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001315 ASSERT_EQ(InputEventType::MOTION, event->getType());
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08001316 const MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
1317 EXPECT_EQ(AMOTION_EVENT_ACTION_OUTSIDE, motionEvent.getActionMasked());
1318 EXPECT_EQ(0.f, motionEvent.getRawPointerCoords(0)->getX());
1319 EXPECT_EQ(0.f, motionEvent.getRawPointerCoords(0)->getY());
1320 }
1321
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001322 void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) {
1323 ASSERT_NE(mInputReceiver, nullptr)
1324 << "Cannot consume events from a window with no receiver";
1325 mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode);
1326 }
1327
Prabir Pradhan99987712020-11-10 18:43:05 -08001328 void consumeCaptureEvent(bool hasCapture) {
1329 ASSERT_NE(mInputReceiver, nullptr)
1330 << "Cannot consume events from a window with no receiver";
1331 mInputReceiver->consumeCaptureEvent(hasCapture);
1332 }
1333
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08001334 void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) {
1335 MotionEvent* motionEvent = consumeMotion();
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08001336 ASSERT_NE(nullptr, motionEvent) << "Did not get a motion event, but expected " << matcher;
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08001337 ASSERT_THAT(*motionEvent, matcher);
1338 }
1339
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001340 void consumeEvent(InputEventType expectedEventType, int32_t expectedAction,
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001341 std::optional<int32_t> expectedDisplayId,
1342 std::optional<int32_t> expectedFlags) {
chaviwd1c23182019-12-20 18:44:56 -08001343 ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver";
1344 mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId,
1345 expectedFlags);
1346 }
1347
arthurhungb89ccb02020-12-30 16:19:01 +08001348 void consumeDragEvent(bool isExiting, float x, float y) {
1349 mInputReceiver->consumeDragEvent(isExiting, x, y);
1350 }
1351
Antonio Kantekf16f2832021-09-28 04:39:20 +00001352 void consumeTouchModeEvent(bool inTouchMode) {
1353 ASSERT_NE(mInputReceiver, nullptr)
1354 << "Cannot consume events from a window with no receiver";
1355 mInputReceiver->consumeTouchModeEvent(inTouchMode);
1356 }
1357
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001358 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001359 if (mInputReceiver == nullptr) {
1360 ADD_FAILURE() << "Invalid receive event on window with no receiver";
1361 return std::nullopt;
1362 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001363 return mInputReceiver->receiveEvent(outEvent);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001364 }
1365
1366 void finishEvent(uint32_t sequenceNum) {
1367 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
1368 mInputReceiver->finishEvent(sequenceNum);
1369 }
1370
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00001371 void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) {
1372 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
1373 mInputReceiver->sendTimeline(inputEventId, timeline);
1374 }
1375
chaviwaf87b3e2019-10-01 16:59:28 -07001376 InputEvent* consume() {
1377 if (mInputReceiver == nullptr) {
1378 return nullptr;
1379 }
1380 return mInputReceiver->consume();
1381 }
1382
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00001383 MotionEvent* consumeMotion() {
1384 InputEvent* event = consume();
1385 if (event == nullptr) {
1386 ADD_FAILURE() << "Consume failed : no event";
1387 return nullptr;
1388 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001389 if (event->getType() != InputEventType::MOTION) {
1390 ADD_FAILURE() << "Instead of motion event, got " << *event;
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00001391 return nullptr;
1392 }
1393 return static_cast<MotionEvent*>(event);
1394 }
1395
Arthur Hungb92218b2018-08-14 12:00:21 +08001396 void assertNoEvents() {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001397 if (mInputReceiver == nullptr &&
Prabir Pradhan51e7db02022-02-07 06:02:57 -08001398 mInfo.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001399 return; // Can't receive events if the window does not have input channel
1400 }
1401 ASSERT_NE(nullptr, mInputReceiver)
1402 << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL";
chaviwd1c23182019-12-20 18:44:56 -08001403 mInputReceiver->assertNoEvents();
Arthur Hungb92218b2018-08-14 12:00:21 +08001404 }
1405
chaviwaf87b3e2019-10-01 16:59:28 -07001406 sp<IBinder> getToken() { return mInfo.token; }
1407
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001408 const std::string& getName() { return mName; }
1409
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001410 void setOwnerInfo(int32_t ownerPid, int32_t ownerUid) {
1411 mInfo.ownerPid = ownerPid;
1412 mInfo.ownerUid = ownerUid;
1413 }
1414
Prabir Pradhanedd96402022-02-15 01:46:16 -08001415 int32_t getPid() const { return mInfo.ownerPid; }
1416
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001417 void destroyReceiver() { mInputReceiver = nullptr; }
1418
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001419 int getChannelFd() { return mInputReceiver->getChannelFd(); }
1420
chaviwd1c23182019-12-20 18:44:56 -08001421private:
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001422 const std::string mName;
chaviwd1c23182019-12-20 18:44:56 -08001423 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001424 static std::atomic<int32_t> sId; // each window gets a unique id, like in surfaceflinger
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001425};
1426
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001427std::atomic<int32_t> FakeWindowHandle::sId{1};
1428
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001429static InputEventInjectionResult injectKey(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001430 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t repeatCount,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001431 int32_t displayId = ADISPLAY_ID_NONE,
1432 InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001433 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Prabir Pradhan5735a322022-04-11 17:23:34 +00001434 bool allowKeyRepeat = true, std::optional<int32_t> targetUid = {},
1435 uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
Arthur Hungb92218b2018-08-14 12:00:21 +08001436 KeyEvent event;
1437 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1438
1439 // Define a valid key down event.
Garfield Tanfbe732e2020-01-24 11:26:14 -08001440 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId,
Harry Cutts33476232023-01-30 19:57:29 +00001441 INVALID_HMAC, action, /*flags=*/0, AKEYCODE_A, KEY_A, AMETA_NONE, repeatCount,
1442 currentTime, currentTime);
Arthur Hungb92218b2018-08-14 12:00:21 +08001443
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001444 if (!allowKeyRepeat) {
1445 policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT;
1446 }
Arthur Hungb92218b2018-08-14 12:00:21 +08001447 // Inject event until dispatch out.
Prabir Pradhan5735a322022-04-11 17:23:34 +00001448 return dispatcher->injectInputEvent(&event, targetUid, syncMode, injectionTimeout, policyFlags);
Arthur Hungb92218b2018-08-14 12:00:21 +08001449}
1450
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001451static InputEventInjectionResult injectKeyDown(const std::unique_ptr<InputDispatcher>& dispatcher,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001452 int32_t displayId = ADISPLAY_ID_NONE) {
Harry Cutts33476232023-01-30 19:57:29 +00001453 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001454}
1455
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001456// Inject a down event that has key repeat disabled. This allows InputDispatcher to idle without
1457// sending a subsequent key up. When key repeat is enabled, the dispatcher cannot idle because it
1458// has to be woken up to process the repeating key.
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001459static InputEventInjectionResult injectKeyDownNoRepeat(
1460 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t displayId = ADISPLAY_ID_NONE) {
Harry Cutts33476232023-01-30 19:57:29 +00001461 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001462 InputEventInjectionSync::WAIT_FOR_RESULT, INJECT_EVENT_TIMEOUT,
Harry Cutts33476232023-01-30 19:57:29 +00001463 /*allowKeyRepeat=*/false);
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001464}
1465
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001466static InputEventInjectionResult injectKeyUp(const std::unique_ptr<InputDispatcher>& dispatcher,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001467 int32_t displayId = ADISPLAY_ID_NONE) {
Harry Cutts33476232023-01-30 19:57:29 +00001468 return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /*repeatCount=*/0, displayId);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001469}
1470
Garfield Tandf26e862020-07-01 20:18:19 -07001471class PointerBuilder {
1472public:
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001473 PointerBuilder(int32_t id, ToolType toolType) {
Garfield Tandf26e862020-07-01 20:18:19 -07001474 mProperties.clear();
1475 mProperties.id = id;
1476 mProperties.toolType = toolType;
1477 mCoords.clear();
1478 }
1479
1480 PointerBuilder& x(float x) { return axis(AMOTION_EVENT_AXIS_X, x); }
1481
1482 PointerBuilder& y(float y) { return axis(AMOTION_EVENT_AXIS_Y, y); }
1483
1484 PointerBuilder& axis(int32_t axis, float value) {
1485 mCoords.setAxisValue(axis, value);
1486 return *this;
1487 }
1488
1489 PointerProperties buildProperties() const { return mProperties; }
1490
1491 PointerCoords buildCoords() const { return mCoords; }
1492
1493private:
1494 PointerProperties mProperties;
1495 PointerCoords mCoords;
1496};
1497
1498class MotionEventBuilder {
1499public:
1500 MotionEventBuilder(int32_t action, int32_t source) {
1501 mAction = action;
1502 mSource = source;
1503 mEventTime = systemTime(SYSTEM_TIME_MONOTONIC);
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08001504 mDownTime = mEventTime;
Garfield Tandf26e862020-07-01 20:18:19 -07001505 }
1506
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08001507 MotionEventBuilder& deviceId(int32_t deviceId) {
1508 mDeviceId = deviceId;
1509 return *this;
1510 }
1511
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08001512 MotionEventBuilder& downTime(nsecs_t downTime) {
1513 mDownTime = downTime;
1514 return *this;
1515 }
1516
Garfield Tandf26e862020-07-01 20:18:19 -07001517 MotionEventBuilder& eventTime(nsecs_t eventTime) {
1518 mEventTime = eventTime;
1519 return *this;
1520 }
1521
1522 MotionEventBuilder& displayId(int32_t displayId) {
1523 mDisplayId = displayId;
1524 return *this;
1525 }
1526
1527 MotionEventBuilder& actionButton(int32_t actionButton) {
1528 mActionButton = actionButton;
1529 return *this;
1530 }
1531
arthurhung6d4bed92021-03-17 11:59:33 +08001532 MotionEventBuilder& buttonState(int32_t buttonState) {
1533 mButtonState = buttonState;
Garfield Tandf26e862020-07-01 20:18:19 -07001534 return *this;
1535 }
1536
1537 MotionEventBuilder& rawXCursorPosition(float rawXCursorPosition) {
1538 mRawXCursorPosition = rawXCursorPosition;
1539 return *this;
1540 }
1541
1542 MotionEventBuilder& rawYCursorPosition(float rawYCursorPosition) {
1543 mRawYCursorPosition = rawYCursorPosition;
1544 return *this;
1545 }
1546
1547 MotionEventBuilder& pointer(PointerBuilder pointer) {
1548 mPointers.push_back(pointer);
1549 return *this;
1550 }
1551
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08001552 MotionEventBuilder& addFlag(uint32_t flags) {
1553 mFlags |= flags;
1554 return *this;
1555 }
1556
Garfield Tandf26e862020-07-01 20:18:19 -07001557 MotionEvent build() {
1558 std::vector<PointerProperties> pointerProperties;
1559 std::vector<PointerCoords> pointerCoords;
1560 for (const PointerBuilder& pointer : mPointers) {
1561 pointerProperties.push_back(pointer.buildProperties());
1562 pointerCoords.push_back(pointer.buildCoords());
1563 }
1564
1565 // Set mouse cursor position for the most common cases to avoid boilerplate.
1566 if (mSource == AINPUT_SOURCE_MOUSE &&
Siarhei Vishniakou70c6ee82023-05-15 17:43:48 -07001567 !MotionEvent::isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition)) {
Garfield Tandf26e862020-07-01 20:18:19 -07001568 mRawXCursorPosition = pointerCoords[0].getX();
1569 mRawYCursorPosition = pointerCoords[0].getY();
1570 }
1571
1572 MotionEvent event;
chaviw9eaa22c2020-07-01 16:21:27 -07001573 ui::Transform identityTransform;
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08001574 event.initialize(InputEvent::nextId(), mDeviceId, mSource, mDisplayId, INVALID_HMAC,
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08001575 mAction, mActionButton, mFlags, /* edgeFlags */ 0, AMETA_NONE,
chaviw9eaa22c2020-07-01 16:21:27 -07001576 mButtonState, MotionClassification::NONE, identityTransform,
1577 /* xPrecision */ 0, /* yPrecision */ 0, mRawXCursorPosition,
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08001578 mRawYCursorPosition, identityTransform, mDownTime, mEventTime,
Prabir Pradhanb9b18502021-08-26 12:30:32 -07001579 mPointers.size(), pointerProperties.data(), pointerCoords.data());
Garfield Tandf26e862020-07-01 20:18:19 -07001580
1581 return event;
1582 }
1583
1584private:
1585 int32_t mAction;
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08001586 int32_t mDeviceId = DEVICE_ID;
Garfield Tandf26e862020-07-01 20:18:19 -07001587 int32_t mSource;
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08001588 nsecs_t mDownTime;
Garfield Tandf26e862020-07-01 20:18:19 -07001589 nsecs_t mEventTime;
1590 int32_t mDisplayId{ADISPLAY_ID_DEFAULT};
1591 int32_t mActionButton{0};
1592 int32_t mButtonState{0};
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08001593 int32_t mFlags{0};
Garfield Tandf26e862020-07-01 20:18:19 -07001594 float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
1595 float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
1596
1597 std::vector<PointerBuilder> mPointers;
1598};
1599
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08001600class MotionArgsBuilder {
1601public:
1602 MotionArgsBuilder(int32_t action, int32_t source) {
1603 mAction = action;
1604 mSource = source;
1605 mEventTime = systemTime(SYSTEM_TIME_MONOTONIC);
1606 mDownTime = mEventTime;
1607 }
1608
1609 MotionArgsBuilder& deviceId(int32_t deviceId) {
1610 mDeviceId = deviceId;
1611 return *this;
1612 }
1613
1614 MotionArgsBuilder& downTime(nsecs_t downTime) {
1615 mDownTime = downTime;
1616 return *this;
1617 }
1618
1619 MotionArgsBuilder& eventTime(nsecs_t eventTime) {
1620 mEventTime = eventTime;
1621 return *this;
1622 }
1623
1624 MotionArgsBuilder& displayId(int32_t displayId) {
1625 mDisplayId = displayId;
1626 return *this;
1627 }
1628
1629 MotionArgsBuilder& policyFlags(int32_t policyFlags) {
1630 mPolicyFlags = policyFlags;
1631 return *this;
1632 }
1633
1634 MotionArgsBuilder& actionButton(int32_t actionButton) {
1635 mActionButton = actionButton;
1636 return *this;
1637 }
1638
1639 MotionArgsBuilder& buttonState(int32_t buttonState) {
1640 mButtonState = buttonState;
1641 return *this;
1642 }
1643
1644 MotionArgsBuilder& rawXCursorPosition(float rawXCursorPosition) {
1645 mRawXCursorPosition = rawXCursorPosition;
1646 return *this;
1647 }
1648
1649 MotionArgsBuilder& rawYCursorPosition(float rawYCursorPosition) {
1650 mRawYCursorPosition = rawYCursorPosition;
1651 return *this;
1652 }
1653
1654 MotionArgsBuilder& pointer(PointerBuilder pointer) {
1655 mPointers.push_back(pointer);
1656 return *this;
1657 }
1658
1659 MotionArgsBuilder& addFlag(uint32_t flags) {
1660 mFlags |= flags;
1661 return *this;
1662 }
1663
Harry Cuttsb166c002023-05-09 13:06:05 +00001664 MotionArgsBuilder& classification(MotionClassification classification) {
1665 mClassification = classification;
1666 return *this;
1667 }
1668
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08001669 NotifyMotionArgs build() {
1670 std::vector<PointerProperties> pointerProperties;
1671 std::vector<PointerCoords> pointerCoords;
1672 for (const PointerBuilder& pointer : mPointers) {
1673 pointerProperties.push_back(pointer.buildProperties());
1674 pointerCoords.push_back(pointer.buildCoords());
1675 }
1676
1677 // Set mouse cursor position for the most common cases to avoid boilerplate.
1678 if (mSource == AINPUT_SOURCE_MOUSE &&
Siarhei Vishniakou70c6ee82023-05-15 17:43:48 -07001679 !MotionEvent::isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition)) {
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08001680 mRawXCursorPosition = pointerCoords[0].getX();
1681 mRawYCursorPosition = pointerCoords[0].getY();
1682 }
1683
1684 NotifyMotionArgs args(InputEvent::nextId(), mEventTime, /*readTime=*/mEventTime, mDeviceId,
1685 mSource, mDisplayId, mPolicyFlags, mAction, mActionButton, mFlags,
Harry Cuttsb166c002023-05-09 13:06:05 +00001686 AMETA_NONE, mButtonState, mClassification, /*edgeFlags=*/0,
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08001687 mPointers.size(), pointerProperties.data(), pointerCoords.data(),
1688 /*xPrecision=*/0, /*yPrecision=*/0, mRawXCursorPosition,
1689 mRawYCursorPosition, mDownTime, /*videoFrames=*/{});
1690
1691 return args;
1692 }
1693
1694private:
1695 int32_t mAction;
1696 int32_t mDeviceId = DEVICE_ID;
1697 int32_t mSource;
1698 nsecs_t mDownTime;
1699 nsecs_t mEventTime;
1700 int32_t mDisplayId{ADISPLAY_ID_DEFAULT};
1701 int32_t mPolicyFlags = DEFAULT_POLICY_FLAGS;
1702 int32_t mActionButton{0};
1703 int32_t mButtonState{0};
1704 int32_t mFlags{0};
Harry Cuttsb166c002023-05-09 13:06:05 +00001705 MotionClassification mClassification{MotionClassification::NONE};
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08001706 float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
1707 float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
1708
1709 std::vector<PointerBuilder> mPointers;
1710};
1711
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001712static InputEventInjectionResult injectMotionEvent(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001713 const std::unique_ptr<InputDispatcher>& dispatcher, const MotionEvent& event,
Garfield Tandf26e862020-07-01 20:18:19 -07001714 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Prabir Pradhan5735a322022-04-11 17:23:34 +00001715 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT,
1716 std::optional<int32_t> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
1717 return dispatcher->injectInputEvent(&event, targetUid, injectionMode, injectionTimeout,
1718 policyFlags);
Garfield Tandf26e862020-07-01 20:18:19 -07001719}
1720
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001721static InputEventInjectionResult injectMotionEvent(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001722 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t source,
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001723 int32_t displayId, const PointF& position = {100, 200},
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001724 const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001725 AMOTION_EVENT_INVALID_CURSOR_POSITION},
1726 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001727 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Prabir Pradhan5735a322022-04-11 17:23:34 +00001728 nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC),
1729 std::optional<int32_t> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
Garfield Tandf26e862020-07-01 20:18:19 -07001730 MotionEvent event = MotionEventBuilder(action, source)
1731 .displayId(displayId)
1732 .eventTime(eventTime)
1733 .rawXCursorPosition(cursorPosition.x)
1734 .rawYCursorPosition(cursorPosition.y)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001735 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER)
Garfield Tandf26e862020-07-01 20:18:19 -07001736 .x(position.x)
1737 .y(position.y))
1738 .build();
Arthur Hungb92218b2018-08-14 12:00:21 +08001739
1740 // Inject event until dispatch out.
Prabir Pradhan5735a322022-04-11 17:23:34 +00001741 return injectMotionEvent(dispatcher, event, injectionTimeout, injectionMode, targetUid,
1742 policyFlags);
Arthur Hungb92218b2018-08-14 12:00:21 +08001743}
1744
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001745static InputEventInjectionResult injectMotionDown(
1746 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t source, int32_t displayId,
1747 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001748 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location);
Garfield Tan00f511d2019-06-12 16:55:40 -07001749}
1750
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001751static InputEventInjectionResult injectMotionUp(const std::unique_ptr<InputDispatcher>& dispatcher,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001752 int32_t source, int32_t displayId,
1753 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001754 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location);
Michael Wright3a240c42019-12-10 20:53:41 +00001755}
1756
Jackal Guof9696682018-10-05 12:23:23 +08001757static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) {
1758 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1759 // Define a valid key event.
Harry Cutts33476232023-01-30 19:57:29 +00001760 NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001761 displayId, POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0, AKEYCODE_A,
1762 KEY_A, AMETA_NONE, currentTime);
Jackal Guof9696682018-10-05 12:23:23 +08001763
1764 return args;
1765}
1766
Josep del Riob3981622023-04-18 15:49:45 +00001767static NotifyKeyArgs generateSystemShortcutArgs(int32_t action,
1768 int32_t displayId = ADISPLAY_ID_NONE) {
1769 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1770 // Define a valid key event.
1771 NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
1772 displayId, 0, action, /* flags */ 0, AKEYCODE_C, KEY_C, AMETA_META_ON,
1773 currentTime);
1774
1775 return args;
1776}
1777
1778static NotifyKeyArgs generateAssistantKeyArgs(int32_t action,
1779 int32_t displayId = ADISPLAY_ID_NONE) {
1780 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1781 // Define a valid key event.
1782 NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
1783 displayId, 0, action, /* flags */ 0, AKEYCODE_ASSIST, KEY_ASSISTANT,
1784 AMETA_NONE, currentTime);
1785
1786 return args;
1787}
1788
Prabir Pradhan678438e2023-04-13 19:32:51 +00001789[[nodiscard]] static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source,
1790 int32_t displayId,
1791 const std::vector<PointF>& points) {
chaviwd1c23182019-12-20 18:44:56 -08001792 size_t pointerCount = points.size();
chaviwaf87b3e2019-10-01 16:59:28 -07001793 if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) {
1794 EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer";
1795 }
1796
chaviwd1c23182019-12-20 18:44:56 -08001797 PointerProperties pointerProperties[pointerCount];
1798 PointerCoords pointerCoords[pointerCount];
Jackal Guof9696682018-10-05 12:23:23 +08001799
chaviwd1c23182019-12-20 18:44:56 -08001800 for (size_t i = 0; i < pointerCount; i++) {
1801 pointerProperties[i].clear();
1802 pointerProperties[i].id = i;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001803 pointerProperties[i].toolType = ToolType::FINGER;
Jackal Guof9696682018-10-05 12:23:23 +08001804
chaviwd1c23182019-12-20 18:44:56 -08001805 pointerCoords[i].clear();
1806 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x);
1807 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y);
1808 }
Jackal Guof9696682018-10-05 12:23:23 +08001809
1810 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1811 // Define a valid motion event.
Harry Cutts33476232023-01-30 19:57:29 +00001812 NotifyMotionArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, source, displayId,
Garfield Tan00f511d2019-06-12 16:55:40 -07001813 POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0,
1814 AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
chaviwd1c23182019-12-20 18:44:56 -08001815 AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties,
1816 pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0,
Garfield Tan00f511d2019-06-12 16:55:40 -07001817 AMOTION_EVENT_INVALID_CURSOR_POSITION,
1818 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {});
Jackal Guof9696682018-10-05 12:23:23 +08001819
1820 return args;
1821}
1822
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08001823static NotifyMotionArgs generateTouchArgs(int32_t action, const std::vector<PointF>& points) {
1824 return generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, points);
1825}
1826
chaviwd1c23182019-12-20 18:44:56 -08001827static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) {
1828 return generateMotionArgs(action, source, displayId, {PointF{100, 200}});
1829}
1830
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001831static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs(
1832 const PointerCaptureRequest& request) {
Harry Cutts33476232023-01-30 19:57:29 +00001833 return NotifyPointerCaptureChangedArgs(/*id=*/0, systemTime(SYSTEM_TIME_MONOTONIC), request);
Prabir Pradhan99987712020-11-10 18:43:05 -08001834}
1835
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001836/**
1837 * When a window unexpectedly disposes of its input channel, policy should be notified about the
1838 * broken channel.
1839 */
1840TEST_F(InputDispatcherTest, WhenInputChannelBreaks_PolicyIsNotified) {
1841 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1842 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001843 sp<FakeWindowHandle>::make(application, mDispatcher,
1844 "Window that breaks its input channel", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001845
1846 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1847
1848 // Window closes its channel, but the window remains.
1849 window->destroyReceiver();
1850 mFakePolicy->assertNotifyInputChannelBrokenWasCalled(window->getInfo()->token);
1851}
1852
Arthur Hungb92218b2018-08-14 12:00:21 +08001853TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001854 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001855 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1856 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001857
Arthur Hung72d8dc32020-03-28 00:48:39 +00001858 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001859 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1860 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1861 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001862
1863 // Window should receive motion event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001864 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001865}
1866
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001867TEST_F(InputDispatcherTest, WhenDisplayNotSpecified_InjectMotionToDefaultDisplay) {
1868 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001869 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1870 "Fake Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001871
1872 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1873 // Inject a MotionEvent to an unknown display.
1874 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1875 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_NONE))
1876 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1877
1878 // Window should receive motion event.
1879 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1880}
1881
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001882/**
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08001883 * Calling setInputWindows once should not cause any issues.
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001884 * This test serves as a sanity check for the next test, where setInputWindows is
1885 * called twice.
1886 */
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08001887TEST_F(InputDispatcherTest, SetInputWindowOnceWithSingleTouchWindow) {
Chris Yea209fde2020-07-22 13:54:51 -07001888 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001889 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1890 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001891 window->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001892
1893 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001894 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001895 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1896 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001897 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001898
1899 // Window should receive motion event.
1900 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1901}
1902
1903/**
1904 * Calling setInputWindows twice, with the same info, should not cause any issues.
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001905 */
1906TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001907 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001908 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1909 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001910 window->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001911
1912 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1913 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001914 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001915 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1916 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001917 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001918
1919 // Window should receive motion event.
1920 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1921}
1922
Arthur Hungb92218b2018-08-14 12:00:21 +08001923// The foreground window should receive the first touch down event.
1924TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001925 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001926 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001927 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001928 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001929 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001930
Arthur Hung72d8dc32020-03-28 00:48:39 +00001931 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001932 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1933 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1934 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001935
1936 // Top window should receive the touch down event. Second window should not receive anything.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001937 windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001938 windowSecond->assertNoEvents();
1939}
1940
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001941/**
1942 * Two windows: A top window, and a wallpaper behind the window.
1943 * Touch goes to the top window, and then top window disappears. Ensure that wallpaper window
1944 * gets ACTION_CANCEL.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001945 * 1. foregroundWindow <-- dup touch to wallpaper
1946 * 2. wallpaperWindow <-- is wallpaper
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001947 */
1948TEST_F(InputDispatcherTest, WhenForegroundWindowDisappears_WallpaperTouchIsCanceled) {
1949 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1950 sp<FakeWindowHandle> foregroundWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001951 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001952 foregroundWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001953 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001954 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001955 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001956
1957 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}});
1958 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1959 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1960 {100, 200}))
1961 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1962
1963 // Both foreground window and its wallpaper should receive the touch down
1964 foregroundWindow->consumeMotionDown();
1965 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1966
1967 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1968 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
1969 ADISPLAY_ID_DEFAULT, {110, 200}))
1970 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1971
1972 foregroundWindow->consumeMotionMove();
1973 wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1974
1975 // Now the foreground window goes away, but the wallpaper stays
1976 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}});
1977 foregroundWindow->consumeMotionCancel();
1978 // Since the "parent" window of the wallpaper is gone, wallpaper should receive cancel, too.
1979 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1980}
1981
1982/**
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001983 * Two fingers down on the window, and lift off the first finger.
1984 * Next, cancel the gesture to the window by removing the window. Make sure that the CANCEL event
1985 * contains a single pointer.
1986 */
1987TEST_F(InputDispatcherTest, CancelAfterPointer0Up) {
1988 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1989 sp<FakeWindowHandle> window =
1990 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
1991
1992 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001993 // First touch pointer down on right window
Prabir Pradhan678438e2023-04-13 19:32:51 +00001994 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
1995 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
1996 .build());
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001997 // Second touch pointer down
Prabir Pradhan678438e2023-04-13 19:32:51 +00001998 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
1999 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2000 .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100))
2001 .build());
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08002002 // First touch pointer lifts. The second one remains down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002003 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
2004 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2005 .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100))
2006 .build());
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08002007 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2008 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
2009 window->consumeMotionEvent(WithMotionAction(POINTER_0_UP));
2010
2011 // Remove the window. The gesture should be canceled
2012 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
2013 const std::map<int32_t, PointF> expectedPointers{{1, PointF{110, 100}}};
2014 window->consumeMotionEvent(
2015 AllOf(WithMotionAction(ACTION_CANCEL), WithPointers(expectedPointers)));
2016}
2017
2018/**
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08002019 * Same test as WhenForegroundWindowDisappears_WallpaperTouchIsCanceled above,
2020 * with the following differences:
2021 * After ACTION_DOWN, Wallpaper window hangs up its channel, which forces the dispatcher to
2022 * clean up the connection.
2023 * This later may crash dispatcher during ACTION_CANCEL synthesis, if the dispatcher is not careful.
2024 * Ensure that there's no crash in the dispatcher.
2025 */
2026TEST_F(InputDispatcherTest, WhenWallpaperDisappears_NoCrash) {
2027 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2028 sp<FakeWindowHandle> foregroundWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002029 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002030 foregroundWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08002031 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002032 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002033 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08002034
2035 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}});
2036 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2037 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2038 {100, 200}))
2039 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2040
2041 // Both foreground window and its wallpaper should receive the touch down
2042 foregroundWindow->consumeMotionDown();
2043 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2044
2045 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2046 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
2047 ADISPLAY_ID_DEFAULT, {110, 200}))
2048 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2049
2050 foregroundWindow->consumeMotionMove();
2051 wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2052
2053 // Wallpaper closes its channel, but the window remains.
2054 wallpaperWindow->destroyReceiver();
2055 mFakePolicy->assertNotifyInputChannelBrokenWasCalled(wallpaperWindow->getInfo()->token);
2056
2057 // Now the foreground window goes away, but the wallpaper stays, even though its channel
2058 // is no longer valid.
2059 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}});
2060 foregroundWindow->consumeMotionCancel();
2061}
2062
Arthur Hungc539dbb2022-12-08 07:45:36 +00002063class ShouldSplitTouchFixture : public InputDispatcherTest,
2064 public ::testing::WithParamInterface<bool> {};
2065INSTANTIATE_TEST_SUITE_P(InputDispatcherTest, ShouldSplitTouchFixture,
2066 ::testing::Values(true, false));
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08002067/**
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002068 * A single window that receives touch (on top), and a wallpaper window underneath it.
2069 * The top window gets a multitouch gesture.
2070 * Ensure that wallpaper gets the same gesture.
2071 */
Arthur Hungc539dbb2022-12-08 07:45:36 +00002072TEST_P(ShouldSplitTouchFixture, WallpaperWindowReceivesMultiTouch) {
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002073 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Arthur Hungc539dbb2022-12-08 07:45:36 +00002074 sp<FakeWindowHandle> foregroundWindow =
2075 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
2076 foregroundWindow->setDupTouchToWallpaper(true);
2077 foregroundWindow->setPreventSplitting(GetParam());
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002078
2079 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002080 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002081 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002082
Arthur Hungc539dbb2022-12-08 07:45:36 +00002083 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}});
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002084
2085 // Touch down on top window
2086 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2087 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2088 {100, 100}))
2089 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2090
2091 // Both top window and its wallpaper should receive the touch down
Arthur Hungc539dbb2022-12-08 07:45:36 +00002092 foregroundWindow->consumeMotionDown();
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002093 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2094
2095 // Second finger down on the top window
2096 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002097 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002098 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002099 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
2100 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002101 .build();
2102 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2103 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
2104 InputEventInjectionSync::WAIT_FOR_RESULT))
2105 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2106
Harry Cutts33476232023-01-30 19:57:29 +00002107 foregroundWindow->consumeMotionPointerDown(/*pointerIndex=*/1);
2108 wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002109 expectedWallpaperFlags);
Arthur Hungc539dbb2022-12-08 07:45:36 +00002110
2111 const MotionEvent secondFingerUpEvent =
2112 MotionEventBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
2113 .displayId(ADISPLAY_ID_DEFAULT)
2114 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002115 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
2116 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
Arthur Hungc539dbb2022-12-08 07:45:36 +00002117 .build();
2118 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2119 injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT,
2120 InputEventInjectionSync::WAIT_FOR_RESULT))
2121 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2122 foregroundWindow->consumeMotionPointerUp(0);
2123 wallpaperWindow->consumeMotionPointerUp(0, ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2124
2125 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08002126 injectMotionEvent(mDispatcher,
2127 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2128 AINPUT_SOURCE_TOUCHSCREEN)
2129 .displayId(ADISPLAY_ID_DEFAULT)
2130 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
2131 .pointer(PointerBuilder(/* id */ 1,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002132 ToolType::FINGER)
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08002133 .x(100)
2134 .y(100))
2135 .build(),
2136 INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT))
Arthur Hungc539dbb2022-12-08 07:45:36 +00002137 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2138 foregroundWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
2139 wallpaperWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002140}
2141
2142/**
2143 * Two windows: a window on the left and window on the right.
2144 * A third window, wallpaper, is behind both windows, and spans both top windows.
2145 * The first touch down goes to the left window. A second pointer touches down on the right window.
2146 * The touch is split, so both left and right windows should receive ACTION_DOWN.
2147 * The wallpaper will get the full event, so it should receive ACTION_DOWN followed by
2148 * ACTION_POINTER_DOWN(1).
2149 */
2150TEST_F(InputDispatcherTest, TwoWindows_SplitWallpaperTouch) {
2151 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2152 sp<FakeWindowHandle> leftWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002153 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002154 leftWindow->setFrame(Rect(0, 0, 200, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002155 leftWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002156
2157 sp<FakeWindowHandle> rightWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002158 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002159 rightWindow->setFrame(Rect(200, 0, 400, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002160 rightWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002161
2162 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002163 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002164 wallpaperWindow->setFrame(Rect(0, 0, 400, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002165 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002166
2167 mDispatcher->setInputWindows(
2168 {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}});
2169
2170 // Touch down on left window
2171 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2172 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2173 {100, 100}))
2174 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2175
2176 // Both foreground window and its wallpaper should receive the touch down
2177 leftWindow->consumeMotionDown();
2178 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2179
2180 // Second finger down on the right window
2181 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002182 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002183 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002184 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
2185 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(300).y(100))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002186 .build();
2187 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2188 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
2189 InputEventInjectionSync::WAIT_FOR_RESULT))
2190 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2191
2192 leftWindow->consumeMotionMove();
2193 // Since the touch is split, right window gets ACTION_DOWN
2194 rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Harry Cutts33476232023-01-30 19:57:29 +00002195 wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002196 expectedWallpaperFlags);
2197
2198 // Now, leftWindow, which received the first finger, disappears.
2199 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {rightWindow, wallpaperWindow}}});
2200 leftWindow->consumeMotionCancel();
2201 // Since a "parent" window of the wallpaper is gone, wallpaper should receive cancel, too.
2202 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2203
2204 // The pointer that's still down on the right window moves, and goes to the right window only.
2205 // As far as the dispatcher's concerned though, both pointers are still present.
2206 const MotionEvent secondFingerMoveEvent =
2207 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2208 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002209 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
2210 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(310).y(110))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002211 .build();
2212 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2213 injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT,
2214 InputEventInjectionSync::WAIT_FOR_RESULT));
2215 rightWindow->consumeMotionMove();
2216
2217 leftWindow->assertNoEvents();
2218 rightWindow->assertNoEvents();
2219 wallpaperWindow->assertNoEvents();
2220}
2221
Arthur Hungc539dbb2022-12-08 07:45:36 +00002222/**
2223 * Two windows: a window on the left with dup touch to wallpaper and window on the right without it.
2224 * The touch slips to the right window. so left window and wallpaper should receive ACTION_CANCEL
2225 * The right window should receive ACTION_DOWN.
2226 */
2227TEST_F(InputDispatcherTest, WallpaperWindowWhenSlippery) {
Arthur Hung74c248d2022-11-23 07:09:59 +00002228 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Arthur Hungc539dbb2022-12-08 07:45:36 +00002229 sp<FakeWindowHandle> leftWindow =
2230 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2231 leftWindow->setFrame(Rect(0, 0, 200, 200));
2232 leftWindow->setDupTouchToWallpaper(true);
2233 leftWindow->setSlippery(true);
2234
2235 sp<FakeWindowHandle> rightWindow =
2236 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2237 rightWindow->setFrame(Rect(200, 0, 400, 200));
Arthur Hung74c248d2022-11-23 07:09:59 +00002238
2239 sp<FakeWindowHandle> wallpaperWindow =
2240 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
2241 wallpaperWindow->setIsWallpaper(true);
Arthur Hung74c248d2022-11-23 07:09:59 +00002242
Arthur Hungc539dbb2022-12-08 07:45:36 +00002243 mDispatcher->setInputWindows(
2244 {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}});
Arthur Hung74c248d2022-11-23 07:09:59 +00002245
Arthur Hungc539dbb2022-12-08 07:45:36 +00002246 // Touch down on left window
Arthur Hung74c248d2022-11-23 07:09:59 +00002247 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2248 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Arthur Hungc539dbb2022-12-08 07:45:36 +00002249 {100, 100}))
Arthur Hung74c248d2022-11-23 07:09:59 +00002250 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungc539dbb2022-12-08 07:45:36 +00002251
2252 // Both foreground window and its wallpaper should receive the touch down
2253 leftWindow->consumeMotionDown();
Arthur Hung74c248d2022-11-23 07:09:59 +00002254 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2255
Arthur Hungc539dbb2022-12-08 07:45:36 +00002256 // Move to right window, the left window should receive cancel.
Arthur Hung74c248d2022-11-23 07:09:59 +00002257 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Arthur Hungc539dbb2022-12-08 07:45:36 +00002258 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
2259 ADISPLAY_ID_DEFAULT, {201, 100}))
Arthur Hung74c248d2022-11-23 07:09:59 +00002260 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2261
Arthur Hungc539dbb2022-12-08 07:45:36 +00002262 leftWindow->consumeMotionCancel();
2263 rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2264 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Arthur Hung74c248d2022-11-23 07:09:59 +00002265}
2266
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002267/**
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002268 * The policy typically sets POLICY_FLAG_PASS_TO_USER to the events. But when the display is not
2269 * interactive, it might stop sending this flag.
2270 * In this test, we check that if the policy stops sending this flag mid-gesture, we still ensure
2271 * to have a consistent input stream.
2272 *
2273 * Test procedure:
2274 * DOWN -> POINTER_DOWN -> (stop sending POLICY_FLAG_PASS_TO_USER) -> CANCEL.
2275 * DOWN (new gesture).
2276 *
2277 * In the bad implementation, we could potentially drop the CANCEL event, and get an inconsistent
2278 * state in the dispatcher. This would cause the final DOWN event to not be delivered to the app.
2279 *
2280 * We technically just need a single window here, but we are using two windows (spy on top and a
2281 * regular window below) to emulate the actual situation where it happens on the device.
2282 */
2283TEST_F(InputDispatcherTest, TwoPointerCancelInconsistentPolicy) {
2284 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2285 sp<FakeWindowHandle> spyWindow =
2286 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
2287 spyWindow->setFrame(Rect(0, 0, 200, 200));
2288 spyWindow->setTrustedOverlay(true);
2289 spyWindow->setSpy(true);
2290
2291 sp<FakeWindowHandle> window =
2292 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2293 window->setFrame(Rect(0, 0, 200, 200));
2294
2295 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
2296 const int32_t touchDeviceId = 4;
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002297
2298 // Two pointers down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002299 mDispatcher->notifyMotion(
2300 MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2301 .deviceId(touchDeviceId)
2302 .policyFlags(DEFAULT_POLICY_FLAGS)
2303 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2304 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002305
Prabir Pradhan678438e2023-04-13 19:32:51 +00002306 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2307 .deviceId(touchDeviceId)
2308 .policyFlags(DEFAULT_POLICY_FLAGS)
2309 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2310 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
2311 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002312 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2313 spyWindow->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
2314 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2315 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
2316
2317 // Cancel the current gesture. Send the cancel without the default policy flags.
Prabir Pradhan678438e2023-04-13 19:32:51 +00002318 mDispatcher->notifyMotion(
2319 MotionArgsBuilder(AMOTION_EVENT_ACTION_CANCEL, AINPUT_SOURCE_TOUCHSCREEN)
2320 .deviceId(touchDeviceId)
2321 .policyFlags(0)
2322 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2323 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
2324 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002325 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL));
2326 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL));
2327
2328 // We don't need to reset the device to reproduce the issue, but the reset event typically
2329 // follows, so we keep it here to model the actual listener behaviour more closely.
Prabir Pradhan678438e2023-04-13 19:32:51 +00002330 mDispatcher->notifyDeviceReset({/*id=*/1, systemTime(SYSTEM_TIME_MONOTONIC), touchDeviceId});
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002331
2332 // Start new gesture
Prabir Pradhan678438e2023-04-13 19:32:51 +00002333 mDispatcher->notifyMotion(
2334 MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2335 .deviceId(touchDeviceId)
2336 .policyFlags(DEFAULT_POLICY_FLAGS)
2337 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2338 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002339 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2340 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2341
2342 // No more events
2343 spyWindow->assertNoEvents();
2344 window->assertNoEvents();
2345}
2346
2347/**
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002348 * Two windows: a window on the left and a window on the right.
2349 * Mouse is hovered from the right window into the left window.
2350 * Next, we tap on the left window, where the cursor was last seen.
2351 * The second tap is done onto the right window.
2352 * The mouse and tap are from two different devices.
2353 * We technically don't need to set the downtime / eventtime for these events, but setting these
2354 * explicitly helps during debugging.
2355 * This test reproduces a crash where there is a mismatch between the downTime and eventTime.
2356 * In the buggy implementation, a tap on the right window would cause a crash.
2357 */
2358TEST_F(InputDispatcherTest, HoverFromLeftToRightAndTap) {
2359 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2360 sp<FakeWindowHandle> leftWindow =
2361 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2362 leftWindow->setFrame(Rect(0, 0, 200, 200));
2363
2364 sp<FakeWindowHandle> rightWindow =
2365 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2366 rightWindow->setFrame(Rect(200, 0, 400, 200));
2367
2368 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}});
2369 // All times need to start at the current time, otherwise the dispatcher will drop the events as
2370 // stale.
2371 const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC);
2372 const int32_t mouseDeviceId = 6;
2373 const int32_t touchDeviceId = 4;
2374 // Move the cursor from right
2375 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2376 injectMotionEvent(mDispatcher,
2377 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2378 AINPUT_SOURCE_MOUSE)
2379 .deviceId(mouseDeviceId)
2380 .downTime(baseTime + 10)
2381 .eventTime(baseTime + 20)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002382 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002383 .x(300)
2384 .y(100))
2385 .build()));
2386 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2387
2388 // .. to the left window
2389 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2390 injectMotionEvent(mDispatcher,
2391 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2392 AINPUT_SOURCE_MOUSE)
2393 .deviceId(mouseDeviceId)
2394 .downTime(baseTime + 10)
2395 .eventTime(baseTime + 30)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002396 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002397 .x(110)
2398 .y(100))
2399 .build()));
2400 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2401 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2402 // Now tap the left window
2403 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2404 injectMotionEvent(mDispatcher,
2405 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2406 AINPUT_SOURCE_TOUCHSCREEN)
2407 .deviceId(touchDeviceId)
2408 .downTime(baseTime + 40)
2409 .eventTime(baseTime + 40)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002410 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002411 .x(100)
2412 .y(100))
2413 .build()));
2414 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2415 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2416
2417 // release tap
2418 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2419 injectMotionEvent(mDispatcher,
2420 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2421 AINPUT_SOURCE_TOUCHSCREEN)
2422 .deviceId(touchDeviceId)
2423 .downTime(baseTime + 40)
2424 .eventTime(baseTime + 50)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002425 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002426 .x(100)
2427 .y(100))
2428 .build()));
2429 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2430
2431 // Tap the window on the right
2432 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2433 injectMotionEvent(mDispatcher,
2434 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2435 AINPUT_SOURCE_TOUCHSCREEN)
2436 .deviceId(touchDeviceId)
2437 .downTime(baseTime + 60)
2438 .eventTime(baseTime + 60)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002439 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002440 .x(300)
2441 .y(100))
2442 .build()));
2443 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2444
2445 // release tap
2446 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2447 injectMotionEvent(mDispatcher,
2448 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2449 AINPUT_SOURCE_TOUCHSCREEN)
2450 .deviceId(touchDeviceId)
2451 .downTime(baseTime + 60)
2452 .eventTime(baseTime + 70)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002453 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002454 .x(300)
2455 .y(100))
2456 .build()));
2457 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2458
2459 // No more events
2460 leftWindow->assertNoEvents();
2461 rightWindow->assertNoEvents();
2462}
2463
2464/**
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002465 * Start hovering in a window. While this hover is still active, make another window appear on top.
2466 * The top, obstructing window has no input channel, so it's not supposed to receive input.
2467 * While the top window is present, the hovering is stopped.
2468 * Later, hovering gets resumed again.
2469 * Ensure that new hover gesture is handled correctly.
2470 * This test reproduces a crash where the HOVER_EXIT event wasn't getting dispatched correctly
2471 * to the window that's currently being hovered over.
2472 */
2473TEST_F(InputDispatcherTest, HoverWhileWindowAppears) {
2474 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2475 sp<FakeWindowHandle> window =
2476 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2477 window->setFrame(Rect(0, 0, 200, 200));
2478
2479 // Only a single window is present at first
2480 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2481
2482 // Start hovering in the window
2483 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2484 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2485 .build());
2486 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2487
2488 // Now, an obscuring window appears!
2489 sp<FakeWindowHandle> obscuringWindow =
2490 sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window",
2491 ADISPLAY_ID_DEFAULT,
2492 /*token=*/std::make_optional<sp<IBinder>>(nullptr));
2493 obscuringWindow->setFrame(Rect(0, 0, 200, 200));
2494 obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED);
2495 obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
2496 obscuringWindow->setNoInputChannel(true);
2497 obscuringWindow->setFocusable(false);
2498 obscuringWindow->setAlpha(1.0);
2499 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
2500
2501 // While this new obscuring window is present, the hovering is stopped
2502 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
2503 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2504 .build());
2505 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2506
2507 // Now the obscuring window goes away.
2508 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2509
2510 // And a new hover gesture starts.
2511 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2512 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2513 .build());
2514 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2515}
2516
2517/**
2518 * Same test as 'HoverWhileWindowAppears' above, but here, we also send some HOVER_MOVE events to
2519 * the obscuring window.
2520 */
2521TEST_F(InputDispatcherTest, HoverMoveWhileWindowAppears) {
2522 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2523 sp<FakeWindowHandle> window =
2524 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2525 window->setFrame(Rect(0, 0, 200, 200));
2526
2527 // Only a single window is present at first
2528 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2529
2530 // Start hovering in the window
2531 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2532 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2533 .build());
2534 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2535
2536 // Now, an obscuring window appears!
2537 sp<FakeWindowHandle> obscuringWindow =
2538 sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window",
2539 ADISPLAY_ID_DEFAULT,
2540 /*token=*/std::make_optional<sp<IBinder>>(nullptr));
2541 obscuringWindow->setFrame(Rect(0, 0, 200, 200));
2542 obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED);
2543 obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
2544 obscuringWindow->setNoInputChannel(true);
2545 obscuringWindow->setFocusable(false);
2546 obscuringWindow->setAlpha(1.0);
2547 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
2548
2549 // While this new obscuring window is present, the hovering continues. The event can't go to the
2550 // bottom window due to obstructed touches, so it should generate HOVER_EXIT for that window.
2551 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2552 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2553 .build());
2554 obscuringWindow->assertNoEvents();
2555 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2556
2557 // Now the obscuring window goes away.
2558 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2559
2560 // Hovering continues in the same position. The hovering pointer re-enters the bottom window,
2561 // so it should generate a HOVER_ENTER
2562 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2563 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2564 .build());
2565 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2566
2567 // Now the MOVE should be getting dispatched normally
2568 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2569 .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110))
2570 .build());
2571 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE));
2572}
2573
2574/**
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002575 * Two windows: a window on the left and a window on the right.
2576 * Mouse is clicked on the left window and remains down. Touch is touched on the right and remains
2577 * down. Then, on the left window, also place second touch pointer down.
2578 * This test tries to reproduce a crash.
2579 * In the buggy implementation, second pointer down on the left window would cause a crash.
2580 */
2581TEST_F(InputDispatcherTest, MultiDeviceSplitTouch) {
2582 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2583 sp<FakeWindowHandle> leftWindow =
2584 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2585 leftWindow->setFrame(Rect(0, 0, 200, 200));
2586
2587 sp<FakeWindowHandle> rightWindow =
2588 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2589 rightWindow->setFrame(Rect(200, 0, 400, 200));
2590
2591 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}});
2592
2593 const int32_t touchDeviceId = 4;
2594 const int32_t mouseDeviceId = 6;
2595 NotifyMotionArgs args;
2596
2597 // Start hovering over the left window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002598 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
2599 .deviceId(mouseDeviceId)
2600 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2601 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002602 leftWindow->consumeMotionEvent(
2603 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
2604
2605 // Mouse down on left window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002606 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2607 .deviceId(mouseDeviceId)
2608 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2609 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2610 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002611
2612 leftWindow->consumeMotionEvent(
2613 AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId)));
2614 leftWindow->consumeMotionEvent(
2615 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
2616
Prabir Pradhan678438e2023-04-13 19:32:51 +00002617 mDispatcher->notifyMotion(
2618 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
2619 .deviceId(mouseDeviceId)
2620 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2621 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2622 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2623 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002624 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
2625
2626 // First touch pointer down on right window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002627 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2628 .deviceId(touchDeviceId)
2629 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2630 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002631 leftWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL));
2632
2633 rightWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2634
2635 // Second touch pointer down on left window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002636 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2637 .deviceId(touchDeviceId)
2638 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2639 .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100))
2640 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002641 leftWindow->consumeMotionEvent(
2642 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
2643 // This MOVE event is not necessary (doesn't carry any new information), but it's there in the
2644 // current implementation.
2645 const std::map<int32_t, PointF> expectedPointers{{0, PointF{100, 100}}};
2646 rightWindow->consumeMotionEvent(
2647 AllOf(WithMotionAction(ACTION_MOVE), WithPointers(expectedPointers)));
2648
2649 leftWindow->assertNoEvents();
2650 rightWindow->assertNoEvents();
2651}
2652
2653/**
2654 * On a single window, use two different devices: mouse and touch.
2655 * Touch happens first, with two pointers going down, and then the first pointer leaving.
2656 * Mouse is clicked next, which causes the touch stream to be aborted with ACTION_CANCEL.
2657 * Finally, a second touch pointer goes down again. Ensure the second touch pointer is ignored,
2658 * because the mouse is currently down, and a POINTER_DOWN event from the touchscreen does not
2659 * represent a new gesture.
2660 */
2661TEST_F(InputDispatcherTest, MixedTouchAndMouseWithPointerDown) {
2662 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2663 sp<FakeWindowHandle> window =
2664 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2665 window->setFrame(Rect(0, 0, 400, 400));
2666
2667 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2668
2669 const int32_t touchDeviceId = 4;
2670 const int32_t mouseDeviceId = 6;
2671 NotifyMotionArgs args;
2672
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08002673 // First touch pointer down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002674 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2675 .deviceId(touchDeviceId)
2676 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2677 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002678 // Second touch pointer down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002679 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2680 .deviceId(touchDeviceId)
2681 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2682 .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100))
2683 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002684 // First touch pointer lifts. The second one remains down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002685 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
2686 .deviceId(touchDeviceId)
2687 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2688 .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100))
2689 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002690 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2691 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
2692 window->consumeMotionEvent(WithMotionAction(POINTER_0_UP));
2693
2694 // Mouse down. The touch should be canceled
Prabir Pradhan678438e2023-04-13 19:32:51 +00002695 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2696 .deviceId(mouseDeviceId)
2697 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2698 .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100))
2699 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002700
2701 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId),
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08002702 WithPointerCount(1u)));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002703 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
2704
Prabir Pradhan678438e2023-04-13 19:32:51 +00002705 mDispatcher->notifyMotion(
2706 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
2707 .deviceId(mouseDeviceId)
2708 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2709 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2710 .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100))
2711 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002712 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
2713
2714 // Second touch pointer down.
Prabir Pradhan678438e2023-04-13 19:32:51 +00002715 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2716 .deviceId(touchDeviceId)
2717 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2718 .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100))
2719 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002720 // The pointer_down event should be ignored
2721 window->assertNoEvents();
2722}
2723
2724/**
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08002725 * Inject a touch down and then send a new event via 'notifyMotion'. Ensure the new event cancels
2726 * the injected event.
2727 */
2728TEST_F(InputDispatcherTest, UnfinishedInjectedEvent) {
2729 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2730 sp<FakeWindowHandle> window =
2731 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2732 window->setFrame(Rect(0, 0, 400, 400));
2733
2734 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2735
2736 const int32_t touchDeviceId = 4;
2737 NotifyMotionArgs args;
2738 // Pretend a test injects an ACTION_DOWN mouse event, but forgets to lift up the touch after
2739 // completion.
2740 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2741 injectMotionEvent(mDispatcher,
2742 MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2743 .deviceId(ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002744 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08002745 .x(50)
2746 .y(50))
2747 .build()));
2748 window->consumeMotionEvent(
2749 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(VIRTUAL_KEYBOARD_ID)));
2750
2751 // Now a real touch comes. Rather than crashing or dropping the real event, the injected pointer
2752 // should be canceled and the new gesture should take over.
Prabir Pradhan678438e2023-04-13 19:32:51 +00002753 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2754 .deviceId(touchDeviceId)
2755 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2756 .build());
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08002757
2758 window->consumeMotionEvent(
2759 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(VIRTUAL_KEYBOARD_ID)));
2760 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
2761}
2762
2763/**
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002764 * This test is similar to the test above, but the sequence of injected events is different.
2765 *
2766 * Two windows: a window on the left and a window on the right.
2767 * Mouse is hovered over the left window.
2768 * Next, we tap on the left window, where the cursor was last seen.
2769 *
2770 * After that, we inject one finger down onto the right window, and then a second finger down onto
2771 * the left window.
2772 * The touch is split, so this last gesture should cause 2 ACTION_DOWN events, one in the right
2773 * window (first), and then another on the left window (second).
2774 * This test reproduces a crash where there is a mismatch between the downTime and eventTime.
2775 * In the buggy implementation, second finger down on the left window would cause a crash.
2776 */
2777TEST_F(InputDispatcherTest, HoverTapAndSplitTouch) {
2778 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2779 sp<FakeWindowHandle> leftWindow =
2780 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2781 leftWindow->setFrame(Rect(0, 0, 200, 200));
2782
2783 sp<FakeWindowHandle> rightWindow =
2784 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2785 rightWindow->setFrame(Rect(200, 0, 400, 200));
2786
2787 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}});
2788
2789 const int32_t mouseDeviceId = 6;
2790 const int32_t touchDeviceId = 4;
2791 // Hover over the left window. Keep the cursor there.
2792 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2793 injectMotionEvent(mDispatcher,
2794 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
2795 AINPUT_SOURCE_MOUSE)
2796 .deviceId(mouseDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002797 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002798 .x(50)
2799 .y(50))
2800 .build()));
2801 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2802
2803 // Tap on left window
2804 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2805 injectMotionEvent(mDispatcher,
2806 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2807 AINPUT_SOURCE_TOUCHSCREEN)
2808 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002809 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002810 .x(100)
2811 .y(100))
2812 .build()));
2813
2814 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2815 injectMotionEvent(mDispatcher,
2816 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2817 AINPUT_SOURCE_TOUCHSCREEN)
2818 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002819 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002820 .x(100)
2821 .y(100))
2822 .build()));
2823 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2824 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2825 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2826
2827 // First finger down on right window
2828 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2829 injectMotionEvent(mDispatcher,
2830 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2831 AINPUT_SOURCE_TOUCHSCREEN)
2832 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002833 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002834 .x(300)
2835 .y(100))
2836 .build()));
2837 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2838
2839 // Second finger down on the left window
2840 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2841 injectMotionEvent(mDispatcher,
2842 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2843 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002844 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002845 .x(300)
2846 .y(100))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002847 .pointer(PointerBuilder(1, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002848 .x(100)
2849 .y(100))
2850 .build()));
2851 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2852 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_MOVE));
2853
2854 // No more events
2855 leftWindow->assertNoEvents();
2856 rightWindow->assertNoEvents();
2857}
2858
2859/**
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002860 * Start hovering with a stylus device, and then tap with a touch device. Ensure no crash occurs.
2861 * While the touch is down, new hover events from the stylus device should be ignored. After the
2862 * touch is gone, stylus hovering should start working again.
2863 */
2864TEST_F(InputDispatcherTest, StylusHoverAndTouchTap) {
2865 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2866 sp<FakeWindowHandle> window =
2867 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2868 window->setFrame(Rect(0, 0, 200, 200));
2869
2870 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2871
2872 const int32_t stylusDeviceId = 5;
2873 const int32_t touchDeviceId = 4;
2874 // Start hovering with stylus
2875 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2876 injectMotionEvent(mDispatcher,
2877 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
2878 AINPUT_SOURCE_STYLUS)
2879 .deviceId(stylusDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002880 .pointer(PointerBuilder(0, ToolType::STYLUS)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002881 .x(50)
2882 .y(50))
2883 .build()));
2884 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2885
2886 // Finger down on the window
2887 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2888 injectMotionEvent(mDispatcher,
2889 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2890 AINPUT_SOURCE_TOUCHSCREEN)
2891 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002892 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002893 .x(100)
2894 .y(100))
2895 .build()));
2896 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2897 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2898
2899 // Try to continue hovering with stylus. Since we are already down, injection should fail
2900 ASSERT_EQ(InputEventInjectionResult::FAILED,
2901 injectMotionEvent(mDispatcher,
2902 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2903 AINPUT_SOURCE_STYLUS)
2904 .deviceId(stylusDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002905 .pointer(PointerBuilder(0, ToolType::STYLUS)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002906 .x(50)
2907 .y(50))
2908 .build()));
2909 // No event should be sent. This event should be ignored because a pointer from another device
2910 // is already down.
2911
2912 // Lift up the finger
2913 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2914 injectMotionEvent(mDispatcher,
2915 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2916 AINPUT_SOURCE_TOUCHSCREEN)
2917 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002918 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002919 .x(100)
2920 .y(100))
2921 .build()));
2922 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2923
2924 // Now that the touch is gone, stylus hovering should start working again
2925 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2926 injectMotionEvent(mDispatcher,
2927 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2928 AINPUT_SOURCE_STYLUS)
2929 .deviceId(stylusDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002930 .pointer(PointerBuilder(0, ToolType::STYLUS)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002931 .x(50)
2932 .y(50))
2933 .build()));
2934 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2935 // No more events
2936 window->assertNoEvents();
2937}
2938
2939/**
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002940 * A spy window above a window with no input channel.
2941 * Start hovering with a stylus device, and then tap with it.
2942 * Ensure spy window receives the entire sequence.
2943 */
2944TEST_F(InputDispatcherTest, StylusHoverAndDownNoInputChannel) {
2945 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2946 sp<FakeWindowHandle> spyWindow =
2947 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
2948 spyWindow->setFrame(Rect(0, 0, 200, 200));
2949 spyWindow->setTrustedOverlay(true);
2950 spyWindow->setSpy(true);
2951 sp<FakeWindowHandle> window =
2952 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2953 window->setNoInputChannel(true);
2954 window->setFrame(Rect(0, 0, 200, 200));
2955
2956 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
2957
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002958 // Start hovering with stylus
Prabir Pradhan678438e2023-04-13 19:32:51 +00002959 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2960 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2961 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002962 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2963 // Stop hovering
Prabir Pradhan678438e2023-04-13 19:32:51 +00002964 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
2965 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2966 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002967 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2968
2969 // Stylus touches down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002970 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS)
2971 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2972 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002973 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2974
2975 // Stylus goes up
Prabir Pradhan678438e2023-04-13 19:32:51 +00002976 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_STYLUS)
2977 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2978 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002979 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_UP));
2980
2981 // Again hover
Prabir Pradhan678438e2023-04-13 19:32:51 +00002982 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2983 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2984 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002985 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2986 // Stop hovering
Prabir Pradhan678438e2023-04-13 19:32:51 +00002987 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
2988 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2989 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002990 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2991
2992 // No more events
2993 spyWindow->assertNoEvents();
2994 window->assertNoEvents();
2995}
2996
2997/**
2998 * Start hovering with a mouse, and then tap with a touch device. Pilfer the touch stream.
2999 * Next, click with the mouse device. Both windows (spy and regular) should receive the new mouse
3000 * ACTION_DOWN event because that's a new gesture, and pilfering should no longer be active.
3001 * While the mouse is down, new move events from the touch device should be ignored.
3002 */
3003TEST_F(InputDispatcherTest, TouchPilferAndMouseMove) {
3004 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3005 sp<FakeWindowHandle> spyWindow =
3006 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
3007 spyWindow->setFrame(Rect(0, 0, 200, 200));
3008 spyWindow->setTrustedOverlay(true);
3009 spyWindow->setSpy(true);
3010 sp<FakeWindowHandle> window =
3011 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3012 window->setFrame(Rect(0, 0, 200, 200));
3013
3014 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
3015
3016 const int32_t mouseDeviceId = 7;
3017 const int32_t touchDeviceId = 4;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003018
3019 // Hover a bit with mouse first
Prabir Pradhan678438e2023-04-13 19:32:51 +00003020 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
3021 .deviceId(mouseDeviceId)
3022 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
3023 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003024 spyWindow->consumeMotionEvent(
3025 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
3026 window->consumeMotionEvent(
3027 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
3028
3029 // Start touching
Prabir Pradhan678438e2023-04-13 19:32:51 +00003030 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3031 .deviceId(touchDeviceId)
3032 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
3033 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003034 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3035 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3036 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
3037 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
3038
Prabir Pradhan678438e2023-04-13 19:32:51 +00003039 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
3040 .deviceId(touchDeviceId)
3041 .pointer(PointerBuilder(0, ToolType::FINGER).x(55).y(55))
3042 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003043 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
3044 window->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
3045
3046 // Pilfer the stream
3047 EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken()));
3048 window->consumeMotionEvent(WithMotionAction(ACTION_CANCEL));
3049
Prabir Pradhan678438e2023-04-13 19:32:51 +00003050 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
3051 .deviceId(touchDeviceId)
3052 .pointer(PointerBuilder(0, ToolType::FINGER).x(60).y(60))
3053 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003054 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
3055
3056 // Mouse down
Prabir Pradhan678438e2023-04-13 19:32:51 +00003057 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3058 .deviceId(mouseDeviceId)
3059 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3060 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
3061 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003062
3063 spyWindow->consumeMotionEvent(
3064 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId)));
3065 spyWindow->consumeMotionEvent(
3066 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
3067 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
3068
Prabir Pradhan678438e2023-04-13 19:32:51 +00003069 mDispatcher->notifyMotion(
3070 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
3071 .deviceId(mouseDeviceId)
3072 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3073 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
3074 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
3075 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003076 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
3077 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
3078
3079 // Mouse move!
Prabir Pradhan678438e2023-04-13 19:32:51 +00003080 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE)
3081 .deviceId(mouseDeviceId)
3082 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3083 .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110))
3084 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003085 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
3086 window->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
3087
3088 // Touch move!
Prabir Pradhan678438e2023-04-13 19:32:51 +00003089 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
3090 .deviceId(touchDeviceId)
3091 .pointer(PointerBuilder(0, ToolType::FINGER).x(65).y(65))
3092 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003093
3094 // No more events
3095 spyWindow->assertNoEvents();
3096 window->assertNoEvents();
3097}
3098
3099/**
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003100 * On the display, have a single window, and also an area where there's no window.
3101 * First pointer touches the "no window" area of the screen. Second pointer touches the window.
3102 * Make sure that the window receives the second pointer, and first pointer is simply ignored.
3103 */
3104TEST_F(InputDispatcherTest, SplitWorksWhenEmptyAreaIsTouched) {
3105 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3106 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003107 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003108
3109 mDispatcher->setInputWindows({{DISPLAY_ID, {window}}});
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003110
3111 // Touch down on the empty space
Prabir Pradhan678438e2023-04-13 19:32:51 +00003112 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{-1, -1}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003113
3114 mDispatcher->waitForIdle();
3115 window->assertNoEvents();
3116
3117 // Now touch down on the window with another pointer
Prabir Pradhan678438e2023-04-13 19:32:51 +00003118 mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{-1, -1}, {10, 10}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003119 mDispatcher->waitForIdle();
3120 window->consumeMotionDown();
3121}
3122
3123/**
3124 * Same test as above, but instead of touching the empty space, the first touch goes to
3125 * non-touchable window.
3126 */
3127TEST_F(InputDispatcherTest, SplitWorksWhenNonTouchableWindowIsTouched) {
3128 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3129 sp<FakeWindowHandle> window1 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003130 sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003131 window1->setTouchableRegion(Region{{0, 0, 100, 100}});
3132 window1->setTouchable(false);
3133 sp<FakeWindowHandle> window2 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003134 sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003135 window2->setTouchableRegion(Region{{100, 0, 200, 100}});
3136
3137 mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}});
3138
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003139 // Touch down on the non-touchable window
Prabir Pradhan678438e2023-04-13 19:32:51 +00003140 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003141
3142 mDispatcher->waitForIdle();
3143 window1->assertNoEvents();
3144 window2->assertNoEvents();
3145
3146 // Now touch down on the window with another pointer
Prabir Pradhan678438e2023-04-13 19:32:51 +00003147 mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003148 mDispatcher->waitForIdle();
3149 window2->consumeMotionDown();
3150}
3151
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003152/**
3153 * When splitting touch events the downTime should be adjusted such that the downTime corresponds
3154 * to the event time of the first ACTION_DOWN sent to the particular window.
3155 */
3156TEST_F(InputDispatcherTest, SplitTouchesSendCorrectActionDownTime) {
3157 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3158 sp<FakeWindowHandle> window1 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003159 sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003160 window1->setTouchableRegion(Region{{0, 0, 100, 100}});
3161 sp<FakeWindowHandle> window2 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003162 sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003163 window2->setTouchableRegion(Region{{100, 0, 200, 100}});
3164
3165 mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}});
3166
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003167 // Touch down on the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00003168 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003169
3170 mDispatcher->waitForIdle();
3171 InputEvent* inputEvent1 = window1->consume();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08003172 ASSERT_NE(inputEvent1, nullptr);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003173 window2->assertNoEvents();
3174 MotionEvent& motionEvent1 = static_cast<MotionEvent&>(*inputEvent1);
3175 nsecs_t downTimeForWindow1 = motionEvent1.getDownTime();
3176 ASSERT_EQ(motionEvent1.getDownTime(), motionEvent1.getEventTime());
3177
3178 // Now touch down on the window with another pointer
Prabir Pradhan678438e2023-04-13 19:32:51 +00003179 mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003180 mDispatcher->waitForIdle();
3181 InputEvent* inputEvent2 = window2->consume();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08003182 ASSERT_NE(inputEvent2, nullptr);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003183 MotionEvent& motionEvent2 = static_cast<MotionEvent&>(*inputEvent2);
3184 nsecs_t downTimeForWindow2 = motionEvent2.getDownTime();
3185 ASSERT_NE(downTimeForWindow1, downTimeForWindow2);
3186 ASSERT_EQ(motionEvent2.getDownTime(), motionEvent2.getEventTime());
3187
3188 // Now move the pointer on the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00003189 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{50, 50}, {151, 51}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003190 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08003191 window2->consumeMotionEvent(WithDownTime(downTimeForWindow2));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003192
3193 // Now add new touch down on the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00003194 mDispatcher->notifyMotion(generateTouchArgs(POINTER_2_DOWN, {{50, 50}, {151, 51}, {150, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003195 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08003196 window2->consumeMotionEvent(WithDownTime(downTimeForWindow2));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003197
3198 // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line
3199 window1->consumeMotionMove();
3200 window1->assertNoEvents();
3201
3202 // Now move the pointer on the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00003203 mDispatcher->notifyMotion(
3204 generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{51, 51}, {151, 51}, {150, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003205 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08003206 window1->consumeMotionEvent(WithDownTime(downTimeForWindow1));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003207
Prabir Pradhan678438e2023-04-13 19:32:51 +00003208 mDispatcher->notifyMotion(
3209 generateTouchArgs(POINTER_3_DOWN, {{51, 51}, {151, 51}, {150, 50}, {50, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003210 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08003211 window1->consumeMotionEvent(WithDownTime(downTimeForWindow1));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003212}
3213
Garfield Tandf26e862020-07-01 20:18:19 -07003214TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) {
Chris Yea209fde2020-07-22 13:54:51 -07003215 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07003216 sp<FakeWindowHandle> windowLeft =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003217 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07003218 windowLeft->setFrame(Rect(0, 0, 600, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07003219 sp<FakeWindowHandle> windowRight =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003220 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07003221 windowRight->setFrame(Rect(600, 0, 1200, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07003222
3223 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3224
3225 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
3226
3227 // Start cursor position in right window so that we can move the cursor to left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003228 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003229 injectMotionEvent(mDispatcher,
3230 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3231 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003232 .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003233 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003234 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07003235
3236 // Move cursor into left window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003237 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003238 injectMotionEvent(mDispatcher,
3239 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3240 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003241 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003242 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003243 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
3244 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07003245
3246 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003247 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003248 injectMotionEvent(mDispatcher,
3249 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3250 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003251 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003252 .build()));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003253 windowLeft->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3254 windowLeft->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
Garfield Tandf26e862020-07-01 20:18:19 -07003255
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003256 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003257 injectMotionEvent(mDispatcher,
3258 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
3259 AINPUT_SOURCE_MOUSE)
3260 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3261 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003262 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003263 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003264 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
Garfield Tandf26e862020-07-01 20:18:19 -07003265
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003266 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003267 injectMotionEvent(mDispatcher,
3268 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
3269 AINPUT_SOURCE_MOUSE)
3270 .buttonState(0)
3271 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003272 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003273 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003274 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE));
Garfield Tandf26e862020-07-01 20:18:19 -07003275
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003276 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003277 injectMotionEvent(mDispatcher,
3278 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
3279 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003280 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003281 .build()));
3282 windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT);
3283
3284 // Move mouse cursor back to right window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003285 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003286 injectMotionEvent(mDispatcher,
3287 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3288 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003289 .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003290 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003291 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003292
3293 // No more events
3294 windowLeft->assertNoEvents();
3295 windowRight->assertNoEvents();
3296}
3297
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003298/**
3299 * Put two fingers down (and don't release them) and click the mouse button.
3300 * The clicking of mouse is a new ACTION_DOWN event. Since it's from a different device, the
3301 * currently active gesture should be canceled, and the new one should proceed.
3302 */
3303TEST_F(InputDispatcherTest, TwoPointersDownMouseClick) {
3304 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3305 sp<FakeWindowHandle> window =
3306 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3307 window->setFrame(Rect(0, 0, 600, 800));
3308
3309 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3310
3311 const int32_t touchDeviceId = 4;
3312 const int32_t mouseDeviceId = 6;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003313
3314 // Two pointers down
Prabir Pradhan678438e2023-04-13 19:32:51 +00003315 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3316 .deviceId(touchDeviceId)
3317 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
3318 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003319
Prabir Pradhan678438e2023-04-13 19:32:51 +00003320 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3321 .deviceId(touchDeviceId)
3322 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
3323 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
3324 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003325 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
3326 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
3327
3328 // Inject a series of mouse events for a mouse click
Prabir Pradhan678438e2023-04-13 19:32:51 +00003329 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3330 .deviceId(mouseDeviceId)
3331 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3332 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
3333 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003334 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId),
3335 WithPointerCount(2u)));
3336 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
3337
Prabir Pradhan678438e2023-04-13 19:32:51 +00003338 mDispatcher->notifyMotion(
3339 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
3340 .deviceId(mouseDeviceId)
3341 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3342 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
3343 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
3344 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003345 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
3346
3347 // Try to send more touch events while the mouse is down. Since it's a continuation of an
3348 // already canceled gesture, it should be ignored.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003349 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
3350 .deviceId(touchDeviceId)
3351 .pointer(PointerBuilder(0, ToolType::FINGER).x(101).y(101))
3352 .pointer(PointerBuilder(1, ToolType::FINGER).x(121).y(121))
3353 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003354 window->assertNoEvents();
3355}
3356
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003357TEST_F(InputDispatcherTest, HoverWithSpyWindows) {
3358 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3359
3360 sp<FakeWindowHandle> spyWindow =
3361 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
3362 spyWindow->setFrame(Rect(0, 0, 600, 800));
3363 spyWindow->setTrustedOverlay(true);
3364 spyWindow->setSpy(true);
3365 sp<FakeWindowHandle> window =
3366 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3367 window->setFrame(Rect(0, 0, 600, 800));
3368
3369 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3370 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
3371
3372 // Send mouse cursor to the window
3373 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3374 injectMotionEvent(mDispatcher,
3375 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3376 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003377 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003378 .x(100)
3379 .y(100))
3380 .build()));
3381
3382 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3383 WithSource(AINPUT_SOURCE_MOUSE)));
3384 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3385 WithSource(AINPUT_SOURCE_MOUSE)));
3386
3387 window->assertNoEvents();
3388 spyWindow->assertNoEvents();
Garfield Tandf26e862020-07-01 20:18:19 -07003389}
3390
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003391TEST_F(InputDispatcherTest, MouseAndTouchWithSpyWindows) {
3392 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3393
3394 sp<FakeWindowHandle> spyWindow =
3395 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
3396 spyWindow->setFrame(Rect(0, 0, 600, 800));
3397 spyWindow->setTrustedOverlay(true);
3398 spyWindow->setSpy(true);
3399 sp<FakeWindowHandle> window =
3400 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3401 window->setFrame(Rect(0, 0, 600, 800));
3402
3403 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3404 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
3405
3406 // Send mouse cursor to the window
3407 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3408 injectMotionEvent(mDispatcher,
3409 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3410 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003411 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003412 .x(100)
3413 .y(100))
3414 .build()));
3415
3416 // Move mouse cursor
3417 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3418 injectMotionEvent(mDispatcher,
3419 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3420 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003421 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003422 .x(110)
3423 .y(110))
3424 .build()));
3425
3426 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3427 WithSource(AINPUT_SOURCE_MOUSE)));
3428 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3429 WithSource(AINPUT_SOURCE_MOUSE)));
3430 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
3431 WithSource(AINPUT_SOURCE_MOUSE)));
3432 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
3433 WithSource(AINPUT_SOURCE_MOUSE)));
3434 // Touch down on the window
3435 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3436 injectMotionEvent(mDispatcher,
3437 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
3438 AINPUT_SOURCE_TOUCHSCREEN)
3439 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003440 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003441 .x(200)
3442 .y(200))
3443 .build()));
3444 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
3445 WithSource(AINPUT_SOURCE_MOUSE)));
3446 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
3447 WithSource(AINPUT_SOURCE_MOUSE)));
3448 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3449 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3450 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3451 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3452
3453 // pilfer the motion, retaining the gesture on the spy window.
3454 EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken()));
3455 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
3456 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3457
3458 // Touch UP on the window
3459 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3460 injectMotionEvent(mDispatcher,
3461 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
3462 AINPUT_SOURCE_TOUCHSCREEN)
3463 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003464 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003465 .x(200)
3466 .y(200))
3467 .build()));
3468 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3469 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3470
3471 // Previously, a touch was pilfered. However, that gesture was just finished. Now, we are going
3472 // to send a new gesture. It should again go to both windows (spy and the window below), just
3473 // like the first gesture did, before pilfering. The window configuration has not changed.
3474
3475 // One more tap - DOWN
3476 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3477 injectMotionEvent(mDispatcher,
3478 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
3479 AINPUT_SOURCE_TOUCHSCREEN)
3480 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003481 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003482 .x(250)
3483 .y(250))
3484 .build()));
3485 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3486 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3487 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3488 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3489
3490 // Touch UP on the window
3491 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3492 injectMotionEvent(mDispatcher,
3493 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
3494 AINPUT_SOURCE_TOUCHSCREEN)
3495 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003496 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003497 .x(250)
3498 .y(250))
3499 .build()));
3500 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3501 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3502 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3503 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3504
3505 window->assertNoEvents();
3506 spyWindow->assertNoEvents();
3507}
3508
Garfield Tandf26e862020-07-01 20:18:19 -07003509// This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected
3510// directly in this test.
3511TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) {
Chris Yea209fde2020-07-22 13:54:51 -07003512 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07003513 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003514 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07003515 window->setFrame(Rect(0, 0, 1200, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07003516
3517 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3518
3519 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3520
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003521 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003522 injectMotionEvent(mDispatcher,
3523 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3524 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003525 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003526 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003527 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07003528 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003529 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003530 injectMotionEvent(mDispatcher,
3531 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3532 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003533 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003534 .build()));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003535 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3536 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
Garfield Tandf26e862020-07-01 20:18:19 -07003537
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003538 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003539 injectMotionEvent(mDispatcher,
3540 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
3541 AINPUT_SOURCE_MOUSE)
3542 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3543 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003544 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003545 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003546 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
Garfield Tandf26e862020-07-01 20:18:19 -07003547
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003548 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003549 injectMotionEvent(mDispatcher,
3550 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
3551 AINPUT_SOURCE_MOUSE)
3552 .buttonState(0)
3553 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003554 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003555 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003556 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE));
Garfield Tandf26e862020-07-01 20:18:19 -07003557
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003558 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003559 injectMotionEvent(mDispatcher,
3560 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
3561 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003562 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003563 .build()));
3564 window->consumeMotionUp(ADISPLAY_ID_DEFAULT);
3565
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07003566 // We already canceled the hovering implicitly by injecting the "DOWN" event without lifting the
3567 // hover first. Therefore, injection of HOVER_EXIT is inconsistent, and should fail.
3568 ASSERT_EQ(InputEventInjectionResult::FAILED,
Garfield Tandf26e862020-07-01 20:18:19 -07003569 injectMotionEvent(mDispatcher,
3570 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT,
3571 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003572 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003573 .build()));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003574 window->assertNoEvents();
Garfield Tandf26e862020-07-01 20:18:19 -07003575}
3576
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003577/**
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003578 * Hover over a window, and then remove that window. Make sure that HOVER_EXIT for that event
3579 * is generated.
3580 */
3581TEST_F(InputDispatcherTest, HoverExitIsSentToRemovedWindow) {
3582 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3583 sp<FakeWindowHandle> window =
3584 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3585 window->setFrame(Rect(0, 0, 1200, 800));
3586
3587 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3588
3589 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3590
3591 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3592 injectMotionEvent(mDispatcher,
3593 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3594 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003595 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003596 .x(300)
3597 .y(400))
3598 .build()));
3599 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
3600
3601 // Remove the window, but keep the channel.
3602 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
3603 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
3604}
3605
3606/**
Daniel Norman5fe59da2023-08-02 16:39:45 -07003607 * Test that invalid HOVER events sent by accessibility do not cause a fatal crash.
3608 */
3609TEST_F(InputDispatcherTest, InvalidA11yHoverStreamDoesNotCrash) {
3610 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3611 sp<FakeWindowHandle> window =
3612 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3613 window->setFrame(Rect(0, 0, 1200, 800));
3614 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3615 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3616
3617 MotionEventBuilder hoverEnterBuilder =
3618 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
3619 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
3620 .addFlag(AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
3621 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3622 injectMotionEvent(mDispatcher, hoverEnterBuilder.build()));
3623 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3624 injectMotionEvent(mDispatcher, hoverEnterBuilder.build()));
3625 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
3626 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
3627}
3628
3629/**
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003630 * If mouse is hovering when the touch goes down, the hovering should be stopped via HOVER_EXIT.
3631 */
3632TEST_F(InputDispatcherTest, TouchDownAfterMouseHover) {
3633 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3634 sp<FakeWindowHandle> window =
3635 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3636 window->setFrame(Rect(0, 0, 100, 100));
3637
3638 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3639
3640 const int32_t mouseDeviceId = 7;
3641 const int32_t touchDeviceId = 4;
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003642
3643 // Start hovering with the mouse
Prabir Pradhan678438e2023-04-13 19:32:51 +00003644 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
3645 .deviceId(mouseDeviceId)
3646 .pointer(PointerBuilder(0, ToolType::MOUSE).x(10).y(10))
3647 .build());
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003648 window->consumeMotionEvent(
3649 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
3650
3651 // Touch goes down
Prabir Pradhan678438e2023-04-13 19:32:51 +00003652 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3653 .deviceId(touchDeviceId)
3654 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
3655 .build());
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003656
3657 window->consumeMotionEvent(
3658 AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId)));
3659 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
3660}
3661
3662/**
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003663 * Inject a mouse hover event followed by a tap from touchscreen.
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003664 * The tap causes a HOVER_EXIT event to be generated because the current event
3665 * stream's source has been switched.
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003666 */
3667TEST_F(InputDispatcherTest, MouseHoverAndTouchTap) {
3668 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3669 sp<FakeWindowHandle> window =
3670 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3671 window->setFrame(Rect(0, 0, 100, 100));
3672
3673 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3674
3675 // Inject a hover_move from mouse.
3676 NotifyMotionArgs motionArgs =
3677 generateMotionArgs(AMOTION_EVENT_ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE,
3678 ADISPLAY_ID_DEFAULT, {{50, 50}});
3679 motionArgs.xCursorPosition = 50;
3680 motionArgs.yCursorPosition = 50;
Prabir Pradhan678438e2023-04-13 19:32:51 +00003681 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003682 ASSERT_NO_FATAL_FAILURE(
3683 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3684 WithSource(AINPUT_SOURCE_MOUSE))));
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003685
3686 // Tap on the window
Prabir Pradhan678438e2023-04-13 19:32:51 +00003687 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3688 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3689 {{10, 10}}));
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003690 ASSERT_NO_FATAL_FAILURE(
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003691 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
3692 WithSource(AINPUT_SOURCE_MOUSE))));
3693
3694 ASSERT_NO_FATAL_FAILURE(
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003695 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3696 WithSource(AINPUT_SOURCE_TOUCHSCREEN))));
3697
Prabir Pradhan678438e2023-04-13 19:32:51 +00003698 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3699 ADISPLAY_ID_DEFAULT, {{10, 10}}));
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003700 ASSERT_NO_FATAL_FAILURE(
3701 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3702 WithSource(AINPUT_SOURCE_TOUCHSCREEN))));
3703}
3704
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003705TEST_F(InputDispatcherTest, HoverEnterMoveRemoveWindowsInSecondDisplay) {
3706 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3707 sp<FakeWindowHandle> windowDefaultDisplay =
3708 sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay",
3709 ADISPLAY_ID_DEFAULT);
3710 windowDefaultDisplay->setFrame(Rect(0, 0, 600, 800));
3711 sp<FakeWindowHandle> windowSecondDisplay =
3712 sp<FakeWindowHandle>::make(application, mDispatcher, "SecondDisplay",
3713 SECOND_DISPLAY_ID);
3714 windowSecondDisplay->setFrame(Rect(0, 0, 600, 800));
3715
3716 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}},
3717 {SECOND_DISPLAY_ID, {windowSecondDisplay}}});
3718
3719 // Set cursor position in window in default display and check that hover enter and move
3720 // events are generated.
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(300)
3728 .y(600))
3729 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003730 windowDefaultDisplay->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003731
3732 // Remove all windows in secondary display and check that no event happens on window in
3733 // primary display.
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003734 mDispatcher->setInputWindows(
3735 {{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}}, {SECOND_DISPLAY_ID, {}}});
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003736 windowDefaultDisplay->assertNoEvents();
3737
3738 // Move cursor position in window in default display and check that only hover move
3739 // event is generated and not hover enter event.
3740 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}},
3741 {SECOND_DISPLAY_ID, {windowSecondDisplay}}});
3742 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3743 injectMotionEvent(mDispatcher,
3744 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3745 AINPUT_SOURCE_MOUSE)
3746 .displayId(ADISPLAY_ID_DEFAULT)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003747 .pointer(PointerBuilder(0, ToolType::MOUSE)
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003748 .x(400)
3749 .y(700))
3750 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003751 windowDefaultDisplay->consumeMotionEvent(
3752 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
3753 WithSource(AINPUT_SOURCE_MOUSE)));
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003754 windowDefaultDisplay->assertNoEvents();
3755}
3756
Garfield Tan00f511d2019-06-12 16:55:40 -07003757TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) {
Chris Yea209fde2020-07-22 13:54:51 -07003758 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tan00f511d2019-06-12 16:55:40 -07003759
3760 sp<FakeWindowHandle> windowLeft =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003761 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07003762 windowLeft->setFrame(Rect(0, 0, 600, 800));
Garfield Tan00f511d2019-06-12 16:55:40 -07003763 sp<FakeWindowHandle> windowRight =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003764 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07003765 windowRight->setFrame(Rect(600, 0, 1200, 800));
Garfield Tan00f511d2019-06-12 16:55:40 -07003766
3767 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3768
Arthur Hung72d8dc32020-03-28 00:48:39 +00003769 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
Garfield Tan00f511d2019-06-12 16:55:40 -07003770
3771 // Inject an event with coordinate in the area of right window, with mouse cursor in the area of
3772 // left window. This event should be dispatched to the left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003773 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tan00f511d2019-06-12 16:55:40 -07003774 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003775 ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400}));
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08003776 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07003777 windowRight->assertNoEvents();
3778}
3779
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003780TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) {
Chris Yea209fde2020-07-22 13:54:51 -07003781 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003782 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3783 "Fake Window", ADISPLAY_ID_DEFAULT);
Vishnu Nair47074b82020-08-14 11:54:47 -07003784 window->setFocusable(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003785
Arthur Hung72d8dc32020-03-28 00:48:39 +00003786 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003787 setFocusedWindow(window);
3788
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003789 window->consumeFocusEvent(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003790
Prabir Pradhan678438e2023-04-13 19:32:51 +00003791 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003792
3793 // Window should receive key down event.
3794 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3795
3796 // When device reset happens, that key stream should be terminated with FLAG_CANCELED
3797 // on the app side.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003798 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07003799 window->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003800 AKEY_EVENT_FLAG_CANCELED);
3801}
3802
3803TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) {
Chris Yea209fde2020-07-22 13:54:51 -07003804 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003805 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3806 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003807
Arthur Hung72d8dc32020-03-28 00:48:39 +00003808 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003809
Prabir Pradhan678438e2023-04-13 19:32:51 +00003810 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3811 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003812
3813 // Window should receive motion down event.
3814 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3815
3816 // When device reset happens, that motion stream should be terminated with ACTION_CANCEL
3817 // on the app side.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003818 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08003819 window->consumeMotionEvent(
3820 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003821}
3822
Siarhei Vishniakou0686f0c2023-05-02 11:56:15 -07003823TEST_F(InputDispatcherTest, NotifyDeviceResetCancelsHoveringStream) {
3824 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3825 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3826 "Fake Window", ADISPLAY_ID_DEFAULT);
3827
3828 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3829
3830 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
3831 .pointer(PointerBuilder(0, ToolType::STYLUS).x(10).y(10))
3832 .build());
3833
3834 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
3835
3836 // When device reset happens, that hover stream should be terminated with ACTION_HOVER_EXIT
3837 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
3838 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3839
3840 // After the device has been reset, a new hovering stream can be sent to the window
3841 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
3842 .pointer(PointerBuilder(0, ToolType::STYLUS).x(15).y(15))
3843 .build());
3844 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
3845}
3846
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003847TEST_F(InputDispatcherTest, InterceptKeyByPolicy) {
3848 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003849 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3850 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003851 window->setFocusable(true);
3852
3853 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3854 setFocusedWindow(window);
3855
3856 window->consumeFocusEvent(true);
3857
Prabir Pradhan678438e2023-04-13 19:32:51 +00003858 const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003859 const std::chrono::milliseconds interceptKeyTimeout = 50ms;
3860 const nsecs_t injectTime = keyArgs.eventTime;
3861 mFakePolicy->setInterceptKeyTimeout(interceptKeyTimeout);
Prabir Pradhan678438e2023-04-13 19:32:51 +00003862 mDispatcher->notifyKey(keyArgs);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003863 // The dispatching time should be always greater than or equal to intercept key timeout.
3864 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3865 ASSERT_TRUE((systemTime(SYSTEM_TIME_MONOTONIC) - injectTime) >=
3866 std::chrono::nanoseconds(interceptKeyTimeout).count());
3867}
3868
3869TEST_F(InputDispatcherTest, InterceptKeyIfKeyUp) {
3870 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003871 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3872 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003873 window->setFocusable(true);
3874
3875 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3876 setFocusedWindow(window);
3877
3878 window->consumeFocusEvent(true);
3879
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003880 mFakePolicy->setInterceptKeyTimeout(150ms);
Prabir Pradhan678438e2023-04-13 19:32:51 +00003881 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
3882 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003883
3884 // Window should receive key event immediately when same key up.
3885 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3886 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
3887}
3888
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003889/**
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08003890 * Two windows. First is a regular window. Second does not overlap with the first, and has
3891 * WATCH_OUTSIDE_TOUCH.
3892 * Both windows are owned by the same UID.
3893 * Tap first window. Make sure that the second window receives ACTION_OUTSIDE with correct, non-zero
3894 * coordinates. The coordinates are not zeroed out because both windows are owned by the same UID.
3895 */
3896TEST_F(InputDispatcherTest, ActionOutsideForOwnedWindowHasValidCoordinates) {
3897 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3898 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3899 "First Window", ADISPLAY_ID_DEFAULT);
3900 window->setFrame(Rect{0, 0, 100, 100});
3901
3902 sp<FakeWindowHandle> outsideWindow =
3903 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3904 ADISPLAY_ID_DEFAULT);
3905 outsideWindow->setFrame(Rect{100, 100, 200, 200});
3906 outsideWindow->setWatchOutsideTouch(true);
3907 // outsideWindow must be above 'window' to receive ACTION_OUTSIDE events when 'window' is tapped
3908 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {outsideWindow, window}}});
3909
3910 // Tap on first window.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003911 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3912 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3913 {PointF{50, 50}}));
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08003914 window->consumeMotionDown();
3915 // The coordinates of the tap in 'outsideWindow' are relative to its top left corner.
3916 // Therefore, we should offset them by (100, 100) relative to the screen's top left corner.
3917 outsideWindow->consumeMotionEvent(
3918 AllOf(WithMotionAction(ACTION_OUTSIDE), WithCoords(-50, -50)));
3919}
3920
3921/**
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003922 * This test documents the behavior of WATCH_OUTSIDE_TOUCH. The window will get ACTION_OUTSIDE when
3923 * a another pointer causes ACTION_DOWN to be sent to another window for the first time. Only one
3924 * ACTION_OUTSIDE event is sent per gesture.
3925 */
3926TEST_F(InputDispatcherTest, ActionOutsideSentOnlyWhenAWindowIsTouched) {
3927 // There are three windows that do not overlap. `window` wants to WATCH_OUTSIDE_TOUCH.
3928 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003929 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3930 "First Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003931 window->setWatchOutsideTouch(true);
3932 window->setFrame(Rect{0, 0, 100, 100});
3933 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003934 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3935 ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003936 secondWindow->setFrame(Rect{100, 100, 200, 200});
3937 sp<FakeWindowHandle> thirdWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003938 sp<FakeWindowHandle>::make(application, mDispatcher, "Third Window",
3939 ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003940 thirdWindow->setFrame(Rect{200, 200, 300, 300});
3941 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, secondWindow, thirdWindow}}});
3942
3943 // First pointer lands outside all windows. `window` does not get ACTION_OUTSIDE.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003944 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3945 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3946 {PointF{-10, -10}}));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003947 window->assertNoEvents();
3948 secondWindow->assertNoEvents();
3949
3950 // The second pointer lands inside `secondWindow`, which should receive a DOWN event.
3951 // Now, `window` should get ACTION_OUTSIDE.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003952 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3953 ADISPLAY_ID_DEFAULT,
3954 {PointF{-10, -10}, PointF{105, 105}}));
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08003955 const std::map<int32_t, PointF> expectedPointers{{0, PointF{-10, -10}}, {1, PointF{105, 105}}};
3956 window->consumeMotionEvent(
3957 AllOf(WithMotionAction(ACTION_OUTSIDE), WithPointers(expectedPointers)));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003958 secondWindow->consumeMotionDown();
3959 thirdWindow->assertNoEvents();
3960
3961 // The third pointer lands inside `thirdWindow`, which should receive a DOWN event. There is
3962 // no ACTION_OUTSIDE sent to `window` because one has already been sent for this gesture.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003963 mDispatcher->notifyMotion(
3964 generateMotionArgs(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3965 {PointF{-10, -10}, PointF{105, 105}, PointF{205, 205}}));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003966 window->assertNoEvents();
3967 secondWindow->consumeMotionMove();
3968 thirdWindow->consumeMotionDown();
3969}
3970
Prabir Pradhan814fe082022-07-22 20:22:18 +00003971TEST_F(InputDispatcherTest, OnWindowInfosChanged_RemoveAllWindowsOnDisplay) {
3972 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003973 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3974 "Fake Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan814fe082022-07-22 20:22:18 +00003975 window->setFocusable(true);
3976
Patrick Williamsd828f302023-04-28 17:52:08 -05003977 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Prabir Pradhan814fe082022-07-22 20:22:18 +00003978 setFocusedWindow(window);
3979
3980 window->consumeFocusEvent(true);
3981
Prabir Pradhan678438e2023-04-13 19:32:51 +00003982 const NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
3983 const NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
3984 mDispatcher->notifyKey(keyDown);
3985 mDispatcher->notifyKey(keyUp);
Prabir Pradhan814fe082022-07-22 20:22:18 +00003986
3987 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3988 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
3989
3990 // All windows are removed from the display. Ensure that we can no longer dispatch to it.
Patrick Williamsd828f302023-04-28 17:52:08 -05003991 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
Prabir Pradhan814fe082022-07-22 20:22:18 +00003992
3993 window->consumeFocusEvent(false);
3994
Prabir Pradhan678438e2023-04-13 19:32:51 +00003995 mDispatcher->notifyKey(keyDown);
3996 mDispatcher->notifyKey(keyUp);
Prabir Pradhan814fe082022-07-22 20:22:18 +00003997 window->assertNoEvents();
3998}
3999
Arthur Hung96483742022-11-15 03:30:48 +00004000TEST_F(InputDispatcherTest, NonSplitTouchableWindowReceivesMultiTouch) {
4001 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4002 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4003 "Fake Window", ADISPLAY_ID_DEFAULT);
4004 // Ensure window is non-split and have some transform.
4005 window->setPreventSplitting(true);
4006 window->setWindowOffset(20, 40);
Patrick Williamsd828f302023-04-28 17:52:08 -05004007 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Arthur Hung96483742022-11-15 03:30:48 +00004008
4009 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4010 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4011 {50, 50}))
4012 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4013 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4014
4015 const MotionEvent secondFingerDownEvent =
4016 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4017 .displayId(ADISPLAY_ID_DEFAULT)
4018 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004019 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
4020 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(-30).y(-50))
Arthur Hung96483742022-11-15 03:30:48 +00004021 .build();
4022 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4023 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
4024 InputEventInjectionSync::WAIT_FOR_RESULT))
4025 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4026
4027 const MotionEvent* event = window->consumeMotion();
4028 EXPECT_EQ(POINTER_1_DOWN, event->getAction());
4029 EXPECT_EQ(70, event->getX(0)); // 50 + 20
4030 EXPECT_EQ(90, event->getY(0)); // 50 + 40
4031 EXPECT_EQ(-10, event->getX(1)); // -30 + 20
4032 EXPECT_EQ(-10, event->getY(1)); // -50 + 40
4033}
4034
Harry Cuttsb166c002023-05-09 13:06:05 +00004035TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeOnlySentToTrustedOverlays) {
4036 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4037 sp<FakeWindowHandle> window =
4038 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4039 window->setFrame(Rect(0, 0, 400, 400));
4040 sp<FakeWindowHandle> trustedOverlay =
4041 sp<FakeWindowHandle>::make(application, mDispatcher, "Trusted Overlay",
4042 ADISPLAY_ID_DEFAULT);
4043 trustedOverlay->setSpy(true);
4044 trustedOverlay->setTrustedOverlay(true);
4045
4046 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {trustedOverlay, window}}});
4047
4048 // Start a three-finger touchpad swipe
4049 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
4050 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
4051 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4052 .build());
4053 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE)
4054 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
4055 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
4056 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4057 .build());
4058 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE)
4059 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
4060 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
4061 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100))
4062 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4063 .build());
4064
4065 trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
4066 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
4067 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_DOWN));
4068
4069 // Move the swipe a bit
4070 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE)
4071 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4072 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
4073 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
4074 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4075 .build());
4076
4077 trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
4078
4079 // End the swipe
4080 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE)
4081 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4082 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
4083 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
4084 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4085 .build());
4086 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE)
4087 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4088 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
4089 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4090 .build());
4091 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE)
4092 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4093 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4094 .build());
4095
4096 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_UP));
4097 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_UP));
4098 trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_UP));
4099
4100 window->assertNoEvents();
4101}
4102
4103TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeNotSentToSingleWindow) {
4104 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4105 sp<FakeWindowHandle> window =
4106 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4107 window->setFrame(Rect(0, 0, 400, 400));
4108 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4109
4110 // Start a three-finger touchpad swipe
4111 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
4112 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
4113 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4114 .build());
4115 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE)
4116 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
4117 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
4118 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4119 .build());
4120 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE)
4121 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
4122 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
4123 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100))
4124 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4125 .build());
4126
4127 // Move the swipe a bit
4128 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE)
4129 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4130 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
4131 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
4132 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4133 .build());
4134
4135 // End the swipe
4136 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE)
4137 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4138 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
4139 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
4140 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4141 .build());
4142 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE)
4143 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4144 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
4145 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4146 .build());
4147 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE)
4148 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4149 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4150 .build());
4151
4152 window->assertNoEvents();
4153}
4154
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00004155/**
Hu Guoba4ed762023-09-17 20:51:08 +08004156 * When there are multiple screens, such as screen projection to TV or screen recording, if the
4157 * cancel event occurs, the coordinates of the cancel event should be sent to the target screen, and
4158 * its coordinates should be converted by the transform of the windows of target screen.
4159 */
4160TEST_F(InputDispatcherTest, WhenMultiDisplayWindowSameToken_DispatchCancelToTargetDisplay) {
4161 // This case will create a window and a spy window on the default display and mirror
4162 // window on the second display. cancel event is sent through spy window pilferPointers
4163 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4164
4165 sp<FakeWindowHandle> spyWindowDefaultDisplay =
4166 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
4167 spyWindowDefaultDisplay->setTrustedOverlay(true);
4168 spyWindowDefaultDisplay->setSpy(true);
4169
4170 sp<FakeWindowHandle> windowDefaultDisplay =
4171 sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay",
4172 ADISPLAY_ID_DEFAULT);
4173 windowDefaultDisplay->setWindowTransform(1, 0, 0, 1);
4174
4175 sp<FakeWindowHandle> windowSecondDisplay =
4176 windowDefaultDisplay->clone(application, mDispatcher, SECOND_DISPLAY_ID);
4177 windowSecondDisplay->setWindowTransform(2, 0, 0, 2);
4178
4179 // Add the windows to the dispatcher
4180 mDispatcher->onWindowInfosChanged(
4181 {{*spyWindowDefaultDisplay->getInfo(), *windowDefaultDisplay->getInfo(),
4182 *windowSecondDisplay->getInfo()},
4183 {},
4184 0,
4185 0});
4186
4187 // Send down to ADISPLAY_ID_DEFAULT
4188 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4189 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4190 {100, 100}))
4191 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4192
4193 spyWindowDefaultDisplay->consumeMotionDown();
4194 windowDefaultDisplay->consumeMotionDown();
4195
4196 EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindowDefaultDisplay->getToken()));
4197
4198 // windowDefaultDisplay gets cancel
4199 MotionEvent* event = windowDefaultDisplay->consumeMotion();
4200 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, event->getAction());
4201
4202 // The cancel event is sent to windowDefaultDisplay of the ADISPLAY_ID_DEFAULT display, so the
4203 // coordinates of the cancel are converted by windowDefaultDisplay's transform, the x and y
4204 // coordinates are both 100, otherwise if the cancel event is sent to windowSecondDisplay of
4205 // SECOND_DISPLAY_ID, the x and y coordinates are 200
4206 EXPECT_EQ(100, event->getX(0));
4207 EXPECT_EQ(100, event->getY(0));
4208}
4209
4210/**
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004211 * Ensure the correct coordinate spaces are used by InputDispatcher.
4212 *
4213 * InputDispatcher works in the display space, so its coordinate system is relative to the display
4214 * panel. Windows get events in the window space, and get raw coordinates in the logical display
4215 * space.
4216 */
4217class InputDispatcherDisplayProjectionTest : public InputDispatcherTest {
4218public:
4219 void SetUp() override {
4220 InputDispatcherTest::SetUp();
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004221 removeAllWindowsAndDisplays();
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004222 }
4223
4224 void addDisplayInfo(int displayId, const ui::Transform& transform) {
4225 gui::DisplayInfo info;
4226 info.displayId = displayId;
4227 info.transform = transform;
4228 mDisplayInfos.push_back(std::move(info));
Patrick Williamsd828f302023-04-28 17:52:08 -05004229 mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0});
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004230 }
4231
4232 void addWindow(const sp<WindowInfoHandle>& windowHandle) {
4233 mWindowInfos.push_back(*windowHandle->getInfo());
Patrick Williamsd828f302023-04-28 17:52:08 -05004234 mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0});
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004235 }
4236
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004237 void removeAllWindowsAndDisplays() {
4238 mDisplayInfos.clear();
4239 mWindowInfos.clear();
4240 }
4241
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004242 // Set up a test scenario where the display has a scaled projection and there are two windows
4243 // on the display.
4244 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupScaledDisplayScenario() {
4245 // The display has a projection that has a scale factor of 2 and 4 in the x and y directions
4246 // respectively.
4247 ui::Transform displayTransform;
4248 displayTransform.set(2, 0, 0, 4);
4249 addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform);
4250
4251 std::shared_ptr<FakeApplicationHandle> application =
4252 std::make_shared<FakeApplicationHandle>();
4253
4254 // Add two windows to the display. Their frames are represented in the display space.
4255 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004256 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4257 ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004258 firstWindow->setFrame(Rect(0, 0, 100, 200), displayTransform);
4259 addWindow(firstWindow);
4260
4261 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004262 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4263 ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004264 secondWindow->setFrame(Rect(100, 200, 200, 400), displayTransform);
4265 addWindow(secondWindow);
4266 return {std::move(firstWindow), std::move(secondWindow)};
4267 }
4268
4269private:
4270 std::vector<gui::DisplayInfo> mDisplayInfos;
4271 std::vector<gui::WindowInfo> mWindowInfos;
4272};
4273
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004274TEST_F(InputDispatcherDisplayProjectionTest, HitTestCoordinateSpaceConsistency) {
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004275 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
4276 // Send down to the first window. The point is represented in the display space. The point is
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004277 // selected so that if the hit test was performed with the point and the bounds being in
4278 // different coordinate spaces, the event would end up in the incorrect window.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004279 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4280 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4281 {PointF{75, 55}}));
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004282
4283 firstWindow->consumeMotionDown();
4284 secondWindow->assertNoEvents();
4285}
4286
4287// Ensure that when a MotionEvent is injected through the InputDispatcher::injectInputEvent() API,
4288// the event should be treated as being in the logical display space.
4289TEST_F(InputDispatcherDisplayProjectionTest, InjectionInLogicalDisplaySpace) {
4290 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
4291 // Send down to the first window. The point is represented in the logical display space. The
4292 // point is selected so that if the hit test was done in logical display space, then it would
4293 // end up in the incorrect window.
4294 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4295 PointF{75 * 2, 55 * 4});
4296
4297 firstWindow->consumeMotionDown();
4298 secondWindow->assertNoEvents();
4299}
4300
Prabir Pradhandaa2f142021-12-10 09:30:08 +00004301// Ensure that when a MotionEvent that has a custom transform is injected, the post-transformed
4302// event should be treated as being in the logical display space.
4303TEST_F(InputDispatcherDisplayProjectionTest, InjectionWithTransformInLogicalDisplaySpace) {
4304 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
4305
4306 const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0};
4307 ui::Transform injectedEventTransform;
4308 injectedEventTransform.set(matrix);
4309 const vec2 expectedPoint{75, 55}; // The injected point in the logical display space.
4310 const vec2 untransformedPoint = injectedEventTransform.inverse().transform(expectedPoint);
4311
4312 MotionEvent event = MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4313 .displayId(ADISPLAY_ID_DEFAULT)
4314 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004315 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER)
Prabir Pradhandaa2f142021-12-10 09:30:08 +00004316 .x(untransformedPoint.x)
4317 .y(untransformedPoint.y))
4318 .build();
4319 event.transform(matrix);
4320
4321 injectMotionEvent(mDispatcher, event, INJECT_EVENT_TIMEOUT,
4322 InputEventInjectionSync::WAIT_FOR_RESULT);
4323
4324 firstWindow->consumeMotionDown();
4325 secondWindow->assertNoEvents();
4326}
4327
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004328TEST_F(InputDispatcherDisplayProjectionTest, WindowGetsEventsInCorrectCoordinateSpace) {
4329 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
4330
4331 // Send down to the second window.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004332 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4333 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4334 {PointF{150, 220}}));
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004335
4336 firstWindow->assertNoEvents();
4337 const MotionEvent* event = secondWindow->consumeMotion();
Siarhei Vishniakoub681c202023-05-01 11:22:33 -07004338 ASSERT_NE(nullptr, event);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004339 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, event->getAction());
4340
4341 // Ensure that the events from the "getRaw" API are in logical display coordinates.
4342 EXPECT_EQ(300, event->getRawX(0));
4343 EXPECT_EQ(880, event->getRawY(0));
4344
4345 // Ensure that the x and y values are in the window's coordinate space.
4346 // The left-top of the second window is at (100, 200) in display space, which is (200, 800) in
4347 // the logical display space. This will be the origin of the window space.
4348 EXPECT_EQ(100, event->getX(0));
4349 EXPECT_EQ(80, event->getY(0));
4350}
4351
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004352/** Ensure consistent behavior of InputDispatcher in all orientations. */
4353class InputDispatcherDisplayOrientationFixture
4354 : public InputDispatcherDisplayProjectionTest,
4355 public ::testing::WithParamInterface<ui::Rotation> {};
4356
4357// This test verifies the touchable region of a window for all rotations of the display by tapping
4358// in different locations on the display, specifically points close to the four corners of a
4359// window.
4360TEST_P(InputDispatcherDisplayOrientationFixture, HitTestInDifferentOrientations) {
4361 constexpr static int32_t displayWidth = 400;
4362 constexpr static int32_t displayHeight = 800;
4363
4364 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4365
4366 const auto rotation = GetParam();
4367
4368 // Set up the display with the specified rotation.
4369 const bool isRotated = rotation == ui::ROTATION_90 || rotation == ui::ROTATION_270;
4370 const int32_t logicalDisplayWidth = isRotated ? displayHeight : displayWidth;
4371 const int32_t logicalDisplayHeight = isRotated ? displayWidth : displayHeight;
4372 const ui::Transform displayTransform(ui::Transform::toRotationFlags(rotation),
4373 logicalDisplayWidth, logicalDisplayHeight);
4374 addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform);
4375
4376 // Create a window with its bounds determined in the logical display.
4377 const Rect frameInLogicalDisplay(100, 100, 200, 300);
4378 const Rect frameInDisplay = displayTransform.inverse().transform(frameInLogicalDisplay);
4379 sp<FakeWindowHandle> window =
4380 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4381 window->setFrame(frameInDisplay, displayTransform);
4382 addWindow(window);
4383
4384 // The following points in logical display space should be inside the window.
4385 static const std::array<vec2, 4> insidePoints{
4386 {{100, 100}, {199.99, 100}, {100, 299.99}, {199.99, 299.99}}};
4387 for (const auto pointInsideWindow : insidePoints) {
4388 const vec2 p = displayTransform.inverse().transform(pointInsideWindow);
4389 const PointF pointInDisplaySpace{p.x, p.y};
Prabir Pradhan678438e2023-04-13 19:32:51 +00004390 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4391 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4392 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004393 window->consumeMotionDown();
4394
Prabir Pradhan678438e2023-04-13 19:32:51 +00004395 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP,
4396 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4397 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004398 window->consumeMotionUp();
4399 }
4400
4401 // The following points in logical display space should be outside the window.
4402 static const std::array<vec2, 5> outsidePoints{
4403 {{200, 100}, {100, 300}, {200, 300}, {100, 99.99}, {99.99, 100}}};
4404 for (const auto pointOutsideWindow : outsidePoints) {
4405 const vec2 p = displayTransform.inverse().transform(pointOutsideWindow);
4406 const PointF pointInDisplaySpace{p.x, p.y};
Prabir Pradhan678438e2023-04-13 19:32:51 +00004407 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4408 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4409 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004410
Prabir Pradhan678438e2023-04-13 19:32:51 +00004411 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP,
4412 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4413 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004414 }
4415 window->assertNoEvents();
4416}
4417
4418// Run the precision tests for all rotations.
4419INSTANTIATE_TEST_SUITE_P(InputDispatcherDisplayOrientationTests,
4420 InputDispatcherDisplayOrientationFixture,
4421 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
4422 ui::ROTATION_270),
4423 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
4424 return ftl::enum_string(testParamInfo.param);
4425 });
4426
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004427using TransferFunction = std::function<bool(const std::unique_ptr<InputDispatcher>& dispatcher,
4428 sp<IBinder>, sp<IBinder>)>;
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004429
4430class TransferTouchFixture : public InputDispatcherTest,
4431 public ::testing::WithParamInterface<TransferFunction> {};
4432
4433TEST_P(TransferTouchFixture, TransferTouch_OnePointer) {
Chris Yea209fde2020-07-22 13:54:51 -07004434 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004435
4436 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004437 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004438 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4439 ADISPLAY_ID_DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00004440 firstWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004441 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004442 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4443 ADISPLAY_ID_DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00004444 sp<FakeWindowHandle> wallpaper =
4445 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
4446 wallpaper->setIsWallpaper(true);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004447 // Add the windows to the dispatcher
Arthur Hungc539dbb2022-12-08 07:45:36 +00004448 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow, wallpaper}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08004449
4450 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004451 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4452 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00004453
Svet Ganov5d3bc372020-01-26 23:11:07 -08004454 // Only the first window should get the down event
4455 firstWindow->consumeMotionDown();
4456 secondWindow->assertNoEvents();
Arthur Hungc539dbb2022-12-08 07:45:36 +00004457 wallpaper->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004458
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004459 // Transfer touch to the second window
4460 TransferFunction f = GetParam();
4461 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4462 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004463 // The first window gets cancel and the second gets down
4464 firstWindow->consumeMotionCancel();
4465 secondWindow->consumeMotionDown();
Arthur Hungc539dbb2022-12-08 07:45:36 +00004466 wallpaper->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004467
4468 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004469 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4470 ADISPLAY_ID_DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004471 // The first window gets no events and the second gets up
4472 firstWindow->assertNoEvents();
4473 secondWindow->consumeMotionUp();
Arthur Hungc539dbb2022-12-08 07:45:36 +00004474 wallpaper->assertNoEvents();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004475}
4476
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004477/**
4478 * When 'transferTouch' API is invoked, dispatcher needs to find the "best" window to take touch
4479 * from. When we have spy windows, there are several windows to choose from: either spy, or the
4480 * 'real' (non-spy) window. Always prefer the 'real' window because that's what would be most
4481 * natural to the user.
4482 * In this test, we are sending a pointer to both spy window and first window. We then try to
4483 * transfer touch to the second window. The dispatcher should identify the first window as the
4484 * one that should lose the gesture, and therefore the action should be to move the gesture from
4485 * the first window to the second.
4486 * The main goal here is to test the behaviour of 'transferTouch' API, but it's still valid to test
4487 * the other API, as well.
4488 */
4489TEST_P(TransferTouchFixture, TransferTouch_MultipleWindowsWithSpy) {
4490 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4491
4492 // Create a couple of windows + a spy window
4493 sp<FakeWindowHandle> spyWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004494 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004495 spyWindow->setTrustedOverlay(true);
4496 spyWindow->setSpy(true);
4497 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004498 sp<FakeWindowHandle>::make(application, mDispatcher, "First", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004499 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004500 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004501
4502 // Add the windows to the dispatcher
4503 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, firstWindow, secondWindow}}});
4504
4505 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004506 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4507 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004508 // Only the first window and spy should get the down event
4509 spyWindow->consumeMotionDown();
4510 firstWindow->consumeMotionDown();
4511
4512 // Transfer touch to the second window. Non-spy window should be preferred over the spy window
4513 // if f === 'transferTouch'.
4514 TransferFunction f = GetParam();
4515 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4516 ASSERT_TRUE(success);
4517 // The first window gets cancel and the second gets down
4518 firstWindow->consumeMotionCancel();
4519 secondWindow->consumeMotionDown();
4520
4521 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004522 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4523 ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004524 // The first window gets no events and the second+spy get up
4525 firstWindow->assertNoEvents();
4526 spyWindow->consumeMotionUp();
4527 secondWindow->consumeMotionUp();
4528}
4529
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004530TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07004531 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004532
4533 PointF touchPoint = {10, 10};
4534
4535 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004536 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004537 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4538 ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08004539 firstWindow->setPreventSplitting(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004540 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004541 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4542 ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08004543 secondWindow->setPreventSplitting(true);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004544
4545 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00004546 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08004547
4548 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004549 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4550 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4551 {touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004552 // Only the first window should get the down event
4553 firstWindow->consumeMotionDown();
4554 secondWindow->assertNoEvents();
4555
4556 // Send pointer down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004557 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4558 ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004559 // Only the first window should get the pointer down event
4560 firstWindow->consumeMotionPointerDown(1);
4561 secondWindow->assertNoEvents();
4562
4563 // Transfer touch focus to the second window
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004564 TransferFunction f = GetParam();
4565 bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4566 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004567 // The first window gets cancel and the second gets down and pointer down
4568 firstWindow->consumeMotionCancel();
4569 secondWindow->consumeMotionDown();
4570 secondWindow->consumeMotionPointerDown(1);
4571
4572 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004573 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
4574 ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004575 // The first window gets nothing and the second gets pointer up
4576 firstWindow->assertNoEvents();
4577 secondWindow->consumeMotionPointerUp(1);
4578
4579 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004580 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4581 ADISPLAY_ID_DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004582 // The first window gets nothing and the second gets up
4583 firstWindow->assertNoEvents();
4584 secondWindow->consumeMotionUp();
4585}
4586
Arthur Hungc539dbb2022-12-08 07:45:36 +00004587TEST_P(TransferTouchFixture, TransferTouch_MultipleWallpapers) {
4588 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4589
4590 // Create a couple of windows
4591 sp<FakeWindowHandle> firstWindow =
4592 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4593 ADISPLAY_ID_DEFAULT);
4594 firstWindow->setDupTouchToWallpaper(true);
4595 sp<FakeWindowHandle> secondWindow =
4596 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4597 ADISPLAY_ID_DEFAULT);
4598 secondWindow->setDupTouchToWallpaper(true);
4599
4600 sp<FakeWindowHandle> wallpaper1 =
4601 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper1", ADISPLAY_ID_DEFAULT);
4602 wallpaper1->setIsWallpaper(true);
4603
4604 sp<FakeWindowHandle> wallpaper2 =
4605 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper2", ADISPLAY_ID_DEFAULT);
4606 wallpaper2->setIsWallpaper(true);
4607 // Add the windows to the dispatcher
4608 mDispatcher->setInputWindows(
4609 {{ADISPLAY_ID_DEFAULT, {firstWindow, wallpaper1, secondWindow, wallpaper2}}});
4610
4611 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004612 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4613 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00004614
4615 // Only the first window should get the down event
4616 firstWindow->consumeMotionDown();
4617 secondWindow->assertNoEvents();
4618 wallpaper1->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4619 wallpaper2->assertNoEvents();
4620
4621 // Transfer touch focus to the second window
4622 TransferFunction f = GetParam();
4623 bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4624 ASSERT_TRUE(success);
4625
4626 // The first window gets cancel and the second gets down
4627 firstWindow->consumeMotionCancel();
4628 secondWindow->consumeMotionDown();
4629 wallpaper1->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4630 wallpaper2->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4631
4632 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004633 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4634 ADISPLAY_ID_DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00004635 // The first window gets no events and the second gets up
4636 firstWindow->assertNoEvents();
4637 secondWindow->consumeMotionUp();
4638 wallpaper1->assertNoEvents();
4639 wallpaper2->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4640}
4641
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004642// For the cases of single pointer touch and two pointers non-split touch, the api's
4643// 'transferTouch' and 'transferTouchFocus' are equivalent in behaviour. They only differ
4644// for the case where there are multiple pointers split across several windows.
4645INSTANTIATE_TEST_SUITE_P(TransferFunctionTests, TransferTouchFixture,
4646 ::testing::Values(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004647 [&](const std::unique_ptr<InputDispatcher>& dispatcher,
4648 sp<IBinder> /*ignored*/, sp<IBinder> destChannelToken) {
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004649 return dispatcher->transferTouch(destChannelToken,
4650 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004651 },
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004652 [&](const std::unique_ptr<InputDispatcher>& dispatcher,
4653 sp<IBinder> from, sp<IBinder> to) {
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004654 return dispatcher->transferTouchFocus(from, to,
Harry Cutts33476232023-01-30 19:57:29 +00004655 /*isDragAndDrop=*/false);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004656 }));
4657
Svet Ganov5d3bc372020-01-26 23:11:07 -08004658TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07004659 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004660
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004661 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004662 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4663 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004664 firstWindow->setFrame(Rect(0, 0, 600, 400));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004665
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004666 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004667 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4668 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004669 secondWindow->setFrame(Rect(0, 400, 600, 800));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004670
4671 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00004672 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08004673
4674 PointF pointInFirst = {300, 200};
4675 PointF pointInSecond = {300, 600};
4676
4677 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004678 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4679 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4680 {pointInFirst}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004681 // Only the first window should get the down event
4682 firstWindow->consumeMotionDown();
4683 secondWindow->assertNoEvents();
4684
4685 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004686 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4687 ADISPLAY_ID_DEFAULT,
4688 {pointInFirst, pointInSecond}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004689 // The first window gets a move and the second a down
4690 firstWindow->consumeMotionMove();
4691 secondWindow->consumeMotionDown();
4692
4693 // Transfer touch focus to the second window
4694 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
4695 // The first window gets cancel and the new gets pointer down (it already saw down)
4696 firstWindow->consumeMotionCancel();
4697 secondWindow->consumeMotionPointerDown(1);
4698
4699 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004700 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
4701 ADISPLAY_ID_DEFAULT,
4702 {pointInFirst, pointInSecond}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004703 // The first window gets nothing and the second gets pointer up
4704 firstWindow->assertNoEvents();
4705 secondWindow->consumeMotionPointerUp(1);
4706
4707 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004708 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4709 ADISPLAY_ID_DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004710 // The first window gets nothing and the second gets up
4711 firstWindow->assertNoEvents();
4712 secondWindow->consumeMotionUp();
4713}
4714
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004715// Same as TransferTouchFocus_TwoPointersSplitTouch, but using 'transferTouch' api.
4716// Unlike 'transferTouchFocus', calling 'transferTouch' when there are two windows receiving
4717// touch is not supported, so the touch should continue on those windows and the transferred-to
4718// window should get nothing.
4719TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) {
4720 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4721
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004722 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004723 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4724 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004725 firstWindow->setFrame(Rect(0, 0, 600, 400));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004726
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004727 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004728 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4729 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004730 secondWindow->setFrame(Rect(0, 400, 600, 800));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004731
4732 // Add the windows to the dispatcher
4733 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
4734
4735 PointF pointInFirst = {300, 200};
4736 PointF pointInSecond = {300, 600};
4737
4738 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004739 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4740 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4741 {pointInFirst}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004742 // Only the first window should get the down event
4743 firstWindow->consumeMotionDown();
4744 secondWindow->assertNoEvents();
4745
4746 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004747 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4748 ADISPLAY_ID_DEFAULT,
4749 {pointInFirst, pointInSecond}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004750 // The first window gets a move and the second a down
4751 firstWindow->consumeMotionMove();
4752 secondWindow->consumeMotionDown();
4753
4754 // Transfer touch focus to the second window
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004755 const bool transferred =
4756 mDispatcher->transferTouch(secondWindow->getToken(), ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004757 // The 'transferTouch' call should not succeed, because there are 2 touched windows
4758 ASSERT_FALSE(transferred);
4759 firstWindow->assertNoEvents();
4760 secondWindow->assertNoEvents();
4761
4762 // The rest of the dispatch should proceed as normal
4763 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004764 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
4765 ADISPLAY_ID_DEFAULT,
4766 {pointInFirst, pointInSecond}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004767 // The first window gets MOVE and the second gets pointer up
4768 firstWindow->consumeMotionMove();
4769 secondWindow->consumeMotionUp();
4770
4771 // Send up event to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004772 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4773 ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004774 // The first window gets nothing and the second gets up
4775 firstWindow->consumeMotionUp();
4776 secondWindow->assertNoEvents();
4777}
4778
Arthur Hungabbb9d82021-09-01 14:52:30 +00004779// This case will create two windows and one mirrored window on the default display and mirror
4780// two windows on the second display. It will test if 'transferTouchFocus' works fine if we put
4781// the windows info of second display before default display.
4782TEST_F(InputDispatcherTest, TransferTouchFocus_CloneSurface) {
4783 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4784 sp<FakeWindowHandle> firstWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004785 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004786 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004787 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004788 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004789 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004790
4791 sp<FakeWindowHandle> mirrorWindowInPrimary =
4792 firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT);
4793 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004794
4795 sp<FakeWindowHandle> firstWindowInSecondary =
4796 firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
4797 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004798
4799 sp<FakeWindowHandle> secondWindowInSecondary =
4800 secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
4801 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004802
4803 // Update window info, let it find window handle of second display first.
4804 mDispatcher->setInputWindows(
4805 {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}},
4806 {ADISPLAY_ID_DEFAULT,
4807 {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}});
4808
4809 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4810 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4811 {50, 50}))
4812 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4813
4814 // Window should receive motion event.
4815 firstWindowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4816
4817 // Transfer touch focus
4818 ASSERT_TRUE(mDispatcher->transferTouchFocus(firstWindowInPrimary->getToken(),
4819 secondWindowInPrimary->getToken()));
4820 // The first window gets cancel.
4821 firstWindowInPrimary->consumeMotionCancel();
4822 secondWindowInPrimary->consumeMotionDown();
4823
4824 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4825 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4826 ADISPLAY_ID_DEFAULT, {150, 50}))
4827 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4828 firstWindowInPrimary->assertNoEvents();
4829 secondWindowInPrimary->consumeMotionMove();
4830
4831 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4832 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4833 {150, 50}))
4834 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4835 firstWindowInPrimary->assertNoEvents();
4836 secondWindowInPrimary->consumeMotionUp();
4837}
4838
4839// Same as TransferTouchFocus_CloneSurface, but this touch on the secondary display and use
4840// 'transferTouch' api.
4841TEST_F(InputDispatcherTest, TransferTouch_CloneSurface) {
4842 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4843 sp<FakeWindowHandle> firstWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004844 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004845 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004846 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004847 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004848 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004849
4850 sp<FakeWindowHandle> mirrorWindowInPrimary =
4851 firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT);
4852 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004853
4854 sp<FakeWindowHandle> firstWindowInSecondary =
4855 firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
4856 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004857
4858 sp<FakeWindowHandle> secondWindowInSecondary =
4859 secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
4860 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004861
4862 // Update window info, let it find window handle of second display first.
4863 mDispatcher->setInputWindows(
4864 {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}},
4865 {ADISPLAY_ID_DEFAULT,
4866 {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}});
4867
4868 // Touch on second display.
4869 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4870 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {50, 50}))
4871 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4872
4873 // Window should receive motion event.
4874 firstWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID);
4875
4876 // Transfer touch focus
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004877 ASSERT_TRUE(mDispatcher->transferTouch(secondWindowInSecondary->getToken(), SECOND_DISPLAY_ID));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004878
4879 // The first window gets cancel.
4880 firstWindowInPrimary->consumeMotionCancel(SECOND_DISPLAY_ID);
4881 secondWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID);
4882
4883 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4884 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4885 SECOND_DISPLAY_ID, {150, 50}))
4886 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4887 firstWindowInPrimary->assertNoEvents();
4888 secondWindowInPrimary->consumeMotionMove(SECOND_DISPLAY_ID);
4889
4890 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4891 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {150, 50}))
4892 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4893 firstWindowInPrimary->assertNoEvents();
4894 secondWindowInPrimary->consumeMotionUp(SECOND_DISPLAY_ID);
4895}
4896
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004897TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07004898 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004899 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4900 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004901
Vishnu Nair47074b82020-08-14 11:54:47 -07004902 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004903 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004904 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004905
4906 window->consumeFocusEvent(true);
4907
Prabir Pradhan678438e2023-04-13 19:32:51 +00004908 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004909
4910 // Window should receive key down event.
4911 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Josep del Riob3981622023-04-18 15:49:45 +00004912
4913 // Should have poked user activity
4914 mFakePolicy->assertUserActivityPoked();
4915}
4916
4917TEST_F(InputDispatcherTest, FocusedWindow_DisableUserActivity) {
4918 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4919 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4920 "Fake Window", ADISPLAY_ID_DEFAULT);
4921
4922 window->setDisableUserActivity(true);
4923 window->setFocusable(true);
4924 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4925 setFocusedWindow(window);
4926
4927 window->consumeFocusEvent(true);
4928
4929 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
4930
4931 // Window should receive key down event.
4932 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4933
4934 // Should have poked user activity
4935 mFakePolicy->assertUserActivityNotPoked();
4936}
4937
4938TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveSystemShortcut) {
4939 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4940 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4941 "Fake Window", ADISPLAY_ID_DEFAULT);
4942
4943 window->setFocusable(true);
4944 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4945 setFocusedWindow(window);
4946
4947 window->consumeFocusEvent(true);
4948
4949 mDispatcher->notifyKey(generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
4950 mDispatcher->waitForIdle();
4951
4952 // System key is not passed down
4953 window->assertNoEvents();
4954
4955 // Should have poked user activity
4956 mFakePolicy->assertUserActivityPoked();
4957}
4958
4959TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveAssistantKey) {
4960 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4961 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4962 "Fake Window", ADISPLAY_ID_DEFAULT);
4963
4964 window->setFocusable(true);
4965 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4966 setFocusedWindow(window);
4967
4968 window->consumeFocusEvent(true);
4969
4970 mDispatcher->notifyKey(generateAssistantKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
4971 mDispatcher->waitForIdle();
4972
4973 // System key is not passed down
4974 window->assertNoEvents();
4975
4976 // Should have poked user activity
4977 mFakePolicy->assertUserActivityPoked();
4978}
4979
4980TEST_F(InputDispatcherTest, FocusedWindow_SystemKeyIgnoresDisableUserActivity) {
4981 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4982 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4983 "Fake Window", ADISPLAY_ID_DEFAULT);
4984
4985 window->setDisableUserActivity(true);
4986 window->setFocusable(true);
4987 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4988 setFocusedWindow(window);
4989
4990 window->consumeFocusEvent(true);
4991
4992 mDispatcher->notifyKey(generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
4993 mDispatcher->waitForIdle();
4994
4995 // System key is not passed down
4996 window->assertNoEvents();
4997
4998 // Should have poked user activity
4999 mFakePolicy->assertUserActivityPoked();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005000}
5001
5002TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07005003 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005004 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5005 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005006
Arthur Hung72d8dc32020-03-28 00:48:39 +00005007 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005008
Prabir Pradhan678438e2023-04-13 19:32:51 +00005009 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005010 mDispatcher->waitForIdle();
5011
5012 window->assertNoEvents();
5013}
5014
5015// If a window is touchable, but does not have focus, it should receive motion events, but not keys
5016TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) {
Chris Yea209fde2020-07-22 13:54:51 -07005017 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005018 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5019 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005020
Arthur Hung72d8dc32020-03-28 00:48:39 +00005021 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005022
5023 // Send key
Prabir Pradhan678438e2023-04-13 19:32:51 +00005024 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005025 // Send motion
Prabir Pradhan678438e2023-04-13 19:32:51 +00005026 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5027 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005028
5029 // Window should receive only the motion event
5030 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5031 window->assertNoEvents(); // Key event or focus event will not be received
5032}
5033
arthurhungea3f4fc2020-12-21 23:18:53 +08005034TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) {
5035 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5036
arthurhungea3f4fc2020-12-21 23:18:53 +08005037 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005038 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
5039 ADISPLAY_ID_DEFAULT);
arthurhungea3f4fc2020-12-21 23:18:53 +08005040 firstWindow->setFrame(Rect(0, 0, 600, 400));
arthurhungea3f4fc2020-12-21 23:18:53 +08005041
arthurhungea3f4fc2020-12-21 23:18:53 +08005042 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005043 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
5044 ADISPLAY_ID_DEFAULT);
arthurhungea3f4fc2020-12-21 23:18:53 +08005045 secondWindow->setFrame(Rect(0, 400, 600, 800));
arthurhungea3f4fc2020-12-21 23:18:53 +08005046
5047 // Add the windows to the dispatcher
5048 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
5049
5050 PointF pointInFirst = {300, 200};
5051 PointF pointInSecond = {300, 600};
5052
5053 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005054 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5055 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5056 {pointInFirst}));
arthurhungea3f4fc2020-12-21 23:18:53 +08005057 // Only the first window should get the down event
5058 firstWindow->consumeMotionDown();
5059 secondWindow->assertNoEvents();
5060
5061 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005062 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5063 ADISPLAY_ID_DEFAULT,
5064 {pointInFirst, pointInSecond}));
arthurhungea3f4fc2020-12-21 23:18:53 +08005065 // The first window gets a move and the second a down
5066 firstWindow->consumeMotionMove();
5067 secondWindow->consumeMotionDown();
5068
5069 // Send pointer cancel to the second window
5070 NotifyMotionArgs pointerUpMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08005071 generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
arthurhungea3f4fc2020-12-21 23:18:53 +08005072 {pointInFirst, pointInSecond});
5073 pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED;
Prabir Pradhan678438e2023-04-13 19:32:51 +00005074 mDispatcher->notifyMotion(pointerUpMotionArgs);
arthurhungea3f4fc2020-12-21 23:18:53 +08005075 // The first window gets move and the second gets cancel.
5076 firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
5077 secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
5078
5079 // Send up event.
Prabir Pradhan678438e2023-04-13 19:32:51 +00005080 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
5081 ADISPLAY_ID_DEFAULT));
arthurhungea3f4fc2020-12-21 23:18:53 +08005082 // The first window gets up and the second gets nothing.
5083 firstWindow->consumeMotionUp();
5084 secondWindow->assertNoEvents();
5085}
5086
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00005087TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) {
5088 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5089
5090 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005091 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00005092 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5093 std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline;
5094 graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2;
5095 graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3;
5096
Harry Cutts33476232023-01-30 19:57:29 +00005097 window->sendTimeline(/*inputEventId=*/1, graphicsTimeline);
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00005098 window->assertNoEvents();
5099 mDispatcher->waitForIdle();
5100}
5101
chaviwd1c23182019-12-20 18:44:56 -08005102class FakeMonitorReceiver {
Michael Wright3a240c42019-12-10 20:53:41 +00005103public:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07005104 FakeMonitorReceiver(const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005105 int32_t displayId) {
Garfield Tan15601662020-09-22 15:32:38 -07005106 base::Result<std::unique_ptr<InputChannel>> channel =
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08005107 dispatcher->createInputMonitor(displayId, name, MONITOR_PID);
Garfield Tan15601662020-09-22 15:32:38 -07005108 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
Michael Wright3a240c42019-12-10 20:53:41 +00005109 }
5110
chaviwd1c23182019-12-20 18:44:56 -08005111 sp<IBinder> getToken() { return mInputReceiver->getToken(); }
5112
5113 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005114 mInputReceiver->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId,
5115 expectedFlags);
chaviwd1c23182019-12-20 18:44:56 -08005116 }
5117
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005118 std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); }
5119
5120 void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); }
5121
chaviwd1c23182019-12-20 18:44:56 -08005122 void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005123 mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN,
chaviwd1c23182019-12-20 18:44:56 -08005124 expectedDisplayId, expectedFlags);
5125 }
5126
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005127 void consumeMotionMove(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005128 mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005129 expectedDisplayId, expectedFlags);
5130 }
5131
chaviwd1c23182019-12-20 18:44:56 -08005132 void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005133 mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_UP,
chaviwd1c23182019-12-20 18:44:56 -08005134 expectedDisplayId, expectedFlags);
5135 }
5136
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005137 void consumeMotionCancel(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08005138 mInputReceiver->consumeMotionEvent(
5139 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
5140 WithDisplayId(expectedDisplayId),
5141 WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED)));
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005142 }
5143
Arthur Hungfbfa5722021-11-16 02:45:54 +00005144 void consumeMotionPointerDown(int32_t pointerIdx) {
5145 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
5146 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005147 mInputReceiver->consumeEvent(InputEventType::MOTION, action, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00005148 /*expectedFlags=*/0);
Arthur Hungfbfa5722021-11-16 02:45:54 +00005149 }
5150
Evan Rosky84f07f02021-04-16 10:42:42 -07005151 MotionEvent* consumeMotion() {
5152 InputEvent* event = mInputReceiver->consume();
5153 if (!event) {
5154 ADD_FAILURE() << "No event was produced";
5155 return nullptr;
5156 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005157 if (event->getType() != InputEventType::MOTION) {
5158 ADD_FAILURE() << "Expected MotionEvent, got " << *event;
Evan Rosky84f07f02021-04-16 10:42:42 -07005159 return nullptr;
5160 }
5161 return static_cast<MotionEvent*>(event);
5162 }
5163
chaviwd1c23182019-12-20 18:44:56 -08005164 void assertNoEvents() { mInputReceiver->assertNoEvents(); }
5165
5166private:
5167 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Michael Wright3a240c42019-12-10 20:53:41 +00005168};
5169
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005170using InputDispatcherMonitorTest = InputDispatcherTest;
5171
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005172/**
5173 * Two entities that receive touch: A window, and a global monitor.
5174 * The touch goes to the window, and then the window disappears.
5175 * The monitor does not get cancel right away. But if more events come in, the touch gets canceled
5176 * for the monitor, as well.
5177 * 1. foregroundWindow
5178 * 2. monitor <-- global monitor (doesn't observe z order, receives all events)
5179 */
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005180TEST_F(InputDispatcherMonitorTest, MonitorTouchIsCanceledWhenForegroundWindowDisappears) {
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005181 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5182 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005183 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005184
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005185 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005186
5187 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5188 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5189 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5190 {100, 200}))
5191 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5192
5193 // Both the foreground window and the global monitor should receive the touch down
5194 window->consumeMotionDown();
5195 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
5196
5197 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5198 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5199 ADISPLAY_ID_DEFAULT, {110, 200}))
5200 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5201
5202 window->consumeMotionMove();
5203 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
5204
5205 // Now the foreground window goes away
5206 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
5207 window->consumeMotionCancel();
5208 monitor.assertNoEvents(); // Global monitor does not get a cancel yet
5209
5210 // If more events come in, there will be no more foreground window to send them to. This will
5211 // cause a cancel for the monitor, as well.
5212 ASSERT_EQ(InputEventInjectionResult::FAILED,
5213 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5214 ADISPLAY_ID_DEFAULT, {120, 200}))
5215 << "Injection should fail because the window was removed";
5216 window->assertNoEvents();
5217 // Global monitor now gets the cancel
5218 monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
5219}
5220
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005221TEST_F(InputDispatcherMonitorTest, ReceivesMotionEvents) {
Chris Yea209fde2020-07-22 13:54:51 -07005222 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005223 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5224 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005225 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00005226
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005227 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00005228
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005229 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00005230 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005231 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00005232 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08005233 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00005234}
5235
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005236TEST_F(InputDispatcherMonitorTest, MonitorCannotPilferPointers) {
5237 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00005238
Chris Yea209fde2020-07-22 13:54:51 -07005239 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005240 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5241 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005242 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00005243
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005244 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00005245 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005246 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
chaviwd1c23182019-12-20 18:44:56 -08005247 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005248 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00005249
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005250 // Pilfer pointers from the monitor.
5251 // This should not do anything and the window should continue to receive events.
5252 EXPECT_NE(OK, mDispatcher->pilferPointers(monitor.getToken()));
Michael Wright3a240c42019-12-10 20:53:41 +00005253
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005254 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005255 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5256 ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005257 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005258
5259 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
5260 window->consumeMotionMove(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00005261}
5262
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005263TEST_F(InputDispatcherMonitorTest, NoWindowTransform) {
Evan Rosky84f07f02021-04-16 10:42:42 -07005264 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005265 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5266 "Fake Window", ADISPLAY_ID_DEFAULT);
Evan Rosky84f07f02021-04-16 10:42:42 -07005267 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5268 window->setWindowOffset(20, 40);
5269 window->setWindowTransform(0, 1, -1, 0);
5270
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005271 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Evan Rosky84f07f02021-04-16 10:42:42 -07005272
5273 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5274 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
5275 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5276 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5277 MotionEvent* event = monitor.consumeMotion();
5278 // Even though window has transform, gesture monitor must not.
5279 ASSERT_EQ(ui::Transform(), event->getTransform());
5280}
5281
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005282TEST_F(InputDispatcherMonitorTest, InjectionFailsWithNoWindow) {
Arthur Hungb3307ee2021-10-14 10:57:37 +00005283 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005284 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Arthur Hungb3307ee2021-10-14 10:57:37 +00005285
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005286 ASSERT_EQ(InputEventInjectionResult::FAILED,
Arthur Hungb3307ee2021-10-14 10:57:37 +00005287 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005288 << "Injection should fail if there is a monitor, but no touchable window";
5289 monitor.assertNoEvents();
Arthur Hungb3307ee2021-10-14 10:57:37 +00005290}
5291
chaviw81e2bb92019-12-18 15:03:51 -08005292TEST_F(InputDispatcherTest, TestMoveEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07005293 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005294 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5295 "Fake Window", ADISPLAY_ID_DEFAULT);
chaviw81e2bb92019-12-18 15:03:51 -08005296
Arthur Hung72d8dc32020-03-28 00:48:39 +00005297 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
chaviw81e2bb92019-12-18 15:03:51 -08005298
5299 NotifyMotionArgs motionArgs =
5300 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5301 ADISPLAY_ID_DEFAULT);
5302
Prabir Pradhan678438e2023-04-13 19:32:51 +00005303 mDispatcher->notifyMotion(motionArgs);
chaviw81e2bb92019-12-18 15:03:51 -08005304 // Window should receive motion down event.
5305 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5306
5307 motionArgs.action = AMOTION_EVENT_ACTION_MOVE;
Garfield Tanc51d1ba2020-01-28 13:24:04 -08005308 motionArgs.id += 1;
chaviw81e2bb92019-12-18 15:03:51 -08005309 motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
5310 motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
5311 motionArgs.pointerCoords[0].getX() - 10);
5312
Prabir Pradhan678438e2023-04-13 19:32:51 +00005313 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005314 window->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00005315 /*expectedFlags=*/0);
chaviw81e2bb92019-12-18 15:03:51 -08005316}
5317
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005318/**
5319 * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to
5320 * the device default right away. In the test scenario, we check both the default value,
5321 * and the action of enabling / disabling.
5322 */
5323TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) {
Chris Yea209fde2020-07-22 13:54:51 -07005324 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005325 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5326 "Test window", ADISPLAY_ID_DEFAULT);
Antonio Kantekea47acb2021-12-23 12:41:25 -08005327 const WindowInfo& windowInfo = *window->getInfo();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005328
5329 // Set focused application.
5330 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07005331 window->setFocusable(true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005332
5333 SCOPED_TRACE("Check default value of touch mode");
Arthur Hung72d8dc32020-03-28 00:48:39 +00005334 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005335 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00005336 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005337
5338 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07005339 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005340 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Harry Cutts33476232023-01-30 19:57:29 +00005341 window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005342
5343 SCOPED_TRACE("Disable touch mode");
Antonio Kantekea47acb2021-12-23 12:41:25 -08005344 mDispatcher->setInTouchMode(false, windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00005345 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00005346 window->consumeTouchModeEvent(false);
Vishnu Nair47074b82020-08-14 11:54:47 -07005347 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005348 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005349 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00005350 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/false);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005351
5352 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07005353 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005354 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Harry Cutts33476232023-01-30 19:57:29 +00005355 window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/false);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005356
5357 SCOPED_TRACE("Enable touch mode again");
Antonio Kantekea47acb2021-12-23 12:41:25 -08005358 mDispatcher->setInTouchMode(true, windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00005359 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00005360 window->consumeTouchModeEvent(true);
Vishnu Nair47074b82020-08-14 11:54:47 -07005361 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005362 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005363 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00005364 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005365
5366 window->assertNoEvents();
5367}
5368
Gang Wange9087892020-01-07 12:17:14 -05005369TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07005370 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005371 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5372 "Test window", ADISPLAY_ID_DEFAULT);
Gang Wange9087892020-01-07 12:17:14 -05005373
5374 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07005375 window->setFocusable(true);
Gang Wange9087892020-01-07 12:17:14 -05005376
Arthur Hung72d8dc32020-03-28 00:48:39 +00005377 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005378 setFocusedWindow(window);
5379
Harry Cutts33476232023-01-30 19:57:29 +00005380 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Gang Wange9087892020-01-07 12:17:14 -05005381
Prabir Pradhan678438e2023-04-13 19:32:51 +00005382 const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
5383 mDispatcher->notifyKey(keyArgs);
Gang Wange9087892020-01-07 12:17:14 -05005384
5385 InputEvent* event = window->consume();
5386 ASSERT_NE(event, nullptr);
5387
5388 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
5389 ASSERT_NE(verified, nullptr);
5390 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY);
5391
5392 ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos);
5393 ASSERT_EQ(keyArgs.deviceId, verified->deviceId);
5394 ASSERT_EQ(keyArgs.source, verified->source);
5395 ASSERT_EQ(keyArgs.displayId, verified->displayId);
5396
5397 const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified);
5398
5399 ASSERT_EQ(keyArgs.action, verifiedKey.action);
Gang Wange9087892020-01-07 12:17:14 -05005400 ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08005401 ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos);
Gang Wange9087892020-01-07 12:17:14 -05005402 ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode);
5403 ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode);
5404 ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState);
5405 ASSERT_EQ(0, verifiedKey.repeatCount);
5406}
5407
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005408TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07005409 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005410 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5411 "Test window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005412
5413 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5414
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07005415 ui::Transform transform;
5416 transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
5417
5418 gui::DisplayInfo displayInfo;
5419 displayInfo.displayId = ADISPLAY_ID_DEFAULT;
5420 displayInfo.transform = transform;
5421
Patrick Williamsd828f302023-04-28 17:52:08 -05005422 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {displayInfo}, 0, 0});
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005423
Prabir Pradhan678438e2023-04-13 19:32:51 +00005424 const NotifyMotionArgs motionArgs =
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005425 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5426 ADISPLAY_ID_DEFAULT);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005427 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005428
5429 InputEvent* event = window->consume();
5430 ASSERT_NE(event, nullptr);
5431
5432 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
5433 ASSERT_NE(verified, nullptr);
5434 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION);
5435
5436 EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos);
5437 EXPECT_EQ(motionArgs.deviceId, verified->deviceId);
5438 EXPECT_EQ(motionArgs.source, verified->source);
5439 EXPECT_EQ(motionArgs.displayId, verified->displayId);
5440
5441 const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified);
5442
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07005443 const vec2 rawXY =
5444 MotionEvent::calculateTransformedXY(motionArgs.source, transform,
5445 motionArgs.pointerCoords[0].getXYValue());
5446 EXPECT_EQ(rawXY.x, verifiedMotion.rawX);
5447 EXPECT_EQ(rawXY.y, verifiedMotion.rawY);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005448 EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005449 EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08005450 EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005451 EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState);
5452 EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState);
5453}
5454
chaviw09c8d2d2020-08-24 15:48:26 -07005455/**
5456 * Ensure that separate calls to sign the same data are generating the same key.
5457 * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance
5458 * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky
5459 * tests.
5460 */
5461TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) {
5462 KeyEvent event = getTestKeyEvent();
5463 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
5464
5465 std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent);
5466 std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent);
5467 ASSERT_EQ(hmac1, hmac2);
5468}
5469
5470/**
5471 * Ensure that changes in VerifiedKeyEvent produce a different hmac.
5472 */
5473TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) {
5474 KeyEvent event = getTestKeyEvent();
5475 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
5476 std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent);
5477
5478 verifiedEvent.deviceId += 1;
5479 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5480
5481 verifiedEvent.source += 1;
5482 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5483
5484 verifiedEvent.eventTimeNanos += 1;
5485 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5486
5487 verifiedEvent.displayId += 1;
5488 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5489
5490 verifiedEvent.action += 1;
5491 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5492
5493 verifiedEvent.downTimeNanos += 1;
5494 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5495
5496 verifiedEvent.flags += 1;
5497 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5498
5499 verifiedEvent.keyCode += 1;
5500 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5501
5502 verifiedEvent.scanCode += 1;
5503 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5504
5505 verifiedEvent.metaState += 1;
5506 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5507
5508 verifiedEvent.repeatCount += 1;
5509 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5510}
5511
Vishnu Nair958da932020-08-21 17:12:37 -07005512TEST_F(InputDispatcherTest, SetFocusedWindow) {
5513 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5514 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005515 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005516 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005517 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005518 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5519
5520 // Top window is also focusable but is not granted focus.
5521 windowTop->setFocusable(true);
5522 windowSecond->setFocusable(true);
5523 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
5524 setFocusedWindow(windowSecond);
5525
5526 windowSecond->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005527 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5528 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005529
5530 // Focused window should receive event.
5531 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
5532 windowTop->assertNoEvents();
5533}
5534
5535TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) {
5536 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5537 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005538 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005539 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5540
5541 window->setFocusable(true);
5542 // Release channel for window is no longer valid.
5543 window->releaseChannel();
5544 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5545 setFocusedWindow(window);
5546
5547 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005548 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
5549 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07005550
5551 // window channel is invalid, so it should not receive any input event.
5552 window->assertNoEvents();
5553}
5554
5555TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) {
5556 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5557 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005558 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08005559 window->setFocusable(false);
Vishnu Nair958da932020-08-21 17:12:37 -07005560 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5561
Vishnu Nair958da932020-08-21 17:12:37 -07005562 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5563 setFocusedWindow(window);
5564
5565 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005566 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
5567 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07005568
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08005569 // window is not focusable, so it should not receive any input event.
Vishnu Nair958da932020-08-21 17:12:37 -07005570 window->assertNoEvents();
5571}
5572
5573TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) {
5574 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5575 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005576 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005577 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005578 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005579 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5580
5581 windowTop->setFocusable(true);
5582 windowSecond->setFocusable(true);
5583 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
5584 setFocusedWindow(windowTop);
5585 windowTop->consumeFocusEvent(true);
5586
Chavi Weingarten847e8512023-03-29 00:26:09 +00005587 windowTop->editInfo()->focusTransferTarget = windowSecond->getToken();
5588 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005589 windowSecond->consumeFocusEvent(true);
5590 windowTop->consumeFocusEvent(false);
5591
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005592 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5593 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005594
5595 // Focused window should receive event.
5596 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
5597}
5598
Chavi Weingarten847e8512023-03-29 00:26:09 +00005599TEST_F(InputDispatcherTest, SetFocusedWindow_TransferFocusTokenNotFocusable) {
Vishnu Nair958da932020-08-21 17:12:37 -07005600 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5601 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005602 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005603 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005604 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005605 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5606
5607 windowTop->setFocusable(true);
Chavi Weingarten847e8512023-03-29 00:26:09 +00005608 windowSecond->setFocusable(false);
5609 windowTop->editInfo()->focusTransferTarget = windowSecond->getToken();
Vishnu Nair958da932020-08-21 17:12:37 -07005610 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Chavi Weingarten847e8512023-03-29 00:26:09 +00005611 setFocusedWindow(windowTop);
5612 windowTop->consumeFocusEvent(true);
Vishnu Nair958da932020-08-21 17:12:37 -07005613
Chavi Weingarten847e8512023-03-29 00:26:09 +00005614 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5615 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005616
5617 // Event should be dropped.
Chavi Weingarten847e8512023-03-29 00:26:09 +00005618 windowTop->consumeKeyDown(ADISPLAY_ID_NONE);
Vishnu Nair958da932020-08-21 17:12:37 -07005619 windowSecond->assertNoEvents();
5620}
5621
5622TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) {
5623 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5624 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005625 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005626 sp<FakeWindowHandle> previousFocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005627 sp<FakeWindowHandle>::make(application, mDispatcher, "previousFocusedWindow",
5628 ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005629 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5630
5631 window->setFocusable(true);
5632 previousFocusedWindow->setFocusable(true);
5633 window->setVisible(false);
5634 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}});
5635 setFocusedWindow(previousFocusedWindow);
5636 previousFocusedWindow->consumeFocusEvent(true);
5637
5638 // Requesting focus on invisible window takes focus from currently focused window.
5639 setFocusedWindow(window);
5640 previousFocusedWindow->consumeFocusEvent(false);
5641
5642 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005643 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00005644 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
5645 InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07005646
5647 // Window does not get focus event or key down.
5648 window->assertNoEvents();
5649
5650 // Window becomes visible.
5651 window->setVisible(true);
5652 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5653
5654 // Window receives focus event.
5655 window->consumeFocusEvent(true);
5656 // Focused window receives key down.
5657 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5658}
5659
Vishnu Nair599f1412021-06-21 10:39:58 -07005660TEST_F(InputDispatcherTest, DisplayRemoved) {
5661 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5662 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005663 sp<FakeWindowHandle>::make(application, mDispatcher, "window", ADISPLAY_ID_DEFAULT);
Vishnu Nair599f1412021-06-21 10:39:58 -07005664 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5665
5666 // window is granted focus.
5667 window->setFocusable(true);
5668 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5669 setFocusedWindow(window);
5670 window->consumeFocusEvent(true);
5671
5672 // When a display is removed window loses focus.
5673 mDispatcher->displayRemoved(ADISPLAY_ID_DEFAULT);
5674 window->consumeFocusEvent(false);
5675}
5676
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005677/**
5678 * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY,
5679 * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top
5680 * of the 'slipperyEnterWindow'.
5681 *
5682 * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such
5683 * a way so that the touched location is no longer covered by the top window.
5684 *
5685 * Next, inject a MOVE event. Because the top window already moved earlier, this event is now
5686 * positioned over the bottom (slipperyEnterWindow) only. And because the top window had
5687 * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive
5688 * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting
5689 * with ACTION_DOWN).
5690 * Thus, the touch has been transferred from the top window into the bottom window, because the top
5691 * window moved itself away from the touched location and had Flag::SLIPPERY.
5692 *
5693 * Even though the top window moved away from the touched location, it is still obscuring the bottom
5694 * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_
5695 * OBSCURED should be set for the MotionEvent that reaches the bottom window.
5696 *
5697 * In this test, we ensure that the event received by the bottom window has
5698 * FLAG_WINDOW_IS_PARTIALLY_OBSCURED.
5699 */
5700TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) {
Prabir Pradhan5735a322022-04-11 17:23:34 +00005701 constexpr int32_t SLIPPERY_PID = WINDOW_PID + 1;
5702 constexpr int32_t SLIPPERY_UID = WINDOW_UID + 1;
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005703
5704 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5705 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5706
5707 sp<FakeWindowHandle> slipperyExitWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005708 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08005709 slipperyExitWindow->setSlippery(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005710 // Make sure this one overlaps the bottom window
5711 slipperyExitWindow->setFrame(Rect(25, 25, 75, 75));
5712 // Change the owner uid/pid of the window so that it is considered to be occluding the bottom
5713 // one. Windows with the same owner are not considered to be occluding each other.
5714 slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID);
5715
5716 sp<FakeWindowHandle> slipperyEnterWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005717 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005718 slipperyExitWindow->setFrame(Rect(0, 0, 100, 100));
5719
5720 mDispatcher->setInputWindows(
5721 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
5722
5723 // Use notifyMotion instead of injecting to avoid dealing with injection permissions
Prabir Pradhan678438e2023-04-13 19:32:51 +00005724 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5725 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5726 {{50, 50}}));
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005727 slipperyExitWindow->consumeMotionDown();
5728 slipperyExitWindow->setFrame(Rect(70, 70, 100, 100));
5729 mDispatcher->setInputWindows(
5730 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
5731
Prabir Pradhan678438e2023-04-13 19:32:51 +00005732 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_MOVE,
5733 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5734 {{51, 51}}));
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005735
5736 slipperyExitWindow->consumeMotionCancel();
5737
5738 slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT,
5739 AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
5740}
5741
Siarhei Vishniakouafa08cc2023-05-08 22:35:50 -07005742/**
5743 * Two windows, one on the left and another on the right. The left window is slippery. The right
5744 * window isn't eligible to receive touch because it specifies InputConfig::DROP_INPUT. When the
5745 * touch moves from the left window into the right window, the gesture should continue to go to the
5746 * left window. Touch shouldn't slip because the right window can't receive touches. This test
5747 * reproduces a crash.
5748 */
5749TEST_F(InputDispatcherTest, TouchSlippingIntoWindowThatDropsTouches) {
5750 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5751
5752 sp<FakeWindowHandle> leftSlipperyWindow =
5753 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
5754 leftSlipperyWindow->setSlippery(true);
5755 leftSlipperyWindow->setFrame(Rect(0, 0, 100, 100));
5756
5757 sp<FakeWindowHandle> rightDropTouchesWindow =
5758 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
5759 rightDropTouchesWindow->setFrame(Rect(100, 0, 200, 100));
5760 rightDropTouchesWindow->setDropInput(true);
5761
5762 mDispatcher->setInputWindows(
5763 {{ADISPLAY_ID_DEFAULT, {leftSlipperyWindow, rightDropTouchesWindow}}});
5764
5765 // Start touch in the left window
5766 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
5767 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
5768 .build());
5769 leftSlipperyWindow->consumeMotionDown();
5770
5771 // And move it into the right window
5772 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
5773 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50))
5774 .build());
5775
5776 // Since the right window isn't eligible to receive input, touch does not slip.
5777 // The left window continues to receive the gesture.
5778 leftSlipperyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
5779 rightDropTouchesWindow->assertNoEvents();
5780}
5781
Garfield Tan1c7bc862020-01-28 13:24:04 -08005782class InputDispatcherKeyRepeatTest : public InputDispatcherTest {
5783protected:
5784 static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms
5785 static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms
5786
Chris Yea209fde2020-07-22 13:54:51 -07005787 std::shared_ptr<FakeApplicationHandle> mApp;
Garfield Tan1c7bc862020-01-28 13:24:04 -08005788 sp<FakeWindowHandle> mWindow;
5789
5790 virtual void SetUp() override {
Prabir Pradhana41d2442023-04-20 21:30:40 +00005791 mFakePolicy = std::make_unique<FakeInputDispatcherPolicy>();
Garfield Tan1c7bc862020-01-28 13:24:04 -08005792 mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY);
Prabir Pradhana41d2442023-04-20 21:30:40 +00005793 mDispatcher = std::make_unique<InputDispatcher>(*mFakePolicy);
Prabir Pradhan87112a72023-04-20 19:13:39 +00005794 mDispatcher->requestRefreshConfiguration();
Garfield Tan1c7bc862020-01-28 13:24:04 -08005795 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
5796 ASSERT_EQ(OK, mDispatcher->start());
5797
5798 setUpWindow();
5799 }
5800
5801 void setUpWindow() {
Chris Yea209fde2020-07-22 13:54:51 -07005802 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005803 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005804
Vishnu Nair47074b82020-08-14 11:54:47 -07005805 mWindow->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005806 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005807 setFocusedWindow(mWindow);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005808 mWindow->consumeFocusEvent(true);
5809 }
5810
Chris Ye2ad95392020-09-01 13:44:44 -07005811 void sendAndConsumeKeyDown(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08005812 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07005813 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08005814 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event
Prabir Pradhan678438e2023-04-13 19:32:51 +00005815 mDispatcher->notifyKey(keyArgs);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005816
5817 // Window should receive key down event.
5818 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5819 }
5820
5821 void expectKeyRepeatOnce(int32_t repeatCount) {
5822 SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount));
5823 InputEvent* repeatEvent = mWindow->consume();
5824 ASSERT_NE(nullptr, repeatEvent);
5825
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005826 ASSERT_EQ(InputEventType::KEY, repeatEvent->getType());
Garfield Tan1c7bc862020-01-28 13:24:04 -08005827
5828 KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent);
5829 uint32_t eventAction = repeatKeyEvent->getAction();
5830 EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction);
5831 EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount());
5832 }
5833
Chris Ye2ad95392020-09-01 13:44:44 -07005834 void sendAndConsumeKeyUp(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08005835 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07005836 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08005837 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event
Prabir Pradhan678438e2023-04-13 19:32:51 +00005838 mDispatcher->notifyKey(keyArgs);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005839
5840 // Window should receive key down event.
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005841 mWindow->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00005842 /*expectedFlags=*/0);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005843 }
5844};
5845
5846TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) {
Harry Cutts33476232023-01-30 19:57:29 +00005847 sendAndConsumeKeyDown(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005848 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5849 expectKeyRepeatOnce(repeatCount);
5850 }
5851}
5852
5853TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) {
Harry Cutts33476232023-01-30 19:57:29 +00005854 sendAndConsumeKeyDown(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005855 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5856 expectKeyRepeatOnce(repeatCount);
5857 }
Harry Cutts33476232023-01-30 19:57:29 +00005858 sendAndConsumeKeyDown(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07005859 /* repeatCount will start from 1 for deviceId 2 */
Garfield Tan1c7bc862020-01-28 13:24:04 -08005860 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5861 expectKeyRepeatOnce(repeatCount);
5862 }
5863}
5864
5865TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) {
Harry Cutts33476232023-01-30 19:57:29 +00005866 sendAndConsumeKeyDown(/*deviceId=*/1);
5867 expectKeyRepeatOnce(/*repeatCount=*/1);
5868 sendAndConsumeKeyUp(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005869 mWindow->assertNoEvents();
5870}
5871
5872TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) {
Harry Cutts33476232023-01-30 19:57:29 +00005873 sendAndConsumeKeyDown(/*deviceId=*/1);
5874 expectKeyRepeatOnce(/*repeatCount=*/1);
5875 sendAndConsumeKeyDown(/*deviceId=*/2);
5876 expectKeyRepeatOnce(/*repeatCount=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005877 // Stale key up from device 1.
Harry Cutts33476232023-01-30 19:57:29 +00005878 sendAndConsumeKeyUp(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005879 // Device 2 is still down, keep repeating
Harry Cutts33476232023-01-30 19:57:29 +00005880 expectKeyRepeatOnce(/*repeatCount=*/2);
5881 expectKeyRepeatOnce(/*repeatCount=*/3);
Chris Ye2ad95392020-09-01 13:44:44 -07005882 // Device 2 key up
Harry Cutts33476232023-01-30 19:57:29 +00005883 sendAndConsumeKeyUp(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07005884 mWindow->assertNoEvents();
5885}
5886
5887TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) {
Harry Cutts33476232023-01-30 19:57:29 +00005888 sendAndConsumeKeyDown(/*deviceId=*/1);
5889 expectKeyRepeatOnce(/*repeatCount=*/1);
5890 sendAndConsumeKeyDown(/*deviceId=*/2);
5891 expectKeyRepeatOnce(/*repeatCount=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005892 // Device 2 which holds the key repeating goes up, expect the repeating to stop.
Harry Cutts33476232023-01-30 19:57:29 +00005893 sendAndConsumeKeyUp(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07005894 // Device 1 still holds key down, but the repeating was already stopped
Garfield Tan1c7bc862020-01-28 13:24:04 -08005895 mWindow->assertNoEvents();
5896}
5897
liushenxiang42232912021-05-21 20:24:09 +08005898TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) {
5899 sendAndConsumeKeyDown(DEVICE_ID);
Harry Cutts33476232023-01-30 19:57:29 +00005900 expectKeyRepeatOnce(/*repeatCount=*/1);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005901 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
liushenxiang42232912021-05-21 20:24:09 +08005902 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT,
5903 AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS);
5904 mWindow->assertNoEvents();
5905}
5906
Garfield Tan1c7bc862020-01-28 13:24:04 -08005907TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) {
Michael Wrighte1cbbd62023-02-22 19:32:13 +00005908 GTEST_SKIP() << "Flaky test (b/270393106)";
Harry Cutts33476232023-01-30 19:57:29 +00005909 sendAndConsumeKeyDown(/*deviceId=*/1);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005910 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5911 InputEvent* repeatEvent = mWindow->consume();
5912 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
5913 EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER,
5914 IdGenerator::getSource(repeatEvent->getId()));
5915 }
5916}
5917
5918TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) {
Michael Wrighte1cbbd62023-02-22 19:32:13 +00005919 GTEST_SKIP() << "Flaky test (b/270393106)";
Harry Cutts33476232023-01-30 19:57:29 +00005920 sendAndConsumeKeyDown(/*deviceId=*/1);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005921
5922 std::unordered_set<int32_t> idSet;
5923 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5924 InputEvent* repeatEvent = mWindow->consume();
5925 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
5926 int32_t id = repeatEvent->getId();
5927 EXPECT_EQ(idSet.end(), idSet.find(id));
5928 idSet.insert(id);
5929 }
5930}
5931
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005932/* Test InputDispatcher for MultiDisplay */
5933class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest {
5934public:
Prabir Pradhan3608aad2019-10-02 17:08:26 -07005935 virtual void SetUp() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005936 InputDispatcherTest::SetUp();
Arthur Hungb92218b2018-08-14 12:00:21 +08005937
Chris Yea209fde2020-07-22 13:54:51 -07005938 application1 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005939 windowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005940 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005941
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005942 // Set focus window for primary display, but focused display would be second one.
5943 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1);
Vishnu Nair47074b82020-08-14 11:54:47 -07005944 windowInPrimary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005945 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005946 setFocusedWindow(windowInPrimary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005947 windowInPrimary->consumeFocusEvent(true);
Arthur Hungb92218b2018-08-14 12:00:21 +08005948
Chris Yea209fde2020-07-22 13:54:51 -07005949 application2 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005950 windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005951 sp<FakeWindowHandle>::make(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005952 // Set focus to second display window.
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005953 // Set focus display to second one.
5954 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
5955 // Set focus window for second display.
5956 mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2);
Vishnu Nair47074b82020-08-14 11:54:47 -07005957 windowInSecondary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005958 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005959 setFocusedWindow(windowInSecondary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005960 windowInSecondary->consumeFocusEvent(true);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005961 }
5962
Prabir Pradhan3608aad2019-10-02 17:08:26 -07005963 virtual void TearDown() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005964 InputDispatcherTest::TearDown();
5965
Chris Yea209fde2020-07-22 13:54:51 -07005966 application1.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005967 windowInPrimary.clear();
Chris Yea209fde2020-07-22 13:54:51 -07005968 application2.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005969 windowInSecondary.clear();
5970 }
5971
5972protected:
Chris Yea209fde2020-07-22 13:54:51 -07005973 std::shared_ptr<FakeApplicationHandle> application1;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005974 sp<FakeWindowHandle> windowInPrimary;
Chris Yea209fde2020-07-22 13:54:51 -07005975 std::shared_ptr<FakeApplicationHandle> application2;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005976 sp<FakeWindowHandle> windowInSecondary;
5977};
5978
5979TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) {
5980 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005981 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5982 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
5983 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005984 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08005985 windowInSecondary->assertNoEvents();
5986
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005987 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005988 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5989 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
5990 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08005991 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005992 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +08005993}
5994
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005995TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) {
Tiger Huang721e26f2018-07-24 22:26:19 +08005996 // Test inject a key down with display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005997 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5998 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005999 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08006000 windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Tiger Huang721e26f2018-07-24 22:26:19 +08006001 windowInSecondary->assertNoEvents();
6002
6003 // Test inject a key down without display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08006004 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006005 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08006006 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08006007 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hungb92218b2018-08-14 12:00:21 +08006008
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006009 // Remove all windows in secondary display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00006010 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}});
Arthur Hungb92218b2018-08-14 12:00:21 +08006011
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006012 // Old focus should receive a cancel event.
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07006013 windowInSecondary->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE,
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08006014 AKEY_EVENT_FLAG_CANCELED);
Arthur Hungb92218b2018-08-14 12:00:21 +08006015
6016 // Test inject a key down, should timeout because of no target window.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08006017 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006018 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Arthur Hungb92218b2018-08-14 12:00:21 +08006019 windowInPrimary->assertNoEvents();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006020 windowInSecondary->consumeFocusEvent(false);
Arthur Hungb92218b2018-08-14 12:00:21 +08006021 windowInSecondary->assertNoEvents();
6022}
6023
Arthur Hung2fbf37f2018-09-13 18:16:41 +08006024// Test per-display input monitors for motion event.
6025TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) {
chaviwd1c23182019-12-20 18:44:56 -08006026 FakeMonitorReceiver monitorInPrimary =
6027 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
6028 FakeMonitorReceiver monitorInSecondary =
6029 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08006030
6031 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006032 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6033 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
6034 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08006035 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08006036 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08006037 windowInSecondary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08006038 monitorInSecondary.assertNoEvents();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08006039
6040 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006041 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6042 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
6043 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08006044 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08006045 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08006046 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
chaviwd1c23182019-12-20 18:44:56 -08006047 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08006048
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08006049 // Lift up the touch from the second display
6050 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6051 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
6052 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6053 windowInSecondary->consumeMotionUp(SECOND_DISPLAY_ID);
6054 monitorInSecondary.consumeMotionUp(SECOND_DISPLAY_ID);
6055
Arthur Hung2fbf37f2018-09-13 18:16:41 +08006056 // Test inject a non-pointer motion event.
6057 // If specific a display, it will dispatch to the focused window of particular display,
6058 // or it will dispatch to the focused window of focused display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006059 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6060 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE))
6061 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08006062 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08006063 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08006064 windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08006065 monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08006066}
6067
6068// Test per-display input monitors for key event.
6069TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) {
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006070 // Input monitor per display.
chaviwd1c23182019-12-20 18:44:56 -08006071 FakeMonitorReceiver monitorInPrimary =
6072 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
6073 FakeMonitorReceiver monitorInSecondary =
6074 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08006075
6076 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006077 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
6078 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08006079 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08006080 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08006081 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08006082 monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08006083}
6084
Vishnu Nair958da932020-08-21 17:12:37 -07006085TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) {
6086 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006087 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07006088 secondWindowInPrimary->setFocusable(true);
6089 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}});
6090 setFocusedWindow(secondWindowInPrimary);
6091 windowInPrimary->consumeFocusEvent(false);
6092 secondWindowInPrimary->consumeFocusEvent(true);
6093
6094 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006095 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
6096 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07006097 windowInPrimary->assertNoEvents();
6098 windowInSecondary->assertNoEvents();
6099 secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
6100}
6101
Arthur Hungdfd528e2021-12-08 13:23:04 +00006102TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CancelTouch_MultiDisplay) {
6103 FakeMonitorReceiver monitorInPrimary =
6104 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
6105 FakeMonitorReceiver monitorInSecondary =
6106 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
6107
6108 // Test touch down on primary display.
6109 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6110 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
6111 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6112 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6113 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
6114
6115 // Test touch down on second display.
6116 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6117 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
6118 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6119 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
6120 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
6121
6122 // Trigger cancel touch.
6123 mDispatcher->cancelCurrentTouch();
6124 windowInPrimary->consumeMotionCancel(ADISPLAY_ID_DEFAULT);
6125 monitorInPrimary.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
6126 windowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID);
6127 monitorInSecondary.consumeMotionCancel(SECOND_DISPLAY_ID);
6128
6129 // Test inject a move motion event, no window/monitor should receive the event.
6130 ASSERT_EQ(InputEventInjectionResult::FAILED,
6131 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6132 ADISPLAY_ID_DEFAULT, {110, 200}))
6133 << "Inject motion event should return InputEventInjectionResult::FAILED";
6134 windowInPrimary->assertNoEvents();
6135 monitorInPrimary.assertNoEvents();
6136
6137 ASSERT_EQ(InputEventInjectionResult::FAILED,
6138 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6139 SECOND_DISPLAY_ID, {110, 200}))
6140 << "Inject motion event should return InputEventInjectionResult::FAILED";
6141 windowInSecondary->assertNoEvents();
6142 monitorInSecondary.assertNoEvents();
6143}
6144
Jackal Guof9696682018-10-05 12:23:23 +08006145class InputFilterTest : public InputDispatcherTest {
6146protected:
Prabir Pradhan81420cc2021-09-06 10:28:50 -07006147 void testNotifyMotion(int32_t displayId, bool expectToBeFiltered,
6148 const ui::Transform& transform = ui::Transform()) {
Jackal Guof9696682018-10-05 12:23:23 +08006149 NotifyMotionArgs motionArgs;
6150
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006151 motionArgs =
6152 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Prabir Pradhan678438e2023-04-13 19:32:51 +00006153 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006154 motionArgs =
6155 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Prabir Pradhan678438e2023-04-13 19:32:51 +00006156 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006157 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08006158 if (expectToBeFiltered) {
Prabir Pradhan81420cc2021-09-06 10:28:50 -07006159 const auto xy = transform.transform(motionArgs.pointerCoords->getXYValue());
6160 mFakePolicy->assertFilterInputEventWasCalled(motionArgs, xy);
Jackal Guof9696682018-10-05 12:23:23 +08006161 } else {
6162 mFakePolicy->assertFilterInputEventWasNotCalled();
6163 }
6164 }
6165
6166 void testNotifyKey(bool expectToBeFiltered) {
6167 NotifyKeyArgs keyArgs;
6168
6169 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
Prabir Pradhan678438e2023-04-13 19:32:51 +00006170 mDispatcher->notifyKey(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08006171 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP);
Prabir Pradhan678438e2023-04-13 19:32:51 +00006172 mDispatcher->notifyKey(keyArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006173 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08006174
6175 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08006176 mFakePolicy->assertFilterInputEventWasCalled(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08006177 } else {
6178 mFakePolicy->assertFilterInputEventWasNotCalled();
6179 }
6180 }
6181};
6182
6183// Test InputFilter for MotionEvent
6184TEST_F(InputFilterTest, MotionEvent_InputFilter) {
6185 // Since the InputFilter is disabled by default, check if touch events aren't filtered.
6186 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
6187 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
6188
6189 // Enable InputFilter
6190 mDispatcher->setInputFilterEnabled(true);
6191 // Test touch on both primary and second display, and check if both events are filtered.
6192 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true);
6193 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true);
6194
6195 // Disable InputFilter
6196 mDispatcher->setInputFilterEnabled(false);
6197 // Test touch on both primary and second display, and check if both events aren't filtered.
6198 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
6199 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
6200}
6201
6202// Test InputFilter for KeyEvent
6203TEST_F(InputFilterTest, KeyEvent_InputFilter) {
6204 // Since the InputFilter is disabled by default, check if key event aren't filtered.
6205 testNotifyKey(/*expectToBeFiltered*/ false);
6206
6207 // Enable InputFilter
6208 mDispatcher->setInputFilterEnabled(true);
6209 // Send a key event, and check if it is filtered.
6210 testNotifyKey(/*expectToBeFiltered*/ true);
6211
6212 // Disable InputFilter
6213 mDispatcher->setInputFilterEnabled(false);
6214 // Send a key event, and check if it isn't filtered.
6215 testNotifyKey(/*expectToBeFiltered*/ false);
6216}
6217
Prabir Pradhan81420cc2021-09-06 10:28:50 -07006218// Ensure that MotionEvents sent to the InputFilter through InputListener are converted to the
6219// logical display coordinate space.
6220TEST_F(InputFilterTest, MotionEvent_UsesLogicalDisplayCoordinates_notifyMotion) {
6221 ui::Transform firstDisplayTransform;
6222 firstDisplayTransform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
6223 ui::Transform secondDisplayTransform;
6224 secondDisplayTransform.set({-6.6, -5.5, -4.4, -3.3, -2.2, -1.1, 0, 0, 1});
6225
6226 std::vector<gui::DisplayInfo> displayInfos(2);
6227 displayInfos[0].displayId = ADISPLAY_ID_DEFAULT;
6228 displayInfos[0].transform = firstDisplayTransform;
6229 displayInfos[1].displayId = SECOND_DISPLAY_ID;
6230 displayInfos[1].transform = secondDisplayTransform;
6231
Patrick Williamsd828f302023-04-28 17:52:08 -05006232 mDispatcher->onWindowInfosChanged({{}, displayInfos, 0, 0});
Prabir Pradhan81420cc2021-09-06 10:28:50 -07006233
6234 // Enable InputFilter
6235 mDispatcher->setInputFilterEnabled(true);
6236
6237 // Ensure the correct transforms are used for the displays.
6238 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true, firstDisplayTransform);
6239 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true, secondDisplayTransform);
6240}
6241
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006242class InputFilterInjectionPolicyTest : public InputDispatcherTest {
6243protected:
6244 virtual void SetUp() override {
6245 InputDispatcherTest::SetUp();
6246
6247 /**
6248 * We don't need to enable input filter to test the injected event policy, but we enabled it
6249 * here to make the tests more realistic, since this policy only matters when inputfilter is
6250 * on.
6251 */
6252 mDispatcher->setInputFilterEnabled(true);
6253
6254 std::shared_ptr<InputApplicationHandle> application =
6255 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006256 mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Test Window",
6257 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006258
6259 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
6260 mWindow->setFocusable(true);
6261 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6262 setFocusedWindow(mWindow);
6263 mWindow->consumeFocusEvent(true);
6264 }
6265
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006266 void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
6267 int32_t flags) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006268 KeyEvent event;
6269
6270 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
6271 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD,
6272 ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A,
Harry Cutts33476232023-01-30 19:57:29 +00006273 KEY_A, AMETA_NONE, /*repeatCount=*/0, eventTime, eventTime);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006274 const int32_t additionalPolicyFlags =
6275 POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT;
6276 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00006277 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006278 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
6279 policyFlags | additionalPolicyFlags));
6280
6281 InputEvent* received = mWindow->consume();
6282 ASSERT_NE(nullptr, received);
6283 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07006284 ASSERT_EQ(received->getType(), InputEventType::KEY);
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006285 KeyEvent& keyEvent = static_cast<KeyEvent&>(*received);
6286 ASSERT_EQ(flags, keyEvent.getFlags());
6287 }
6288
6289 void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
6290 int32_t flags) {
6291 MotionEvent event;
6292 PointerProperties pointerProperties[1];
6293 PointerCoords pointerCoords[1];
6294 pointerProperties[0].clear();
6295 pointerProperties[0].id = 0;
6296 pointerCoords[0].clear();
6297 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300);
6298 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400);
6299
6300 ui::Transform identityTransform;
6301 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
6302 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN,
6303 DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0,
6304 AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE,
6305 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -07006306 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, eventTime,
Evan Rosky09576692021-07-01 12:22:09 -07006307 eventTime,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006308 /*pointerCount*/ 1, pointerProperties, pointerCoords);
6309
6310 const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER;
6311 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00006312 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006313 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
6314 policyFlags | additionalPolicyFlags));
6315
6316 InputEvent* received = mWindow->consume();
6317 ASSERT_NE(nullptr, received);
6318 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07006319 ASSERT_EQ(received->getType(), InputEventType::MOTION);
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006320 MotionEvent& motionEvent = static_cast<MotionEvent&>(*received);
6321 ASSERT_EQ(flags, motionEvent.getFlags());
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006322 }
6323
6324private:
6325 sp<FakeWindowHandle> mWindow;
6326};
6327
6328TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) {
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006329 // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input
6330 // filter. Without it, the event will no different from a regularly injected event, and the
6331 // injected device id will be overwritten.
Harry Cutts33476232023-01-30 19:57:29 +00006332 testInjectedKey(POLICY_FLAG_FILTERED, /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
6333 /*flags=*/0);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006334}
6335
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006336TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006337 testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Harry Cutts33476232023-01-30 19:57:29 +00006338 /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006339 AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
6340}
6341
6342TEST_F(InputFilterInjectionPolicyTest,
6343 MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
6344 testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Harry Cutts33476232023-01-30 19:57:29 +00006345 /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006346 AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006347}
6348
6349TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) {
Harry Cutts33476232023-01-30 19:57:29 +00006350 testInjectedKey(/*policyFlags=*/0, /*injectedDeviceId=*/3,
6351 /*resolvedDeviceId=*/VIRTUAL_KEYBOARD_ID, /*flags=*/0);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006352}
6353
chaviwfd6d3512019-03-25 13:23:49 -07006354class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest {
Prabir Pradhan3608aad2019-10-02 17:08:26 -07006355 virtual void SetUp() override {
chaviwfd6d3512019-03-25 13:23:49 -07006356 InputDispatcherTest::SetUp();
6357
Chris Yea209fde2020-07-22 13:54:51 -07006358 std::shared_ptr<FakeApplicationHandle> application =
6359 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006360 mUnfocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006361 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07006362 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
chaviwfd6d3512019-03-25 13:23:49 -07006363
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006364 mFocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006365 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006366 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
chaviwfd6d3512019-03-25 13:23:49 -07006367
6368 // Set focused application.
6369 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07006370 mFocusedWindow->setFocusable(true);
chaviwfd6d3512019-03-25 13:23:49 -07006371
6372 // Expect one focus window exist in display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00006373 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07006374 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006375 mFocusedWindow->consumeFocusEvent(true);
chaviwfd6d3512019-03-25 13:23:49 -07006376 }
6377
Prabir Pradhan3608aad2019-10-02 17:08:26 -07006378 virtual void TearDown() override {
chaviwfd6d3512019-03-25 13:23:49 -07006379 InputDispatcherTest::TearDown();
6380
6381 mUnfocusedWindow.clear();
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006382 mFocusedWindow.clear();
chaviwfd6d3512019-03-25 13:23:49 -07006383 }
6384
6385protected:
6386 sp<FakeWindowHandle> mUnfocusedWindow;
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006387 sp<FakeWindowHandle> mFocusedWindow;
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07006388 static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60};
chaviwfd6d3512019-03-25 13:23:49 -07006389};
6390
6391// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
6392// DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received
6393// the onPointerDownOutsideFocus callback.
6394TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006395 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07006396 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6397 {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006398 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006399 mUnfocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07006400
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006401 ASSERT_TRUE(mDispatcher->waitForIdle());
chaviwfd6d3512019-03-25 13:23:49 -07006402 mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken());
6403}
6404
6405// Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action
6406// DOWN on the window that doesn't have focus. Ensure no window received the
6407// onPointerDownOutsideFocus callback.
6408TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006409 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07006410 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006411 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006412 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07006413
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006414 ASSERT_TRUE(mDispatcher->waitForIdle());
6415 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07006416}
6417
6418// Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't
6419// have focus. Ensure no window received the onPointerDownOutsideFocus callback.
6420TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08006421 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6422 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006423 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006424 mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07006425
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006426 ASSERT_TRUE(mDispatcher->waitForIdle());
6427 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07006428}
6429
6430// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
6431// DOWN on the window that already has focus. Ensure no window received the
6432// onPointerDownOutsideFocus callback.
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006433TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006434 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006435 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07006436 FOCUSED_WINDOW_TOUCH_POINT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006437 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006438 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07006439
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006440 ASSERT_TRUE(mDispatcher->waitForIdle());
6441 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07006442}
6443
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08006444// Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag
6445// NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback.
6446TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) {
6447 const MotionEvent event =
6448 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
6449 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006450 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(20).y(20))
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08006451 .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE)
6452 .build();
6453 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(mDispatcher, event))
6454 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6455 mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
6456
6457 ASSERT_TRUE(mDispatcher->waitForIdle());
6458 mFakePolicy->assertOnPointerDownWasNotCalled();
6459 // Ensure that the unfocused window did not receive any FOCUS events.
6460 mUnfocusedWindow->assertNoEvents();
6461}
6462
chaviwaf87b3e2019-10-01 16:59:28 -07006463// These tests ensures we can send touch events to a single client when there are multiple input
6464// windows that point to the same client token.
6465class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest {
6466 virtual void SetUp() override {
6467 InputDispatcherTest::SetUp();
6468
Chris Yea209fde2020-07-22 13:54:51 -07006469 std::shared_ptr<FakeApplicationHandle> application =
6470 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006471 mWindow1 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 1",
6472 ADISPLAY_ID_DEFAULT);
chaviwaf87b3e2019-10-01 16:59:28 -07006473 mWindow1->setFrame(Rect(0, 0, 100, 100));
6474
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006475 mWindow2 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 2",
6476 ADISPLAY_ID_DEFAULT, mWindow1->getToken());
chaviwaf87b3e2019-10-01 16:59:28 -07006477 mWindow2->setFrame(Rect(100, 100, 200, 200));
6478
Arthur Hung72d8dc32020-03-28 00:48:39 +00006479 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
chaviwaf87b3e2019-10-01 16:59:28 -07006480 }
6481
6482protected:
6483 sp<FakeWindowHandle> mWindow1;
6484 sp<FakeWindowHandle> mWindow2;
6485
6486 // Helper function to convert the point from screen coordinates into the window's space
chaviw3277faf2021-05-19 16:45:23 -05006487 static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) {
chaviw1ff3d1e2020-07-01 15:53:47 -07006488 vec2 vals = windowInfo->transform.transform(point.x, point.y);
6489 return {vals.x, vals.y};
chaviwaf87b3e2019-10-01 16:59:28 -07006490 }
6491
6492 void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction,
6493 const std::vector<PointF>& points) {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006494 const std::string name = window->getName();
chaviwaf87b3e2019-10-01 16:59:28 -07006495 InputEvent* event = window->consume();
6496
6497 ASSERT_NE(nullptr, event) << name.c_str()
6498 << ": consumer should have returned non-NULL event.";
6499
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07006500 ASSERT_EQ(InputEventType::MOTION, event->getType())
6501 << name.c_str() << ": expected MotionEvent, got " << *event;
chaviwaf87b3e2019-10-01 16:59:28 -07006502
6503 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006504 assertMotionAction(expectedAction, motionEvent.getAction());
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08006505 ASSERT_EQ(points.size(), motionEvent.getPointerCount());
chaviwaf87b3e2019-10-01 16:59:28 -07006506
6507 for (size_t i = 0; i < points.size(); i++) {
6508 float expectedX = points[i].x;
6509 float expectedY = points[i].y;
6510
6511 EXPECT_EQ(expectedX, motionEvent.getX(i))
6512 << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str()
6513 << ", got " << motionEvent.getX(i);
6514 EXPECT_EQ(expectedY, motionEvent.getY(i))
6515 << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str()
6516 << ", got " << motionEvent.getY(i);
6517 }
6518 }
chaviw9eaa22c2020-07-01 16:21:27 -07006519
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08006520 void touchAndAssertPositions(int32_t action, const std::vector<PointF>& touchedPoints,
chaviw9eaa22c2020-07-01 16:21:27 -07006521 std::vector<PointF> expectedPoints) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00006522 mDispatcher->notifyMotion(generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN,
6523 ADISPLAY_ID_DEFAULT, touchedPoints));
chaviw9eaa22c2020-07-01 16:21:27 -07006524
6525 // Always consume from window1 since it's the window that has the InputReceiver
6526 consumeMotionEvent(mWindow1, action, expectedPoints);
6527 }
chaviwaf87b3e2019-10-01 16:59:28 -07006528};
6529
6530TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) {
6531 // Touch Window 1
6532 PointF touchedPoint = {10, 10};
6533 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006534 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006535
6536 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07006537 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006538
6539 // Touch Window 2
6540 touchedPoint = {150, 150};
6541 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006542 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006543}
6544
chaviw9eaa22c2020-07-01 16:21:27 -07006545TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) {
6546 // Set scale value for window2
chaviwaf87b3e2019-10-01 16:59:28 -07006547 mWindow2->setWindowScale(0.5f, 0.5f);
6548
6549 // Touch Window 1
6550 PointF touchedPoint = {10, 10};
6551 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006552 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006553 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07006554 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006555
6556 // Touch Window 2
6557 touchedPoint = {150, 150};
6558 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006559 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
6560 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006561
chaviw9eaa22c2020-07-01 16:21:27 -07006562 // Update the transform so rotation is set
6563 mWindow2->setWindowTransform(0, -1, 1, 0);
6564 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
6565 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006566}
6567
chaviw9eaa22c2020-07-01 16:21:27 -07006568TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006569 mWindow2->setWindowScale(0.5f, 0.5f);
6570
6571 // Touch Window 1
6572 std::vector<PointF> touchedPoints = {PointF{10, 10}};
6573 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07006574 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006575
6576 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07006577 touchedPoints.push_back(PointF{150, 150});
6578 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006579 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006580
chaviw9eaa22c2020-07-01 16:21:27 -07006581 // Release Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006582 touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07006583 expectedPoints.pop_back();
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006584
chaviw9eaa22c2020-07-01 16:21:27 -07006585 // Update the transform so rotation is set for Window 2
6586 mWindow2->setWindowTransform(0, -1, 1, 0);
6587 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006588 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006589}
6590
chaviw9eaa22c2020-07-01 16:21:27 -07006591TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006592 mWindow2->setWindowScale(0.5f, 0.5f);
6593
6594 // Touch Window 1
6595 std::vector<PointF> touchedPoints = {PointF{10, 10}};
6596 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07006597 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006598
6599 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07006600 touchedPoints.push_back(PointF{150, 150});
6601 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006602
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006603 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006604
6605 // Move both windows
6606 touchedPoints = {{20, 20}, {175, 175}};
6607 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
6608 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
6609
chaviw9eaa22c2020-07-01 16:21:27 -07006610 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006611
chaviw9eaa22c2020-07-01 16:21:27 -07006612 // Release Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006613 touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07006614 expectedPoints.pop_back();
6615
6616 // Touch Window 2
6617 mWindow2->setWindowTransform(0, -1, 1, 0);
6618 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006619 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07006620
6621 // Move both windows
6622 touchedPoints = {{20, 20}, {175, 175}};
6623 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
6624 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
6625
6626 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006627}
6628
6629TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) {
6630 mWindow1->setWindowScale(0.5f, 0.5f);
6631
6632 // Touch Window 1
6633 std::vector<PointF> touchedPoints = {PointF{10, 10}};
6634 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07006635 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006636
6637 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07006638 touchedPoints.push_back(PointF{150, 150});
6639 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006640
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006641 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006642
6643 // Move both windows
6644 touchedPoints = {{20, 20}, {175, 175}};
6645 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
6646 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
6647
chaviw9eaa22c2020-07-01 16:21:27 -07006648 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006649}
6650
Siarhei Vishniakou0f6558d2023-04-21 12:05:13 -07006651/**
6652 * When one of the windows is slippery, the touch should not slip into the other window with the
6653 * same input channel.
6654 */
6655TEST_F(InputDispatcherMultiWindowSameTokenTests, TouchDoesNotSlipEvenIfSlippery) {
6656 mWindow1->setSlippery(true);
6657 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
6658
6659 // Touch down in window 1
6660 mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6661 ADISPLAY_ID_DEFAULT, {{50, 50}}));
6662 consumeMotionEvent(mWindow1, ACTION_DOWN, {{50, 50}});
6663
6664 // Move touch to be above window 2. Even though window 1 is slippery, touch should not slip.
6665 // That means the gesture should continue normally, without any ACTION_CANCEL or ACTION_DOWN
6666 // getting generated.
6667 mDispatcher->notifyMotion(generateMotionArgs(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6668 ADISPLAY_ID_DEFAULT, {{150, 150}}));
6669
6670 consumeMotionEvent(mWindow1, ACTION_MOVE, {{150, 150}});
6671}
6672
Siarhei Vishniakoud5876ba2023-05-15 17:58:34 -07006673/**
6674 * When hover starts in one window and continues into the other, there should be a HOVER_EXIT and
6675 * a HOVER_ENTER generated, even if the windows have the same token. This is because the new window
6676 * that the pointer is hovering over may have a different transform.
6677 */
6678TEST_F(InputDispatcherMultiWindowSameTokenTests, HoverIntoClone) {
6679 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
6680
6681 // Start hover in window 1
6682 mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_ENTER, AINPUT_SOURCE_TOUCHSCREEN,
6683 ADISPLAY_ID_DEFAULT, {{50, 50}}));
6684 consumeMotionEvent(mWindow1, ACTION_HOVER_ENTER,
6685 {getPointInWindow(mWindow1->getInfo(), PointF{50, 50})});
6686
6687 // Move hover to window 2.
6688 mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6689 ADISPLAY_ID_DEFAULT, {{150, 150}}));
6690
6691 consumeMotionEvent(mWindow1, ACTION_HOVER_EXIT, {{50, 50}});
6692 consumeMotionEvent(mWindow1, ACTION_HOVER_ENTER,
6693 {getPointInWindow(mWindow2->getInfo(), PointF{150, 150})});
6694}
6695
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006696class InputDispatcherSingleWindowAnr : public InputDispatcherTest {
6697 virtual void SetUp() override {
6698 InputDispatcherTest::SetUp();
6699
Chris Yea209fde2020-07-22 13:54:51 -07006700 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006701 mApplication->setDispatchingTimeout(20ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006702 mWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "TestWindow",
6703 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006704 mWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05006705 mWindow->setDispatchingTimeout(30ms);
Vishnu Nair47074b82020-08-14 11:54:47 -07006706 mWindow->setFocusable(true);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006707
6708 // Set focused application.
6709 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
6710
6711 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07006712 setFocusedWindow(mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006713 mWindow->consumeFocusEvent(true);
6714 }
6715
6716 virtual void TearDown() override {
6717 InputDispatcherTest::TearDown();
6718 mWindow.clear();
6719 }
6720
6721protected:
Chris Yea209fde2020-07-22 13:54:51 -07006722 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006723 sp<FakeWindowHandle> mWindow;
6724 static constexpr PointF WINDOW_LOCATION = {20, 20};
6725
6726 void tapOnWindow() {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006727 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006728 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6729 WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006730 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006731 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6732 WINDOW_LOCATION));
6733 }
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006734
6735 sp<FakeWindowHandle> addSpyWindow() {
6736 sp<FakeWindowHandle> spy =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006737 sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006738 spy->setTrustedOverlay(true);
6739 spy->setFocusable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08006740 spy->setSpy(true);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006741 spy->setDispatchingTimeout(30ms);
6742 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, mWindow}}});
6743 return spy;
6744 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006745};
6746
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006747// Send a tap and respond, which should not cause an ANR.
6748TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) {
6749 tapOnWindow();
6750 mWindow->consumeMotionDown();
6751 mWindow->consumeMotionUp();
6752 ASSERT_TRUE(mDispatcher->waitForIdle());
6753 mFakePolicy->assertNotifyAnrWasNotCalled();
6754}
6755
6756// Send a regular key and respond, which should not cause an ANR.
6757TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08006758 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006759 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
6760 ASSERT_TRUE(mDispatcher->waitForIdle());
6761 mFakePolicy->assertNotifyAnrWasNotCalled();
6762}
6763
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05006764TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) {
6765 mWindow->setFocusable(false);
6766 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6767 mWindow->consumeFocusEvent(false);
6768
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006769 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006770 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6771 InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms,
6772 /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006773 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05006774 // Key will not go to window because we have no focused window.
6775 // The 'no focused window' ANR timer should start instead.
6776
6777 // Now, the focused application goes away.
6778 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr);
6779 // The key should get dropped and there should be no ANR.
6780
6781 ASSERT_TRUE(mDispatcher->waitForIdle());
6782 mFakePolicy->assertNotifyAnrWasNotCalled();
6783}
6784
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006785// Send an event to the app and have the app not respond right away.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006786// When ANR is raised, policy will tell the dispatcher to cancel the events for that window.
6787// So InputDispatcher will enqueue ACTION_CANCEL event as well.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006788TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006789 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006790 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6791 WINDOW_LOCATION));
6792
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006793 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN
6794 ASSERT_TRUE(sequenceNum);
6795 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006796 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006797
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006798 mWindow->finishEvent(*sequenceNum);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08006799 mWindow->consumeMotionEvent(
6800 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006801 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006802 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006803}
6804
6805// Send a key to the app and have the app not respond right away.
6806TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) {
6807 // Inject a key, and don't respond - expect that ANR is called.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08006808 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006809 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent();
6810 ASSERT_TRUE(sequenceNum);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006811 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006812 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006813 ASSERT_TRUE(mDispatcher->waitForIdle());
6814}
6815
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006816// We have a focused application, but no focused window
6817TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) {
Vishnu Nair47074b82020-08-14 11:54:47 -07006818 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006819 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6820 mWindow->consumeFocusEvent(false);
6821
6822 // taps on the window work as normal
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006823 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006824 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6825 WINDOW_LOCATION));
6826 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
6827 mDispatcher->waitForIdle();
6828 mFakePolicy->assertNotifyAnrWasNotCalled();
6829
6830 // Once a focused event arrives, we get an ANR for this application
6831 // We specify the injection timeout to be smaller than the application timeout, to ensure that
6832 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006833 const InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006834 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6835 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006836 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006837 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Vishnu Naire4df8752022-09-08 09:17:55 -07006838 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006839 ASSERT_TRUE(mDispatcher->waitForIdle());
6840}
6841
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08006842/**
6843 * Make sure the stale key is dropped before causing an ANR. So even if there's no focused window,
6844 * there will not be an ANR.
6845 */
6846TEST_F(InputDispatcherSingleWindowAnr, StaleKeyEventDoesNotAnr) {
6847 mWindow->setFocusable(false);
6848 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6849 mWindow->consumeFocusEvent(false);
6850
6851 KeyEvent event;
6852 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC) -
6853 std::chrono::nanoseconds(STALE_EVENT_TIMEOUT).count();
6854
6855 // Define a valid key down event that is stale (too old).
6856 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
6857 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, /* flags */ 0, AKEYCODE_A, KEY_A,
Harry Cutts33476232023-01-30 19:57:29 +00006858 AMETA_NONE, /*repeatCount=*/1, eventTime, eventTime);
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08006859
6860 const int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER;
6861
6862 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006863 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08006864 InputEventInjectionSync::WAIT_FOR_RESULT,
6865 INJECT_EVENT_TIMEOUT, policyFlags);
6866 ASSERT_EQ(InputEventInjectionResult::FAILED, result)
6867 << "Injection should fail because the event is stale";
6868
6869 ASSERT_TRUE(mDispatcher->waitForIdle());
6870 mFakePolicy->assertNotifyAnrWasNotCalled();
6871 mWindow->assertNoEvents();
6872}
6873
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006874// We have a focused application, but no focused window
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006875// Make sure that we don't notify policy twice about the same ANR.
6876TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) {
Vishnu Nair47074b82020-08-14 11:54:47 -07006877 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006878 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6879 mWindow->consumeFocusEvent(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006880
6881 // Once a focused event arrives, we get an ANR for this application
6882 // We specify the injection timeout to be smaller than the application timeout, to ensure that
6883 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006884 const InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006885 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6886 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006887 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Vishnu Naire4df8752022-09-08 09:17:55 -07006888 const std::chrono::duration appTimeout =
6889 mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
6890 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006891
Vishnu Naire4df8752022-09-08 09:17:55 -07006892 std::this_thread::sleep_for(appTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006893 // ANR should not be raised again. It is up to policy to do that if it desires.
6894 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006895
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006896 // If we now get a focused window, the ANR should stop, but the policy handles that via
6897 // 'notifyFocusChanged' callback. This is implemented in the policy so we can't test it here.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006898 ASSERT_TRUE(mDispatcher->waitForIdle());
6899}
6900
6901// We have a focused application, but no focused window
6902TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) {
Vishnu Nair47074b82020-08-14 11:54:47 -07006903 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006904 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6905 mWindow->consumeFocusEvent(false);
6906
6907 // Once a focused event arrives, we get an ANR for this application
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006908 const InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006909 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006910 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
6911 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006912
Vishnu Naire4df8752022-09-08 09:17:55 -07006913 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
6914 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006915
6916 // Future focused events get dropped right away
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006917 ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006918 ASSERT_TRUE(mDispatcher->waitForIdle());
6919 mWindow->assertNoEvents();
6920}
6921
6922/**
6923 * Ensure that the implementation is valid. Since we are using multiset to keep track of the
6924 * ANR timeouts, we are allowing entries with identical timestamps in the same connection.
6925 * If we process 1 of the events, but ANR on the second event with the same timestamp,
6926 * the ANR mechanism should still work.
6927 *
6928 * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the
6929 * DOWN event, while not responding on the second one.
6930 */
6931TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) {
6932 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
6933 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6934 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
6935 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
6936 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006937 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006938
6939 // Now send ACTION_UP, with identical timestamp
6940 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
6941 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
6942 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
6943 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006944 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006945
6946 // We have now sent down and up. Let's consume first event and then ANR on the second.
6947 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6948 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006949 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006950}
6951
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006952// A spy window can receive an ANR
6953TEST_F(InputDispatcherSingleWindowAnr, SpyWindowAnr) {
6954 sp<FakeWindowHandle> spy = addSpyWindow();
6955
6956 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6957 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6958 WINDOW_LOCATION));
6959 mWindow->consumeMotionDown();
6960
6961 std::optional<uint32_t> sequenceNum = spy->receiveEvent(); // ACTION_DOWN
6962 ASSERT_TRUE(sequenceNum);
6963 const std::chrono::duration timeout = spy->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006964 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, spy);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006965
6966 spy->finishEvent(*sequenceNum);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08006967 spy->consumeMotionEvent(
6968 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006969 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006970 mFakePolicy->assertNotifyWindowResponsiveWasCalled(spy->getToken(), mWindow->getPid());
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006971}
6972
6973// If an app is not responding to a key event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006974// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006975TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnKey) {
6976 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006977
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006978 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6979 injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006980 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006981 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006982
6983 // Stuck on the ACTION_UP
6984 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006985 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006986
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006987 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006988 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006989 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6990 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006991
6992 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion
6993 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006994 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006995 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006996 spy->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006997}
6998
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006999// If an app is not responding to a motion event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007000// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007001TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnMotion) {
7002 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007003
7004 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007005 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7006 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007007
7008 mWindow->consumeMotionDown();
7009 // Stuck on the ACTION_UP
7010 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08007011 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007012
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007013 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007014 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007015 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7016 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007017
7018 mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion
7019 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08007020 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007021 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007022 spy->assertNoEvents();
7023}
7024
7025TEST_F(InputDispatcherSingleWindowAnr, UnresponsiveMonitorAnr) {
7026 mDispatcher->setMonitorDispatchingTimeoutForTest(30ms);
7027
7028 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
7029
7030 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7031 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7032 WINDOW_LOCATION));
7033
7034 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7035 const std::optional<uint32_t> consumeSeq = monitor.receiveEvent();
7036 ASSERT_TRUE(consumeSeq);
7037
Prabir Pradhanedd96402022-02-15 01:46:16 -08007038 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(30ms, monitor.getToken(), MONITOR_PID);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007039
7040 monitor.finishEvent(*consumeSeq);
7041 monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
7042
7043 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08007044 mFakePolicy->assertNotifyWindowResponsiveWasCalled(monitor.getToken(), MONITOR_PID);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007045}
7046
7047// If a window is unresponsive, then you get anr. if the window later catches up and starts to
7048// process events, you don't get an anr. When the window later becomes unresponsive again, you
7049// get an ANR again.
7050// 1. tap -> block on ACTION_UP -> receive ANR
7051// 2. consume all pending events (= queue becomes healthy again)
7052// 3. tap again -> block on ACTION_UP again -> receive ANR second time
7053TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) {
7054 tapOnWindow();
7055
7056 mWindow->consumeMotionDown();
7057 // Block on ACTION_UP
7058 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08007059 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007060 mWindow->consumeMotionUp(); // Now the connection should be healthy again
7061 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08007062 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007063 mWindow->assertNoEvents();
7064
7065 tapOnWindow();
7066 mWindow->consumeMotionDown();
Prabir Pradhanedd96402022-02-15 01:46:16 -08007067 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007068 mWindow->consumeMotionUp();
7069
7070 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08007071 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007072 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007073 mWindow->assertNoEvents();
7074}
7075
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007076// If a connection remains unresponsive for a while, make sure policy is only notified once about
7077// it.
7078TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007079 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007080 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7081 WINDOW_LOCATION));
7082
7083 const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08007084 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007085 std::this_thread::sleep_for(windowTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007086 // 'notifyConnectionUnresponsive' should only be called once per connection
7087 mFakePolicy->assertNotifyAnrWasNotCalled();
7088 // When the ANR happened, dispatcher should abort the current event stream via ACTION_CANCEL
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007089 mWindow->consumeMotionDown();
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08007090 mWindow->consumeMotionEvent(
7091 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007092 mWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007093 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08007094 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007095 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007096}
7097
7098/**
7099 * If a window is processing a motion event, and then a key event comes in, the key event should
7100 * not to to the focused window until the motion is processed.
7101 *
7102 * Warning!!!
7103 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
7104 * and the injection timeout that we specify when injecting the key.
7105 * We must have the injection timeout (10ms) be smaller than
7106 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
7107 *
7108 * If that value changes, this test should also change.
7109 */
7110TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) {
7111 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
7112 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
7113
7114 tapOnWindow();
7115 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
7116 ASSERT_TRUE(downSequenceNum);
7117 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
7118 ASSERT_TRUE(upSequenceNum);
7119 // Don't finish the events yet, and send a key
7120 // Injection will "succeed" because we will eventually give up and send the key to the focused
7121 // window even if motions are still being processed. But because the injection timeout is short,
7122 // we will receive INJECTION_TIMED_OUT as the result.
7123
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007124 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00007125 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007126 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
7127 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007128 // Key will not be sent to the window, yet, because the window is still processing events
7129 // and the key remains pending, waiting for the touch events to be processed
7130 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
7131 ASSERT_FALSE(keySequenceNum);
7132
7133 std::this_thread::sleep_for(500ms);
7134 // if we wait long enough though, dispatcher will give up, and still send the key
7135 // to the focused window, even though we have not yet finished the motion event
7136 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
7137 mWindow->finishEvent(*downSequenceNum);
7138 mWindow->finishEvent(*upSequenceNum);
7139}
7140
7141/**
7142 * If a window is processing a motion event, and then a key event comes in, the key event should
7143 * not go to the focused window until the motion is processed.
7144 * If then a new motion comes in, then the pending key event should be going to the currently
7145 * focused window right away.
7146 */
7147TEST_F(InputDispatcherSingleWindowAnr,
7148 PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) {
7149 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
7150 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
7151
7152 tapOnWindow();
7153 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
7154 ASSERT_TRUE(downSequenceNum);
7155 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
7156 ASSERT_TRUE(upSequenceNum);
7157 // Don't finish the events yet, and send a key
7158 // Injection is async, so it will succeed
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007159 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00007160 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
7161 InputEventInjectionSync::NONE));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007162 // At this point, key is still pending, and should not be sent to the application yet.
7163 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
7164 ASSERT_FALSE(keySequenceNum);
7165
7166 // Now tap down again. It should cause the pending key to go to the focused window right away.
7167 tapOnWindow();
7168 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd
7169 // the other events yet. We can finish events in any order.
7170 mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN
7171 mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP
7172 mWindow->consumeMotionDown();
7173 mWindow->consumeMotionUp();
7174 mWindow->assertNoEvents();
7175}
7176
7177class InputDispatcherMultiWindowAnr : public InputDispatcherTest {
7178 virtual void SetUp() override {
7179 InputDispatcherTest::SetUp();
7180
Chris Yea209fde2020-07-22 13:54:51 -07007181 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007182 mApplication->setDispatchingTimeout(10ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007183 mUnfocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Unfocused",
7184 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007185 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007186 // Adding FLAG_WATCH_OUTSIDE_TOUCH to receive ACTION_OUTSIDE when another window is tapped
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007187 mUnfocusedWindow->setWatchOutsideTouch(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007188
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007189 mFocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Focused",
7190 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05007191 mFocusedWindow->setDispatchingTimeout(30ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007192 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007193
7194 // Set focused application.
7195 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
Vishnu Nair47074b82020-08-14 11:54:47 -07007196 mFocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007197
7198 // Expect one focus window exist in display.
7199 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07007200 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007201 mFocusedWindow->consumeFocusEvent(true);
7202 }
7203
7204 virtual void TearDown() override {
7205 InputDispatcherTest::TearDown();
7206
7207 mUnfocusedWindow.clear();
7208 mFocusedWindow.clear();
7209 }
7210
7211protected:
Chris Yea209fde2020-07-22 13:54:51 -07007212 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007213 sp<FakeWindowHandle> mUnfocusedWindow;
7214 sp<FakeWindowHandle> mFocusedWindow;
7215 static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20};
7216 static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75};
7217 static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40};
7218
7219 void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); }
7220
7221 void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); }
7222
7223private:
7224 void tap(const PointF& location) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007225 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007226 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7227 location));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007228 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007229 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7230 location));
7231 }
7232};
7233
7234// If we have 2 windows that are both unresponsive, the one with the shortest timeout
7235// should be ANR'd first.
7236TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007237 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007238 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7239 FOCUSED_WINDOW_LOCATION))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007240 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007241 mFocusedWindow->consumeMotionDown();
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07007242 mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00007243 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007244 // We consumed all events, so no ANR
7245 ASSERT_TRUE(mDispatcher->waitForIdle());
7246 mFakePolicy->assertNotifyAnrWasNotCalled();
7247
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007248 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007249 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7250 FOCUSED_WINDOW_LOCATION));
7251 std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent();
7252 ASSERT_TRUE(unfocusedSequenceNum);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007253
7254 const std::chrono::duration timeout =
7255 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08007256 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007257 // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event
7258 // sequence to make it consistent
7259 mFocusedWindow->consumeMotionCancel();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007260 mUnfocusedWindow->finishEvent(*unfocusedSequenceNum);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007261 mFocusedWindow->consumeMotionDown();
7262 // This cancel is generated because the connection was unresponsive
7263 mFocusedWindow->consumeMotionCancel();
7264 mFocusedWindow->assertNoEvents();
7265 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007266 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08007267 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
7268 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007269 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007270}
7271
7272// If we have 2 windows with identical timeouts that are both unresponsive,
7273// it doesn't matter which order they should have ANR.
7274// But we should receive ANR for both.
7275TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) {
7276 // Set the timeout for unfocused window to match the focused window
7277 mUnfocusedWindow->setDispatchingTimeout(10ms);
7278 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
7279
7280 tapOnFocusedWindow();
7281 // we should have ACTION_DOWN/ACTION_UP on focused window and ACTION_OUTSIDE on unfocused window
Prabir Pradhanedd96402022-02-15 01:46:16 -08007282 sp<IBinder> anrConnectionToken1, anrConnectionToken2;
7283 ASSERT_NO_FATAL_FAILURE(anrConnectionToken1 = mFakePolicy->getUnresponsiveWindowToken(10ms));
7284 ASSERT_NO_FATAL_FAILURE(anrConnectionToken2 = mFakePolicy->getUnresponsiveWindowToken(0ms));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007285
7286 // We don't know which window will ANR first. But both of them should happen eventually.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007287 ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 ||
7288 mFocusedWindow->getToken() == anrConnectionToken2);
7289 ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 ||
7290 mUnfocusedWindow->getToken() == anrConnectionToken2);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007291
7292 ASSERT_TRUE(mDispatcher->waitForIdle());
7293 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007294
7295 mFocusedWindow->consumeMotionDown();
7296 mFocusedWindow->consumeMotionUp();
7297 mUnfocusedWindow->consumeMotionOutside();
7298
Prabir Pradhanedd96402022-02-15 01:46:16 -08007299 sp<IBinder> responsiveToken1, responsiveToken2;
7300 ASSERT_NO_FATAL_FAILURE(responsiveToken1 = mFakePolicy->getResponsiveWindowToken());
7301 ASSERT_NO_FATAL_FAILURE(responsiveToken2 = mFakePolicy->getResponsiveWindowToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007302
7303 // Both applications should be marked as responsive, in any order
7304 ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 ||
7305 mFocusedWindow->getToken() == responsiveToken2);
7306 ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 ||
7307 mUnfocusedWindow->getToken() == responsiveToken2);
7308 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007309}
7310
7311// If a window is already not responding, the second tap on the same window should be ignored.
7312// We should also log an error to account for the dropped event (not tested here).
7313// At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events.
7314TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) {
7315 tapOnFocusedWindow();
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07007316 mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00007317 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007318 // Receive the events, but don't respond
7319 std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN
7320 ASSERT_TRUE(downEventSequenceNum);
7321 std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP
7322 ASSERT_TRUE(upEventSequenceNum);
7323 const std::chrono::duration timeout =
7324 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08007325 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007326
7327 // Tap once again
7328 // We cannot use "tapOnFocusedWindow" because it asserts the injection result to be success
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007329 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007330 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7331 FOCUSED_WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007332 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007333 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7334 FOCUSED_WINDOW_LOCATION));
7335 // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a
7336 // valid touch target
7337 mUnfocusedWindow->assertNoEvents();
7338
7339 // Consume the first tap
7340 mFocusedWindow->finishEvent(*downEventSequenceNum);
7341 mFocusedWindow->finishEvent(*upEventSequenceNum);
7342 ASSERT_TRUE(mDispatcher->waitForIdle());
7343 // The second tap did not go to the focused window
7344 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007345 // Since all events are finished, connection should be deemed healthy again
Prabir Pradhanedd96402022-02-15 01:46:16 -08007346 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
7347 mFocusedWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007348 mFakePolicy->assertNotifyAnrWasNotCalled();
7349}
7350
7351// If you tap outside of all windows, there will not be ANR
7352TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007353 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007354 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7355 LOCATION_OUTSIDE_ALL_WINDOWS));
7356 ASSERT_TRUE(mDispatcher->waitForIdle());
7357 mFakePolicy->assertNotifyAnrWasNotCalled();
7358}
7359
7360// Since the focused window is paused, tapping on it should not produce any events
7361TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) {
7362 mFocusedWindow->setPaused(true);
7363 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
7364
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007365 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007366 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7367 FOCUSED_WINDOW_LOCATION));
7368
7369 std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT));
7370 ASSERT_TRUE(mDispatcher->waitForIdle());
7371 // Should not ANR because the window is paused, and touches shouldn't go to it
7372 mFakePolicy->assertNotifyAnrWasNotCalled();
7373
7374 mFocusedWindow->assertNoEvents();
7375 mUnfocusedWindow->assertNoEvents();
7376}
7377
7378/**
7379 * If a window is processing a motion event, and then a key event comes in, the key event should
7380 * not to to the focused window until the motion is processed.
7381 * If a different window becomes focused at this time, the key should go to that window instead.
7382 *
7383 * Warning!!!
7384 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
7385 * and the injection timeout that we specify when injecting the key.
7386 * We must have the injection timeout (10ms) be smaller than
7387 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
7388 *
7389 * If that value changes, this test should also change.
7390 */
7391TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) {
7392 // Set a long ANR timeout to prevent it from triggering
7393 mFocusedWindow->setDispatchingTimeout(2s);
7394 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
7395
7396 tapOnUnfocusedWindow();
7397 std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent();
7398 ASSERT_TRUE(downSequenceNum);
7399 std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent();
7400 ASSERT_TRUE(upSequenceNum);
7401 // Don't finish the events yet, and send a key
7402 // Injection will succeed because we will eventually give up and send the key to the focused
7403 // window even if motions are still being processed.
7404
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007405 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00007406 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
7407 InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007408 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007409 // Key will not be sent to the window, yet, because the window is still processing events
7410 // and the key remains pending, waiting for the touch events to be processed
7411 std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent();
7412 ASSERT_FALSE(keySequenceNum);
7413
7414 // Switch the focus to the "unfocused" window that we tapped. Expect the key to go there
Vishnu Nair47074b82020-08-14 11:54:47 -07007415 mFocusedWindow->setFocusable(false);
7416 mUnfocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007417 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07007418 setFocusedWindow(mUnfocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007419
7420 // Focus events should precede the key events
7421 mUnfocusedWindow->consumeFocusEvent(true);
7422 mFocusedWindow->consumeFocusEvent(false);
7423
7424 // Finish the tap events, which should unblock dispatcher
7425 mUnfocusedWindow->finishEvent(*downSequenceNum);
7426 mUnfocusedWindow->finishEvent(*upSequenceNum);
7427
7428 // Now that all queues are cleared and no backlog in the connections, the key event
7429 // can finally go to the newly focused "mUnfocusedWindow".
7430 mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
7431 mFocusedWindow->assertNoEvents();
7432 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007433 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007434}
7435
7436// When the touch stream is split across 2 windows, and one of them does not respond,
7437// then ANR should be raised and the touch should be canceled for the unresponsive window.
7438// The other window should not be affected by that.
7439TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) {
7440 // Touch Window 1
Prabir Pradhan678438e2023-04-13 19:32:51 +00007441 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7442 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7443 {FOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07007444 mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00007445 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007446
7447 // Touch Window 2
Prabir Pradhan678438e2023-04-13 19:32:51 +00007448 mDispatcher->notifyMotion(
7449 generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7450 {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007451
7452 const std::chrono::duration timeout =
7453 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08007454 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007455
7456 mUnfocusedWindow->consumeMotionDown();
7457 mFocusedWindow->consumeMotionDown();
7458 // Focused window may or may not receive ACTION_MOVE
7459 // But it should definitely receive ACTION_CANCEL due to the ANR
7460 InputEvent* event;
7461 std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event);
7462 ASSERT_TRUE(moveOrCancelSequenceNum);
7463 mFocusedWindow->finishEvent(*moveOrCancelSequenceNum);
7464 ASSERT_NE(nullptr, event);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07007465 ASSERT_EQ(event->getType(), InputEventType::MOTION);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007466 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
7467 if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) {
7468 mFocusedWindow->consumeMotionCancel();
7469 } else {
7470 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction());
7471 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007472 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08007473 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
7474 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007475
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007476 mUnfocusedWindow->assertNoEvents();
7477 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007478 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007479}
7480
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05007481/**
7482 * If we have no focused window, and a key comes in, we start the ANR timer.
7483 * The focused application should add a focused window before the timer runs out to prevent ANR.
7484 *
7485 * If the user touches another application during this time, the key should be dropped.
7486 * Next, if a new focused window comes in, without toggling the focused application,
7487 * then no ANR should occur.
7488 *
7489 * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication',
7490 * but in some cases the policy may not update the focused application.
7491 */
7492TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) {
7493 std::shared_ptr<FakeApplicationHandle> focusedApplication =
7494 std::make_shared<FakeApplicationHandle>();
7495 focusedApplication->setDispatchingTimeout(60ms);
7496 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication);
7497 // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused.
7498 mFocusedWindow->setFocusable(false);
7499
7500 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
7501 mFocusedWindow->consumeFocusEvent(false);
7502
7503 // Send a key. The ANR timer should start because there is no focused window.
7504 // 'focusedApplication' will get blamed if this timer completes.
7505 // Key will not be sent anywhere because we have no focused window. It will remain pending.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007506 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00007507 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
7508 InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms,
7509 /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007510 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05007511
7512 // Wait until dispatcher starts the "no focused window" timer. If we don't wait here,
7513 // then the injected touches won't cause the focused event to get dropped.
7514 // The dispatcher only checks for whether the queue should be pruned upon queueing.
7515 // If we inject the touch right away and the ANR timer hasn't started, the touch event would
7516 // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'.
7517 // For this test, it means that the key would get delivered to the window once it becomes
7518 // focused.
7519 std::this_thread::sleep_for(10ms);
7520
7521 // Touch unfocused window. This should force the pending key to get dropped.
Prabir Pradhan678438e2023-04-13 19:32:51 +00007522 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7523 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7524 {UNFOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05007525
7526 // We do not consume the motion right away, because that would require dispatcher to first
7527 // process (== drop) the key event, and by that time, ANR will be raised.
7528 // Set the focused window first.
7529 mFocusedWindow->setFocusable(true);
7530 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
7531 setFocusedWindow(mFocusedWindow);
7532 mFocusedWindow->consumeFocusEvent(true);
7533 // We do not call "setFocusedApplication" here, even though the newly focused window belongs
7534 // to another application. This could be a bug / behaviour in the policy.
7535
7536 mUnfocusedWindow->consumeMotionDown();
7537
7538 ASSERT_TRUE(mDispatcher->waitForIdle());
7539 // Should not ANR because we actually have a focused window. It was just added too slowly.
7540 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled());
7541}
7542
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007543// These tests ensure we cannot send touch events to a window that's positioned behind a window
7544// that has feature NO_INPUT_CHANNEL.
7545// Layout:
7546// Top (closest to user)
7547// mNoInputWindow (above all windows)
7548// mBottomWindow
7549// Bottom (furthest from user)
7550class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest {
7551 virtual void SetUp() override {
7552 InputDispatcherTest::SetUp();
7553
7554 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007555 mNoInputWindow =
7556 sp<FakeWindowHandle>::make(mApplication, mDispatcher,
7557 "Window without input channel", ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00007558 /*token=*/std::make_optional<sp<IBinder>>(nullptr));
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007559 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007560 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
7561 // It's perfectly valid for this window to not have an associated input channel
7562
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007563 mBottomWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Bottom window",
7564 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007565 mBottomWindow->setFrame(Rect(0, 0, 100, 100));
7566
7567 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
7568 }
7569
7570protected:
7571 std::shared_ptr<FakeApplicationHandle> mApplication;
7572 sp<FakeWindowHandle> mNoInputWindow;
7573 sp<FakeWindowHandle> mBottomWindow;
7574};
7575
7576TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) {
7577 PointF touchedPoint = {10, 10};
7578
Prabir Pradhan678438e2023-04-13 19:32:51 +00007579 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7580 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7581 {touchedPoint}));
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007582
7583 mNoInputWindow->assertNoEvents();
7584 // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have
7585 // an input channel, it is not marked as FLAG_NOT_TOUCHABLE,
7586 // and therefore should prevent mBottomWindow from receiving touches
7587 mBottomWindow->assertNoEvents();
7588}
7589
7590/**
7591 * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel,
7592 * ensure that this window does not receive any touches, and blocks touches to windows underneath.
7593 */
7594TEST_F(InputDispatcherMultiWindowOcclusionTests,
7595 NoInputChannelFeature_DropsTouchesWithValidChannel) {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007596 mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher,
7597 "Window with input channel and NO_INPUT_CHANNEL",
7598 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007599
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007600 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007601 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
7602 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
7603
7604 PointF touchedPoint = {10, 10};
7605
Prabir Pradhan678438e2023-04-13 19:32:51 +00007606 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7607 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7608 {touchedPoint}));
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007609
7610 mNoInputWindow->assertNoEvents();
7611 mBottomWindow->assertNoEvents();
7612}
7613
Vishnu Nair958da932020-08-21 17:12:37 -07007614class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest {
7615protected:
7616 std::shared_ptr<FakeApplicationHandle> mApp;
7617 sp<FakeWindowHandle> mWindow;
7618 sp<FakeWindowHandle> mMirror;
7619
7620 virtual void SetUp() override {
7621 InputDispatcherTest::SetUp();
7622 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007623 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
7624 mMirror = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindowMirror",
7625 ADISPLAY_ID_DEFAULT, mWindow->getToken());
Vishnu Nair958da932020-08-21 17:12:37 -07007626 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
7627 mWindow->setFocusable(true);
7628 mMirror->setFocusable(true);
7629 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7630 }
7631};
7632
7633TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) {
7634 // Request focus on a mirrored window
7635 setFocusedWindow(mMirror);
7636
7637 // window gets focused
7638 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007639 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7640 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007641 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
7642}
7643
7644// A focused & mirrored window remains focused only if the window and its mirror are both
7645// focusable.
7646TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) {
7647 setFocusedWindow(mMirror);
7648
7649 // window gets focused
7650 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007651 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7652 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007653 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007654 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7655 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007656 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7657
7658 mMirror->setFocusable(false);
7659 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7660
7661 // window loses focus since one of the windows associated with the token in not focusable
7662 mWindow->consumeFocusEvent(false);
7663
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007664 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
7665 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07007666 mWindow->assertNoEvents();
7667}
7668
7669// A focused & mirrored window remains focused until the window and its mirror both become
7670// invisible.
7671TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) {
7672 setFocusedWindow(mMirror);
7673
7674 // window gets focused
7675 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007676 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7677 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007678 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007679 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7680 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007681 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7682
7683 mMirror->setVisible(false);
7684 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7685
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007686 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7687 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007688 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007689 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7690 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007691 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7692
7693 mWindow->setVisible(false);
7694 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7695
7696 // window loses focus only after all windows associated with the token become invisible.
7697 mWindow->consumeFocusEvent(false);
7698
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007699 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
7700 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07007701 mWindow->assertNoEvents();
7702}
7703
7704// A focused & mirrored window remains focused until both windows are removed.
7705TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) {
7706 setFocusedWindow(mMirror);
7707
7708 // window gets focused
7709 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007710 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7711 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007712 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007713 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7714 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007715 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7716
7717 // single window is removed but the window token remains focused
7718 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}});
7719
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007720 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7721 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007722 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007723 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7724 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007725 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7726
7727 // Both windows are removed
7728 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
7729 mWindow->consumeFocusEvent(false);
7730
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007731 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
7732 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07007733 mWindow->assertNoEvents();
7734}
7735
7736// Focus request can be pending until one window becomes visible.
7737TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) {
7738 // Request focus on an invisible mirror.
7739 mWindow->setVisible(false);
7740 mMirror->setVisible(false);
7741 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7742 setFocusedWindow(mMirror);
7743
7744 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007745 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00007746 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
7747 InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07007748
7749 mMirror->setVisible(true);
7750 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7751
7752 // window gets focused
7753 mWindow->consumeFocusEvent(true);
7754 // window gets the pending key event
7755 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
7756}
Prabir Pradhan99987712020-11-10 18:43:05 -08007757
7758class InputDispatcherPointerCaptureTests : public InputDispatcherTest {
7759protected:
7760 std::shared_ptr<FakeApplicationHandle> mApp;
7761 sp<FakeWindowHandle> mWindow;
7762 sp<FakeWindowHandle> mSecondWindow;
7763
7764 void SetUp() override {
7765 InputDispatcherTest::SetUp();
7766 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007767 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -08007768 mWindow->setFocusable(true);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007769 mSecondWindow =
7770 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -08007771 mSecondWindow->setFocusable(true);
7772
7773 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
7774 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}});
7775
7776 setFocusedWindow(mWindow);
7777 mWindow->consumeFocusEvent(true);
7778 }
7779
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007780 void notifyPointerCaptureChanged(const PointerCaptureRequest& request) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00007781 mDispatcher->notifyPointerCaptureChanged(generatePointerCaptureChangedArgs(request));
Prabir Pradhan99987712020-11-10 18:43:05 -08007782 }
7783
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007784 PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window,
7785 bool enabled) {
Prabir Pradhan99987712020-11-10 18:43:05 -08007786 mDispatcher->requestPointerCapture(window->getToken(), enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007787 auto request = mFakePolicy->assertSetPointerCaptureCalled(enabled);
7788 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08007789 window->consumeCaptureEvent(enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007790 return request;
Prabir Pradhan99987712020-11-10 18:43:05 -08007791 }
7792};
7793
7794TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) {
7795 // Ensure that capture cannot be obtained for unfocused windows.
7796 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
7797 mFakePolicy->assertSetPointerCaptureNotCalled();
7798 mSecondWindow->assertNoEvents();
7799
7800 // Ensure that capture can be enabled from the focus window.
7801 requestAndVerifyPointerCapture(mWindow, true);
7802
7803 // Ensure that capture cannot be disabled from a window that does not have capture.
7804 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false);
7805 mFakePolicy->assertSetPointerCaptureNotCalled();
7806
7807 // Ensure that capture can be disabled from the window with capture.
7808 requestAndVerifyPointerCapture(mWindow, false);
7809}
7810
7811TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007812 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08007813
7814 setFocusedWindow(mSecondWindow);
7815
7816 // Ensure that the capture disabled event was sent first.
7817 mWindow->consumeCaptureEvent(false);
7818 mWindow->consumeFocusEvent(false);
7819 mSecondWindow->consumeFocusEvent(true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007820 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08007821
7822 // Ensure that additional state changes from InputReader are not sent to the window.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007823 notifyPointerCaptureChanged({});
7824 notifyPointerCaptureChanged(request);
7825 notifyPointerCaptureChanged({});
Prabir Pradhan99987712020-11-10 18:43:05 -08007826 mWindow->assertNoEvents();
7827 mSecondWindow->assertNoEvents();
7828 mFakePolicy->assertSetPointerCaptureNotCalled();
7829}
7830
7831TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007832 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08007833
7834 // InputReader unexpectedly disables and enables pointer capture.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007835 notifyPointerCaptureChanged({});
7836 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08007837
7838 // Ensure that Pointer Capture is disabled.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007839 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08007840 mWindow->consumeCaptureEvent(false);
7841 mWindow->assertNoEvents();
7842}
7843
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007844TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) {
7845 requestAndVerifyPointerCapture(mWindow, true);
7846
7847 // The first window loses focus.
7848 setFocusedWindow(mSecondWindow);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007849 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007850 mWindow->consumeCaptureEvent(false);
7851
7852 // Request Pointer Capture from the second window before the notification from InputReader
7853 // arrives.
7854 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007855 auto request = mFakePolicy->assertSetPointerCaptureCalled(true);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007856
7857 // InputReader notifies Pointer Capture was disabled (because of the focus change).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007858 notifyPointerCaptureChanged({});
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007859
7860 // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007861 notifyPointerCaptureChanged(request);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007862
7863 mSecondWindow->consumeFocusEvent(true);
7864 mSecondWindow->consumeCaptureEvent(true);
7865}
7866
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007867TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) {
7868 // App repeatedly enables and disables capture.
7869 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
7870 auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
7871 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
7872 mFakePolicy->assertSetPointerCaptureCalled(false);
7873 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
7874 auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
7875
7876 // InputReader notifies that PointerCapture has been enabled for the first request. Since the
7877 // first request is now stale, this should do nothing.
7878 notifyPointerCaptureChanged(firstRequest);
7879 mWindow->assertNoEvents();
7880
7881 // InputReader notifies that the second request was enabled.
7882 notifyPointerCaptureChanged(secondRequest);
7883 mWindow->consumeCaptureEvent(true);
7884}
7885
Prabir Pradhan7092e262022-05-03 16:51:09 +00007886TEST_F(InputDispatcherPointerCaptureTests, RapidToggleRequests) {
7887 requestAndVerifyPointerCapture(mWindow, true);
7888
7889 // App toggles pointer capture off and on.
7890 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
7891 mFakePolicy->assertSetPointerCaptureCalled(false);
7892
7893 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
7894 auto enableRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
7895
7896 // InputReader notifies that the latest "enable" request was processed, while skipping over the
7897 // preceding "disable" request.
7898 notifyPointerCaptureChanged(enableRequest);
7899
7900 // Since pointer capture was never disabled during the rapid toggle, the window does not receive
7901 // any notifications.
7902 mWindow->assertNoEvents();
7903}
7904
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007905class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest {
7906protected:
7907 constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8;
Bernardo Rufino7393d172021-02-26 13:56:11 +00007908
7909 constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9;
7910 static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY);
7911
7912 constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7;
7913 static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
7914
7915 // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold
7916 constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5;
7917 static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
7918 static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) <
7919 MAXIMUM_OBSCURING_OPACITY);
7920
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007921 static const int32_t TOUCHED_APP_UID = 10001;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007922 static const int32_t APP_B_UID = 10002;
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007923 static const int32_t APP_C_UID = 10003;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007924
7925 sp<FakeWindowHandle> mTouchWindow;
7926
7927 virtual void SetUp() override {
7928 InputDispatcherTest::SetUp();
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007929 mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched");
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007930 mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY);
7931 }
7932
7933 virtual void TearDown() override {
7934 InputDispatcherTest::TearDown();
7935 mTouchWindow.clear();
7936 }
7937
chaviw3277faf2021-05-19 16:45:23 -05007938 sp<FakeWindowHandle> getOccludingWindow(int32_t uid, std::string name, TouchOcclusionMode mode,
7939 float alpha = 1.0f) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007940 sp<FakeWindowHandle> window = getWindow(uid, name);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007941 window->setTouchable(false);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007942 window->setTouchOcclusionMode(mode);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007943 window->setAlpha(alpha);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007944 return window;
7945 }
7946
7947 sp<FakeWindowHandle> getWindow(int32_t uid, std::string name) {
7948 std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>();
7949 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007950 sp<FakeWindowHandle>::make(app, mDispatcher, name, ADISPLAY_ID_DEFAULT);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007951 // Generate an arbitrary PID based on the UID
7952 window->setOwnerInfo(1777 + (uid % 10000), uid);
7953 return window;
7954 }
7955
7956 void touch(const std::vector<PointF>& points = {PointF{100, 200}}) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00007957 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7958 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7959 points));
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007960 }
7961};
7962
7963TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007964 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007965 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007966 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007967
7968 touch();
7969
7970 mTouchWindow->assertNoEvents();
7971}
7972
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007973TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufino7393d172021-02-26 13:56:11 +00007974 WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) {
7975 const sp<FakeWindowHandle>& w =
7976 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f);
7977 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7978
7979 touch();
7980
7981 mTouchWindow->assertNoEvents();
7982}
7983
7984TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007985 WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) {
7986 const sp<FakeWindowHandle>& w =
7987 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
7988 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7989
7990 touch();
7991
7992 w->assertNoEvents();
7993}
7994
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007995TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007996 const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW);
7997 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007998
7999 touch();
8000
8001 mTouchWindow->consumeAnyMotionDown();
8002}
8003
8004TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008005 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00008006 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008007 w->setFrame(Rect(0, 0, 50, 50));
8008 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00008009
8010 touch({PointF{100, 100}});
8011
8012 mTouchWindow->consumeAnyMotionDown();
8013}
8014
8015TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008016 const sp<FakeWindowHandle>& w =
Bernardo Rufino6d52e542021-02-15 18:38:10 +00008017 getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008018 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8019
8020 touch();
8021
8022 mTouchWindow->consumeAnyMotionDown();
8023}
8024
8025TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) {
8026 const sp<FakeWindowHandle>& w =
8027 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
8028 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00008029
8030 touch();
8031
8032 mTouchWindow->consumeAnyMotionDown();
8033}
8034
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00008035TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) {
8036 const sp<FakeWindowHandle>& w =
8037 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
8038 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8039
8040 touch();
8041
8042 w->assertNoEvents();
8043}
8044
8045/**
8046 * This is important to make sure apps can't indirectly learn the position of touches (outside vs
8047 * inside) while letting them pass-through. Note that even though touch passes through the occluding
8048 * window, the occluding window will still receive ACTION_OUTSIDE event.
8049 */
8050TEST_F(InputDispatcherUntrustedTouchesTest,
8051 WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) {
8052 const sp<FakeWindowHandle>& w =
8053 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008054 w->setWatchOutsideTouch(true);
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00008055 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8056
8057 touch();
8058
8059 w->consumeMotionOutside();
8060}
8061
8062TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) {
8063 const sp<FakeWindowHandle>& w =
8064 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008065 w->setWatchOutsideTouch(true);
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00008066 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8067
8068 touch();
8069
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08008070 w->consumeMotionOutsideWithZeroedCoords();
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00008071}
8072
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00008073TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008074 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008075 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8076 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008077 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8078
8079 touch();
8080
8081 mTouchWindow->consumeAnyMotionDown();
8082}
8083
8084TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) {
8085 const sp<FakeWindowHandle>& w =
8086 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8087 MAXIMUM_OBSCURING_OPACITY);
8088 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00008089
8090 touch();
8091
8092 mTouchWindow->consumeAnyMotionDown();
8093}
8094
8095TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008096 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008097 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8098 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008099 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8100
8101 touch();
8102
8103 mTouchWindow->assertNoEvents();
8104}
8105
8106TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) {
8107 // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91
8108 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008109 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
8110 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008111 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008112 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
8113 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008114 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
8115
8116 touch();
8117
8118 mTouchWindow->assertNoEvents();
8119}
8120
8121TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) {
8122 // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75
8123 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008124 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
8125 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008126 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008127 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
8128 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008129 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
8130
8131 touch();
8132
8133 mTouchWindow->consumeAnyMotionDown();
8134}
8135
8136TEST_F(InputDispatcherUntrustedTouchesTest,
8137 WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) {
8138 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008139 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8140 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008141 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008142 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
8143 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008144 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
8145
8146 touch();
8147
8148 mTouchWindow->consumeAnyMotionDown();
8149}
8150
8151TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) {
8152 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008153 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8154 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008155 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008156 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
8157 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008158 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00008159
8160 touch();
8161
8162 mTouchWindow->assertNoEvents();
8163}
8164
Bernardo Rufino6d52e542021-02-15 18:38:10 +00008165TEST_F(InputDispatcherUntrustedTouchesTest,
8166 WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) {
8167 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008168 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
8169 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00008170 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008171 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8172 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00008173 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
8174
8175 touch();
8176
8177 mTouchWindow->assertNoEvents();
8178}
8179
8180TEST_F(InputDispatcherUntrustedTouchesTest,
8181 WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) {
8182 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008183 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
8184 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00008185 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008186 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8187 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00008188 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
8189
8190 touch();
8191
8192 mTouchWindow->consumeAnyMotionDown();
8193}
8194
8195TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) {
8196 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008197 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
8198 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00008199 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8200
8201 touch();
8202
8203 mTouchWindow->consumeAnyMotionDown();
8204}
8205
8206TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) {
8207 const sp<FakeWindowHandle>& w =
8208 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED);
8209 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8210
8211 touch();
8212
8213 mTouchWindow->consumeAnyMotionDown();
8214}
8215
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00008216TEST_F(InputDispatcherUntrustedTouchesTest,
8217 OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) {
8218 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
8219 const sp<FakeWindowHandle>& w =
8220 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f);
8221 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8222
8223 touch();
8224
8225 mTouchWindow->assertNoEvents();
8226}
8227
8228TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) {
8229 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
8230 const sp<FakeWindowHandle>& w =
8231 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f);
8232 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8233
8234 touch();
8235
8236 mTouchWindow->consumeAnyMotionDown();
8237}
8238
8239TEST_F(InputDispatcherUntrustedTouchesTest,
8240 OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) {
8241 mDispatcher->setMaximumObscuringOpacityForTouch(1.0f);
8242 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008243 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8244 OPACITY_ABOVE_THRESHOLD);
8245 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8246
8247 touch();
8248
8249 mTouchWindow->consumeAnyMotionDown();
8250}
8251
8252TEST_F(InputDispatcherUntrustedTouchesTest,
8253 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) {
8254 const sp<FakeWindowHandle>& w1 =
8255 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
8256 OPACITY_BELOW_THRESHOLD);
8257 const sp<FakeWindowHandle>& w2 =
8258 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8259 OPACITY_BELOW_THRESHOLD);
8260 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
8261
8262 touch();
8263
8264 mTouchWindow->assertNoEvents();
8265}
8266
8267/**
8268 * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the
8269 * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold
8270 * (which alone would result in allowing touches) does not affect the blocking behavior.
8271 */
8272TEST_F(InputDispatcherUntrustedTouchesTest,
8273 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) {
8274 const sp<FakeWindowHandle>& wB =
8275 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
8276 OPACITY_BELOW_THRESHOLD);
8277 const sp<FakeWindowHandle>& wC =
8278 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
8279 OPACITY_BELOW_THRESHOLD);
8280 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
8281
8282 touch();
8283
8284 mTouchWindow->assertNoEvents();
8285}
8286
8287/**
8288 * This test is testing that a window from a different UID but with same application token doesn't
8289 * block the touch. Apps can share the application token for close UI collaboration for example.
8290 */
8291TEST_F(InputDispatcherUntrustedTouchesTest,
8292 WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) {
8293 const sp<FakeWindowHandle>& w =
8294 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
8295 w->setApplicationToken(mTouchWindow->getApplicationToken());
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00008296 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8297
8298 touch();
8299
8300 mTouchWindow->consumeAnyMotionDown();
8301}
8302
arthurhungb89ccb02020-12-30 16:19:01 +08008303class InputDispatcherDragTests : public InputDispatcherTest {
8304protected:
8305 std::shared_ptr<FakeApplicationHandle> mApp;
8306 sp<FakeWindowHandle> mWindow;
8307 sp<FakeWindowHandle> mSecondWindow;
8308 sp<FakeWindowHandle> mDragWindow;
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008309 sp<FakeWindowHandle> mSpyWindow;
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008310 // Mouse would force no-split, set the id as non-zero to verify if drag state could track it.
8311 static constexpr int32_t MOUSE_POINTER_ID = 1;
arthurhungb89ccb02020-12-30 16:19:01 +08008312
8313 void SetUp() override {
8314 InputDispatcherTest::SetUp();
8315 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008316 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +08008317 mWindow->setFrame(Rect(0, 0, 100, 100));
arthurhungb89ccb02020-12-30 16:19:01 +08008318
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008319 mSecondWindow =
8320 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +08008321 mSecondWindow->setFrame(Rect(100, 0, 200, 100));
arthurhungb89ccb02020-12-30 16:19:01 +08008322
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008323 mSpyWindow =
8324 sp<FakeWindowHandle>::make(mApp, mDispatcher, "SpyWindow", ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008325 mSpyWindow->setSpy(true);
8326 mSpyWindow->setTrustedOverlay(true);
8327 mSpyWindow->setFrame(Rect(0, 0, 200, 100));
8328
arthurhungb89ccb02020-12-30 16:19:01 +08008329 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008330 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mSpyWindow, mWindow, mSecondWindow}}});
arthurhungb89ccb02020-12-30 16:19:01 +08008331 }
8332
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008333 void injectDown(int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
8334 switch (fromSource) {
8335 case AINPUT_SOURCE_TOUCHSCREEN:
8336 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8337 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
8338 ADISPLAY_ID_DEFAULT, {50, 50}))
8339 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8340 break;
8341 case AINPUT_SOURCE_STYLUS:
8342 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8343 injectMotionEvent(
8344 mDispatcher,
8345 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
8346 AINPUT_SOURCE_STYLUS)
8347 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008348 .pointer(PointerBuilder(0, ToolType::STYLUS)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008349 .x(50)
8350 .y(50))
8351 .build()));
8352 break;
8353 case AINPUT_SOURCE_MOUSE:
8354 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8355 injectMotionEvent(
8356 mDispatcher,
8357 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
8358 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
8359 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008360 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008361 .x(50)
8362 .y(50))
8363 .build()));
8364 break;
8365 default:
8366 FAIL() << "Source " << fromSource << " doesn't support drag and drop";
8367 }
arthurhungb89ccb02020-12-30 16:19:01 +08008368
8369 // Window should receive motion event.
8370 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008371 // Spy window should also receive motion event
8372 mSpyWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung54745652022-04-20 07:17:41 +00008373 }
8374
8375 // Start performing drag, we will create a drag window and transfer touch to it.
8376 // @param sendDown : if true, send a motion down on first window before perform drag and drop.
8377 // Returns true on success.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008378 bool startDrag(bool sendDown = true, int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
Arthur Hung54745652022-04-20 07:17:41 +00008379 if (sendDown) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008380 injectDown(fromSource);
Arthur Hung54745652022-04-20 07:17:41 +00008381 }
arthurhungb89ccb02020-12-30 16:19:01 +08008382
8383 // The drag window covers the entire display
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008384 mDragWindow =
8385 sp<FakeWindowHandle>::make(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008386 mDragWindow->setTouchableRegion(Region{{0, 0, 0, 0}});
arthurhungb89ccb02020-12-30 16:19:01 +08008387 mDispatcher->setInputWindows(
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008388 {{ADISPLAY_ID_DEFAULT, {mDragWindow, mSpyWindow, mWindow, mSecondWindow}}});
arthurhungb89ccb02020-12-30 16:19:01 +08008389
8390 // Transfer touch focus to the drag window
Arthur Hung54745652022-04-20 07:17:41 +00008391 bool transferred =
8392 mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(),
Harry Cutts33476232023-01-30 19:57:29 +00008393 /*isDragDrop=*/true);
Arthur Hung54745652022-04-20 07:17:41 +00008394 if (transferred) {
8395 mWindow->consumeMotionCancel();
8396 mDragWindow->consumeMotionDown();
8397 }
8398 return transferred;
arthurhungb89ccb02020-12-30 16:19:01 +08008399 }
8400};
8401
8402TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008403 startDrag();
arthurhungb89ccb02020-12-30 16:19:01 +08008404
8405 // Move on window.
8406 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8407 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8408 ADISPLAY_ID_DEFAULT, {50, 50}))
8409 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8410 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8411 mWindow->consumeDragEvent(false, 50, 50);
8412 mSecondWindow->assertNoEvents();
8413
8414 // Move to another window.
8415 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8416 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8417 ADISPLAY_ID_DEFAULT, {150, 50}))
8418 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8419 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8420 mWindow->consumeDragEvent(true, 150, 50);
8421 mSecondWindow->consumeDragEvent(false, 50, 50);
8422
8423 // Move back to original window.
8424 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8425 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8426 ADISPLAY_ID_DEFAULT, {50, 50}))
8427 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8428 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8429 mWindow->consumeDragEvent(false, 50, 50);
8430 mSecondWindow->consumeDragEvent(true, -50, 50);
8431
8432 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8433 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {50, 50}))
8434 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8435 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8436 mWindow->assertNoEvents();
8437 mSecondWindow->assertNoEvents();
8438}
8439
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008440TEST_F(InputDispatcherDragTests, DragEnterAndPointerDownPilfersPointers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008441 startDrag();
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008442
8443 // No cancel event after drag start
8444 mSpyWindow->assertNoEvents();
8445
8446 const MotionEvent secondFingerDownEvent =
8447 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8448 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008449 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
8450 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60))
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008451 .build();
8452 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8453 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8454 InputEventInjectionSync::WAIT_FOR_RESULT))
8455 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8456
8457 // Receives cancel for first pointer after next pointer down
8458 mSpyWindow->consumeMotionCancel();
8459 mSpyWindow->consumeMotionDown();
8460
8461 mSpyWindow->assertNoEvents();
8462}
8463
arthurhungf452d0b2021-01-06 00:19:52 +08008464TEST_F(InputDispatcherDragTests, DragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008465 startDrag();
arthurhungf452d0b2021-01-06 00:19:52 +08008466
8467 // Move on window.
8468 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8469 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8470 ADISPLAY_ID_DEFAULT, {50, 50}))
8471 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8472 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8473 mWindow->consumeDragEvent(false, 50, 50);
8474 mSecondWindow->assertNoEvents();
8475
8476 // Move to another window.
8477 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8478 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8479 ADISPLAY_ID_DEFAULT, {150, 50}))
8480 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8481 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8482 mWindow->consumeDragEvent(true, 150, 50);
8483 mSecondWindow->consumeDragEvent(false, 50, 50);
8484
8485 // drop to another window.
8486 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8487 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8488 {150, 50}))
8489 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8490 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8491 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
8492 mWindow->assertNoEvents();
8493 mSecondWindow->assertNoEvents();
8494}
8495
arthurhung6d4bed92021-03-17 11:59:33 +08008496TEST_F(InputDispatcherDragTests, StylusDragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008497 startDrag(true, AINPUT_SOURCE_STYLUS);
arthurhung6d4bed92021-03-17 11:59:33 +08008498
8499 // Move on window and keep button pressed.
8500 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8501 injectMotionEvent(mDispatcher,
8502 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
8503 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008504 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +08008505 .build()))
8506 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8507 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8508 mWindow->consumeDragEvent(false, 50, 50);
8509 mSecondWindow->assertNoEvents();
8510
8511 // Move to another window and release button, expect to drop item.
8512 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8513 injectMotionEvent(mDispatcher,
8514 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
8515 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008516 .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +08008517 .build()))
8518 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8519 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8520 mWindow->assertNoEvents();
8521 mSecondWindow->assertNoEvents();
8522 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
8523
8524 // nothing to the window.
8525 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8526 injectMotionEvent(mDispatcher,
8527 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS)
8528 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008529 .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +08008530 .build()))
8531 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8532 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8533 mWindow->assertNoEvents();
8534 mSecondWindow->assertNoEvents();
8535}
8536
Arthur Hung54745652022-04-20 07:17:41 +00008537TEST_F(InputDispatcherDragTests, DragAndDropOnInvalidWindow) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008538 startDrag();
Arthur Hung6d0571e2021-04-09 20:18:16 +08008539
8540 // Set second window invisible.
8541 mSecondWindow->setVisible(false);
8542 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}});
8543
8544 // Move on window.
8545 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8546 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8547 ADISPLAY_ID_DEFAULT, {50, 50}))
8548 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8549 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8550 mWindow->consumeDragEvent(false, 50, 50);
8551 mSecondWindow->assertNoEvents();
8552
8553 // Move to another window.
8554 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8555 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8556 ADISPLAY_ID_DEFAULT, {150, 50}))
8557 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8558 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8559 mWindow->consumeDragEvent(true, 150, 50);
8560 mSecondWindow->assertNoEvents();
8561
8562 // drop to another window.
8563 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8564 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8565 {150, 50}))
8566 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8567 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8568 mFakePolicy->assertDropTargetEquals(nullptr);
8569 mWindow->assertNoEvents();
8570 mSecondWindow->assertNoEvents();
8571}
8572
Arthur Hung54745652022-04-20 07:17:41 +00008573TEST_F(InputDispatcherDragTests, NoDragAndDropWhenMultiFingers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008574 // Ensure window could track pointerIds if it didn't support split touch.
8575 mWindow->setPreventSplitting(true);
8576
Arthur Hung54745652022-04-20 07:17:41 +00008577 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8578 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8579 {50, 50}))
8580 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8581 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8582
8583 const MotionEvent secondFingerDownEvent =
8584 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8585 .displayId(ADISPLAY_ID_DEFAULT)
8586 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008587 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
8588 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(75).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008589 .build();
8590 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8591 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8592 InputEventInjectionSync::WAIT_FOR_RESULT))
8593 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Harry Cutts33476232023-01-30 19:57:29 +00008594 mWindow->consumeMotionPointerDown(/*pointerIndex=*/1);
Arthur Hung54745652022-04-20 07:17:41 +00008595
8596 // Should not perform drag and drop when window has multi fingers.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008597 ASSERT_FALSE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +00008598}
8599
8600TEST_F(InputDispatcherDragTests, DragAndDropWhenSplitTouch) {
8601 // First down on second window.
8602 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8603 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8604 {150, 50}))
8605 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8606
8607 mSecondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8608
8609 // Second down on first window.
8610 const MotionEvent secondFingerDownEvent =
8611 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8612 .displayId(ADISPLAY_ID_DEFAULT)
8613 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008614 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
8615 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008616 .build();
8617 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8618 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8619 InputEventInjectionSync::WAIT_FOR_RESULT))
8620 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8621 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8622
8623 // Perform drag and drop from first window.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008624 ASSERT_TRUE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +00008625
8626 // Move on window.
8627 const MotionEvent secondFingerMoveEvent =
8628 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
8629 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008630 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
8631 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008632 .build();
8633 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8634 injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT,
8635 InputEventInjectionSync::WAIT_FOR_RESULT));
8636 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8637 mWindow->consumeDragEvent(false, 50, 50);
8638 mSecondWindow->consumeMotionMove();
8639
8640 // Release the drag pointer should perform drop.
8641 const MotionEvent secondFingerUpEvent =
8642 MotionEventBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
8643 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008644 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
8645 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008646 .build();
8647 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8648 injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT,
8649 InputEventInjectionSync::WAIT_FOR_RESULT));
8650 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8651 mFakePolicy->assertDropTargetEquals(mWindow->getToken());
8652 mWindow->assertNoEvents();
8653 mSecondWindow->consumeMotionMove();
8654}
8655
Arthur Hung3915c1f2022-05-31 07:17:17 +00008656TEST_F(InputDispatcherDragTests, DragAndDropWhenMultiDisplays) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008657 startDrag();
Arthur Hung3915c1f2022-05-31 07:17:17 +00008658
8659 // Update window of second display.
8660 sp<FakeWindowHandle> windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008661 sp<FakeWindowHandle>::make(mApp, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung3915c1f2022-05-31 07:17:17 +00008662 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
8663
8664 // Let second display has a touch state.
8665 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8666 injectMotionEvent(mDispatcher,
8667 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
8668 AINPUT_SOURCE_TOUCHSCREEN)
8669 .displayId(SECOND_DISPLAY_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008670 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
Arthur Hung3915c1f2022-05-31 07:17:17 +00008671 .build()));
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07008672 windowInSecondary->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN,
Harry Cutts33476232023-01-30 19:57:29 +00008673 SECOND_DISPLAY_ID, /*expectedFlag=*/0);
Arthur Hung3915c1f2022-05-31 07:17:17 +00008674 // Update window again.
8675 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
8676
8677 // Move on window.
8678 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8679 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8680 ADISPLAY_ID_DEFAULT, {50, 50}))
8681 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8682 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8683 mWindow->consumeDragEvent(false, 50, 50);
8684 mSecondWindow->assertNoEvents();
8685
8686 // Move to another window.
8687 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8688 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8689 ADISPLAY_ID_DEFAULT, {150, 50}))
8690 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8691 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8692 mWindow->consumeDragEvent(true, 150, 50);
8693 mSecondWindow->consumeDragEvent(false, 50, 50);
8694
8695 // drop to another window.
8696 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8697 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8698 {150, 50}))
8699 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8700 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8701 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
8702 mWindow->assertNoEvents();
8703 mSecondWindow->assertNoEvents();
8704}
8705
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008706TEST_F(InputDispatcherDragTests, MouseDragAndDrop) {
8707 startDrag(true, AINPUT_SOURCE_MOUSE);
8708 // Move on window.
8709 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8710 injectMotionEvent(mDispatcher,
8711 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
8712 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
8713 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008714 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008715 .x(50)
8716 .y(50))
8717 .build()))
8718 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8719 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8720 mWindow->consumeDragEvent(false, 50, 50);
8721 mSecondWindow->assertNoEvents();
8722
8723 // Move to another window.
8724 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8725 injectMotionEvent(mDispatcher,
8726 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
8727 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
8728 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008729 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008730 .x(150)
8731 .y(50))
8732 .build()))
8733 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8734 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8735 mWindow->consumeDragEvent(true, 150, 50);
8736 mSecondWindow->consumeDragEvent(false, 50, 50);
8737
8738 // drop to another window.
8739 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8740 injectMotionEvent(mDispatcher,
8741 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
8742 .buttonState(0)
8743 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008744 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008745 .x(150)
8746 .y(50))
8747 .build()))
8748 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8749 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8750 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
8751 mWindow->assertNoEvents();
8752 mSecondWindow->assertNoEvents();
8753}
8754
Vishnu Nair062a8672021-09-03 16:07:44 -07008755class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {};
8756
8757TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) {
8758 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008759 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
8760 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008761 window->setDropInput(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008762 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
8763 window->setFocusable(true);
8764 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
8765 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00008766 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008767
8768 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +00008769 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008770 window->assertNoEvents();
8771
Prabir Pradhan678438e2023-04-13 19:32:51 +00008772 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8773 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008774 window->assertNoEvents();
8775
8776 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008777 window->setDropInput(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008778 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
8779
Prabir Pradhan678438e2023-04-13 19:32:51 +00008780 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008781 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
8782
Prabir Pradhan678438e2023-04-13 19:32:51 +00008783 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8784 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008785 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8786 window->assertNoEvents();
8787}
8788
8789TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) {
8790 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
8791 std::make_shared<FakeApplicationHandle>();
8792 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008793 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
8794 ADISPLAY_ID_DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -07008795 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
8796 obscuringWindow->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008797 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008798 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008799 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
8800 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008801 window->setDropInputIfObscured(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008802 window->setOwnerInfo(222, 222);
8803 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
8804 window->setFocusable(true);
8805 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
8806 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00008807 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008808
8809 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +00008810 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008811 window->assertNoEvents();
8812
Prabir Pradhan678438e2023-04-13 19:32:51 +00008813 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8814 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008815 window->assertNoEvents();
8816
8817 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008818 window->setDropInputIfObscured(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008819 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
8820
Prabir Pradhan678438e2023-04-13 19:32:51 +00008821 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008822 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
8823
Prabir Pradhan678438e2023-04-13 19:32:51 +00008824 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8825 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008826 window->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
8827 window->assertNoEvents();
8828}
8829
8830TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) {
8831 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
8832 std::make_shared<FakeApplicationHandle>();
8833 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008834 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
8835 ADISPLAY_ID_DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -07008836 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
8837 obscuringWindow->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008838 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008839 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008840 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
8841 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008842 window->setDropInputIfObscured(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008843 window->setOwnerInfo(222, 222);
8844 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
8845 window->setFocusable(true);
8846 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
8847 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00008848 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008849
8850 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +00008851 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008852 window->assertNoEvents();
8853
Prabir Pradhan678438e2023-04-13 19:32:51 +00008854 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8855 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008856 window->assertNoEvents();
8857
8858 // When the window is no longer obscured because it went on top, it should get input
8859 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, obscuringWindow}}});
8860
Prabir Pradhan678438e2023-04-13 19:32:51 +00008861 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008862 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
8863
Prabir Pradhan678438e2023-04-13 19:32:51 +00008864 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8865 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008866 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8867 window->assertNoEvents();
8868}
8869
Antonio Kantekf16f2832021-09-28 04:39:20 +00008870class InputDispatcherTouchModeChangedTests : public InputDispatcherTest {
8871protected:
8872 std::shared_ptr<FakeApplicationHandle> mApp;
Antonio Kantek15beb512022-06-13 22:35:41 +00008873 std::shared_ptr<FakeApplicationHandle> mSecondaryApp;
Antonio Kantekf16f2832021-09-28 04:39:20 +00008874 sp<FakeWindowHandle> mWindow;
8875 sp<FakeWindowHandle> mSecondWindow;
Antonio Kantek15beb512022-06-13 22:35:41 +00008876 sp<FakeWindowHandle> mThirdWindow;
Antonio Kantekf16f2832021-09-28 04:39:20 +00008877
8878 void SetUp() override {
8879 InputDispatcherTest::SetUp();
8880
8881 mApp = std::make_shared<FakeApplicationHandle>();
Antonio Kantek15beb512022-06-13 22:35:41 +00008882 mSecondaryApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008883 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008884 mWindow->setFocusable(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00008885 setFocusedWindow(mWindow);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008886 mSecondWindow =
8887 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008888 mSecondWindow->setFocusable(true);
Antonio Kantek15beb512022-06-13 22:35:41 +00008889 mThirdWindow =
8890 sp<FakeWindowHandle>::make(mSecondaryApp, mDispatcher,
8891 "TestWindow3_SecondaryDisplay", SECOND_DISPLAY_ID);
8892 mThirdWindow->setFocusable(true);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008893
8894 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Antonio Kantek15beb512022-06-13 22:35:41 +00008895 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}},
8896 {SECOND_DISPLAY_ID, {mThirdWindow}}});
8897 mThirdWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008898 mWindow->consumeFocusEvent(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00008899
Antonio Kantek15beb512022-06-13 22:35:41 +00008900 // Set main display initial touch mode to InputDispatcher::kDefaultInTouchMode.
Prabir Pradhan5735a322022-04-11 17:23:34 +00008901 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, WINDOW_PID,
Harry Cutts33476232023-01-30 19:57:29 +00008902 WINDOW_UID, /*hasPermission=*/true, ADISPLAY_ID_DEFAULT)) {
Antonio Kantek48710e42022-03-24 14:19:30 -07008903 mWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
8904 mSecondWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +00008905 mThirdWindow->assertNoEvents();
8906 }
8907
8908 // Set secondary display initial touch mode to InputDispatcher::kDefaultInTouchMode.
8909 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, SECONDARY_WINDOW_PID,
Harry Cutts33476232023-01-30 19:57:29 +00008910 SECONDARY_WINDOW_UID, /*hasPermission=*/true,
Antonio Kantek15beb512022-06-13 22:35:41 +00008911 SECOND_DISPLAY_ID)) {
8912 mWindow->assertNoEvents();
8913 mSecondWindow->assertNoEvents();
8914 mThirdWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek48710e42022-03-24 14:19:30 -07008915 }
Antonio Kantekf16f2832021-09-28 04:39:20 +00008916 }
8917
Antonio Kantek15beb512022-06-13 22:35:41 +00008918 void changeAndVerifyTouchModeInMainDisplayOnly(bool inTouchMode, int32_t pid, int32_t uid,
8919 bool hasPermission) {
Antonio Kanteka042c022022-07-06 16:51:07 -07008920 ASSERT_TRUE(mDispatcher->setInTouchMode(inTouchMode, pid, uid, hasPermission,
8921 ADISPLAY_ID_DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +00008922 mWindow->consumeTouchModeEvent(inTouchMode);
8923 mSecondWindow->consumeTouchModeEvent(inTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +00008924 mThirdWindow->assertNoEvents();
Antonio Kantekf16f2832021-09-28 04:39:20 +00008925 }
8926};
8927
Antonio Kantek26defcf2022-02-08 01:12:27 +00008928TEST_F(InputDispatcherTouchModeChangedTests, FocusedWindowCanChangeTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -08008929 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek15beb512022-06-13 22:35:41 +00008930 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode,
8931 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008932 /* hasPermission=*/false);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008933}
8934
Antonio Kantek26defcf2022-02-08 01:12:27 +00008935TEST_F(InputDispatcherTouchModeChangedTests, NonFocusedWindowOwnerCannotChangeTouchMode) {
8936 const WindowInfo& windowInfo = *mWindow->getInfo();
8937 int32_t ownerPid = windowInfo.ownerPid;
8938 int32_t ownerUid = windowInfo.ownerUid;
8939 mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1);
8940 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, ownerPid,
Harry Cutts33476232023-01-30 19:57:29 +00008941 ownerUid, /*hasPermission=*/false,
Antonio Kanteka042c022022-07-06 16:51:07 -07008942 ADISPLAY_ID_DEFAULT));
Antonio Kantek26defcf2022-02-08 01:12:27 +00008943 mWindow->assertNoEvents();
8944 mSecondWindow->assertNoEvents();
8945}
8946
8947TEST_F(InputDispatcherTouchModeChangedTests, NonWindowOwnerMayChangeTouchModeOnPermissionGranted) {
8948 const WindowInfo& windowInfo = *mWindow->getInfo();
8949 int32_t ownerPid = windowInfo.ownerPid;
8950 int32_t ownerUid = windowInfo.ownerUid;
8951 mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1);
Antonio Kantek15beb512022-06-13 22:35:41 +00008952 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, ownerPid,
Harry Cutts33476232023-01-30 19:57:29 +00008953 ownerUid, /*hasPermission=*/true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00008954}
8955
Antonio Kantekf16f2832021-09-28 04:39:20 +00008956TEST_F(InputDispatcherTouchModeChangedTests, EventIsNotGeneratedIfNotChangingTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -08008957 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek26defcf2022-02-08 01:12:27 +00008958 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode,
8959 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008960 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +00008961 mWindow->assertNoEvents();
8962 mSecondWindow->assertNoEvents();
8963}
8964
Antonio Kantek15beb512022-06-13 22:35:41 +00008965TEST_F(InputDispatcherTouchModeChangedTests, ChangeTouchOnSecondaryDisplayOnly) {
8966 const WindowInfo& windowInfo = *mThirdWindow->getInfo();
8967 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
8968 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008969 /*hasPermission=*/true, SECOND_DISPLAY_ID));
Antonio Kantek15beb512022-06-13 22:35:41 +00008970 mWindow->assertNoEvents();
8971 mSecondWindow->assertNoEvents();
8972 mThirdWindow->consumeTouchModeEvent(!InputDispatcher::kDefaultInTouchMode);
8973}
8974
Antonio Kantek48710e42022-03-24 14:19:30 -07008975TEST_F(InputDispatcherTouchModeChangedTests, CanChangeTouchModeWhenOwningLastInteractedWindow) {
8976 // Interact with the window first.
8977 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
8978 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
8979 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
8980
8981 // Then remove focus.
8982 mWindow->setFocusable(false);
8983 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
8984
8985 // Assert that caller can switch touch mode by owning one of the last interacted window.
8986 const WindowInfo& windowInfo = *mWindow->getInfo();
8987 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
8988 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008989 /*hasPermission=*/false, ADISPLAY_ID_DEFAULT));
Antonio Kantek48710e42022-03-24 14:19:30 -07008990}
8991
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008992class InputDispatcherSpyWindowTest : public InputDispatcherTest {
8993public:
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008994 sp<FakeWindowHandle> createSpy() {
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008995 std::shared_ptr<FakeApplicationHandle> application =
8996 std::make_shared<FakeApplicationHandle>();
8997 std::string name = "Fake Spy ";
8998 name += std::to_string(mSpyCount++);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008999 sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher,
9000 name.c_str(), ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08009001 spy->setSpy(true);
Prabir Pradhan5c85e052021-12-22 02:27:12 -08009002 spy->setTrustedOverlay(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009003 return spy;
9004 }
9005
9006 sp<FakeWindowHandle> createForeground() {
9007 std::shared_ptr<FakeApplicationHandle> application =
9008 std::make_shared<FakeApplicationHandle>();
9009 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009010 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
9011 ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009012 window->setFocusable(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009013 return window;
9014 }
9015
9016private:
9017 int mSpyCount{0};
9018};
9019
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08009020using InputDispatcherSpyWindowDeathTest = InputDispatcherSpyWindowTest;
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009021/**
Prabir Pradhan5c85e052021-12-22 02:27:12 -08009022 * Adding a spy window that is not a trusted overlay causes Dispatcher to abort.
9023 */
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08009024TEST_F(InputDispatcherSpyWindowDeathTest, UntrustedSpy_AbortsDispatcher) {
9025 ScopedSilentDeath _silentDeath;
9026
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009027 auto spy = createSpy();
Prabir Pradhan5c85e052021-12-22 02:27:12 -08009028 spy->setTrustedOverlay(false);
9029 ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}}),
9030 ".* not a trusted overlay");
9031}
9032
9033/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009034 * Input injection into a display with a spy window but no foreground windows should succeed.
9035 */
9036TEST_F(InputDispatcherSpyWindowTest, NoForegroundWindow) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009037 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009038 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}});
9039
9040 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9041 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9042 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9043 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
9044}
9045
9046/**
9047 * Verify the order in which different input windows receive events. The touched foreground window
9048 * (if there is one) should always receive the event first. When there are multiple spy windows, the
9049 * spy windows will receive the event according to their Z-order, where the top-most spy window will
9050 * receive events before ones belows it.
9051 *
9052 * Here, we set up a scenario with four windows in the following Z order from the top:
9053 * spy1, spy2, window, spy3.
9054 * We then inject an event and verify that the foreground "window" receives it first, followed by
9055 * "spy1" and "spy2". The "spy3" does not receive the event because it is underneath the foreground
9056 * window.
9057 */
9058TEST_F(InputDispatcherSpyWindowTest, ReceivesInputInOrder) {
9059 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009060 auto spy1 = createSpy();
9061 auto spy2 = createSpy();
9062 auto spy3 = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009063 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window, spy3}}});
9064 const std::vector<sp<FakeWindowHandle>> channels{spy1, spy2, window, spy3};
9065 const size_t numChannels = channels.size();
9066
Michael Wright8e9a8562022-02-09 13:44:29 +00009067 base::unique_fd epollFd(epoll_create1(EPOLL_CLOEXEC));
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009068 if (!epollFd.ok()) {
9069 FAIL() << "Failed to create epoll fd";
9070 }
9071
9072 for (size_t i = 0; i < numChannels; i++) {
9073 struct epoll_event event = {.events = EPOLLIN, .data.u64 = i};
9074 if (epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, channels[i]->getChannelFd(), &event) < 0) {
9075 FAIL() << "Failed to add fd to epoll";
9076 }
9077 }
9078
9079 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9080 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9081 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9082
9083 std::vector<size_t> eventOrder;
9084 std::vector<struct epoll_event> events(numChannels);
9085 for (;;) {
9086 const int nFds = epoll_wait(epollFd.get(), events.data(), static_cast<int>(numChannels),
9087 (100ms).count());
9088 if (nFds < 0) {
9089 FAIL() << "Failed to call epoll_wait";
9090 }
9091 if (nFds == 0) {
9092 break; // epoll_wait timed out
9093 }
9094 for (int i = 0; i < nFds; i++) {
Colin Cross5b799302022-10-18 21:52:41 -07009095 ASSERT_EQ(static_cast<uint32_t>(EPOLLIN), events[i].events);
Siarhei Vishniakou31977182022-09-30 08:51:23 -07009096 eventOrder.push_back(static_cast<size_t>(events[i].data.u64));
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009097 channels[i]->consumeMotionDown();
9098 }
9099 }
9100
9101 // Verify the order in which the events were received.
9102 EXPECT_EQ(3u, eventOrder.size());
9103 EXPECT_EQ(2u, eventOrder[0]); // index 2: window
9104 EXPECT_EQ(0u, eventOrder[1]); // index 0: spy1
9105 EXPECT_EQ(1u, eventOrder[2]); // index 1: spy2
9106}
9107
9108/**
9109 * A spy window using the NOT_TOUCHABLE flag does not receive events.
9110 */
9111TEST_F(InputDispatcherSpyWindowTest, NotTouchable) {
9112 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009113 auto spy = createSpy();
9114 spy->setTouchable(false);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009115 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9116
9117 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9118 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9119 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9120 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
9121 spy->assertNoEvents();
9122}
9123
9124/**
9125 * A spy window will only receive gestures that originate within its touchable region. Gestures that
9126 * have their ACTION_DOWN outside of the touchable region of the spy window will not be dispatched
9127 * to the window.
9128 */
9129TEST_F(InputDispatcherSpyWindowTest, TouchableRegion) {
9130 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009131 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009132 spy->setTouchableRegion(Region{{0, 0, 20, 20}});
9133 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9134
9135 // Inject an event outside the spy window's touchable region.
9136 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9137 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9138 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9139 window->consumeMotionDown();
9140 spy->assertNoEvents();
9141 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9142 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9143 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9144 window->consumeMotionUp();
9145 spy->assertNoEvents();
9146
9147 // Inject an event inside the spy window's touchable region.
9148 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9149 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9150 {5, 10}))
9151 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9152 window->consumeMotionDown();
9153 spy->consumeMotionDown();
9154}
9155
9156/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009157 * A spy window can listen for touches outside its touchable region using the WATCH_OUTSIDE_TOUCHES
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08009158 * flag, but it will get zero-ed out coordinates if the foreground has a different owner.
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009159 */
9160TEST_F(InputDispatcherSpyWindowTest, WatchOutsideTouches) {
9161 auto window = createForeground();
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08009162 window->setOwnerInfo(12, 34);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009163 auto spy = createSpy();
9164 spy->setWatchOutsideTouch(true);
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08009165 spy->setOwnerInfo(56, 78);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009166 spy->setFrame(Rect{0, 0, 20, 20});
9167 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9168
9169 // Inject an event outside the spy window's frame and touchable region.
9170 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08009171 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9172 {100, 200}))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009173 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9174 window->consumeMotionDown();
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08009175 spy->consumeMotionOutsideWithZeroedCoords();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009176}
9177
9178/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009179 * Even when a spy window spans over multiple foreground windows, the spy should receive all
9180 * pointers that are down within its bounds.
9181 */
9182TEST_F(InputDispatcherSpyWindowTest, ReceivesMultiplePointers) {
9183 auto windowLeft = createForeground();
9184 windowLeft->setFrame({0, 0, 100, 200});
9185 auto windowRight = createForeground();
9186 windowRight->setFrame({100, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009187 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009188 spy->setFrame({0, 0, 200, 200});
9189 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, windowLeft, windowRight}}});
9190
9191 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9192 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9193 {50, 50}))
9194 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9195 windowLeft->consumeMotionDown();
9196 spy->consumeMotionDown();
9197
9198 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08009199 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009200 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009201 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
9202 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009203 .build();
9204 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9205 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9206 InputEventInjectionSync::WAIT_FOR_RESULT))
9207 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9208 windowRight->consumeMotionDown();
Harry Cutts33476232023-01-30 19:57:29 +00009209 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009210}
9211
9212/**
9213 * When the first pointer lands outside the spy window and the second pointer lands inside it, the
9214 * the spy should receive the second pointer with ACTION_DOWN.
9215 */
9216TEST_F(InputDispatcherSpyWindowTest, ReceivesSecondPointerAsDown) {
9217 auto window = createForeground();
9218 window->setFrame({0, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009219 auto spyRight = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009220 spyRight->setFrame({100, 0, 200, 200});
9221 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyRight, window}}});
9222
9223 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9224 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9225 {50, 50}))
9226 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9227 window->consumeMotionDown();
9228 spyRight->assertNoEvents();
9229
9230 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08009231 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009232 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009233 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
9234 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009235 .build();
9236 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9237 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9238 InputEventInjectionSync::WAIT_FOR_RESULT))
9239 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Harry Cutts33476232023-01-30 19:57:29 +00009240 window->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009241 spyRight->consumeMotionDown();
9242}
9243
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009244/**
9245 * The spy window should not be able to affect whether or not touches are split. Only the foreground
9246 * windows should be allowed to control split touch.
9247 */
9248TEST_F(InputDispatcherSpyWindowTest, SplitIfNoForegroundWindowTouched) {
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08009249 // This spy window prevents touch splitting. However, we still expect to split touches
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009250 // because a foreground window has not disabled splitting.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009251 auto spy = createSpy();
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08009252 spy->setPreventSplitting(true);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009253
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009254 auto window = createForeground();
9255 window->setFrame(Rect(0, 0, 100, 100));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009256
9257 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9258
9259 // First finger down, no window touched.
9260 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9261 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9262 {100, 200}))
9263 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9264 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
9265 window->assertNoEvents();
9266
9267 // Second finger down on window, the window should receive touch down.
9268 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08009269 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009270 .displayId(ADISPLAY_ID_DEFAULT)
9271 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009272 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
9273 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009274 .build();
9275 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9276 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9277 InputEventInjectionSync::WAIT_FOR_RESULT))
9278 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9279
9280 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Harry Cutts33476232023-01-30 19:57:29 +00009281 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009282}
9283
9284/**
9285 * A spy window will usually be implemented as an un-focusable window. Verify that these windows
9286 * do not receive key events.
9287 */
9288TEST_F(InputDispatcherSpyWindowTest, UnfocusableSpyDoesNotReceiveKeyEvents) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009289 auto spy = createSpy();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009290 spy->setFocusable(false);
9291
9292 auto window = createForeground();
9293 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9294 setFocusedWindow(window);
9295 window->consumeFocusEvent(true);
9296
9297 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
9298 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
9299 window->consumeKeyDown(ADISPLAY_ID_NONE);
9300
9301 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
9302 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
9303 window->consumeKeyUp(ADISPLAY_ID_NONE);
9304
9305 spy->assertNoEvents();
9306}
9307
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009308using InputDispatcherPilferPointersTest = InputDispatcherSpyWindowTest;
9309
9310/**
9311 * A spy window can pilfer pointers. When this happens, touch gestures used by the spy window that
9312 * are currently sent to any other windows - including other spy windows - will also be cancelled.
9313 */
9314TEST_F(InputDispatcherPilferPointersTest, PilferPointers) {
9315 auto window = createForeground();
9316 auto spy1 = createSpy();
9317 auto spy2 = createSpy();
9318 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window}}});
9319
9320 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9321 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9322 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9323 window->consumeMotionDown();
9324 spy1->consumeMotionDown();
9325 spy2->consumeMotionDown();
9326
9327 // Pilfer pointers from the second spy window.
9328 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy2->getToken()));
9329 spy2->assertNoEvents();
9330 spy1->consumeMotionCancel();
9331 window->consumeMotionCancel();
9332
9333 // The rest of the gesture should only be sent to the second spy window.
9334 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9335 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
9336 ADISPLAY_ID_DEFAULT))
9337 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9338 spy2->consumeMotionMove();
9339 spy1->assertNoEvents();
9340 window->assertNoEvents();
9341}
9342
9343/**
9344 * A spy window can pilfer pointers for a gesture even after the foreground window has been removed
9345 * in the middle of the gesture.
9346 */
9347TEST_F(InputDispatcherPilferPointersTest, CanPilferAfterWindowIsRemovedMidStream) {
9348 auto window = createForeground();
9349 auto spy = createSpy();
9350 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9351
9352 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9353 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9354 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9355 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
9356 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
9357
9358 window->releaseChannel();
9359
9360 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9361
9362 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9363 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9364 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9365 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
9366}
9367
9368/**
9369 * After a spy window pilfers pointers, new pointers that go down in its bounds should be sent to
9370 * the spy, but not to any other windows.
9371 */
9372TEST_F(InputDispatcherPilferPointersTest, ContinuesToReceiveGestureAfterPilfer) {
9373 auto spy = createSpy();
9374 auto window = createForeground();
9375
9376 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9377
9378 // First finger down on the window and the spy.
9379 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9380 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9381 {100, 200}))
9382 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9383 spy->consumeMotionDown();
9384 window->consumeMotionDown();
9385
9386 // Spy window pilfers the pointers.
9387 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9388 window->consumeMotionCancel();
9389
9390 // Second finger down on the window and spy, but the window should not receive the pointer down.
9391 const MotionEvent secondFingerDownEvent =
9392 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9393 .displayId(ADISPLAY_ID_DEFAULT)
9394 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009395 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
9396 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009397 .build();
9398 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9399 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9400 InputEventInjectionSync::WAIT_FOR_RESULT))
9401 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9402
Harry Cutts33476232023-01-30 19:57:29 +00009403 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009404
9405 // Third finger goes down outside all windows, so injection should fail.
9406 const MotionEvent thirdFingerDownEvent =
9407 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9408 .displayId(ADISPLAY_ID_DEFAULT)
9409 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009410 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
9411 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
9412 .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(-5).y(-5))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009413 .build();
9414 ASSERT_EQ(InputEventInjectionResult::FAILED,
9415 injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
9416 InputEventInjectionSync::WAIT_FOR_RESULT))
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08009417 << "Inject motion event should return InputEventInjectionResult::FAILED";
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009418
9419 spy->assertNoEvents();
9420 window->assertNoEvents();
9421}
9422
9423/**
9424 * After a spy window pilfers pointers, only the pointers used by the spy should be canceled
9425 */
9426TEST_F(InputDispatcherPilferPointersTest, PartiallyPilferRequiredPointers) {
9427 auto spy = createSpy();
9428 spy->setFrame(Rect(0, 0, 100, 100));
9429 auto window = createForeground();
9430 window->setFrame(Rect(0, 0, 200, 200));
9431
9432 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9433
9434 // First finger down on the window only
9435 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9436 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9437 {150, 150}))
9438 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9439 window->consumeMotionDown();
9440
9441 // Second finger down on the spy and window
9442 const MotionEvent secondFingerDownEvent =
9443 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9444 .displayId(ADISPLAY_ID_DEFAULT)
9445 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009446 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150))
9447 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009448 .build();
9449 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9450 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9451 InputEventInjectionSync::WAIT_FOR_RESULT))
9452 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9453 spy->consumeMotionDown();
9454 window->consumeMotionPointerDown(1);
9455
9456 // Third finger down on the spy and window
9457 const MotionEvent thirdFingerDownEvent =
9458 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9459 .displayId(ADISPLAY_ID_DEFAULT)
9460 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009461 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150))
9462 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10))
9463 .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009464 .build();
9465 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9466 injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
9467 InputEventInjectionSync::WAIT_FOR_RESULT))
9468 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9469 spy->consumeMotionPointerDown(1);
9470 window->consumeMotionPointerDown(2);
9471
9472 // Spy window pilfers the pointers.
9473 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9474 window->consumeMotionPointerUp(/* idx */ 2, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
9475 window->consumeMotionPointerUp(/* idx */ 1, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
9476
9477 spy->assertNoEvents();
9478 window->assertNoEvents();
9479}
9480
9481/**
9482 * After a spy window pilfers pointers, all pilfered pointers that have already been dispatched to
9483 * other windows should be canceled. If this results in the cancellation of all pointers for some
9484 * window, then that window should receive ACTION_CANCEL.
9485 */
9486TEST_F(InputDispatcherPilferPointersTest, PilferAllRequiredPointers) {
9487 auto spy = createSpy();
9488 spy->setFrame(Rect(0, 0, 100, 100));
9489 auto window = createForeground();
9490 window->setFrame(Rect(0, 0, 200, 200));
9491
9492 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9493
9494 // First finger down on both spy and window
9495 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9496 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9497 {10, 10}))
9498 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9499 window->consumeMotionDown();
9500 spy->consumeMotionDown();
9501
9502 // Second finger down on the spy and window
9503 const MotionEvent secondFingerDownEvent =
9504 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9505 .displayId(ADISPLAY_ID_DEFAULT)
9506 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009507 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10))
9508 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009509 .build();
9510 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9511 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9512 InputEventInjectionSync::WAIT_FOR_RESULT))
9513 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9514 spy->consumeMotionPointerDown(1);
9515 window->consumeMotionPointerDown(1);
9516
9517 // Spy window pilfers the pointers.
9518 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9519 window->consumeMotionCancel();
9520
9521 spy->assertNoEvents();
9522 window->assertNoEvents();
9523}
9524
9525/**
9526 * After a spy window pilfers pointers, new pointers that are not touching the spy window can still
9527 * be sent to other windows
9528 */
9529TEST_F(InputDispatcherPilferPointersTest, CanReceivePointersAfterPilfer) {
9530 auto spy = createSpy();
9531 spy->setFrame(Rect(0, 0, 100, 100));
9532 auto window = createForeground();
9533 window->setFrame(Rect(0, 0, 200, 200));
9534
9535 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9536
9537 // First finger down on both window and spy
9538 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9539 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9540 {10, 10}))
9541 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9542 window->consumeMotionDown();
9543 spy->consumeMotionDown();
9544
9545 // Spy window pilfers the pointers.
9546 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9547 window->consumeMotionCancel();
9548
9549 // Second finger down on the window only
9550 const MotionEvent secondFingerDownEvent =
9551 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9552 .displayId(ADISPLAY_ID_DEFAULT)
9553 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009554 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10))
9555 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009556 .build();
9557 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9558 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9559 InputEventInjectionSync::WAIT_FOR_RESULT))
9560 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9561 window->consumeMotionDown();
9562 window->assertNoEvents();
9563
9564 // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line
9565 spy->consumeMotionMove();
9566 spy->assertNoEvents();
9567}
9568
Prabir Pradhand65552b2021-10-07 11:23:50 -07009569class InputDispatcherStylusInterceptorTest : public InputDispatcherTest {
9570public:
9571 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupStylusOverlayScenario() {
9572 std::shared_ptr<FakeApplicationHandle> overlayApplication =
9573 std::make_shared<FakeApplicationHandle>();
9574 sp<FakeWindowHandle> overlay =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009575 sp<FakeWindowHandle>::make(overlayApplication, mDispatcher,
9576 "Stylus interceptor window", ADISPLAY_ID_DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009577 overlay->setFocusable(false);
9578 overlay->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009579 overlay->setTouchable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08009580 overlay->setInterceptsStylus(true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009581 overlay->setTrustedOverlay(true);
9582
9583 std::shared_ptr<FakeApplicationHandle> application =
9584 std::make_shared<FakeApplicationHandle>();
9585 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009586 sp<FakeWindowHandle>::make(application, mDispatcher, "Application window",
9587 ADISPLAY_ID_DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009588 window->setFocusable(true);
9589 window->setOwnerInfo(222, 222);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009590
9591 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
9592 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9593 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00009594 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009595 return {std::move(overlay), std::move(window)};
9596 }
9597
9598 void sendFingerEvent(int32_t action) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00009599 mDispatcher->notifyMotion(
Prabir Pradhand65552b2021-10-07 11:23:50 -07009600 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
Prabir Pradhan678438e2023-04-13 19:32:51 +00009601 ADISPLAY_ID_DEFAULT, {PointF{20, 20}}));
Prabir Pradhand65552b2021-10-07 11:23:50 -07009602 }
9603
9604 void sendStylusEvent(int32_t action) {
9605 NotifyMotionArgs motionArgs =
9606 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
9607 ADISPLAY_ID_DEFAULT, {PointF{30, 40}});
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009608 motionArgs.pointerProperties[0].toolType = ToolType::STYLUS;
Prabir Pradhan678438e2023-04-13 19:32:51 +00009609 mDispatcher->notifyMotion(motionArgs);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009610 }
9611};
9612
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08009613using InputDispatcherStylusInterceptorDeathTest = InputDispatcherStylusInterceptorTest;
9614
9615TEST_F(InputDispatcherStylusInterceptorDeathTest, UntrustedOverlay_AbortsDispatcher) {
9616 ScopedSilentDeath _silentDeath;
9617
Prabir Pradhand65552b2021-10-07 11:23:50 -07009618 auto [overlay, window] = setupStylusOverlayScenario();
9619 overlay->setTrustedOverlay(false);
9620 // Configuring an untrusted overlay as a stylus interceptor should cause Dispatcher to abort.
9621 ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}),
9622 ".* not a trusted overlay");
9623}
9624
9625TEST_F(InputDispatcherStylusInterceptorTest, ConsmesOnlyStylusEvents) {
9626 auto [overlay, window] = setupStylusOverlayScenario();
9627 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9628
9629 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
9630 overlay->consumeMotionDown();
9631 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
9632 overlay->consumeMotionUp();
9633
9634 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
9635 window->consumeMotionDown();
9636 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
9637 window->consumeMotionUp();
9638
9639 overlay->assertNoEvents();
9640 window->assertNoEvents();
9641}
9642
9643TEST_F(InputDispatcherStylusInterceptorTest, SpyWindowStylusInterceptor) {
9644 auto [overlay, window] = setupStylusOverlayScenario();
Prabir Pradhan51e7db02022-02-07 06:02:57 -08009645 overlay->setSpy(true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009646 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9647
9648 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
9649 overlay->consumeMotionDown();
9650 window->consumeMotionDown();
9651 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
9652 overlay->consumeMotionUp();
9653 window->consumeMotionUp();
9654
9655 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
9656 window->consumeMotionDown();
9657 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
9658 window->consumeMotionUp();
9659
9660 overlay->assertNoEvents();
9661 window->assertNoEvents();
9662}
9663
Prabir Pradhan6dfbf262022-03-14 15:24:30 +00009664/**
9665 * Set up a scenario to test the behavior used by the stylus handwriting detection feature.
9666 * The scenario is as follows:
9667 * - The stylus interceptor overlay is configured as a spy window.
9668 * - The stylus interceptor spy receives the start of a new stylus gesture.
9669 * - It pilfers pointers and then configures itself to no longer be a spy.
9670 * - The stylus interceptor continues to receive the rest of the gesture.
9671 */
9672TEST_F(InputDispatcherStylusInterceptorTest, StylusHandwritingScenario) {
9673 auto [overlay, window] = setupStylusOverlayScenario();
9674 overlay->setSpy(true);
9675 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9676
9677 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
9678 overlay->consumeMotionDown();
9679 window->consumeMotionDown();
9680
9681 // The interceptor pilfers the pointers.
9682 EXPECT_EQ(OK, mDispatcher->pilferPointers(overlay->getToken()));
9683 window->consumeMotionCancel();
9684
9685 // The interceptor configures itself so that it is no longer a spy.
9686 overlay->setSpy(false);
9687 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9688
9689 // It continues to receive the rest of the stylus gesture.
9690 sendStylusEvent(AMOTION_EVENT_ACTION_MOVE);
9691 overlay->consumeMotionMove();
9692 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
9693 overlay->consumeMotionUp();
9694
9695 window->assertNoEvents();
9696}
9697
Prabir Pradhan5735a322022-04-11 17:23:34 +00009698struct User {
9699 int32_t mPid;
9700 int32_t mUid;
9701 uint32_t mPolicyFlags{DEFAULT_POLICY_FLAGS};
9702 std::unique_ptr<InputDispatcher>& mDispatcher;
9703
9704 User(std::unique_ptr<InputDispatcher>& dispatcher, int32_t pid, int32_t uid)
9705 : mPid(pid), mUid(uid), mDispatcher(dispatcher) {}
9706
9707 InputEventInjectionResult injectTargetedMotion(int32_t action) const {
9708 return injectMotionEvent(mDispatcher, action, AINPUT_SOURCE_TOUCHSCREEN,
9709 ADISPLAY_ID_DEFAULT, {100, 200},
9710 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
9711 AMOTION_EVENT_INVALID_CURSOR_POSITION},
9712 INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT,
9713 systemTime(SYSTEM_TIME_MONOTONIC), {mUid}, mPolicyFlags);
9714 }
9715
9716 InputEventInjectionResult injectTargetedKey(int32_t action) const {
Harry Cutts33476232023-01-30 19:57:29 +00009717 return inputdispatcher::injectKey(mDispatcher, action, /*repeatCount=*/0, ADISPLAY_ID_NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +00009718 InputEventInjectionSync::WAIT_FOR_RESULT,
Harry Cutts33476232023-01-30 19:57:29 +00009719 INJECT_EVENT_TIMEOUT, /*allowKeyRepeat=*/false, {mUid},
Prabir Pradhan5735a322022-04-11 17:23:34 +00009720 mPolicyFlags);
9721 }
9722
9723 sp<FakeWindowHandle> createWindow() const {
9724 std::shared_ptr<FakeApplicationHandle> overlayApplication =
9725 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009726 sp<FakeWindowHandle> window =
9727 sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, "Owned Window",
9728 ADISPLAY_ID_DEFAULT);
Prabir Pradhan5735a322022-04-11 17:23:34 +00009729 window->setOwnerInfo(mPid, mUid);
9730 return window;
9731 }
9732};
9733
9734using InputDispatcherTargetedInjectionTest = InputDispatcherTest;
9735
9736TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedWindow) {
9737 auto owner = User(mDispatcher, 10, 11);
9738 auto window = owner.createWindow();
9739 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
9740
9741 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9742 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9743 window->consumeMotionDown();
9744
9745 setFocusedWindow(window);
9746 window->consumeFocusEvent(true);
9747
9748 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9749 owner.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
9750 window->consumeKeyDown(ADISPLAY_ID_NONE);
9751}
9752
9753TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedWindow) {
9754 auto owner = User(mDispatcher, 10, 11);
9755 auto window = owner.createWindow();
9756 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
9757
9758 auto rando = User(mDispatcher, 20, 21);
9759 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
9760 rando.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9761
9762 setFocusedWindow(window);
9763 window->consumeFocusEvent(true);
9764
9765 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
9766 rando.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
9767 window->assertNoEvents();
9768}
9769
9770TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedSpyWindow) {
9771 auto owner = User(mDispatcher, 10, 11);
9772 auto window = owner.createWindow();
9773 auto spy = owner.createWindow();
9774 spy->setSpy(true);
9775 spy->setTrustedOverlay(true);
9776 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9777
9778 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9779 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9780 spy->consumeMotionDown();
9781 window->consumeMotionDown();
9782}
9783
9784TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedSpyWindow) {
9785 auto owner = User(mDispatcher, 10, 11);
9786 auto window = owner.createWindow();
9787
9788 auto rando = User(mDispatcher, 20, 21);
9789 auto randosSpy = rando.createWindow();
9790 randosSpy->setSpy(true);
9791 randosSpy->setTrustedOverlay(true);
9792 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}});
9793
9794 // The event is targeted at owner's window, so injection should succeed, but the spy should
9795 // not receive the event.
9796 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9797 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9798 randosSpy->assertNoEvents();
9799 window->consumeMotionDown();
9800}
9801
9802TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoAnyWindowWhenNotTargeting) {
9803 auto owner = User(mDispatcher, 10, 11);
9804 auto window = owner.createWindow();
9805
9806 auto rando = User(mDispatcher, 20, 21);
9807 auto randosSpy = rando.createWindow();
9808 randosSpy->setSpy(true);
9809 randosSpy->setTrustedOverlay(true);
9810 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}});
9811
9812 // A user that has injection permission can inject into any window.
9813 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9814 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
9815 ADISPLAY_ID_DEFAULT));
9816 randosSpy->consumeMotionDown();
9817 window->consumeMotionDown();
9818
9819 setFocusedWindow(randosSpy);
9820 randosSpy->consumeFocusEvent(true);
9821
9822 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher));
9823 randosSpy->consumeKeyDown(ADISPLAY_ID_NONE);
9824 window->assertNoEvents();
9825}
9826
9827TEST_F(InputDispatcherTargetedInjectionTest, CanGenerateActionOutsideToOtherUids) {
9828 auto owner = User(mDispatcher, 10, 11);
9829 auto window = owner.createWindow();
9830
9831 auto rando = User(mDispatcher, 20, 21);
9832 auto randosWindow = rando.createWindow();
9833 randosWindow->setFrame(Rect{-10, -10, -5, -5});
9834 randosWindow->setWatchOutsideTouch(true);
9835 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosWindow, window}}});
9836
9837 // We allow generation of ACTION_OUTSIDE events into windows owned by different uids.
9838 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9839 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9840 window->consumeMotionDown();
9841 randosWindow->consumeMotionOutside();
9842}
9843
Garfield Tane84e6f92019-08-29 17:28:41 -07009844} // namespace android::inputdispatcher