blob: 6ff420d95104589407a2afc31030dee740374ef8 [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/**
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004156 * Ensure the correct coordinate spaces are used by InputDispatcher.
4157 *
4158 * InputDispatcher works in the display space, so its coordinate system is relative to the display
4159 * panel. Windows get events in the window space, and get raw coordinates in the logical display
4160 * space.
4161 */
4162class InputDispatcherDisplayProjectionTest : public InputDispatcherTest {
4163public:
4164 void SetUp() override {
4165 InputDispatcherTest::SetUp();
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004166 removeAllWindowsAndDisplays();
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004167 }
4168
4169 void addDisplayInfo(int displayId, const ui::Transform& transform) {
4170 gui::DisplayInfo info;
4171 info.displayId = displayId;
4172 info.transform = transform;
4173 mDisplayInfos.push_back(std::move(info));
Patrick Williamsd828f302023-04-28 17:52:08 -05004174 mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0});
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004175 }
4176
4177 void addWindow(const sp<WindowInfoHandle>& windowHandle) {
4178 mWindowInfos.push_back(*windowHandle->getInfo());
Patrick Williamsd828f302023-04-28 17:52:08 -05004179 mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0});
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004180 }
4181
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004182 void removeAllWindowsAndDisplays() {
4183 mDisplayInfos.clear();
4184 mWindowInfos.clear();
4185 }
4186
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004187 // Set up a test scenario where the display has a scaled projection and there are two windows
4188 // on the display.
4189 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupScaledDisplayScenario() {
4190 // The display has a projection that has a scale factor of 2 and 4 in the x and y directions
4191 // respectively.
4192 ui::Transform displayTransform;
4193 displayTransform.set(2, 0, 0, 4);
4194 addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform);
4195
4196 std::shared_ptr<FakeApplicationHandle> application =
4197 std::make_shared<FakeApplicationHandle>();
4198
4199 // Add two windows to the display. Their frames are represented in the display space.
4200 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004201 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4202 ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004203 firstWindow->setFrame(Rect(0, 0, 100, 200), displayTransform);
4204 addWindow(firstWindow);
4205
4206 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004207 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4208 ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004209 secondWindow->setFrame(Rect(100, 200, 200, 400), displayTransform);
4210 addWindow(secondWindow);
4211 return {std::move(firstWindow), std::move(secondWindow)};
4212 }
4213
4214private:
4215 std::vector<gui::DisplayInfo> mDisplayInfos;
4216 std::vector<gui::WindowInfo> mWindowInfos;
4217};
4218
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004219TEST_F(InputDispatcherDisplayProjectionTest, HitTestCoordinateSpaceConsistency) {
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004220 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
4221 // Send down to the first window. The point is represented in the display space. The point is
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004222 // selected so that if the hit test was performed with the point and the bounds being in
4223 // different coordinate spaces, the event would end up in the incorrect window.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004224 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4225 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4226 {PointF{75, 55}}));
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004227
4228 firstWindow->consumeMotionDown();
4229 secondWindow->assertNoEvents();
4230}
4231
4232// Ensure that when a MotionEvent is injected through the InputDispatcher::injectInputEvent() API,
4233// the event should be treated as being in the logical display space.
4234TEST_F(InputDispatcherDisplayProjectionTest, InjectionInLogicalDisplaySpace) {
4235 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
4236 // Send down to the first window. The point is represented in the logical display space. The
4237 // point is selected so that if the hit test was done in logical display space, then it would
4238 // end up in the incorrect window.
4239 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4240 PointF{75 * 2, 55 * 4});
4241
4242 firstWindow->consumeMotionDown();
4243 secondWindow->assertNoEvents();
4244}
4245
Prabir Pradhandaa2f142021-12-10 09:30:08 +00004246// Ensure that when a MotionEvent that has a custom transform is injected, the post-transformed
4247// event should be treated as being in the logical display space.
4248TEST_F(InputDispatcherDisplayProjectionTest, InjectionWithTransformInLogicalDisplaySpace) {
4249 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
4250
4251 const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0};
4252 ui::Transform injectedEventTransform;
4253 injectedEventTransform.set(matrix);
4254 const vec2 expectedPoint{75, 55}; // The injected point in the logical display space.
4255 const vec2 untransformedPoint = injectedEventTransform.inverse().transform(expectedPoint);
4256
4257 MotionEvent event = MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4258 .displayId(ADISPLAY_ID_DEFAULT)
4259 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004260 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER)
Prabir Pradhandaa2f142021-12-10 09:30:08 +00004261 .x(untransformedPoint.x)
4262 .y(untransformedPoint.y))
4263 .build();
4264 event.transform(matrix);
4265
4266 injectMotionEvent(mDispatcher, event, INJECT_EVENT_TIMEOUT,
4267 InputEventInjectionSync::WAIT_FOR_RESULT);
4268
4269 firstWindow->consumeMotionDown();
4270 secondWindow->assertNoEvents();
4271}
4272
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004273TEST_F(InputDispatcherDisplayProjectionTest, WindowGetsEventsInCorrectCoordinateSpace) {
4274 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
4275
4276 // Send down to the second window.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004277 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4278 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4279 {PointF{150, 220}}));
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004280
4281 firstWindow->assertNoEvents();
4282 const MotionEvent* event = secondWindow->consumeMotion();
Siarhei Vishniakoub681c202023-05-01 11:22:33 -07004283 ASSERT_NE(nullptr, event);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004284 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, event->getAction());
4285
4286 // Ensure that the events from the "getRaw" API are in logical display coordinates.
4287 EXPECT_EQ(300, event->getRawX(0));
4288 EXPECT_EQ(880, event->getRawY(0));
4289
4290 // Ensure that the x and y values are in the window's coordinate space.
4291 // The left-top of the second window is at (100, 200) in display space, which is (200, 800) in
4292 // the logical display space. This will be the origin of the window space.
4293 EXPECT_EQ(100, event->getX(0));
4294 EXPECT_EQ(80, event->getY(0));
4295}
4296
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004297/** Ensure consistent behavior of InputDispatcher in all orientations. */
4298class InputDispatcherDisplayOrientationFixture
4299 : public InputDispatcherDisplayProjectionTest,
4300 public ::testing::WithParamInterface<ui::Rotation> {};
4301
4302// This test verifies the touchable region of a window for all rotations of the display by tapping
4303// in different locations on the display, specifically points close to the four corners of a
4304// window.
4305TEST_P(InputDispatcherDisplayOrientationFixture, HitTestInDifferentOrientations) {
4306 constexpr static int32_t displayWidth = 400;
4307 constexpr static int32_t displayHeight = 800;
4308
4309 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4310
4311 const auto rotation = GetParam();
4312
4313 // Set up the display with the specified rotation.
4314 const bool isRotated = rotation == ui::ROTATION_90 || rotation == ui::ROTATION_270;
4315 const int32_t logicalDisplayWidth = isRotated ? displayHeight : displayWidth;
4316 const int32_t logicalDisplayHeight = isRotated ? displayWidth : displayHeight;
4317 const ui::Transform displayTransform(ui::Transform::toRotationFlags(rotation),
4318 logicalDisplayWidth, logicalDisplayHeight);
4319 addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform);
4320
4321 // Create a window with its bounds determined in the logical display.
4322 const Rect frameInLogicalDisplay(100, 100, 200, 300);
4323 const Rect frameInDisplay = displayTransform.inverse().transform(frameInLogicalDisplay);
4324 sp<FakeWindowHandle> window =
4325 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4326 window->setFrame(frameInDisplay, displayTransform);
4327 addWindow(window);
4328
4329 // The following points in logical display space should be inside the window.
4330 static const std::array<vec2, 4> insidePoints{
4331 {{100, 100}, {199.99, 100}, {100, 299.99}, {199.99, 299.99}}};
4332 for (const auto pointInsideWindow : insidePoints) {
4333 const vec2 p = displayTransform.inverse().transform(pointInsideWindow);
4334 const PointF pointInDisplaySpace{p.x, p.y};
Prabir Pradhan678438e2023-04-13 19:32:51 +00004335 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4336 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4337 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004338 window->consumeMotionDown();
4339
Prabir Pradhan678438e2023-04-13 19:32:51 +00004340 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP,
4341 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4342 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004343 window->consumeMotionUp();
4344 }
4345
4346 // The following points in logical display space should be outside the window.
4347 static const std::array<vec2, 5> outsidePoints{
4348 {{200, 100}, {100, 300}, {200, 300}, {100, 99.99}, {99.99, 100}}};
4349 for (const auto pointOutsideWindow : outsidePoints) {
4350 const vec2 p = displayTransform.inverse().transform(pointOutsideWindow);
4351 const PointF pointInDisplaySpace{p.x, p.y};
Prabir Pradhan678438e2023-04-13 19:32:51 +00004352 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4353 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4354 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004355
Prabir Pradhan678438e2023-04-13 19:32:51 +00004356 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP,
4357 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4358 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004359 }
4360 window->assertNoEvents();
4361}
4362
4363// Run the precision tests for all rotations.
4364INSTANTIATE_TEST_SUITE_P(InputDispatcherDisplayOrientationTests,
4365 InputDispatcherDisplayOrientationFixture,
4366 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
4367 ui::ROTATION_270),
4368 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
4369 return ftl::enum_string(testParamInfo.param);
4370 });
4371
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004372using TransferFunction = std::function<bool(const std::unique_ptr<InputDispatcher>& dispatcher,
4373 sp<IBinder>, sp<IBinder>)>;
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004374
4375class TransferTouchFixture : public InputDispatcherTest,
4376 public ::testing::WithParamInterface<TransferFunction> {};
4377
4378TEST_P(TransferTouchFixture, TransferTouch_OnePointer) {
Chris Yea209fde2020-07-22 13:54:51 -07004379 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004380
4381 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004382 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004383 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4384 ADISPLAY_ID_DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00004385 firstWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004386 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004387 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4388 ADISPLAY_ID_DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00004389 sp<FakeWindowHandle> wallpaper =
4390 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
4391 wallpaper->setIsWallpaper(true);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004392 // Add the windows to the dispatcher
Arthur Hungc539dbb2022-12-08 07:45:36 +00004393 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow, wallpaper}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08004394
4395 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004396 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4397 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00004398
Svet Ganov5d3bc372020-01-26 23:11:07 -08004399 // Only the first window should get the down event
4400 firstWindow->consumeMotionDown();
4401 secondWindow->assertNoEvents();
Arthur Hungc539dbb2022-12-08 07:45:36 +00004402 wallpaper->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004403
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004404 // Transfer touch to the second window
4405 TransferFunction f = GetParam();
4406 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4407 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004408 // The first window gets cancel and the second gets down
4409 firstWindow->consumeMotionCancel();
4410 secondWindow->consumeMotionDown();
Arthur Hungc539dbb2022-12-08 07:45:36 +00004411 wallpaper->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004412
4413 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004414 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4415 ADISPLAY_ID_DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004416 // The first window gets no events and the second gets up
4417 firstWindow->assertNoEvents();
4418 secondWindow->consumeMotionUp();
Arthur Hungc539dbb2022-12-08 07:45:36 +00004419 wallpaper->assertNoEvents();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004420}
4421
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004422/**
4423 * When 'transferTouch' API is invoked, dispatcher needs to find the "best" window to take touch
4424 * from. When we have spy windows, there are several windows to choose from: either spy, or the
4425 * 'real' (non-spy) window. Always prefer the 'real' window because that's what would be most
4426 * natural to the user.
4427 * In this test, we are sending a pointer to both spy window and first window. We then try to
4428 * transfer touch to the second window. The dispatcher should identify the first window as the
4429 * one that should lose the gesture, and therefore the action should be to move the gesture from
4430 * the first window to the second.
4431 * The main goal here is to test the behaviour of 'transferTouch' API, but it's still valid to test
4432 * the other API, as well.
4433 */
4434TEST_P(TransferTouchFixture, TransferTouch_MultipleWindowsWithSpy) {
4435 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4436
4437 // Create a couple of windows + a spy window
4438 sp<FakeWindowHandle> spyWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004439 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004440 spyWindow->setTrustedOverlay(true);
4441 spyWindow->setSpy(true);
4442 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004443 sp<FakeWindowHandle>::make(application, mDispatcher, "First", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004444 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004445 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004446
4447 // Add the windows to the dispatcher
4448 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, firstWindow, secondWindow}}});
4449
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));
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004453 // Only the first window and spy should get the down event
4454 spyWindow->consumeMotionDown();
4455 firstWindow->consumeMotionDown();
4456
4457 // Transfer touch to the second window. Non-spy window should be preferred over the spy window
4458 // if f === 'transferTouch'.
4459 TransferFunction f = GetParam();
4460 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4461 ASSERT_TRUE(success);
4462 // The first window gets cancel and the second gets down
4463 firstWindow->consumeMotionCancel();
4464 secondWindow->consumeMotionDown();
4465
4466 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004467 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4468 ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004469 // The first window gets no events and the second+spy get up
4470 firstWindow->assertNoEvents();
4471 spyWindow->consumeMotionUp();
4472 secondWindow->consumeMotionUp();
4473}
4474
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004475TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07004476 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004477
4478 PointF touchPoint = {10, 10};
4479
4480 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004481 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004482 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4483 ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08004484 firstWindow->setPreventSplitting(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004485 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004486 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4487 ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08004488 secondWindow->setPreventSplitting(true);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004489
4490 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00004491 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08004492
4493 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004494 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4495 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4496 {touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004497 // Only the first window should get the down event
4498 firstWindow->consumeMotionDown();
4499 secondWindow->assertNoEvents();
4500
4501 // Send pointer down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004502 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4503 ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004504 // Only the first window should get the pointer down event
4505 firstWindow->consumeMotionPointerDown(1);
4506 secondWindow->assertNoEvents();
4507
4508 // Transfer touch focus to the second window
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004509 TransferFunction f = GetParam();
4510 bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4511 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004512 // The first window gets cancel and the second gets down and pointer down
4513 firstWindow->consumeMotionCancel();
4514 secondWindow->consumeMotionDown();
4515 secondWindow->consumeMotionPointerDown(1);
4516
4517 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004518 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
4519 ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004520 // The first window gets nothing and the second gets pointer up
4521 firstWindow->assertNoEvents();
4522 secondWindow->consumeMotionPointerUp(1);
4523
4524 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004525 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4526 ADISPLAY_ID_DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004527 // The first window gets nothing and the second gets up
4528 firstWindow->assertNoEvents();
4529 secondWindow->consumeMotionUp();
4530}
4531
Arthur Hungc539dbb2022-12-08 07:45:36 +00004532TEST_P(TransferTouchFixture, TransferTouch_MultipleWallpapers) {
4533 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4534
4535 // Create a couple of windows
4536 sp<FakeWindowHandle> firstWindow =
4537 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4538 ADISPLAY_ID_DEFAULT);
4539 firstWindow->setDupTouchToWallpaper(true);
4540 sp<FakeWindowHandle> secondWindow =
4541 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4542 ADISPLAY_ID_DEFAULT);
4543 secondWindow->setDupTouchToWallpaper(true);
4544
4545 sp<FakeWindowHandle> wallpaper1 =
4546 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper1", ADISPLAY_ID_DEFAULT);
4547 wallpaper1->setIsWallpaper(true);
4548
4549 sp<FakeWindowHandle> wallpaper2 =
4550 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper2", ADISPLAY_ID_DEFAULT);
4551 wallpaper2->setIsWallpaper(true);
4552 // Add the windows to the dispatcher
4553 mDispatcher->setInputWindows(
4554 {{ADISPLAY_ID_DEFAULT, {firstWindow, wallpaper1, secondWindow, wallpaper2}}});
4555
4556 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004557 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4558 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00004559
4560 // Only the first window should get the down event
4561 firstWindow->consumeMotionDown();
4562 secondWindow->assertNoEvents();
4563 wallpaper1->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4564 wallpaper2->assertNoEvents();
4565
4566 // Transfer touch focus to the second window
4567 TransferFunction f = GetParam();
4568 bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4569 ASSERT_TRUE(success);
4570
4571 // The first window gets cancel and the second gets down
4572 firstWindow->consumeMotionCancel();
4573 secondWindow->consumeMotionDown();
4574 wallpaper1->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4575 wallpaper2->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4576
4577 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004578 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4579 ADISPLAY_ID_DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00004580 // The first window gets no events and the second gets up
4581 firstWindow->assertNoEvents();
4582 secondWindow->consumeMotionUp();
4583 wallpaper1->assertNoEvents();
4584 wallpaper2->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4585}
4586
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004587// For the cases of single pointer touch and two pointers non-split touch, the api's
4588// 'transferTouch' and 'transferTouchFocus' are equivalent in behaviour. They only differ
4589// for the case where there are multiple pointers split across several windows.
4590INSTANTIATE_TEST_SUITE_P(TransferFunctionTests, TransferTouchFixture,
4591 ::testing::Values(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004592 [&](const std::unique_ptr<InputDispatcher>& dispatcher,
4593 sp<IBinder> /*ignored*/, sp<IBinder> destChannelToken) {
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004594 return dispatcher->transferTouch(destChannelToken,
4595 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004596 },
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004597 [&](const std::unique_ptr<InputDispatcher>& dispatcher,
4598 sp<IBinder> from, sp<IBinder> to) {
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004599 return dispatcher->transferTouchFocus(from, to,
Harry Cutts33476232023-01-30 19:57:29 +00004600 /*isDragAndDrop=*/false);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004601 }));
4602
Svet Ganov5d3bc372020-01-26 23:11:07 -08004603TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07004604 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004605
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004606 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004607 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4608 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004609 firstWindow->setFrame(Rect(0, 0, 600, 400));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004610
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004611 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004612 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4613 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004614 secondWindow->setFrame(Rect(0, 400, 600, 800));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004615
4616 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00004617 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08004618
4619 PointF pointInFirst = {300, 200};
4620 PointF pointInSecond = {300, 600};
4621
4622 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004623 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4624 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4625 {pointInFirst}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004626 // Only the first window should get the down event
4627 firstWindow->consumeMotionDown();
4628 secondWindow->assertNoEvents();
4629
4630 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004631 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4632 ADISPLAY_ID_DEFAULT,
4633 {pointInFirst, pointInSecond}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004634 // The first window gets a move and the second a down
4635 firstWindow->consumeMotionMove();
4636 secondWindow->consumeMotionDown();
4637
4638 // Transfer touch focus to the second window
4639 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
4640 // The first window gets cancel and the new gets pointer down (it already saw down)
4641 firstWindow->consumeMotionCancel();
4642 secondWindow->consumeMotionPointerDown(1);
4643
4644 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004645 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
4646 ADISPLAY_ID_DEFAULT,
4647 {pointInFirst, pointInSecond}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004648 // The first window gets nothing and the second gets pointer up
4649 firstWindow->assertNoEvents();
4650 secondWindow->consumeMotionPointerUp(1);
4651
4652 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004653 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4654 ADISPLAY_ID_DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004655 // The first window gets nothing and the second gets up
4656 firstWindow->assertNoEvents();
4657 secondWindow->consumeMotionUp();
4658}
4659
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004660// Same as TransferTouchFocus_TwoPointersSplitTouch, but using 'transferTouch' api.
4661// Unlike 'transferTouchFocus', calling 'transferTouch' when there are two windows receiving
4662// touch is not supported, so the touch should continue on those windows and the transferred-to
4663// window should get nothing.
4664TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) {
4665 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4666
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004667 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004668 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4669 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004670 firstWindow->setFrame(Rect(0, 0, 600, 400));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004671
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004672 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004673 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4674 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004675 secondWindow->setFrame(Rect(0, 400, 600, 800));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004676
4677 // Add the windows to the dispatcher
4678 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
4679
4680 PointF pointInFirst = {300, 200};
4681 PointF pointInSecond = {300, 600};
4682
4683 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004684 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4685 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4686 {pointInFirst}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004687 // Only the first window should get the down event
4688 firstWindow->consumeMotionDown();
4689 secondWindow->assertNoEvents();
4690
4691 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004692 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4693 ADISPLAY_ID_DEFAULT,
4694 {pointInFirst, pointInSecond}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004695 // The first window gets a move and the second a down
4696 firstWindow->consumeMotionMove();
4697 secondWindow->consumeMotionDown();
4698
4699 // Transfer touch focus to the second window
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004700 const bool transferred =
4701 mDispatcher->transferTouch(secondWindow->getToken(), ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004702 // The 'transferTouch' call should not succeed, because there are 2 touched windows
4703 ASSERT_FALSE(transferred);
4704 firstWindow->assertNoEvents();
4705 secondWindow->assertNoEvents();
4706
4707 // The rest of the dispatch should proceed as normal
4708 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004709 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
4710 ADISPLAY_ID_DEFAULT,
4711 {pointInFirst, pointInSecond}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004712 // The first window gets MOVE and the second gets pointer up
4713 firstWindow->consumeMotionMove();
4714 secondWindow->consumeMotionUp();
4715
4716 // Send up event to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004717 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4718 ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004719 // The first window gets nothing and the second gets up
4720 firstWindow->consumeMotionUp();
4721 secondWindow->assertNoEvents();
4722}
4723
Arthur Hungabbb9d82021-09-01 14:52:30 +00004724// This case will create two windows and one mirrored window on the default display and mirror
4725// two windows on the second display. It will test if 'transferTouchFocus' works fine if we put
4726// the windows info of second display before default display.
4727TEST_F(InputDispatcherTest, TransferTouchFocus_CloneSurface) {
4728 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4729 sp<FakeWindowHandle> firstWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004730 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004731 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004732 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004733 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004734 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004735
4736 sp<FakeWindowHandle> mirrorWindowInPrimary =
4737 firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT);
4738 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004739
4740 sp<FakeWindowHandle> firstWindowInSecondary =
4741 firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
4742 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004743
4744 sp<FakeWindowHandle> secondWindowInSecondary =
4745 secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
4746 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004747
4748 // Update window info, let it find window handle of second display first.
4749 mDispatcher->setInputWindows(
4750 {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}},
4751 {ADISPLAY_ID_DEFAULT,
4752 {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}});
4753
4754 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4755 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4756 {50, 50}))
4757 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4758
4759 // Window should receive motion event.
4760 firstWindowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4761
4762 // Transfer touch focus
4763 ASSERT_TRUE(mDispatcher->transferTouchFocus(firstWindowInPrimary->getToken(),
4764 secondWindowInPrimary->getToken()));
4765 // The first window gets cancel.
4766 firstWindowInPrimary->consumeMotionCancel();
4767 secondWindowInPrimary->consumeMotionDown();
4768
4769 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4770 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4771 ADISPLAY_ID_DEFAULT, {150, 50}))
4772 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4773 firstWindowInPrimary->assertNoEvents();
4774 secondWindowInPrimary->consumeMotionMove();
4775
4776 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4777 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4778 {150, 50}))
4779 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4780 firstWindowInPrimary->assertNoEvents();
4781 secondWindowInPrimary->consumeMotionUp();
4782}
4783
4784// Same as TransferTouchFocus_CloneSurface, but this touch on the secondary display and use
4785// 'transferTouch' api.
4786TEST_F(InputDispatcherTest, TransferTouch_CloneSurface) {
4787 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4788 sp<FakeWindowHandle> firstWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004789 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004790 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004791 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004792 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004793 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004794
4795 sp<FakeWindowHandle> mirrorWindowInPrimary =
4796 firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT);
4797 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004798
4799 sp<FakeWindowHandle> firstWindowInSecondary =
4800 firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
4801 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004802
4803 sp<FakeWindowHandle> secondWindowInSecondary =
4804 secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
4805 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004806
4807 // Update window info, let it find window handle of second display first.
4808 mDispatcher->setInputWindows(
4809 {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}},
4810 {ADISPLAY_ID_DEFAULT,
4811 {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}});
4812
4813 // Touch on second display.
4814 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4815 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {50, 50}))
4816 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4817
4818 // Window should receive motion event.
4819 firstWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID);
4820
4821 // Transfer touch focus
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004822 ASSERT_TRUE(mDispatcher->transferTouch(secondWindowInSecondary->getToken(), SECOND_DISPLAY_ID));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004823
4824 // The first window gets cancel.
4825 firstWindowInPrimary->consumeMotionCancel(SECOND_DISPLAY_ID);
4826 secondWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID);
4827
4828 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4829 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4830 SECOND_DISPLAY_ID, {150, 50}))
4831 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4832 firstWindowInPrimary->assertNoEvents();
4833 secondWindowInPrimary->consumeMotionMove(SECOND_DISPLAY_ID);
4834
4835 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4836 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {150, 50}))
4837 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4838 firstWindowInPrimary->assertNoEvents();
4839 secondWindowInPrimary->consumeMotionUp(SECOND_DISPLAY_ID);
4840}
4841
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004842TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07004843 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004844 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4845 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004846
Vishnu Nair47074b82020-08-14 11:54:47 -07004847 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004848 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004849 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004850
4851 window->consumeFocusEvent(true);
4852
Prabir Pradhan678438e2023-04-13 19:32:51 +00004853 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004854
4855 // Window should receive key down event.
4856 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Josep del Riob3981622023-04-18 15:49:45 +00004857
4858 // Should have poked user activity
4859 mFakePolicy->assertUserActivityPoked();
4860}
4861
4862TEST_F(InputDispatcherTest, FocusedWindow_DisableUserActivity) {
4863 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4864 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4865 "Fake Window", ADISPLAY_ID_DEFAULT);
4866
4867 window->setDisableUserActivity(true);
4868 window->setFocusable(true);
4869 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4870 setFocusedWindow(window);
4871
4872 window->consumeFocusEvent(true);
4873
4874 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
4875
4876 // Window should receive key down event.
4877 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4878
4879 // Should have poked user activity
4880 mFakePolicy->assertUserActivityNotPoked();
4881}
4882
4883TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveSystemShortcut) {
4884 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4885 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4886 "Fake Window", ADISPLAY_ID_DEFAULT);
4887
4888 window->setFocusable(true);
4889 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4890 setFocusedWindow(window);
4891
4892 window->consumeFocusEvent(true);
4893
4894 mDispatcher->notifyKey(generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
4895 mDispatcher->waitForIdle();
4896
4897 // System key is not passed down
4898 window->assertNoEvents();
4899
4900 // Should have poked user activity
4901 mFakePolicy->assertUserActivityPoked();
4902}
4903
4904TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveAssistantKey) {
4905 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4906 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4907 "Fake Window", ADISPLAY_ID_DEFAULT);
4908
4909 window->setFocusable(true);
4910 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4911 setFocusedWindow(window);
4912
4913 window->consumeFocusEvent(true);
4914
4915 mDispatcher->notifyKey(generateAssistantKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
4916 mDispatcher->waitForIdle();
4917
4918 // System key is not passed down
4919 window->assertNoEvents();
4920
4921 // Should have poked user activity
4922 mFakePolicy->assertUserActivityPoked();
4923}
4924
4925TEST_F(InputDispatcherTest, FocusedWindow_SystemKeyIgnoresDisableUserActivity) {
4926 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4927 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4928 "Fake Window", ADISPLAY_ID_DEFAULT);
4929
4930 window->setDisableUserActivity(true);
4931 window->setFocusable(true);
4932 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4933 setFocusedWindow(window);
4934
4935 window->consumeFocusEvent(true);
4936
4937 mDispatcher->notifyKey(generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
4938 mDispatcher->waitForIdle();
4939
4940 // System key is not passed down
4941 window->assertNoEvents();
4942
4943 // Should have poked user activity
4944 mFakePolicy->assertUserActivityPoked();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004945}
4946
4947TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07004948 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004949 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4950 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004951
Arthur Hung72d8dc32020-03-28 00:48:39 +00004952 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004953
Prabir Pradhan678438e2023-04-13 19:32:51 +00004954 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004955 mDispatcher->waitForIdle();
4956
4957 window->assertNoEvents();
4958}
4959
4960// If a window is touchable, but does not have focus, it should receive motion events, but not keys
4961TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) {
Chris Yea209fde2020-07-22 13:54:51 -07004962 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004963 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4964 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004965
Arthur Hung72d8dc32020-03-28 00:48:39 +00004966 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004967
4968 // Send key
Prabir Pradhan678438e2023-04-13 19:32:51 +00004969 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004970 // Send motion
Prabir Pradhan678438e2023-04-13 19:32:51 +00004971 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4972 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004973
4974 // Window should receive only the motion event
4975 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4976 window->assertNoEvents(); // Key event or focus event will not be received
4977}
4978
arthurhungea3f4fc2020-12-21 23:18:53 +08004979TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) {
4980 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4981
arthurhungea3f4fc2020-12-21 23:18:53 +08004982 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004983 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4984 ADISPLAY_ID_DEFAULT);
arthurhungea3f4fc2020-12-21 23:18:53 +08004985 firstWindow->setFrame(Rect(0, 0, 600, 400));
arthurhungea3f4fc2020-12-21 23:18:53 +08004986
arthurhungea3f4fc2020-12-21 23:18:53 +08004987 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004988 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4989 ADISPLAY_ID_DEFAULT);
arthurhungea3f4fc2020-12-21 23:18:53 +08004990 secondWindow->setFrame(Rect(0, 400, 600, 800));
arthurhungea3f4fc2020-12-21 23:18:53 +08004991
4992 // Add the windows to the dispatcher
4993 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
4994
4995 PointF pointInFirst = {300, 200};
4996 PointF pointInSecond = {300, 600};
4997
4998 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004999 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5000 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5001 {pointInFirst}));
arthurhungea3f4fc2020-12-21 23:18:53 +08005002 // Only the first window should get the down event
5003 firstWindow->consumeMotionDown();
5004 secondWindow->assertNoEvents();
5005
5006 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005007 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5008 ADISPLAY_ID_DEFAULT,
5009 {pointInFirst, pointInSecond}));
arthurhungea3f4fc2020-12-21 23:18:53 +08005010 // The first window gets a move and the second a down
5011 firstWindow->consumeMotionMove();
5012 secondWindow->consumeMotionDown();
5013
5014 // Send pointer cancel to the second window
5015 NotifyMotionArgs pointerUpMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08005016 generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
arthurhungea3f4fc2020-12-21 23:18:53 +08005017 {pointInFirst, pointInSecond});
5018 pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED;
Prabir Pradhan678438e2023-04-13 19:32:51 +00005019 mDispatcher->notifyMotion(pointerUpMotionArgs);
arthurhungea3f4fc2020-12-21 23:18:53 +08005020 // The first window gets move and the second gets cancel.
5021 firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
5022 secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
5023
5024 // Send up event.
Prabir Pradhan678438e2023-04-13 19:32:51 +00005025 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
5026 ADISPLAY_ID_DEFAULT));
arthurhungea3f4fc2020-12-21 23:18:53 +08005027 // The first window gets up and the second gets nothing.
5028 firstWindow->consumeMotionUp();
5029 secondWindow->assertNoEvents();
5030}
5031
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00005032TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) {
5033 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5034
5035 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005036 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00005037 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5038 std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline;
5039 graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2;
5040 graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3;
5041
Harry Cutts33476232023-01-30 19:57:29 +00005042 window->sendTimeline(/*inputEventId=*/1, graphicsTimeline);
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00005043 window->assertNoEvents();
5044 mDispatcher->waitForIdle();
5045}
5046
chaviwd1c23182019-12-20 18:44:56 -08005047class FakeMonitorReceiver {
Michael Wright3a240c42019-12-10 20:53:41 +00005048public:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07005049 FakeMonitorReceiver(const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005050 int32_t displayId) {
Garfield Tan15601662020-09-22 15:32:38 -07005051 base::Result<std::unique_ptr<InputChannel>> channel =
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08005052 dispatcher->createInputMonitor(displayId, name, MONITOR_PID);
Garfield Tan15601662020-09-22 15:32:38 -07005053 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
Michael Wright3a240c42019-12-10 20:53:41 +00005054 }
5055
chaviwd1c23182019-12-20 18:44:56 -08005056 sp<IBinder> getToken() { return mInputReceiver->getToken(); }
5057
5058 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005059 mInputReceiver->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId,
5060 expectedFlags);
chaviwd1c23182019-12-20 18:44:56 -08005061 }
5062
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005063 std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); }
5064
5065 void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); }
5066
chaviwd1c23182019-12-20 18:44:56 -08005067 void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005068 mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN,
chaviwd1c23182019-12-20 18:44:56 -08005069 expectedDisplayId, expectedFlags);
5070 }
5071
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005072 void consumeMotionMove(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005073 mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005074 expectedDisplayId, expectedFlags);
5075 }
5076
chaviwd1c23182019-12-20 18:44:56 -08005077 void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005078 mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_UP,
chaviwd1c23182019-12-20 18:44:56 -08005079 expectedDisplayId, expectedFlags);
5080 }
5081
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005082 void consumeMotionCancel(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08005083 mInputReceiver->consumeMotionEvent(
5084 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
5085 WithDisplayId(expectedDisplayId),
5086 WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED)));
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005087 }
5088
Arthur Hungfbfa5722021-11-16 02:45:54 +00005089 void consumeMotionPointerDown(int32_t pointerIdx) {
5090 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
5091 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005092 mInputReceiver->consumeEvent(InputEventType::MOTION, action, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00005093 /*expectedFlags=*/0);
Arthur Hungfbfa5722021-11-16 02:45:54 +00005094 }
5095
Evan Rosky84f07f02021-04-16 10:42:42 -07005096 MotionEvent* consumeMotion() {
5097 InputEvent* event = mInputReceiver->consume();
5098 if (!event) {
5099 ADD_FAILURE() << "No event was produced";
5100 return nullptr;
5101 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005102 if (event->getType() != InputEventType::MOTION) {
5103 ADD_FAILURE() << "Expected MotionEvent, got " << *event;
Evan Rosky84f07f02021-04-16 10:42:42 -07005104 return nullptr;
5105 }
5106 return static_cast<MotionEvent*>(event);
5107 }
5108
chaviwd1c23182019-12-20 18:44:56 -08005109 void assertNoEvents() { mInputReceiver->assertNoEvents(); }
5110
5111private:
5112 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Michael Wright3a240c42019-12-10 20:53:41 +00005113};
5114
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005115using InputDispatcherMonitorTest = InputDispatcherTest;
5116
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005117/**
5118 * Two entities that receive touch: A window, and a global monitor.
5119 * The touch goes to the window, and then the window disappears.
5120 * The monitor does not get cancel right away. But if more events come in, the touch gets canceled
5121 * for the monitor, as well.
5122 * 1. foregroundWindow
5123 * 2. monitor <-- global monitor (doesn't observe z order, receives all events)
5124 */
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005125TEST_F(InputDispatcherMonitorTest, MonitorTouchIsCanceledWhenForegroundWindowDisappears) {
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005126 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5127 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005128 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005129
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005130 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005131
5132 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5133 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5134 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5135 {100, 200}))
5136 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5137
5138 // Both the foreground window and the global monitor should receive the touch down
5139 window->consumeMotionDown();
5140 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
5141
5142 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5143 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5144 ADISPLAY_ID_DEFAULT, {110, 200}))
5145 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5146
5147 window->consumeMotionMove();
5148 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
5149
5150 // Now the foreground window goes away
5151 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
5152 window->consumeMotionCancel();
5153 monitor.assertNoEvents(); // Global monitor does not get a cancel yet
5154
5155 // If more events come in, there will be no more foreground window to send them to. This will
5156 // cause a cancel for the monitor, as well.
5157 ASSERT_EQ(InputEventInjectionResult::FAILED,
5158 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5159 ADISPLAY_ID_DEFAULT, {120, 200}))
5160 << "Injection should fail because the window was removed";
5161 window->assertNoEvents();
5162 // Global monitor now gets the cancel
5163 monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
5164}
5165
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005166TEST_F(InputDispatcherMonitorTest, ReceivesMotionEvents) {
Chris Yea209fde2020-07-22 13:54:51 -07005167 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005168 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5169 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005170 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00005171
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005172 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00005173
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005174 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00005175 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005176 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00005177 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08005178 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00005179}
5180
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005181TEST_F(InputDispatcherMonitorTest, MonitorCannotPilferPointers) {
5182 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00005183
Chris Yea209fde2020-07-22 13:54:51 -07005184 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005185 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5186 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005187 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00005188
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005189 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00005190 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005191 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
chaviwd1c23182019-12-20 18:44:56 -08005192 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005193 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00005194
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005195 // Pilfer pointers from the monitor.
5196 // This should not do anything and the window should continue to receive events.
5197 EXPECT_NE(OK, mDispatcher->pilferPointers(monitor.getToken()));
Michael Wright3a240c42019-12-10 20:53:41 +00005198
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005199 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005200 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5201 ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005202 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005203
5204 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
5205 window->consumeMotionMove(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00005206}
5207
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005208TEST_F(InputDispatcherMonitorTest, NoWindowTransform) {
Evan Rosky84f07f02021-04-16 10:42:42 -07005209 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005210 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5211 "Fake Window", ADISPLAY_ID_DEFAULT);
Evan Rosky84f07f02021-04-16 10:42:42 -07005212 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5213 window->setWindowOffset(20, 40);
5214 window->setWindowTransform(0, 1, -1, 0);
5215
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005216 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Evan Rosky84f07f02021-04-16 10:42:42 -07005217
5218 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5219 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
5220 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5221 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5222 MotionEvent* event = monitor.consumeMotion();
5223 // Even though window has transform, gesture monitor must not.
5224 ASSERT_EQ(ui::Transform(), event->getTransform());
5225}
5226
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005227TEST_F(InputDispatcherMonitorTest, InjectionFailsWithNoWindow) {
Arthur Hungb3307ee2021-10-14 10:57:37 +00005228 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005229 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Arthur Hungb3307ee2021-10-14 10:57:37 +00005230
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005231 ASSERT_EQ(InputEventInjectionResult::FAILED,
Arthur Hungb3307ee2021-10-14 10:57:37 +00005232 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005233 << "Injection should fail if there is a monitor, but no touchable window";
5234 monitor.assertNoEvents();
Arthur Hungb3307ee2021-10-14 10:57:37 +00005235}
5236
chaviw81e2bb92019-12-18 15:03:51 -08005237TEST_F(InputDispatcherTest, TestMoveEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07005238 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005239 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5240 "Fake Window", ADISPLAY_ID_DEFAULT);
chaviw81e2bb92019-12-18 15:03:51 -08005241
Arthur Hung72d8dc32020-03-28 00:48:39 +00005242 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
chaviw81e2bb92019-12-18 15:03:51 -08005243
5244 NotifyMotionArgs motionArgs =
5245 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5246 ADISPLAY_ID_DEFAULT);
5247
Prabir Pradhan678438e2023-04-13 19:32:51 +00005248 mDispatcher->notifyMotion(motionArgs);
chaviw81e2bb92019-12-18 15:03:51 -08005249 // Window should receive motion down event.
5250 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5251
5252 motionArgs.action = AMOTION_EVENT_ACTION_MOVE;
Garfield Tanc51d1ba2020-01-28 13:24:04 -08005253 motionArgs.id += 1;
chaviw81e2bb92019-12-18 15:03:51 -08005254 motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
5255 motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
5256 motionArgs.pointerCoords[0].getX() - 10);
5257
Prabir Pradhan678438e2023-04-13 19:32:51 +00005258 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005259 window->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00005260 /*expectedFlags=*/0);
chaviw81e2bb92019-12-18 15:03:51 -08005261}
5262
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005263/**
5264 * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to
5265 * the device default right away. In the test scenario, we check both the default value,
5266 * and the action of enabling / disabling.
5267 */
5268TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) {
Chris Yea209fde2020-07-22 13:54:51 -07005269 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005270 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5271 "Test window", ADISPLAY_ID_DEFAULT);
Antonio Kantekea47acb2021-12-23 12:41:25 -08005272 const WindowInfo& windowInfo = *window->getInfo();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005273
5274 // Set focused application.
5275 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07005276 window->setFocusable(true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005277
5278 SCOPED_TRACE("Check default value of touch mode");
Arthur Hung72d8dc32020-03-28 00:48:39 +00005279 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005280 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00005281 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005282
5283 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07005284 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005285 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Harry Cutts33476232023-01-30 19:57:29 +00005286 window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005287
5288 SCOPED_TRACE("Disable touch mode");
Antonio Kantekea47acb2021-12-23 12:41:25 -08005289 mDispatcher->setInTouchMode(false, windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00005290 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00005291 window->consumeTouchModeEvent(false);
Vishnu Nair47074b82020-08-14 11:54:47 -07005292 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005293 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005294 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00005295 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/false);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005296
5297 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07005298 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005299 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Harry Cutts33476232023-01-30 19:57:29 +00005300 window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/false);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005301
5302 SCOPED_TRACE("Enable touch mode again");
Antonio Kantekea47acb2021-12-23 12:41:25 -08005303 mDispatcher->setInTouchMode(true, windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00005304 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00005305 window->consumeTouchModeEvent(true);
Vishnu Nair47074b82020-08-14 11:54:47 -07005306 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005307 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005308 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00005309 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005310
5311 window->assertNoEvents();
5312}
5313
Gang Wange9087892020-01-07 12:17:14 -05005314TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07005315 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005316 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5317 "Test window", ADISPLAY_ID_DEFAULT);
Gang Wange9087892020-01-07 12:17:14 -05005318
5319 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07005320 window->setFocusable(true);
Gang Wange9087892020-01-07 12:17:14 -05005321
Arthur Hung72d8dc32020-03-28 00:48:39 +00005322 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005323 setFocusedWindow(window);
5324
Harry Cutts33476232023-01-30 19:57:29 +00005325 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Gang Wange9087892020-01-07 12:17:14 -05005326
Prabir Pradhan678438e2023-04-13 19:32:51 +00005327 const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
5328 mDispatcher->notifyKey(keyArgs);
Gang Wange9087892020-01-07 12:17:14 -05005329
5330 InputEvent* event = window->consume();
5331 ASSERT_NE(event, nullptr);
5332
5333 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
5334 ASSERT_NE(verified, nullptr);
5335 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY);
5336
5337 ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos);
5338 ASSERT_EQ(keyArgs.deviceId, verified->deviceId);
5339 ASSERT_EQ(keyArgs.source, verified->source);
5340 ASSERT_EQ(keyArgs.displayId, verified->displayId);
5341
5342 const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified);
5343
5344 ASSERT_EQ(keyArgs.action, verifiedKey.action);
Gang Wange9087892020-01-07 12:17:14 -05005345 ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08005346 ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos);
Gang Wange9087892020-01-07 12:17:14 -05005347 ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode);
5348 ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode);
5349 ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState);
5350 ASSERT_EQ(0, verifiedKey.repeatCount);
5351}
5352
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005353TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07005354 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005355 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5356 "Test window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005357
5358 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5359
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07005360 ui::Transform transform;
5361 transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
5362
5363 gui::DisplayInfo displayInfo;
5364 displayInfo.displayId = ADISPLAY_ID_DEFAULT;
5365 displayInfo.transform = transform;
5366
Patrick Williamsd828f302023-04-28 17:52:08 -05005367 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {displayInfo}, 0, 0});
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005368
Prabir Pradhan678438e2023-04-13 19:32:51 +00005369 const NotifyMotionArgs motionArgs =
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005370 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5371 ADISPLAY_ID_DEFAULT);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005372 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005373
5374 InputEvent* event = window->consume();
5375 ASSERT_NE(event, nullptr);
5376
5377 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
5378 ASSERT_NE(verified, nullptr);
5379 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION);
5380
5381 EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos);
5382 EXPECT_EQ(motionArgs.deviceId, verified->deviceId);
5383 EXPECT_EQ(motionArgs.source, verified->source);
5384 EXPECT_EQ(motionArgs.displayId, verified->displayId);
5385
5386 const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified);
5387
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07005388 const vec2 rawXY =
5389 MotionEvent::calculateTransformedXY(motionArgs.source, transform,
5390 motionArgs.pointerCoords[0].getXYValue());
5391 EXPECT_EQ(rawXY.x, verifiedMotion.rawX);
5392 EXPECT_EQ(rawXY.y, verifiedMotion.rawY);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005393 EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005394 EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08005395 EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005396 EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState);
5397 EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState);
5398}
5399
chaviw09c8d2d2020-08-24 15:48:26 -07005400/**
5401 * Ensure that separate calls to sign the same data are generating the same key.
5402 * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance
5403 * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky
5404 * tests.
5405 */
5406TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) {
5407 KeyEvent event = getTestKeyEvent();
5408 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
5409
5410 std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent);
5411 std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent);
5412 ASSERT_EQ(hmac1, hmac2);
5413}
5414
5415/**
5416 * Ensure that changes in VerifiedKeyEvent produce a different hmac.
5417 */
5418TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) {
5419 KeyEvent event = getTestKeyEvent();
5420 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
5421 std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent);
5422
5423 verifiedEvent.deviceId += 1;
5424 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5425
5426 verifiedEvent.source += 1;
5427 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5428
5429 verifiedEvent.eventTimeNanos += 1;
5430 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5431
5432 verifiedEvent.displayId += 1;
5433 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5434
5435 verifiedEvent.action += 1;
5436 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5437
5438 verifiedEvent.downTimeNanos += 1;
5439 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5440
5441 verifiedEvent.flags += 1;
5442 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5443
5444 verifiedEvent.keyCode += 1;
5445 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5446
5447 verifiedEvent.scanCode += 1;
5448 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5449
5450 verifiedEvent.metaState += 1;
5451 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5452
5453 verifiedEvent.repeatCount += 1;
5454 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5455}
5456
Vishnu Nair958da932020-08-21 17:12:37 -07005457TEST_F(InputDispatcherTest, SetFocusedWindow) {
5458 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5459 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005460 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005461 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005462 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005463 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5464
5465 // Top window is also focusable but is not granted focus.
5466 windowTop->setFocusable(true);
5467 windowSecond->setFocusable(true);
5468 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
5469 setFocusedWindow(windowSecond);
5470
5471 windowSecond->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005472 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5473 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005474
5475 // Focused window should receive event.
5476 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
5477 windowTop->assertNoEvents();
5478}
5479
5480TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) {
5481 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5482 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005483 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005484 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5485
5486 window->setFocusable(true);
5487 // Release channel for window is no longer valid.
5488 window->releaseChannel();
5489 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5490 setFocusedWindow(window);
5491
5492 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005493 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
5494 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07005495
5496 // window channel is invalid, so it should not receive any input event.
5497 window->assertNoEvents();
5498}
5499
5500TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) {
5501 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5502 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005503 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08005504 window->setFocusable(false);
Vishnu Nair958da932020-08-21 17:12:37 -07005505 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5506
Vishnu Nair958da932020-08-21 17:12:37 -07005507 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5508 setFocusedWindow(window);
5509
5510 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005511 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
5512 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07005513
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08005514 // window is not focusable, so it should not receive any input event.
Vishnu Nair958da932020-08-21 17:12:37 -07005515 window->assertNoEvents();
5516}
5517
5518TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) {
5519 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5520 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005521 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005522 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005523 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005524 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5525
5526 windowTop->setFocusable(true);
5527 windowSecond->setFocusable(true);
5528 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
5529 setFocusedWindow(windowTop);
5530 windowTop->consumeFocusEvent(true);
5531
Chavi Weingarten847e8512023-03-29 00:26:09 +00005532 windowTop->editInfo()->focusTransferTarget = windowSecond->getToken();
5533 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005534 windowSecond->consumeFocusEvent(true);
5535 windowTop->consumeFocusEvent(false);
5536
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005537 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5538 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005539
5540 // Focused window should receive event.
5541 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
5542}
5543
Chavi Weingarten847e8512023-03-29 00:26:09 +00005544TEST_F(InputDispatcherTest, SetFocusedWindow_TransferFocusTokenNotFocusable) {
Vishnu Nair958da932020-08-21 17:12:37 -07005545 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5546 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005547 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005548 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005549 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005550 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5551
5552 windowTop->setFocusable(true);
Chavi Weingarten847e8512023-03-29 00:26:09 +00005553 windowSecond->setFocusable(false);
5554 windowTop->editInfo()->focusTransferTarget = windowSecond->getToken();
Vishnu Nair958da932020-08-21 17:12:37 -07005555 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Chavi Weingarten847e8512023-03-29 00:26:09 +00005556 setFocusedWindow(windowTop);
5557 windowTop->consumeFocusEvent(true);
Vishnu Nair958da932020-08-21 17:12:37 -07005558
Chavi Weingarten847e8512023-03-29 00:26:09 +00005559 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5560 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005561
5562 // Event should be dropped.
Chavi Weingarten847e8512023-03-29 00:26:09 +00005563 windowTop->consumeKeyDown(ADISPLAY_ID_NONE);
Vishnu Nair958da932020-08-21 17:12:37 -07005564 windowSecond->assertNoEvents();
5565}
5566
5567TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) {
5568 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5569 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005570 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005571 sp<FakeWindowHandle> previousFocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005572 sp<FakeWindowHandle>::make(application, mDispatcher, "previousFocusedWindow",
5573 ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005574 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5575
5576 window->setFocusable(true);
5577 previousFocusedWindow->setFocusable(true);
5578 window->setVisible(false);
5579 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}});
5580 setFocusedWindow(previousFocusedWindow);
5581 previousFocusedWindow->consumeFocusEvent(true);
5582
5583 // Requesting focus on invisible window takes focus from currently focused window.
5584 setFocusedWindow(window);
5585 previousFocusedWindow->consumeFocusEvent(false);
5586
5587 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005588 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00005589 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
5590 InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07005591
5592 // Window does not get focus event or key down.
5593 window->assertNoEvents();
5594
5595 // Window becomes visible.
5596 window->setVisible(true);
5597 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5598
5599 // Window receives focus event.
5600 window->consumeFocusEvent(true);
5601 // Focused window receives key down.
5602 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5603}
5604
Vishnu Nair599f1412021-06-21 10:39:58 -07005605TEST_F(InputDispatcherTest, DisplayRemoved) {
5606 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5607 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005608 sp<FakeWindowHandle>::make(application, mDispatcher, "window", ADISPLAY_ID_DEFAULT);
Vishnu Nair599f1412021-06-21 10:39:58 -07005609 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5610
5611 // window is granted focus.
5612 window->setFocusable(true);
5613 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5614 setFocusedWindow(window);
5615 window->consumeFocusEvent(true);
5616
5617 // When a display is removed window loses focus.
5618 mDispatcher->displayRemoved(ADISPLAY_ID_DEFAULT);
5619 window->consumeFocusEvent(false);
5620}
5621
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005622/**
5623 * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY,
5624 * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top
5625 * of the 'slipperyEnterWindow'.
5626 *
5627 * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such
5628 * a way so that the touched location is no longer covered by the top window.
5629 *
5630 * Next, inject a MOVE event. Because the top window already moved earlier, this event is now
5631 * positioned over the bottom (slipperyEnterWindow) only. And because the top window had
5632 * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive
5633 * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting
5634 * with ACTION_DOWN).
5635 * Thus, the touch has been transferred from the top window into the bottom window, because the top
5636 * window moved itself away from the touched location and had Flag::SLIPPERY.
5637 *
5638 * Even though the top window moved away from the touched location, it is still obscuring the bottom
5639 * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_
5640 * OBSCURED should be set for the MotionEvent that reaches the bottom window.
5641 *
5642 * In this test, we ensure that the event received by the bottom window has
5643 * FLAG_WINDOW_IS_PARTIALLY_OBSCURED.
5644 */
5645TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) {
Prabir Pradhan5735a322022-04-11 17:23:34 +00005646 constexpr int32_t SLIPPERY_PID = WINDOW_PID + 1;
5647 constexpr int32_t SLIPPERY_UID = WINDOW_UID + 1;
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005648
5649 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5650 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5651
5652 sp<FakeWindowHandle> slipperyExitWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005653 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08005654 slipperyExitWindow->setSlippery(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005655 // Make sure this one overlaps the bottom window
5656 slipperyExitWindow->setFrame(Rect(25, 25, 75, 75));
5657 // Change the owner uid/pid of the window so that it is considered to be occluding the bottom
5658 // one. Windows with the same owner are not considered to be occluding each other.
5659 slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID);
5660
5661 sp<FakeWindowHandle> slipperyEnterWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005662 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005663 slipperyExitWindow->setFrame(Rect(0, 0, 100, 100));
5664
5665 mDispatcher->setInputWindows(
5666 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
5667
5668 // Use notifyMotion instead of injecting to avoid dealing with injection permissions
Prabir Pradhan678438e2023-04-13 19:32:51 +00005669 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5670 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5671 {{50, 50}}));
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005672 slipperyExitWindow->consumeMotionDown();
5673 slipperyExitWindow->setFrame(Rect(70, 70, 100, 100));
5674 mDispatcher->setInputWindows(
5675 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
5676
Prabir Pradhan678438e2023-04-13 19:32:51 +00005677 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_MOVE,
5678 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5679 {{51, 51}}));
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005680
5681 slipperyExitWindow->consumeMotionCancel();
5682
5683 slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT,
5684 AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
5685}
5686
Siarhei Vishniakouafa08cc2023-05-08 22:35:50 -07005687/**
5688 * Two windows, one on the left and another on the right. The left window is slippery. The right
5689 * window isn't eligible to receive touch because it specifies InputConfig::DROP_INPUT. When the
5690 * touch moves from the left window into the right window, the gesture should continue to go to the
5691 * left window. Touch shouldn't slip because the right window can't receive touches. This test
5692 * reproduces a crash.
5693 */
5694TEST_F(InputDispatcherTest, TouchSlippingIntoWindowThatDropsTouches) {
5695 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5696
5697 sp<FakeWindowHandle> leftSlipperyWindow =
5698 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
5699 leftSlipperyWindow->setSlippery(true);
5700 leftSlipperyWindow->setFrame(Rect(0, 0, 100, 100));
5701
5702 sp<FakeWindowHandle> rightDropTouchesWindow =
5703 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
5704 rightDropTouchesWindow->setFrame(Rect(100, 0, 200, 100));
5705 rightDropTouchesWindow->setDropInput(true);
5706
5707 mDispatcher->setInputWindows(
5708 {{ADISPLAY_ID_DEFAULT, {leftSlipperyWindow, rightDropTouchesWindow}}});
5709
5710 // Start touch in the left window
5711 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
5712 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
5713 .build());
5714 leftSlipperyWindow->consumeMotionDown();
5715
5716 // And move it into the right window
5717 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
5718 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50))
5719 .build());
5720
5721 // Since the right window isn't eligible to receive input, touch does not slip.
5722 // The left window continues to receive the gesture.
5723 leftSlipperyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
5724 rightDropTouchesWindow->assertNoEvents();
5725}
5726
Garfield Tan1c7bc862020-01-28 13:24:04 -08005727class InputDispatcherKeyRepeatTest : public InputDispatcherTest {
5728protected:
5729 static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms
5730 static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms
5731
Chris Yea209fde2020-07-22 13:54:51 -07005732 std::shared_ptr<FakeApplicationHandle> mApp;
Garfield Tan1c7bc862020-01-28 13:24:04 -08005733 sp<FakeWindowHandle> mWindow;
5734
5735 virtual void SetUp() override {
Prabir Pradhana41d2442023-04-20 21:30:40 +00005736 mFakePolicy = std::make_unique<FakeInputDispatcherPolicy>();
Garfield Tan1c7bc862020-01-28 13:24:04 -08005737 mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY);
Prabir Pradhana41d2442023-04-20 21:30:40 +00005738 mDispatcher = std::make_unique<InputDispatcher>(*mFakePolicy);
Prabir Pradhan87112a72023-04-20 19:13:39 +00005739 mDispatcher->requestRefreshConfiguration();
Garfield Tan1c7bc862020-01-28 13:24:04 -08005740 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
5741 ASSERT_EQ(OK, mDispatcher->start());
5742
5743 setUpWindow();
5744 }
5745
5746 void setUpWindow() {
Chris Yea209fde2020-07-22 13:54:51 -07005747 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005748 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005749
Vishnu Nair47074b82020-08-14 11:54:47 -07005750 mWindow->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005751 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005752 setFocusedWindow(mWindow);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005753 mWindow->consumeFocusEvent(true);
5754 }
5755
Chris Ye2ad95392020-09-01 13:44:44 -07005756 void sendAndConsumeKeyDown(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08005757 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07005758 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08005759 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event
Prabir Pradhan678438e2023-04-13 19:32:51 +00005760 mDispatcher->notifyKey(keyArgs);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005761
5762 // Window should receive key down event.
5763 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5764 }
5765
5766 void expectKeyRepeatOnce(int32_t repeatCount) {
5767 SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount));
5768 InputEvent* repeatEvent = mWindow->consume();
5769 ASSERT_NE(nullptr, repeatEvent);
5770
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005771 ASSERT_EQ(InputEventType::KEY, repeatEvent->getType());
Garfield Tan1c7bc862020-01-28 13:24:04 -08005772
5773 KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent);
5774 uint32_t eventAction = repeatKeyEvent->getAction();
5775 EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction);
5776 EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount());
5777 }
5778
Chris Ye2ad95392020-09-01 13:44:44 -07005779 void sendAndConsumeKeyUp(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08005780 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07005781 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08005782 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event
Prabir Pradhan678438e2023-04-13 19:32:51 +00005783 mDispatcher->notifyKey(keyArgs);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005784
5785 // Window should receive key down event.
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005786 mWindow->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00005787 /*expectedFlags=*/0);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005788 }
5789};
5790
5791TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) {
Harry Cutts33476232023-01-30 19:57:29 +00005792 sendAndConsumeKeyDown(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005793 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5794 expectKeyRepeatOnce(repeatCount);
5795 }
5796}
5797
5798TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) {
Harry Cutts33476232023-01-30 19:57:29 +00005799 sendAndConsumeKeyDown(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005800 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5801 expectKeyRepeatOnce(repeatCount);
5802 }
Harry Cutts33476232023-01-30 19:57:29 +00005803 sendAndConsumeKeyDown(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07005804 /* repeatCount will start from 1 for deviceId 2 */
Garfield Tan1c7bc862020-01-28 13:24:04 -08005805 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5806 expectKeyRepeatOnce(repeatCount);
5807 }
5808}
5809
5810TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) {
Harry Cutts33476232023-01-30 19:57:29 +00005811 sendAndConsumeKeyDown(/*deviceId=*/1);
5812 expectKeyRepeatOnce(/*repeatCount=*/1);
5813 sendAndConsumeKeyUp(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005814 mWindow->assertNoEvents();
5815}
5816
5817TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) {
Harry Cutts33476232023-01-30 19:57:29 +00005818 sendAndConsumeKeyDown(/*deviceId=*/1);
5819 expectKeyRepeatOnce(/*repeatCount=*/1);
5820 sendAndConsumeKeyDown(/*deviceId=*/2);
5821 expectKeyRepeatOnce(/*repeatCount=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005822 // Stale key up from device 1.
Harry Cutts33476232023-01-30 19:57:29 +00005823 sendAndConsumeKeyUp(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005824 // Device 2 is still down, keep repeating
Harry Cutts33476232023-01-30 19:57:29 +00005825 expectKeyRepeatOnce(/*repeatCount=*/2);
5826 expectKeyRepeatOnce(/*repeatCount=*/3);
Chris Ye2ad95392020-09-01 13:44:44 -07005827 // Device 2 key up
Harry Cutts33476232023-01-30 19:57:29 +00005828 sendAndConsumeKeyUp(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07005829 mWindow->assertNoEvents();
5830}
5831
5832TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) {
Harry Cutts33476232023-01-30 19:57:29 +00005833 sendAndConsumeKeyDown(/*deviceId=*/1);
5834 expectKeyRepeatOnce(/*repeatCount=*/1);
5835 sendAndConsumeKeyDown(/*deviceId=*/2);
5836 expectKeyRepeatOnce(/*repeatCount=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005837 // Device 2 which holds the key repeating goes up, expect the repeating to stop.
Harry Cutts33476232023-01-30 19:57:29 +00005838 sendAndConsumeKeyUp(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07005839 // Device 1 still holds key down, but the repeating was already stopped
Garfield Tan1c7bc862020-01-28 13:24:04 -08005840 mWindow->assertNoEvents();
5841}
5842
liushenxiang42232912021-05-21 20:24:09 +08005843TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) {
5844 sendAndConsumeKeyDown(DEVICE_ID);
Harry Cutts33476232023-01-30 19:57:29 +00005845 expectKeyRepeatOnce(/*repeatCount=*/1);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005846 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
liushenxiang42232912021-05-21 20:24:09 +08005847 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT,
5848 AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS);
5849 mWindow->assertNoEvents();
5850}
5851
Garfield Tan1c7bc862020-01-28 13:24:04 -08005852TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) {
Michael Wrighte1cbbd62023-02-22 19:32:13 +00005853 GTEST_SKIP() << "Flaky test (b/270393106)";
Harry Cutts33476232023-01-30 19:57:29 +00005854 sendAndConsumeKeyDown(/*deviceId=*/1);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005855 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5856 InputEvent* repeatEvent = mWindow->consume();
5857 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
5858 EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER,
5859 IdGenerator::getSource(repeatEvent->getId()));
5860 }
5861}
5862
5863TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) {
Michael Wrighte1cbbd62023-02-22 19:32:13 +00005864 GTEST_SKIP() << "Flaky test (b/270393106)";
Harry Cutts33476232023-01-30 19:57:29 +00005865 sendAndConsumeKeyDown(/*deviceId=*/1);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005866
5867 std::unordered_set<int32_t> idSet;
5868 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5869 InputEvent* repeatEvent = mWindow->consume();
5870 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
5871 int32_t id = repeatEvent->getId();
5872 EXPECT_EQ(idSet.end(), idSet.find(id));
5873 idSet.insert(id);
5874 }
5875}
5876
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005877/* Test InputDispatcher for MultiDisplay */
5878class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest {
5879public:
Prabir Pradhan3608aad2019-10-02 17:08:26 -07005880 virtual void SetUp() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005881 InputDispatcherTest::SetUp();
Arthur Hungb92218b2018-08-14 12:00:21 +08005882
Chris Yea209fde2020-07-22 13:54:51 -07005883 application1 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005884 windowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005885 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005886
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005887 // Set focus window for primary display, but focused display would be second one.
5888 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1);
Vishnu Nair47074b82020-08-14 11:54:47 -07005889 windowInPrimary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005890 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005891 setFocusedWindow(windowInPrimary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005892 windowInPrimary->consumeFocusEvent(true);
Arthur Hungb92218b2018-08-14 12:00:21 +08005893
Chris Yea209fde2020-07-22 13:54:51 -07005894 application2 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005895 windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005896 sp<FakeWindowHandle>::make(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005897 // Set focus to second display window.
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005898 // Set focus display to second one.
5899 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
5900 // Set focus window for second display.
5901 mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2);
Vishnu Nair47074b82020-08-14 11:54:47 -07005902 windowInSecondary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005903 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005904 setFocusedWindow(windowInSecondary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005905 windowInSecondary->consumeFocusEvent(true);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005906 }
5907
Prabir Pradhan3608aad2019-10-02 17:08:26 -07005908 virtual void TearDown() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005909 InputDispatcherTest::TearDown();
5910
Chris Yea209fde2020-07-22 13:54:51 -07005911 application1.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005912 windowInPrimary.clear();
Chris Yea209fde2020-07-22 13:54:51 -07005913 application2.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005914 windowInSecondary.clear();
5915 }
5916
5917protected:
Chris Yea209fde2020-07-22 13:54:51 -07005918 std::shared_ptr<FakeApplicationHandle> application1;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005919 sp<FakeWindowHandle> windowInPrimary;
Chris Yea209fde2020-07-22 13:54:51 -07005920 std::shared_ptr<FakeApplicationHandle> application2;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005921 sp<FakeWindowHandle> windowInSecondary;
5922};
5923
5924TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) {
5925 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005926 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5927 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
5928 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005929 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08005930 windowInSecondary->assertNoEvents();
5931
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005932 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005933 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5934 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
5935 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08005936 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005937 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +08005938}
5939
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005940TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) {
Tiger Huang721e26f2018-07-24 22:26:19 +08005941 // Test inject a key down with display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005942 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5943 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005944 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005945 windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Tiger Huang721e26f2018-07-24 22:26:19 +08005946 windowInSecondary->assertNoEvents();
5947
5948 // Test inject a key down without display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005949 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005950 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08005951 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005952 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hungb92218b2018-08-14 12:00:21 +08005953
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005954 // Remove all windows in secondary display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00005955 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}});
Arthur Hungb92218b2018-08-14 12:00:21 +08005956
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005957 // Old focus should receive a cancel event.
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005958 windowInSecondary->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE,
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005959 AKEY_EVENT_FLAG_CANCELED);
Arthur Hungb92218b2018-08-14 12:00:21 +08005960
5961 // Test inject a key down, should timeout because of no target window.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005962 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005963 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Arthur Hungb92218b2018-08-14 12:00:21 +08005964 windowInPrimary->assertNoEvents();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005965 windowInSecondary->consumeFocusEvent(false);
Arthur Hungb92218b2018-08-14 12:00:21 +08005966 windowInSecondary->assertNoEvents();
5967}
5968
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005969// Test per-display input monitors for motion event.
5970TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) {
chaviwd1c23182019-12-20 18:44:56 -08005971 FakeMonitorReceiver monitorInPrimary =
5972 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
5973 FakeMonitorReceiver monitorInSecondary =
5974 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005975
5976 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005977 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5978 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
5979 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005980 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08005981 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005982 windowInSecondary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005983 monitorInSecondary.assertNoEvents();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005984
5985 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005986 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5987 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
5988 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005989 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005990 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005991 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
chaviwd1c23182019-12-20 18:44:56 -08005992 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005993
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08005994 // Lift up the touch from the second display
5995 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5996 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
5997 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5998 windowInSecondary->consumeMotionUp(SECOND_DISPLAY_ID);
5999 monitorInSecondary.consumeMotionUp(SECOND_DISPLAY_ID);
6000
Arthur Hung2fbf37f2018-09-13 18:16:41 +08006001 // Test inject a non-pointer motion event.
6002 // If specific a display, it will dispatch to the focused window of particular display,
6003 // or it will dispatch to the focused window of focused display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006004 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6005 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE))
6006 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08006007 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08006008 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08006009 windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08006010 monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08006011}
6012
6013// Test per-display input monitors for key event.
6014TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) {
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006015 // Input monitor per display.
chaviwd1c23182019-12-20 18:44:56 -08006016 FakeMonitorReceiver monitorInPrimary =
6017 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
6018 FakeMonitorReceiver monitorInSecondary =
6019 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08006020
6021 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006022 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
6023 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08006024 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08006025 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08006026 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08006027 monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08006028}
6029
Vishnu Nair958da932020-08-21 17:12:37 -07006030TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) {
6031 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006032 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07006033 secondWindowInPrimary->setFocusable(true);
6034 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}});
6035 setFocusedWindow(secondWindowInPrimary);
6036 windowInPrimary->consumeFocusEvent(false);
6037 secondWindowInPrimary->consumeFocusEvent(true);
6038
6039 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006040 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
6041 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07006042 windowInPrimary->assertNoEvents();
6043 windowInSecondary->assertNoEvents();
6044 secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
6045}
6046
Arthur Hungdfd528e2021-12-08 13:23:04 +00006047TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CancelTouch_MultiDisplay) {
6048 FakeMonitorReceiver monitorInPrimary =
6049 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
6050 FakeMonitorReceiver monitorInSecondary =
6051 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
6052
6053 // Test touch down on primary display.
6054 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6055 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
6056 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6057 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6058 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
6059
6060 // Test touch down on second display.
6061 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6062 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
6063 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6064 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
6065 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
6066
6067 // Trigger cancel touch.
6068 mDispatcher->cancelCurrentTouch();
6069 windowInPrimary->consumeMotionCancel(ADISPLAY_ID_DEFAULT);
6070 monitorInPrimary.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
6071 windowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID);
6072 monitorInSecondary.consumeMotionCancel(SECOND_DISPLAY_ID);
6073
6074 // Test inject a move motion event, no window/monitor should receive the event.
6075 ASSERT_EQ(InputEventInjectionResult::FAILED,
6076 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6077 ADISPLAY_ID_DEFAULT, {110, 200}))
6078 << "Inject motion event should return InputEventInjectionResult::FAILED";
6079 windowInPrimary->assertNoEvents();
6080 monitorInPrimary.assertNoEvents();
6081
6082 ASSERT_EQ(InputEventInjectionResult::FAILED,
6083 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6084 SECOND_DISPLAY_ID, {110, 200}))
6085 << "Inject motion event should return InputEventInjectionResult::FAILED";
6086 windowInSecondary->assertNoEvents();
6087 monitorInSecondary.assertNoEvents();
6088}
6089
Jackal Guof9696682018-10-05 12:23:23 +08006090class InputFilterTest : public InputDispatcherTest {
6091protected:
Prabir Pradhan81420cc2021-09-06 10:28:50 -07006092 void testNotifyMotion(int32_t displayId, bool expectToBeFiltered,
6093 const ui::Transform& transform = ui::Transform()) {
Jackal Guof9696682018-10-05 12:23:23 +08006094 NotifyMotionArgs motionArgs;
6095
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006096 motionArgs =
6097 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Prabir Pradhan678438e2023-04-13 19:32:51 +00006098 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006099 motionArgs =
6100 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Prabir Pradhan678438e2023-04-13 19:32:51 +00006101 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006102 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08006103 if (expectToBeFiltered) {
Prabir Pradhan81420cc2021-09-06 10:28:50 -07006104 const auto xy = transform.transform(motionArgs.pointerCoords->getXYValue());
6105 mFakePolicy->assertFilterInputEventWasCalled(motionArgs, xy);
Jackal Guof9696682018-10-05 12:23:23 +08006106 } else {
6107 mFakePolicy->assertFilterInputEventWasNotCalled();
6108 }
6109 }
6110
6111 void testNotifyKey(bool expectToBeFiltered) {
6112 NotifyKeyArgs keyArgs;
6113
6114 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
Prabir Pradhan678438e2023-04-13 19:32:51 +00006115 mDispatcher->notifyKey(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08006116 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP);
Prabir Pradhan678438e2023-04-13 19:32:51 +00006117 mDispatcher->notifyKey(keyArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006118 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08006119
6120 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08006121 mFakePolicy->assertFilterInputEventWasCalled(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08006122 } else {
6123 mFakePolicy->assertFilterInputEventWasNotCalled();
6124 }
6125 }
6126};
6127
6128// Test InputFilter for MotionEvent
6129TEST_F(InputFilterTest, MotionEvent_InputFilter) {
6130 // Since the InputFilter is disabled by default, check if touch events aren't filtered.
6131 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
6132 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
6133
6134 // Enable InputFilter
6135 mDispatcher->setInputFilterEnabled(true);
6136 // Test touch on both primary and second display, and check if both events are filtered.
6137 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true);
6138 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true);
6139
6140 // Disable InputFilter
6141 mDispatcher->setInputFilterEnabled(false);
6142 // Test touch on both primary and second display, and check if both events aren't filtered.
6143 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
6144 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
6145}
6146
6147// Test InputFilter for KeyEvent
6148TEST_F(InputFilterTest, KeyEvent_InputFilter) {
6149 // Since the InputFilter is disabled by default, check if key event aren't filtered.
6150 testNotifyKey(/*expectToBeFiltered*/ false);
6151
6152 // Enable InputFilter
6153 mDispatcher->setInputFilterEnabled(true);
6154 // Send a key event, and check if it is filtered.
6155 testNotifyKey(/*expectToBeFiltered*/ true);
6156
6157 // Disable InputFilter
6158 mDispatcher->setInputFilterEnabled(false);
6159 // Send a key event, and check if it isn't filtered.
6160 testNotifyKey(/*expectToBeFiltered*/ false);
6161}
6162
Prabir Pradhan81420cc2021-09-06 10:28:50 -07006163// Ensure that MotionEvents sent to the InputFilter through InputListener are converted to the
6164// logical display coordinate space.
6165TEST_F(InputFilterTest, MotionEvent_UsesLogicalDisplayCoordinates_notifyMotion) {
6166 ui::Transform firstDisplayTransform;
6167 firstDisplayTransform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
6168 ui::Transform secondDisplayTransform;
6169 secondDisplayTransform.set({-6.6, -5.5, -4.4, -3.3, -2.2, -1.1, 0, 0, 1});
6170
6171 std::vector<gui::DisplayInfo> displayInfos(2);
6172 displayInfos[0].displayId = ADISPLAY_ID_DEFAULT;
6173 displayInfos[0].transform = firstDisplayTransform;
6174 displayInfos[1].displayId = SECOND_DISPLAY_ID;
6175 displayInfos[1].transform = secondDisplayTransform;
6176
Patrick Williamsd828f302023-04-28 17:52:08 -05006177 mDispatcher->onWindowInfosChanged({{}, displayInfos, 0, 0});
Prabir Pradhan81420cc2021-09-06 10:28:50 -07006178
6179 // Enable InputFilter
6180 mDispatcher->setInputFilterEnabled(true);
6181
6182 // Ensure the correct transforms are used for the displays.
6183 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true, firstDisplayTransform);
6184 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true, secondDisplayTransform);
6185}
6186
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006187class InputFilterInjectionPolicyTest : public InputDispatcherTest {
6188protected:
6189 virtual void SetUp() override {
6190 InputDispatcherTest::SetUp();
6191
6192 /**
6193 * We don't need to enable input filter to test the injected event policy, but we enabled it
6194 * here to make the tests more realistic, since this policy only matters when inputfilter is
6195 * on.
6196 */
6197 mDispatcher->setInputFilterEnabled(true);
6198
6199 std::shared_ptr<InputApplicationHandle> application =
6200 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006201 mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Test Window",
6202 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006203
6204 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
6205 mWindow->setFocusable(true);
6206 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6207 setFocusedWindow(mWindow);
6208 mWindow->consumeFocusEvent(true);
6209 }
6210
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006211 void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
6212 int32_t flags) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006213 KeyEvent event;
6214
6215 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
6216 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD,
6217 ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A,
Harry Cutts33476232023-01-30 19:57:29 +00006218 KEY_A, AMETA_NONE, /*repeatCount=*/0, eventTime, eventTime);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006219 const int32_t additionalPolicyFlags =
6220 POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT;
6221 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00006222 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006223 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
6224 policyFlags | additionalPolicyFlags));
6225
6226 InputEvent* received = mWindow->consume();
6227 ASSERT_NE(nullptr, received);
6228 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07006229 ASSERT_EQ(received->getType(), InputEventType::KEY);
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006230 KeyEvent& keyEvent = static_cast<KeyEvent&>(*received);
6231 ASSERT_EQ(flags, keyEvent.getFlags());
6232 }
6233
6234 void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
6235 int32_t flags) {
6236 MotionEvent event;
6237 PointerProperties pointerProperties[1];
6238 PointerCoords pointerCoords[1];
6239 pointerProperties[0].clear();
6240 pointerProperties[0].id = 0;
6241 pointerCoords[0].clear();
6242 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300);
6243 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400);
6244
6245 ui::Transform identityTransform;
6246 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
6247 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN,
6248 DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0,
6249 AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE,
6250 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -07006251 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, eventTime,
Evan Rosky09576692021-07-01 12:22:09 -07006252 eventTime,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006253 /*pointerCount*/ 1, pointerProperties, pointerCoords);
6254
6255 const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER;
6256 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00006257 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006258 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
6259 policyFlags | additionalPolicyFlags));
6260
6261 InputEvent* received = mWindow->consume();
6262 ASSERT_NE(nullptr, received);
6263 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07006264 ASSERT_EQ(received->getType(), InputEventType::MOTION);
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006265 MotionEvent& motionEvent = static_cast<MotionEvent&>(*received);
6266 ASSERT_EQ(flags, motionEvent.getFlags());
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006267 }
6268
6269private:
6270 sp<FakeWindowHandle> mWindow;
6271};
6272
6273TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) {
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006274 // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input
6275 // filter. Without it, the event will no different from a regularly injected event, and the
6276 // injected device id will be overwritten.
Harry Cutts33476232023-01-30 19:57:29 +00006277 testInjectedKey(POLICY_FLAG_FILTERED, /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
6278 /*flags=*/0);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006279}
6280
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006281TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006282 testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Harry Cutts33476232023-01-30 19:57:29 +00006283 /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006284 AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
6285}
6286
6287TEST_F(InputFilterInjectionPolicyTest,
6288 MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
6289 testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Harry Cutts33476232023-01-30 19:57:29 +00006290 /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006291 AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006292}
6293
6294TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) {
Harry Cutts33476232023-01-30 19:57:29 +00006295 testInjectedKey(/*policyFlags=*/0, /*injectedDeviceId=*/3,
6296 /*resolvedDeviceId=*/VIRTUAL_KEYBOARD_ID, /*flags=*/0);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006297}
6298
chaviwfd6d3512019-03-25 13:23:49 -07006299class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest {
Prabir Pradhan3608aad2019-10-02 17:08:26 -07006300 virtual void SetUp() override {
chaviwfd6d3512019-03-25 13:23:49 -07006301 InputDispatcherTest::SetUp();
6302
Chris Yea209fde2020-07-22 13:54:51 -07006303 std::shared_ptr<FakeApplicationHandle> application =
6304 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006305 mUnfocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006306 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07006307 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
chaviwfd6d3512019-03-25 13:23:49 -07006308
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006309 mFocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006310 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006311 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
chaviwfd6d3512019-03-25 13:23:49 -07006312
6313 // Set focused application.
6314 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07006315 mFocusedWindow->setFocusable(true);
chaviwfd6d3512019-03-25 13:23:49 -07006316
6317 // Expect one focus window exist in display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00006318 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07006319 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006320 mFocusedWindow->consumeFocusEvent(true);
chaviwfd6d3512019-03-25 13:23:49 -07006321 }
6322
Prabir Pradhan3608aad2019-10-02 17:08:26 -07006323 virtual void TearDown() override {
chaviwfd6d3512019-03-25 13:23:49 -07006324 InputDispatcherTest::TearDown();
6325
6326 mUnfocusedWindow.clear();
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006327 mFocusedWindow.clear();
chaviwfd6d3512019-03-25 13:23:49 -07006328 }
6329
6330protected:
6331 sp<FakeWindowHandle> mUnfocusedWindow;
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006332 sp<FakeWindowHandle> mFocusedWindow;
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07006333 static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60};
chaviwfd6d3512019-03-25 13:23:49 -07006334};
6335
6336// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
6337// DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received
6338// the onPointerDownOutsideFocus callback.
6339TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006340 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07006341 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6342 {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006343 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006344 mUnfocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07006345
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006346 ASSERT_TRUE(mDispatcher->waitForIdle());
chaviwfd6d3512019-03-25 13:23:49 -07006347 mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken());
6348}
6349
6350// Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action
6351// DOWN on the window that doesn't have focus. Ensure no window received the
6352// onPointerDownOutsideFocus callback.
6353TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006354 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07006355 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006356 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006357 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07006358
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006359 ASSERT_TRUE(mDispatcher->waitForIdle());
6360 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07006361}
6362
6363// Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't
6364// have focus. Ensure no window received the onPointerDownOutsideFocus callback.
6365TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08006366 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6367 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006368 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006369 mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07006370
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006371 ASSERT_TRUE(mDispatcher->waitForIdle());
6372 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07006373}
6374
6375// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
6376// DOWN on the window that already has focus. Ensure no window received the
6377// onPointerDownOutsideFocus callback.
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006378TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006379 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006380 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07006381 FOCUSED_WINDOW_TOUCH_POINT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006382 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006383 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07006384
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006385 ASSERT_TRUE(mDispatcher->waitForIdle());
6386 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07006387}
6388
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08006389// Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag
6390// NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback.
6391TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) {
6392 const MotionEvent event =
6393 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
6394 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006395 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(20).y(20))
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08006396 .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE)
6397 .build();
6398 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(mDispatcher, event))
6399 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6400 mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
6401
6402 ASSERT_TRUE(mDispatcher->waitForIdle());
6403 mFakePolicy->assertOnPointerDownWasNotCalled();
6404 // Ensure that the unfocused window did not receive any FOCUS events.
6405 mUnfocusedWindow->assertNoEvents();
6406}
6407
chaviwaf87b3e2019-10-01 16:59:28 -07006408// These tests ensures we can send touch events to a single client when there are multiple input
6409// windows that point to the same client token.
6410class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest {
6411 virtual void SetUp() override {
6412 InputDispatcherTest::SetUp();
6413
Chris Yea209fde2020-07-22 13:54:51 -07006414 std::shared_ptr<FakeApplicationHandle> application =
6415 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006416 mWindow1 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 1",
6417 ADISPLAY_ID_DEFAULT);
chaviwaf87b3e2019-10-01 16:59:28 -07006418 mWindow1->setFrame(Rect(0, 0, 100, 100));
6419
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006420 mWindow2 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 2",
6421 ADISPLAY_ID_DEFAULT, mWindow1->getToken());
chaviwaf87b3e2019-10-01 16:59:28 -07006422 mWindow2->setFrame(Rect(100, 100, 200, 200));
6423
Arthur Hung72d8dc32020-03-28 00:48:39 +00006424 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
chaviwaf87b3e2019-10-01 16:59:28 -07006425 }
6426
6427protected:
6428 sp<FakeWindowHandle> mWindow1;
6429 sp<FakeWindowHandle> mWindow2;
6430
6431 // Helper function to convert the point from screen coordinates into the window's space
chaviw3277faf2021-05-19 16:45:23 -05006432 static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) {
chaviw1ff3d1e2020-07-01 15:53:47 -07006433 vec2 vals = windowInfo->transform.transform(point.x, point.y);
6434 return {vals.x, vals.y};
chaviwaf87b3e2019-10-01 16:59:28 -07006435 }
6436
6437 void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction,
6438 const std::vector<PointF>& points) {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006439 const std::string name = window->getName();
chaviwaf87b3e2019-10-01 16:59:28 -07006440 InputEvent* event = window->consume();
6441
6442 ASSERT_NE(nullptr, event) << name.c_str()
6443 << ": consumer should have returned non-NULL event.";
6444
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07006445 ASSERT_EQ(InputEventType::MOTION, event->getType())
6446 << name.c_str() << ": expected MotionEvent, got " << *event;
chaviwaf87b3e2019-10-01 16:59:28 -07006447
6448 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006449 assertMotionAction(expectedAction, motionEvent.getAction());
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08006450 ASSERT_EQ(points.size(), motionEvent.getPointerCount());
chaviwaf87b3e2019-10-01 16:59:28 -07006451
6452 for (size_t i = 0; i < points.size(); i++) {
6453 float expectedX = points[i].x;
6454 float expectedY = points[i].y;
6455
6456 EXPECT_EQ(expectedX, motionEvent.getX(i))
6457 << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str()
6458 << ", got " << motionEvent.getX(i);
6459 EXPECT_EQ(expectedY, motionEvent.getY(i))
6460 << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str()
6461 << ", got " << motionEvent.getY(i);
6462 }
6463 }
chaviw9eaa22c2020-07-01 16:21:27 -07006464
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08006465 void touchAndAssertPositions(int32_t action, const std::vector<PointF>& touchedPoints,
chaviw9eaa22c2020-07-01 16:21:27 -07006466 std::vector<PointF> expectedPoints) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00006467 mDispatcher->notifyMotion(generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN,
6468 ADISPLAY_ID_DEFAULT, touchedPoints));
chaviw9eaa22c2020-07-01 16:21:27 -07006469
6470 // Always consume from window1 since it's the window that has the InputReceiver
6471 consumeMotionEvent(mWindow1, action, expectedPoints);
6472 }
chaviwaf87b3e2019-10-01 16:59:28 -07006473};
6474
6475TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) {
6476 // Touch Window 1
6477 PointF touchedPoint = {10, 10};
6478 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006479 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006480
6481 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07006482 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006483
6484 // Touch Window 2
6485 touchedPoint = {150, 150};
6486 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006487 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006488}
6489
chaviw9eaa22c2020-07-01 16:21:27 -07006490TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) {
6491 // Set scale value for window2
chaviwaf87b3e2019-10-01 16:59:28 -07006492 mWindow2->setWindowScale(0.5f, 0.5f);
6493
6494 // Touch Window 1
6495 PointF touchedPoint = {10, 10};
6496 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006497 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006498 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07006499 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006500
6501 // Touch Window 2
6502 touchedPoint = {150, 150};
6503 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006504 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
6505 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006506
chaviw9eaa22c2020-07-01 16:21:27 -07006507 // Update the transform so rotation is set
6508 mWindow2->setWindowTransform(0, -1, 1, 0);
6509 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
6510 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006511}
6512
chaviw9eaa22c2020-07-01 16:21:27 -07006513TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006514 mWindow2->setWindowScale(0.5f, 0.5f);
6515
6516 // Touch Window 1
6517 std::vector<PointF> touchedPoints = {PointF{10, 10}};
6518 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07006519 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006520
6521 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07006522 touchedPoints.push_back(PointF{150, 150});
6523 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006524 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006525
chaviw9eaa22c2020-07-01 16:21:27 -07006526 // Release Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006527 touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07006528 expectedPoints.pop_back();
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006529
chaviw9eaa22c2020-07-01 16:21:27 -07006530 // Update the transform so rotation is set for Window 2
6531 mWindow2->setWindowTransform(0, -1, 1, 0);
6532 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006533 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006534}
6535
chaviw9eaa22c2020-07-01 16:21:27 -07006536TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006537 mWindow2->setWindowScale(0.5f, 0.5f);
6538
6539 // Touch Window 1
6540 std::vector<PointF> touchedPoints = {PointF{10, 10}};
6541 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07006542 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006543
6544 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07006545 touchedPoints.push_back(PointF{150, 150});
6546 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006547
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006548 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006549
6550 // Move both windows
6551 touchedPoints = {{20, 20}, {175, 175}};
6552 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
6553 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
6554
chaviw9eaa22c2020-07-01 16:21:27 -07006555 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006556
chaviw9eaa22c2020-07-01 16:21:27 -07006557 // Release Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006558 touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07006559 expectedPoints.pop_back();
6560
6561 // Touch Window 2
6562 mWindow2->setWindowTransform(0, -1, 1, 0);
6563 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006564 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07006565
6566 // Move both windows
6567 touchedPoints = {{20, 20}, {175, 175}};
6568 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
6569 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
6570
6571 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006572}
6573
6574TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) {
6575 mWindow1->setWindowScale(0.5f, 0.5f);
6576
6577 // Touch Window 1
6578 std::vector<PointF> touchedPoints = {PointF{10, 10}};
6579 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07006580 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006581
6582 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07006583 touchedPoints.push_back(PointF{150, 150});
6584 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006585
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006586 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006587
6588 // Move both windows
6589 touchedPoints = {{20, 20}, {175, 175}};
6590 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
6591 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
6592
chaviw9eaa22c2020-07-01 16:21:27 -07006593 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006594}
6595
Siarhei Vishniakou0f6558d2023-04-21 12:05:13 -07006596/**
6597 * When one of the windows is slippery, the touch should not slip into the other window with the
6598 * same input channel.
6599 */
6600TEST_F(InputDispatcherMultiWindowSameTokenTests, TouchDoesNotSlipEvenIfSlippery) {
6601 mWindow1->setSlippery(true);
6602 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
6603
6604 // Touch down in window 1
6605 mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6606 ADISPLAY_ID_DEFAULT, {{50, 50}}));
6607 consumeMotionEvent(mWindow1, ACTION_DOWN, {{50, 50}});
6608
6609 // Move touch to be above window 2. Even though window 1 is slippery, touch should not slip.
6610 // That means the gesture should continue normally, without any ACTION_CANCEL or ACTION_DOWN
6611 // getting generated.
6612 mDispatcher->notifyMotion(generateMotionArgs(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6613 ADISPLAY_ID_DEFAULT, {{150, 150}}));
6614
6615 consumeMotionEvent(mWindow1, ACTION_MOVE, {{150, 150}});
6616}
6617
Siarhei Vishniakoud5876ba2023-05-15 17:58:34 -07006618/**
6619 * When hover starts in one window and continues into the other, there should be a HOVER_EXIT and
6620 * a HOVER_ENTER generated, even if the windows have the same token. This is because the new window
6621 * that the pointer is hovering over may have a different transform.
6622 */
6623TEST_F(InputDispatcherMultiWindowSameTokenTests, HoverIntoClone) {
6624 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
6625
6626 // Start hover in window 1
6627 mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_ENTER, AINPUT_SOURCE_TOUCHSCREEN,
6628 ADISPLAY_ID_DEFAULT, {{50, 50}}));
6629 consumeMotionEvent(mWindow1, ACTION_HOVER_ENTER,
6630 {getPointInWindow(mWindow1->getInfo(), PointF{50, 50})});
6631
6632 // Move hover to window 2.
6633 mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6634 ADISPLAY_ID_DEFAULT, {{150, 150}}));
6635
6636 consumeMotionEvent(mWindow1, ACTION_HOVER_EXIT, {{50, 50}});
6637 consumeMotionEvent(mWindow1, ACTION_HOVER_ENTER,
6638 {getPointInWindow(mWindow2->getInfo(), PointF{150, 150})});
6639}
6640
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006641class InputDispatcherSingleWindowAnr : public InputDispatcherTest {
6642 virtual void SetUp() override {
6643 InputDispatcherTest::SetUp();
6644
Chris Yea209fde2020-07-22 13:54:51 -07006645 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006646 mApplication->setDispatchingTimeout(20ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006647 mWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "TestWindow",
6648 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006649 mWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05006650 mWindow->setDispatchingTimeout(30ms);
Vishnu Nair47074b82020-08-14 11:54:47 -07006651 mWindow->setFocusable(true);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006652
6653 // Set focused application.
6654 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
6655
6656 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07006657 setFocusedWindow(mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006658 mWindow->consumeFocusEvent(true);
6659 }
6660
6661 virtual void TearDown() override {
6662 InputDispatcherTest::TearDown();
6663 mWindow.clear();
6664 }
6665
6666protected:
Chris Yea209fde2020-07-22 13:54:51 -07006667 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006668 sp<FakeWindowHandle> mWindow;
6669 static constexpr PointF WINDOW_LOCATION = {20, 20};
6670
6671 void tapOnWindow() {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006672 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006673 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6674 WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006675 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006676 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6677 WINDOW_LOCATION));
6678 }
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006679
6680 sp<FakeWindowHandle> addSpyWindow() {
6681 sp<FakeWindowHandle> spy =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006682 sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006683 spy->setTrustedOverlay(true);
6684 spy->setFocusable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08006685 spy->setSpy(true);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006686 spy->setDispatchingTimeout(30ms);
6687 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, mWindow}}});
6688 return spy;
6689 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006690};
6691
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006692// Send a tap and respond, which should not cause an ANR.
6693TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) {
6694 tapOnWindow();
6695 mWindow->consumeMotionDown();
6696 mWindow->consumeMotionUp();
6697 ASSERT_TRUE(mDispatcher->waitForIdle());
6698 mFakePolicy->assertNotifyAnrWasNotCalled();
6699}
6700
6701// Send a regular key and respond, which should not cause an ANR.
6702TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08006703 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006704 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
6705 ASSERT_TRUE(mDispatcher->waitForIdle());
6706 mFakePolicy->assertNotifyAnrWasNotCalled();
6707}
6708
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05006709TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) {
6710 mWindow->setFocusable(false);
6711 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6712 mWindow->consumeFocusEvent(false);
6713
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006714 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006715 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6716 InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms,
6717 /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006718 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05006719 // Key will not go to window because we have no focused window.
6720 // The 'no focused window' ANR timer should start instead.
6721
6722 // Now, the focused application goes away.
6723 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr);
6724 // The key should get dropped and there should be no ANR.
6725
6726 ASSERT_TRUE(mDispatcher->waitForIdle());
6727 mFakePolicy->assertNotifyAnrWasNotCalled();
6728}
6729
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006730// Send an event to the app and have the app not respond right away.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006731// When ANR is raised, policy will tell the dispatcher to cancel the events for that window.
6732// So InputDispatcher will enqueue ACTION_CANCEL event as well.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006733TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006734 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006735 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6736 WINDOW_LOCATION));
6737
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006738 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN
6739 ASSERT_TRUE(sequenceNum);
6740 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006741 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006742
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006743 mWindow->finishEvent(*sequenceNum);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08006744 mWindow->consumeMotionEvent(
6745 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006746 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006747 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006748}
6749
6750// Send a key to the app and have the app not respond right away.
6751TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) {
6752 // Inject a key, and don't respond - expect that ANR is called.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08006753 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006754 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent();
6755 ASSERT_TRUE(sequenceNum);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006756 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006757 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006758 ASSERT_TRUE(mDispatcher->waitForIdle());
6759}
6760
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006761// We have a focused application, but no focused window
6762TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) {
Vishnu Nair47074b82020-08-14 11:54:47 -07006763 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006764 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6765 mWindow->consumeFocusEvent(false);
6766
6767 // taps on the window work as normal
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006768 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006769 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6770 WINDOW_LOCATION));
6771 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
6772 mDispatcher->waitForIdle();
6773 mFakePolicy->assertNotifyAnrWasNotCalled();
6774
6775 // Once a focused event arrives, we get an ANR for this application
6776 // We specify the injection timeout to be smaller than the application timeout, to ensure that
6777 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006778 const InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006779 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6780 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006781 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006782 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Vishnu Naire4df8752022-09-08 09:17:55 -07006783 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006784 ASSERT_TRUE(mDispatcher->waitForIdle());
6785}
6786
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08006787/**
6788 * Make sure the stale key is dropped before causing an ANR. So even if there's no focused window,
6789 * there will not be an ANR.
6790 */
6791TEST_F(InputDispatcherSingleWindowAnr, StaleKeyEventDoesNotAnr) {
6792 mWindow->setFocusable(false);
6793 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6794 mWindow->consumeFocusEvent(false);
6795
6796 KeyEvent event;
6797 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC) -
6798 std::chrono::nanoseconds(STALE_EVENT_TIMEOUT).count();
6799
6800 // Define a valid key down event that is stale (too old).
6801 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
6802 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, /* flags */ 0, AKEYCODE_A, KEY_A,
Harry Cutts33476232023-01-30 19:57:29 +00006803 AMETA_NONE, /*repeatCount=*/1, eventTime, eventTime);
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08006804
6805 const int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER;
6806
6807 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006808 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08006809 InputEventInjectionSync::WAIT_FOR_RESULT,
6810 INJECT_EVENT_TIMEOUT, policyFlags);
6811 ASSERT_EQ(InputEventInjectionResult::FAILED, result)
6812 << "Injection should fail because the event is stale";
6813
6814 ASSERT_TRUE(mDispatcher->waitForIdle());
6815 mFakePolicy->assertNotifyAnrWasNotCalled();
6816 mWindow->assertNoEvents();
6817}
6818
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006819// We have a focused application, but no focused window
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006820// Make sure that we don't notify policy twice about the same ANR.
6821TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) {
Vishnu Nair47074b82020-08-14 11:54:47 -07006822 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006823 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6824 mWindow->consumeFocusEvent(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006825
6826 // Once a focused event arrives, we get an ANR for this application
6827 // We specify the injection timeout to be smaller than the application timeout, to ensure that
6828 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006829 const InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006830 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6831 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006832 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Vishnu Naire4df8752022-09-08 09:17:55 -07006833 const std::chrono::duration appTimeout =
6834 mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
6835 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006836
Vishnu Naire4df8752022-09-08 09:17:55 -07006837 std::this_thread::sleep_for(appTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006838 // ANR should not be raised again. It is up to policy to do that if it desires.
6839 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006840
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006841 // If we now get a focused window, the ANR should stop, but the policy handles that via
6842 // 'notifyFocusChanged' callback. This is implemented in the policy so we can't test it here.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006843 ASSERT_TRUE(mDispatcher->waitForIdle());
6844}
6845
6846// We have a focused application, but no focused window
6847TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) {
Vishnu Nair47074b82020-08-14 11:54:47 -07006848 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006849 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6850 mWindow->consumeFocusEvent(false);
6851
6852 // Once a focused event arrives, we get an ANR for this application
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006853 const InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006854 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006855 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
6856 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006857
Vishnu Naire4df8752022-09-08 09:17:55 -07006858 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
6859 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006860
6861 // Future focused events get dropped right away
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006862 ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006863 ASSERT_TRUE(mDispatcher->waitForIdle());
6864 mWindow->assertNoEvents();
6865}
6866
6867/**
6868 * Ensure that the implementation is valid. Since we are using multiset to keep track of the
6869 * ANR timeouts, we are allowing entries with identical timestamps in the same connection.
6870 * If we process 1 of the events, but ANR on the second event with the same timestamp,
6871 * the ANR mechanism should still work.
6872 *
6873 * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the
6874 * DOWN event, while not responding on the second one.
6875 */
6876TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) {
6877 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
6878 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6879 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
6880 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
6881 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006882 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006883
6884 // Now send ACTION_UP, with identical timestamp
6885 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
6886 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
6887 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
6888 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006889 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006890
6891 // We have now sent down and up. Let's consume first event and then ANR on the second.
6892 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6893 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006894 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006895}
6896
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006897// A spy window can receive an ANR
6898TEST_F(InputDispatcherSingleWindowAnr, SpyWindowAnr) {
6899 sp<FakeWindowHandle> spy = addSpyWindow();
6900
6901 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6902 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6903 WINDOW_LOCATION));
6904 mWindow->consumeMotionDown();
6905
6906 std::optional<uint32_t> sequenceNum = spy->receiveEvent(); // ACTION_DOWN
6907 ASSERT_TRUE(sequenceNum);
6908 const std::chrono::duration timeout = spy->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006909 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, spy);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006910
6911 spy->finishEvent(*sequenceNum);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08006912 spy->consumeMotionEvent(
6913 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006914 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006915 mFakePolicy->assertNotifyWindowResponsiveWasCalled(spy->getToken(), mWindow->getPid());
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006916}
6917
6918// If an app is not responding to a key event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006919// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006920TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnKey) {
6921 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006922
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006923 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6924 injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006925 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006926 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006927
6928 // Stuck on the ACTION_UP
6929 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006930 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006931
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006932 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006933 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006934 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6935 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006936
6937 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion
6938 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006939 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006940 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006941 spy->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006942}
6943
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006944// If an app is not responding to a motion event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006945// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006946TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnMotion) {
6947 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006948
6949 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006950 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6951 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006952
6953 mWindow->consumeMotionDown();
6954 // Stuck on the ACTION_UP
6955 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006956 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006957
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006958 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006959 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006960 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6961 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006962
6963 mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion
6964 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006965 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006966 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006967 spy->assertNoEvents();
6968}
6969
6970TEST_F(InputDispatcherSingleWindowAnr, UnresponsiveMonitorAnr) {
6971 mDispatcher->setMonitorDispatchingTimeoutForTest(30ms);
6972
6973 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
6974
6975 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6976 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6977 WINDOW_LOCATION));
6978
6979 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6980 const std::optional<uint32_t> consumeSeq = monitor.receiveEvent();
6981 ASSERT_TRUE(consumeSeq);
6982
Prabir Pradhanedd96402022-02-15 01:46:16 -08006983 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(30ms, monitor.getToken(), MONITOR_PID);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006984
6985 monitor.finishEvent(*consumeSeq);
6986 monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
6987
6988 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006989 mFakePolicy->assertNotifyWindowResponsiveWasCalled(monitor.getToken(), MONITOR_PID);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006990}
6991
6992// If a window is unresponsive, then you get anr. if the window later catches up and starts to
6993// process events, you don't get an anr. When the window later becomes unresponsive again, you
6994// get an ANR again.
6995// 1. tap -> block on ACTION_UP -> receive ANR
6996// 2. consume all pending events (= queue becomes healthy again)
6997// 3. tap again -> block on ACTION_UP again -> receive ANR second time
6998TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) {
6999 tapOnWindow();
7000
7001 mWindow->consumeMotionDown();
7002 // Block on ACTION_UP
7003 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08007004 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007005 mWindow->consumeMotionUp(); // Now the connection should be healthy again
7006 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08007007 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007008 mWindow->assertNoEvents();
7009
7010 tapOnWindow();
7011 mWindow->consumeMotionDown();
Prabir Pradhanedd96402022-02-15 01:46:16 -08007012 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007013 mWindow->consumeMotionUp();
7014
7015 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08007016 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007017 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007018 mWindow->assertNoEvents();
7019}
7020
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007021// If a connection remains unresponsive for a while, make sure policy is only notified once about
7022// it.
7023TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007024 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007025 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7026 WINDOW_LOCATION));
7027
7028 const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08007029 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007030 std::this_thread::sleep_for(windowTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007031 // 'notifyConnectionUnresponsive' should only be called once per connection
7032 mFakePolicy->assertNotifyAnrWasNotCalled();
7033 // When the ANR happened, dispatcher should abort the current event stream via ACTION_CANCEL
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007034 mWindow->consumeMotionDown();
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08007035 mWindow->consumeMotionEvent(
7036 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007037 mWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007038 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08007039 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007040 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007041}
7042
7043/**
7044 * If a window is processing a motion event, and then a key event comes in, the key event should
7045 * not to to the focused window until the motion is processed.
7046 *
7047 * Warning!!!
7048 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
7049 * and the injection timeout that we specify when injecting the key.
7050 * We must have the injection timeout (10ms) be smaller than
7051 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
7052 *
7053 * If that value changes, this test should also change.
7054 */
7055TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) {
7056 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
7057 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
7058
7059 tapOnWindow();
7060 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
7061 ASSERT_TRUE(downSequenceNum);
7062 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
7063 ASSERT_TRUE(upSequenceNum);
7064 // Don't finish the events yet, and send a key
7065 // Injection will "succeed" because we will eventually give up and send the key to the focused
7066 // window even if motions are still being processed. But because the injection timeout is short,
7067 // we will receive INJECTION_TIMED_OUT as the result.
7068
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007069 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00007070 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007071 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
7072 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007073 // Key will not be sent to the window, yet, because the window is still processing events
7074 // and the key remains pending, waiting for the touch events to be processed
7075 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
7076 ASSERT_FALSE(keySequenceNum);
7077
7078 std::this_thread::sleep_for(500ms);
7079 // if we wait long enough though, dispatcher will give up, and still send the key
7080 // to the focused window, even though we have not yet finished the motion event
7081 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
7082 mWindow->finishEvent(*downSequenceNum);
7083 mWindow->finishEvent(*upSequenceNum);
7084}
7085
7086/**
7087 * If a window is processing a motion event, and then a key event comes in, the key event should
7088 * not go to the focused window until the motion is processed.
7089 * If then a new motion comes in, then the pending key event should be going to the currently
7090 * focused window right away.
7091 */
7092TEST_F(InputDispatcherSingleWindowAnr,
7093 PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) {
7094 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
7095 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
7096
7097 tapOnWindow();
7098 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
7099 ASSERT_TRUE(downSequenceNum);
7100 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
7101 ASSERT_TRUE(upSequenceNum);
7102 // Don't finish the events yet, and send a key
7103 // Injection is async, so it will succeed
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007104 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00007105 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
7106 InputEventInjectionSync::NONE));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007107 // At this point, key is still pending, and should not be sent to the application yet.
7108 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
7109 ASSERT_FALSE(keySequenceNum);
7110
7111 // Now tap down again. It should cause the pending key to go to the focused window right away.
7112 tapOnWindow();
7113 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd
7114 // the other events yet. We can finish events in any order.
7115 mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN
7116 mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP
7117 mWindow->consumeMotionDown();
7118 mWindow->consumeMotionUp();
7119 mWindow->assertNoEvents();
7120}
7121
7122class InputDispatcherMultiWindowAnr : public InputDispatcherTest {
7123 virtual void SetUp() override {
7124 InputDispatcherTest::SetUp();
7125
Chris Yea209fde2020-07-22 13:54:51 -07007126 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007127 mApplication->setDispatchingTimeout(10ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007128 mUnfocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Unfocused",
7129 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007130 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007131 // Adding FLAG_WATCH_OUTSIDE_TOUCH to receive ACTION_OUTSIDE when another window is tapped
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007132 mUnfocusedWindow->setWatchOutsideTouch(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007133
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007134 mFocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Focused",
7135 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05007136 mFocusedWindow->setDispatchingTimeout(30ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007137 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007138
7139 // Set focused application.
7140 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
Vishnu Nair47074b82020-08-14 11:54:47 -07007141 mFocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007142
7143 // Expect one focus window exist in display.
7144 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07007145 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007146 mFocusedWindow->consumeFocusEvent(true);
7147 }
7148
7149 virtual void TearDown() override {
7150 InputDispatcherTest::TearDown();
7151
7152 mUnfocusedWindow.clear();
7153 mFocusedWindow.clear();
7154 }
7155
7156protected:
Chris Yea209fde2020-07-22 13:54:51 -07007157 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007158 sp<FakeWindowHandle> mUnfocusedWindow;
7159 sp<FakeWindowHandle> mFocusedWindow;
7160 static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20};
7161 static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75};
7162 static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40};
7163
7164 void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); }
7165
7166 void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); }
7167
7168private:
7169 void tap(const PointF& location) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007170 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007171 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7172 location));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007173 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007174 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7175 location));
7176 }
7177};
7178
7179// If we have 2 windows that are both unresponsive, the one with the shortest timeout
7180// should be ANR'd first.
7181TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007182 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007183 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7184 FOCUSED_WINDOW_LOCATION))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007185 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007186 mFocusedWindow->consumeMotionDown();
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07007187 mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00007188 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007189 // We consumed all events, so no ANR
7190 ASSERT_TRUE(mDispatcher->waitForIdle());
7191 mFakePolicy->assertNotifyAnrWasNotCalled();
7192
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007193 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007194 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7195 FOCUSED_WINDOW_LOCATION));
7196 std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent();
7197 ASSERT_TRUE(unfocusedSequenceNum);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007198
7199 const std::chrono::duration timeout =
7200 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08007201 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007202 // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event
7203 // sequence to make it consistent
7204 mFocusedWindow->consumeMotionCancel();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007205 mUnfocusedWindow->finishEvent(*unfocusedSequenceNum);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007206 mFocusedWindow->consumeMotionDown();
7207 // This cancel is generated because the connection was unresponsive
7208 mFocusedWindow->consumeMotionCancel();
7209 mFocusedWindow->assertNoEvents();
7210 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007211 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08007212 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
7213 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007214 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007215}
7216
7217// If we have 2 windows with identical timeouts that are both unresponsive,
7218// it doesn't matter which order they should have ANR.
7219// But we should receive ANR for both.
7220TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) {
7221 // Set the timeout for unfocused window to match the focused window
7222 mUnfocusedWindow->setDispatchingTimeout(10ms);
7223 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
7224
7225 tapOnFocusedWindow();
7226 // we should have ACTION_DOWN/ACTION_UP on focused window and ACTION_OUTSIDE on unfocused window
Prabir Pradhanedd96402022-02-15 01:46:16 -08007227 sp<IBinder> anrConnectionToken1, anrConnectionToken2;
7228 ASSERT_NO_FATAL_FAILURE(anrConnectionToken1 = mFakePolicy->getUnresponsiveWindowToken(10ms));
7229 ASSERT_NO_FATAL_FAILURE(anrConnectionToken2 = mFakePolicy->getUnresponsiveWindowToken(0ms));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007230
7231 // We don't know which window will ANR first. But both of them should happen eventually.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007232 ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 ||
7233 mFocusedWindow->getToken() == anrConnectionToken2);
7234 ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 ||
7235 mUnfocusedWindow->getToken() == anrConnectionToken2);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007236
7237 ASSERT_TRUE(mDispatcher->waitForIdle());
7238 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007239
7240 mFocusedWindow->consumeMotionDown();
7241 mFocusedWindow->consumeMotionUp();
7242 mUnfocusedWindow->consumeMotionOutside();
7243
Prabir Pradhanedd96402022-02-15 01:46:16 -08007244 sp<IBinder> responsiveToken1, responsiveToken2;
7245 ASSERT_NO_FATAL_FAILURE(responsiveToken1 = mFakePolicy->getResponsiveWindowToken());
7246 ASSERT_NO_FATAL_FAILURE(responsiveToken2 = mFakePolicy->getResponsiveWindowToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007247
7248 // Both applications should be marked as responsive, in any order
7249 ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 ||
7250 mFocusedWindow->getToken() == responsiveToken2);
7251 ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 ||
7252 mUnfocusedWindow->getToken() == responsiveToken2);
7253 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007254}
7255
7256// If a window is already not responding, the second tap on the same window should be ignored.
7257// We should also log an error to account for the dropped event (not tested here).
7258// At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events.
7259TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) {
7260 tapOnFocusedWindow();
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07007261 mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00007262 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007263 // Receive the events, but don't respond
7264 std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN
7265 ASSERT_TRUE(downEventSequenceNum);
7266 std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP
7267 ASSERT_TRUE(upEventSequenceNum);
7268 const std::chrono::duration timeout =
7269 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08007270 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007271
7272 // Tap once again
7273 // We cannot use "tapOnFocusedWindow" because it asserts the injection result to be success
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007274 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007275 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7276 FOCUSED_WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007277 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007278 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7279 FOCUSED_WINDOW_LOCATION));
7280 // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a
7281 // valid touch target
7282 mUnfocusedWindow->assertNoEvents();
7283
7284 // Consume the first tap
7285 mFocusedWindow->finishEvent(*downEventSequenceNum);
7286 mFocusedWindow->finishEvent(*upEventSequenceNum);
7287 ASSERT_TRUE(mDispatcher->waitForIdle());
7288 // The second tap did not go to the focused window
7289 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007290 // Since all events are finished, connection should be deemed healthy again
Prabir Pradhanedd96402022-02-15 01:46:16 -08007291 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
7292 mFocusedWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007293 mFakePolicy->assertNotifyAnrWasNotCalled();
7294}
7295
7296// If you tap outside of all windows, there will not be ANR
7297TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007298 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007299 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7300 LOCATION_OUTSIDE_ALL_WINDOWS));
7301 ASSERT_TRUE(mDispatcher->waitForIdle());
7302 mFakePolicy->assertNotifyAnrWasNotCalled();
7303}
7304
7305// Since the focused window is paused, tapping on it should not produce any events
7306TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) {
7307 mFocusedWindow->setPaused(true);
7308 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
7309
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007310 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007311 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7312 FOCUSED_WINDOW_LOCATION));
7313
7314 std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT));
7315 ASSERT_TRUE(mDispatcher->waitForIdle());
7316 // Should not ANR because the window is paused, and touches shouldn't go to it
7317 mFakePolicy->assertNotifyAnrWasNotCalled();
7318
7319 mFocusedWindow->assertNoEvents();
7320 mUnfocusedWindow->assertNoEvents();
7321}
7322
7323/**
7324 * If a window is processing a motion event, and then a key event comes in, the key event should
7325 * not to to the focused window until the motion is processed.
7326 * If a different window becomes focused at this time, the key should go to that window instead.
7327 *
7328 * Warning!!!
7329 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
7330 * and the injection timeout that we specify when injecting the key.
7331 * We must have the injection timeout (10ms) be smaller than
7332 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
7333 *
7334 * If that value changes, this test should also change.
7335 */
7336TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) {
7337 // Set a long ANR timeout to prevent it from triggering
7338 mFocusedWindow->setDispatchingTimeout(2s);
7339 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
7340
7341 tapOnUnfocusedWindow();
7342 std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent();
7343 ASSERT_TRUE(downSequenceNum);
7344 std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent();
7345 ASSERT_TRUE(upSequenceNum);
7346 // Don't finish the events yet, and send a key
7347 // Injection will succeed because we will eventually give up and send the key to the focused
7348 // window even if motions are still being processed.
7349
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007350 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00007351 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
7352 InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007353 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007354 // Key will not be sent to the window, yet, because the window is still processing events
7355 // and the key remains pending, waiting for the touch events to be processed
7356 std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent();
7357 ASSERT_FALSE(keySequenceNum);
7358
7359 // Switch the focus to the "unfocused" window that we tapped. Expect the key to go there
Vishnu Nair47074b82020-08-14 11:54:47 -07007360 mFocusedWindow->setFocusable(false);
7361 mUnfocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007362 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07007363 setFocusedWindow(mUnfocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007364
7365 // Focus events should precede the key events
7366 mUnfocusedWindow->consumeFocusEvent(true);
7367 mFocusedWindow->consumeFocusEvent(false);
7368
7369 // Finish the tap events, which should unblock dispatcher
7370 mUnfocusedWindow->finishEvent(*downSequenceNum);
7371 mUnfocusedWindow->finishEvent(*upSequenceNum);
7372
7373 // Now that all queues are cleared and no backlog in the connections, the key event
7374 // can finally go to the newly focused "mUnfocusedWindow".
7375 mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
7376 mFocusedWindow->assertNoEvents();
7377 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007378 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007379}
7380
7381// When the touch stream is split across 2 windows, and one of them does not respond,
7382// then ANR should be raised and the touch should be canceled for the unresponsive window.
7383// The other window should not be affected by that.
7384TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) {
7385 // Touch Window 1
Prabir Pradhan678438e2023-04-13 19:32:51 +00007386 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7387 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7388 {FOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07007389 mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00007390 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007391
7392 // Touch Window 2
Prabir Pradhan678438e2023-04-13 19:32:51 +00007393 mDispatcher->notifyMotion(
7394 generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7395 {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007396
7397 const std::chrono::duration timeout =
7398 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08007399 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007400
7401 mUnfocusedWindow->consumeMotionDown();
7402 mFocusedWindow->consumeMotionDown();
7403 // Focused window may or may not receive ACTION_MOVE
7404 // But it should definitely receive ACTION_CANCEL due to the ANR
7405 InputEvent* event;
7406 std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event);
7407 ASSERT_TRUE(moveOrCancelSequenceNum);
7408 mFocusedWindow->finishEvent(*moveOrCancelSequenceNum);
7409 ASSERT_NE(nullptr, event);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07007410 ASSERT_EQ(event->getType(), InputEventType::MOTION);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007411 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
7412 if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) {
7413 mFocusedWindow->consumeMotionCancel();
7414 } else {
7415 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction());
7416 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007417 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08007418 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
7419 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007420
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007421 mUnfocusedWindow->assertNoEvents();
7422 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007423 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007424}
7425
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05007426/**
7427 * If we have no focused window, and a key comes in, we start the ANR timer.
7428 * The focused application should add a focused window before the timer runs out to prevent ANR.
7429 *
7430 * If the user touches another application during this time, the key should be dropped.
7431 * Next, if a new focused window comes in, without toggling the focused application,
7432 * then no ANR should occur.
7433 *
7434 * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication',
7435 * but in some cases the policy may not update the focused application.
7436 */
7437TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) {
7438 std::shared_ptr<FakeApplicationHandle> focusedApplication =
7439 std::make_shared<FakeApplicationHandle>();
7440 focusedApplication->setDispatchingTimeout(60ms);
7441 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication);
7442 // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused.
7443 mFocusedWindow->setFocusable(false);
7444
7445 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
7446 mFocusedWindow->consumeFocusEvent(false);
7447
7448 // Send a key. The ANR timer should start because there is no focused window.
7449 // 'focusedApplication' will get blamed if this timer completes.
7450 // Key will not be sent anywhere because we have no focused window. It will remain pending.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007451 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00007452 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
7453 InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms,
7454 /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007455 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05007456
7457 // Wait until dispatcher starts the "no focused window" timer. If we don't wait here,
7458 // then the injected touches won't cause the focused event to get dropped.
7459 // The dispatcher only checks for whether the queue should be pruned upon queueing.
7460 // If we inject the touch right away and the ANR timer hasn't started, the touch event would
7461 // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'.
7462 // For this test, it means that the key would get delivered to the window once it becomes
7463 // focused.
7464 std::this_thread::sleep_for(10ms);
7465
7466 // Touch unfocused window. This should force the pending key to get dropped.
Prabir Pradhan678438e2023-04-13 19:32:51 +00007467 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7468 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7469 {UNFOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05007470
7471 // We do not consume the motion right away, because that would require dispatcher to first
7472 // process (== drop) the key event, and by that time, ANR will be raised.
7473 // Set the focused window first.
7474 mFocusedWindow->setFocusable(true);
7475 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
7476 setFocusedWindow(mFocusedWindow);
7477 mFocusedWindow->consumeFocusEvent(true);
7478 // We do not call "setFocusedApplication" here, even though the newly focused window belongs
7479 // to another application. This could be a bug / behaviour in the policy.
7480
7481 mUnfocusedWindow->consumeMotionDown();
7482
7483 ASSERT_TRUE(mDispatcher->waitForIdle());
7484 // Should not ANR because we actually have a focused window. It was just added too slowly.
7485 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled());
7486}
7487
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007488// These tests ensure we cannot send touch events to a window that's positioned behind a window
7489// that has feature NO_INPUT_CHANNEL.
7490// Layout:
7491// Top (closest to user)
7492// mNoInputWindow (above all windows)
7493// mBottomWindow
7494// Bottom (furthest from user)
7495class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest {
7496 virtual void SetUp() override {
7497 InputDispatcherTest::SetUp();
7498
7499 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007500 mNoInputWindow =
7501 sp<FakeWindowHandle>::make(mApplication, mDispatcher,
7502 "Window without input channel", ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00007503 /*token=*/std::make_optional<sp<IBinder>>(nullptr));
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007504 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007505 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
7506 // It's perfectly valid for this window to not have an associated input channel
7507
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007508 mBottomWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Bottom window",
7509 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007510 mBottomWindow->setFrame(Rect(0, 0, 100, 100));
7511
7512 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
7513 }
7514
7515protected:
7516 std::shared_ptr<FakeApplicationHandle> mApplication;
7517 sp<FakeWindowHandle> mNoInputWindow;
7518 sp<FakeWindowHandle> mBottomWindow;
7519};
7520
7521TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) {
7522 PointF touchedPoint = {10, 10};
7523
Prabir Pradhan678438e2023-04-13 19:32:51 +00007524 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7525 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7526 {touchedPoint}));
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007527
7528 mNoInputWindow->assertNoEvents();
7529 // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have
7530 // an input channel, it is not marked as FLAG_NOT_TOUCHABLE,
7531 // and therefore should prevent mBottomWindow from receiving touches
7532 mBottomWindow->assertNoEvents();
7533}
7534
7535/**
7536 * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel,
7537 * ensure that this window does not receive any touches, and blocks touches to windows underneath.
7538 */
7539TEST_F(InputDispatcherMultiWindowOcclusionTests,
7540 NoInputChannelFeature_DropsTouchesWithValidChannel) {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007541 mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher,
7542 "Window with input channel and NO_INPUT_CHANNEL",
7543 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007544
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007545 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007546 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
7547 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
7548
7549 PointF touchedPoint = {10, 10};
7550
Prabir Pradhan678438e2023-04-13 19:32:51 +00007551 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7552 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7553 {touchedPoint}));
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007554
7555 mNoInputWindow->assertNoEvents();
7556 mBottomWindow->assertNoEvents();
7557}
7558
Vishnu Nair958da932020-08-21 17:12:37 -07007559class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest {
7560protected:
7561 std::shared_ptr<FakeApplicationHandle> mApp;
7562 sp<FakeWindowHandle> mWindow;
7563 sp<FakeWindowHandle> mMirror;
7564
7565 virtual void SetUp() override {
7566 InputDispatcherTest::SetUp();
7567 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007568 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
7569 mMirror = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindowMirror",
7570 ADISPLAY_ID_DEFAULT, mWindow->getToken());
Vishnu Nair958da932020-08-21 17:12:37 -07007571 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
7572 mWindow->setFocusable(true);
7573 mMirror->setFocusable(true);
7574 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7575 }
7576};
7577
7578TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) {
7579 // Request focus on a mirrored window
7580 setFocusedWindow(mMirror);
7581
7582 // window gets focused
7583 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007584 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7585 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007586 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
7587}
7588
7589// A focused & mirrored window remains focused only if the window and its mirror are both
7590// focusable.
7591TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) {
7592 setFocusedWindow(mMirror);
7593
7594 // window gets focused
7595 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007596 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7597 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007598 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007599 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7600 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007601 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7602
7603 mMirror->setFocusable(false);
7604 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7605
7606 // window loses focus since one of the windows associated with the token in not focusable
7607 mWindow->consumeFocusEvent(false);
7608
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007609 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
7610 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07007611 mWindow->assertNoEvents();
7612}
7613
7614// A focused & mirrored window remains focused until the window and its mirror both become
7615// invisible.
7616TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) {
7617 setFocusedWindow(mMirror);
7618
7619 // window gets focused
7620 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007621 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7622 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007623 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007624 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7625 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007626 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7627
7628 mMirror->setVisible(false);
7629 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7630
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007631 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7632 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007633 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007634 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7635 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007636 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7637
7638 mWindow->setVisible(false);
7639 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7640
7641 // window loses focus only after all windows associated with the token become invisible.
7642 mWindow->consumeFocusEvent(false);
7643
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007644 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
7645 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07007646 mWindow->assertNoEvents();
7647}
7648
7649// A focused & mirrored window remains focused until both windows are removed.
7650TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) {
7651 setFocusedWindow(mMirror);
7652
7653 // window gets focused
7654 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007655 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7656 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007657 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007658 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7659 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007660 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7661
7662 // single window is removed but the window token remains focused
7663 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}});
7664
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007665 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7666 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007667 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007668 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7669 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007670 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7671
7672 // Both windows are removed
7673 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
7674 mWindow->consumeFocusEvent(false);
7675
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007676 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
7677 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07007678 mWindow->assertNoEvents();
7679}
7680
7681// Focus request can be pending until one window becomes visible.
7682TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) {
7683 // Request focus on an invisible mirror.
7684 mWindow->setVisible(false);
7685 mMirror->setVisible(false);
7686 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7687 setFocusedWindow(mMirror);
7688
7689 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007690 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00007691 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
7692 InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07007693
7694 mMirror->setVisible(true);
7695 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7696
7697 // window gets focused
7698 mWindow->consumeFocusEvent(true);
7699 // window gets the pending key event
7700 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
7701}
Prabir Pradhan99987712020-11-10 18:43:05 -08007702
7703class InputDispatcherPointerCaptureTests : public InputDispatcherTest {
7704protected:
7705 std::shared_ptr<FakeApplicationHandle> mApp;
7706 sp<FakeWindowHandle> mWindow;
7707 sp<FakeWindowHandle> mSecondWindow;
7708
7709 void SetUp() override {
7710 InputDispatcherTest::SetUp();
7711 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007712 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -08007713 mWindow->setFocusable(true);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007714 mSecondWindow =
7715 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -08007716 mSecondWindow->setFocusable(true);
7717
7718 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
7719 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}});
7720
7721 setFocusedWindow(mWindow);
7722 mWindow->consumeFocusEvent(true);
7723 }
7724
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007725 void notifyPointerCaptureChanged(const PointerCaptureRequest& request) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00007726 mDispatcher->notifyPointerCaptureChanged(generatePointerCaptureChangedArgs(request));
Prabir Pradhan99987712020-11-10 18:43:05 -08007727 }
7728
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007729 PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window,
7730 bool enabled) {
Prabir Pradhan99987712020-11-10 18:43:05 -08007731 mDispatcher->requestPointerCapture(window->getToken(), enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007732 auto request = mFakePolicy->assertSetPointerCaptureCalled(enabled);
7733 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08007734 window->consumeCaptureEvent(enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007735 return request;
Prabir Pradhan99987712020-11-10 18:43:05 -08007736 }
7737};
7738
7739TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) {
7740 // Ensure that capture cannot be obtained for unfocused windows.
7741 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
7742 mFakePolicy->assertSetPointerCaptureNotCalled();
7743 mSecondWindow->assertNoEvents();
7744
7745 // Ensure that capture can be enabled from the focus window.
7746 requestAndVerifyPointerCapture(mWindow, true);
7747
7748 // Ensure that capture cannot be disabled from a window that does not have capture.
7749 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false);
7750 mFakePolicy->assertSetPointerCaptureNotCalled();
7751
7752 // Ensure that capture can be disabled from the window with capture.
7753 requestAndVerifyPointerCapture(mWindow, false);
7754}
7755
7756TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007757 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08007758
7759 setFocusedWindow(mSecondWindow);
7760
7761 // Ensure that the capture disabled event was sent first.
7762 mWindow->consumeCaptureEvent(false);
7763 mWindow->consumeFocusEvent(false);
7764 mSecondWindow->consumeFocusEvent(true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007765 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08007766
7767 // Ensure that additional state changes from InputReader are not sent to the window.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007768 notifyPointerCaptureChanged({});
7769 notifyPointerCaptureChanged(request);
7770 notifyPointerCaptureChanged({});
Prabir Pradhan99987712020-11-10 18:43:05 -08007771 mWindow->assertNoEvents();
7772 mSecondWindow->assertNoEvents();
7773 mFakePolicy->assertSetPointerCaptureNotCalled();
7774}
7775
7776TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007777 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08007778
7779 // InputReader unexpectedly disables and enables pointer capture.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007780 notifyPointerCaptureChanged({});
7781 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08007782
7783 // Ensure that Pointer Capture is disabled.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007784 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08007785 mWindow->consumeCaptureEvent(false);
7786 mWindow->assertNoEvents();
7787}
7788
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007789TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) {
7790 requestAndVerifyPointerCapture(mWindow, true);
7791
7792 // The first window loses focus.
7793 setFocusedWindow(mSecondWindow);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007794 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007795 mWindow->consumeCaptureEvent(false);
7796
7797 // Request Pointer Capture from the second window before the notification from InputReader
7798 // arrives.
7799 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007800 auto request = mFakePolicy->assertSetPointerCaptureCalled(true);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007801
7802 // InputReader notifies Pointer Capture was disabled (because of the focus change).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007803 notifyPointerCaptureChanged({});
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007804
7805 // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007806 notifyPointerCaptureChanged(request);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007807
7808 mSecondWindow->consumeFocusEvent(true);
7809 mSecondWindow->consumeCaptureEvent(true);
7810}
7811
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007812TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) {
7813 // App repeatedly enables and disables capture.
7814 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
7815 auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
7816 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
7817 mFakePolicy->assertSetPointerCaptureCalled(false);
7818 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
7819 auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
7820
7821 // InputReader notifies that PointerCapture has been enabled for the first request. Since the
7822 // first request is now stale, this should do nothing.
7823 notifyPointerCaptureChanged(firstRequest);
7824 mWindow->assertNoEvents();
7825
7826 // InputReader notifies that the second request was enabled.
7827 notifyPointerCaptureChanged(secondRequest);
7828 mWindow->consumeCaptureEvent(true);
7829}
7830
Prabir Pradhan7092e262022-05-03 16:51:09 +00007831TEST_F(InputDispatcherPointerCaptureTests, RapidToggleRequests) {
7832 requestAndVerifyPointerCapture(mWindow, true);
7833
7834 // App toggles pointer capture off and on.
7835 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
7836 mFakePolicy->assertSetPointerCaptureCalled(false);
7837
7838 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
7839 auto enableRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
7840
7841 // InputReader notifies that the latest "enable" request was processed, while skipping over the
7842 // preceding "disable" request.
7843 notifyPointerCaptureChanged(enableRequest);
7844
7845 // Since pointer capture was never disabled during the rapid toggle, the window does not receive
7846 // any notifications.
7847 mWindow->assertNoEvents();
7848}
7849
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007850class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest {
7851protected:
7852 constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8;
Bernardo Rufino7393d172021-02-26 13:56:11 +00007853
7854 constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9;
7855 static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY);
7856
7857 constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7;
7858 static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
7859
7860 // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold
7861 constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5;
7862 static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
7863 static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) <
7864 MAXIMUM_OBSCURING_OPACITY);
7865
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007866 static const int32_t TOUCHED_APP_UID = 10001;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007867 static const int32_t APP_B_UID = 10002;
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007868 static const int32_t APP_C_UID = 10003;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007869
7870 sp<FakeWindowHandle> mTouchWindow;
7871
7872 virtual void SetUp() override {
7873 InputDispatcherTest::SetUp();
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007874 mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched");
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007875 mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY);
7876 }
7877
7878 virtual void TearDown() override {
7879 InputDispatcherTest::TearDown();
7880 mTouchWindow.clear();
7881 }
7882
chaviw3277faf2021-05-19 16:45:23 -05007883 sp<FakeWindowHandle> getOccludingWindow(int32_t uid, std::string name, TouchOcclusionMode mode,
7884 float alpha = 1.0f) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007885 sp<FakeWindowHandle> window = getWindow(uid, name);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007886 window->setTouchable(false);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007887 window->setTouchOcclusionMode(mode);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007888 window->setAlpha(alpha);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007889 return window;
7890 }
7891
7892 sp<FakeWindowHandle> getWindow(int32_t uid, std::string name) {
7893 std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>();
7894 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007895 sp<FakeWindowHandle>::make(app, mDispatcher, name, ADISPLAY_ID_DEFAULT);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007896 // Generate an arbitrary PID based on the UID
7897 window->setOwnerInfo(1777 + (uid % 10000), uid);
7898 return window;
7899 }
7900
7901 void touch(const std::vector<PointF>& points = {PointF{100, 200}}) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00007902 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7903 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7904 points));
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007905 }
7906};
7907
7908TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007909 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007910 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007911 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007912
7913 touch();
7914
7915 mTouchWindow->assertNoEvents();
7916}
7917
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007918TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufino7393d172021-02-26 13:56:11 +00007919 WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) {
7920 const sp<FakeWindowHandle>& w =
7921 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f);
7922 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7923
7924 touch();
7925
7926 mTouchWindow->assertNoEvents();
7927}
7928
7929TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007930 WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) {
7931 const sp<FakeWindowHandle>& w =
7932 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
7933 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7934
7935 touch();
7936
7937 w->assertNoEvents();
7938}
7939
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007940TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007941 const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW);
7942 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007943
7944 touch();
7945
7946 mTouchWindow->consumeAnyMotionDown();
7947}
7948
7949TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007950 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007951 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007952 w->setFrame(Rect(0, 0, 50, 50));
7953 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007954
7955 touch({PointF{100, 100}});
7956
7957 mTouchWindow->consumeAnyMotionDown();
7958}
7959
7960TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007961 const sp<FakeWindowHandle>& w =
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007962 getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007963 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7964
7965 touch();
7966
7967 mTouchWindow->consumeAnyMotionDown();
7968}
7969
7970TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) {
7971 const sp<FakeWindowHandle>& w =
7972 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
7973 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007974
7975 touch();
7976
7977 mTouchWindow->consumeAnyMotionDown();
7978}
7979
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007980TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) {
7981 const sp<FakeWindowHandle>& w =
7982 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
7983 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7984
7985 touch();
7986
7987 w->assertNoEvents();
7988}
7989
7990/**
7991 * This is important to make sure apps can't indirectly learn the position of touches (outside vs
7992 * inside) while letting them pass-through. Note that even though touch passes through the occluding
7993 * window, the occluding window will still receive ACTION_OUTSIDE event.
7994 */
7995TEST_F(InputDispatcherUntrustedTouchesTest,
7996 WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) {
7997 const sp<FakeWindowHandle>& w =
7998 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007999 w->setWatchOutsideTouch(true);
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00008000 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8001
8002 touch();
8003
8004 w->consumeMotionOutside();
8005}
8006
8007TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) {
8008 const sp<FakeWindowHandle>& w =
8009 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008010 w->setWatchOutsideTouch(true);
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00008011 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8012
8013 touch();
8014
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08008015 w->consumeMotionOutsideWithZeroedCoords();
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00008016}
8017
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00008018TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008019 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008020 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8021 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008022 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8023
8024 touch();
8025
8026 mTouchWindow->consumeAnyMotionDown();
8027}
8028
8029TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) {
8030 const sp<FakeWindowHandle>& w =
8031 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8032 MAXIMUM_OBSCURING_OPACITY);
8033 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00008034
8035 touch();
8036
8037 mTouchWindow->consumeAnyMotionDown();
8038}
8039
8040TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008041 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008042 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8043 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008044 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8045
8046 touch();
8047
8048 mTouchWindow->assertNoEvents();
8049}
8050
8051TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) {
8052 // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91
8053 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008054 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
8055 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008056 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008057 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
8058 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008059 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
8060
8061 touch();
8062
8063 mTouchWindow->assertNoEvents();
8064}
8065
8066TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) {
8067 // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75
8068 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008069 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
8070 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008071 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008072 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
8073 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008074 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
8075
8076 touch();
8077
8078 mTouchWindow->consumeAnyMotionDown();
8079}
8080
8081TEST_F(InputDispatcherUntrustedTouchesTest,
8082 WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) {
8083 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008084 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8085 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008086 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008087 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
8088 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008089 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
8090
8091 touch();
8092
8093 mTouchWindow->consumeAnyMotionDown();
8094}
8095
8096TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) {
8097 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008098 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8099 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008100 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008101 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
8102 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008103 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00008104
8105 touch();
8106
8107 mTouchWindow->assertNoEvents();
8108}
8109
Bernardo Rufino6d52e542021-02-15 18:38:10 +00008110TEST_F(InputDispatcherUntrustedTouchesTest,
8111 WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) {
8112 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008113 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
8114 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00008115 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008116 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8117 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00008118 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
8119
8120 touch();
8121
8122 mTouchWindow->assertNoEvents();
8123}
8124
8125TEST_F(InputDispatcherUntrustedTouchesTest,
8126 WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) {
8127 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008128 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
8129 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00008130 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008131 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8132 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00008133 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
8134
8135 touch();
8136
8137 mTouchWindow->consumeAnyMotionDown();
8138}
8139
8140TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) {
8141 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008142 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
8143 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00008144 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8145
8146 touch();
8147
8148 mTouchWindow->consumeAnyMotionDown();
8149}
8150
8151TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) {
8152 const sp<FakeWindowHandle>& w =
8153 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED);
8154 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8155
8156 touch();
8157
8158 mTouchWindow->consumeAnyMotionDown();
8159}
8160
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00008161TEST_F(InputDispatcherUntrustedTouchesTest,
8162 OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) {
8163 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
8164 const sp<FakeWindowHandle>& w =
8165 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f);
8166 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8167
8168 touch();
8169
8170 mTouchWindow->assertNoEvents();
8171}
8172
8173TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) {
8174 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
8175 const sp<FakeWindowHandle>& w =
8176 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f);
8177 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8178
8179 touch();
8180
8181 mTouchWindow->consumeAnyMotionDown();
8182}
8183
8184TEST_F(InputDispatcherUntrustedTouchesTest,
8185 OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) {
8186 mDispatcher->setMaximumObscuringOpacityForTouch(1.0f);
8187 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008188 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8189 OPACITY_ABOVE_THRESHOLD);
8190 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8191
8192 touch();
8193
8194 mTouchWindow->consumeAnyMotionDown();
8195}
8196
8197TEST_F(InputDispatcherUntrustedTouchesTest,
8198 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) {
8199 const sp<FakeWindowHandle>& w1 =
8200 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
8201 OPACITY_BELOW_THRESHOLD);
8202 const sp<FakeWindowHandle>& w2 =
8203 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8204 OPACITY_BELOW_THRESHOLD);
8205 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
8206
8207 touch();
8208
8209 mTouchWindow->assertNoEvents();
8210}
8211
8212/**
8213 * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the
8214 * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold
8215 * (which alone would result in allowing touches) does not affect the blocking behavior.
8216 */
8217TEST_F(InputDispatcherUntrustedTouchesTest,
8218 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) {
8219 const sp<FakeWindowHandle>& wB =
8220 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
8221 OPACITY_BELOW_THRESHOLD);
8222 const sp<FakeWindowHandle>& wC =
8223 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
8224 OPACITY_BELOW_THRESHOLD);
8225 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
8226
8227 touch();
8228
8229 mTouchWindow->assertNoEvents();
8230}
8231
8232/**
8233 * This test is testing that a window from a different UID but with same application token doesn't
8234 * block the touch. Apps can share the application token for close UI collaboration for example.
8235 */
8236TEST_F(InputDispatcherUntrustedTouchesTest,
8237 WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) {
8238 const sp<FakeWindowHandle>& w =
8239 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
8240 w->setApplicationToken(mTouchWindow->getApplicationToken());
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00008241 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8242
8243 touch();
8244
8245 mTouchWindow->consumeAnyMotionDown();
8246}
8247
arthurhungb89ccb02020-12-30 16:19:01 +08008248class InputDispatcherDragTests : public InputDispatcherTest {
8249protected:
8250 std::shared_ptr<FakeApplicationHandle> mApp;
8251 sp<FakeWindowHandle> mWindow;
8252 sp<FakeWindowHandle> mSecondWindow;
8253 sp<FakeWindowHandle> mDragWindow;
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008254 sp<FakeWindowHandle> mSpyWindow;
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008255 // Mouse would force no-split, set the id as non-zero to verify if drag state could track it.
8256 static constexpr int32_t MOUSE_POINTER_ID = 1;
arthurhungb89ccb02020-12-30 16:19:01 +08008257
8258 void SetUp() override {
8259 InputDispatcherTest::SetUp();
8260 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008261 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +08008262 mWindow->setFrame(Rect(0, 0, 100, 100));
arthurhungb89ccb02020-12-30 16:19:01 +08008263
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008264 mSecondWindow =
8265 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +08008266 mSecondWindow->setFrame(Rect(100, 0, 200, 100));
arthurhungb89ccb02020-12-30 16:19:01 +08008267
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008268 mSpyWindow =
8269 sp<FakeWindowHandle>::make(mApp, mDispatcher, "SpyWindow", ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008270 mSpyWindow->setSpy(true);
8271 mSpyWindow->setTrustedOverlay(true);
8272 mSpyWindow->setFrame(Rect(0, 0, 200, 100));
8273
arthurhungb89ccb02020-12-30 16:19:01 +08008274 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008275 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mSpyWindow, mWindow, mSecondWindow}}});
arthurhungb89ccb02020-12-30 16:19:01 +08008276 }
8277
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008278 void injectDown(int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
8279 switch (fromSource) {
8280 case AINPUT_SOURCE_TOUCHSCREEN:
8281 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8282 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
8283 ADISPLAY_ID_DEFAULT, {50, 50}))
8284 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8285 break;
8286 case AINPUT_SOURCE_STYLUS:
8287 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8288 injectMotionEvent(
8289 mDispatcher,
8290 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
8291 AINPUT_SOURCE_STYLUS)
8292 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008293 .pointer(PointerBuilder(0, ToolType::STYLUS)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008294 .x(50)
8295 .y(50))
8296 .build()));
8297 break;
8298 case AINPUT_SOURCE_MOUSE:
8299 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8300 injectMotionEvent(
8301 mDispatcher,
8302 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
8303 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
8304 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008305 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008306 .x(50)
8307 .y(50))
8308 .build()));
8309 break;
8310 default:
8311 FAIL() << "Source " << fromSource << " doesn't support drag and drop";
8312 }
arthurhungb89ccb02020-12-30 16:19:01 +08008313
8314 // Window should receive motion event.
8315 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008316 // Spy window should also receive motion event
8317 mSpyWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung54745652022-04-20 07:17:41 +00008318 }
8319
8320 // Start performing drag, we will create a drag window and transfer touch to it.
8321 // @param sendDown : if true, send a motion down on first window before perform drag and drop.
8322 // Returns true on success.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008323 bool startDrag(bool sendDown = true, int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
Arthur Hung54745652022-04-20 07:17:41 +00008324 if (sendDown) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008325 injectDown(fromSource);
Arthur Hung54745652022-04-20 07:17:41 +00008326 }
arthurhungb89ccb02020-12-30 16:19:01 +08008327
8328 // The drag window covers the entire display
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008329 mDragWindow =
8330 sp<FakeWindowHandle>::make(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008331 mDragWindow->setTouchableRegion(Region{{0, 0, 0, 0}});
arthurhungb89ccb02020-12-30 16:19:01 +08008332 mDispatcher->setInputWindows(
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008333 {{ADISPLAY_ID_DEFAULT, {mDragWindow, mSpyWindow, mWindow, mSecondWindow}}});
arthurhungb89ccb02020-12-30 16:19:01 +08008334
8335 // Transfer touch focus to the drag window
Arthur Hung54745652022-04-20 07:17:41 +00008336 bool transferred =
8337 mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(),
Harry Cutts33476232023-01-30 19:57:29 +00008338 /*isDragDrop=*/true);
Arthur Hung54745652022-04-20 07:17:41 +00008339 if (transferred) {
8340 mWindow->consumeMotionCancel();
8341 mDragWindow->consumeMotionDown();
8342 }
8343 return transferred;
arthurhungb89ccb02020-12-30 16:19:01 +08008344 }
8345};
8346
8347TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008348 startDrag();
arthurhungb89ccb02020-12-30 16:19:01 +08008349
8350 // Move on window.
8351 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8352 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8353 ADISPLAY_ID_DEFAULT, {50, 50}))
8354 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8355 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8356 mWindow->consumeDragEvent(false, 50, 50);
8357 mSecondWindow->assertNoEvents();
8358
8359 // Move to another window.
8360 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8361 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8362 ADISPLAY_ID_DEFAULT, {150, 50}))
8363 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8364 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8365 mWindow->consumeDragEvent(true, 150, 50);
8366 mSecondWindow->consumeDragEvent(false, 50, 50);
8367
8368 // Move back to original window.
8369 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8370 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8371 ADISPLAY_ID_DEFAULT, {50, 50}))
8372 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8373 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8374 mWindow->consumeDragEvent(false, 50, 50);
8375 mSecondWindow->consumeDragEvent(true, -50, 50);
8376
8377 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8378 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {50, 50}))
8379 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8380 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8381 mWindow->assertNoEvents();
8382 mSecondWindow->assertNoEvents();
8383}
8384
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008385TEST_F(InputDispatcherDragTests, DragEnterAndPointerDownPilfersPointers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008386 startDrag();
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008387
8388 // No cancel event after drag start
8389 mSpyWindow->assertNoEvents();
8390
8391 const MotionEvent secondFingerDownEvent =
8392 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8393 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008394 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
8395 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60))
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008396 .build();
8397 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8398 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8399 InputEventInjectionSync::WAIT_FOR_RESULT))
8400 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8401
8402 // Receives cancel for first pointer after next pointer down
8403 mSpyWindow->consumeMotionCancel();
8404 mSpyWindow->consumeMotionDown();
8405
8406 mSpyWindow->assertNoEvents();
8407}
8408
arthurhungf452d0b2021-01-06 00:19:52 +08008409TEST_F(InputDispatcherDragTests, DragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008410 startDrag();
arthurhungf452d0b2021-01-06 00:19:52 +08008411
8412 // Move on window.
8413 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8414 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8415 ADISPLAY_ID_DEFAULT, {50, 50}))
8416 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8417 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8418 mWindow->consumeDragEvent(false, 50, 50);
8419 mSecondWindow->assertNoEvents();
8420
8421 // Move to another window.
8422 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8423 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8424 ADISPLAY_ID_DEFAULT, {150, 50}))
8425 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8426 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8427 mWindow->consumeDragEvent(true, 150, 50);
8428 mSecondWindow->consumeDragEvent(false, 50, 50);
8429
8430 // drop to another window.
8431 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8432 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8433 {150, 50}))
8434 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8435 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8436 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
8437 mWindow->assertNoEvents();
8438 mSecondWindow->assertNoEvents();
8439}
8440
arthurhung6d4bed92021-03-17 11:59:33 +08008441TEST_F(InputDispatcherDragTests, StylusDragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008442 startDrag(true, AINPUT_SOURCE_STYLUS);
arthurhung6d4bed92021-03-17 11:59:33 +08008443
8444 // Move on window and keep button pressed.
8445 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8446 injectMotionEvent(mDispatcher,
8447 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
8448 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008449 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +08008450 .build()))
8451 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8452 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8453 mWindow->consumeDragEvent(false, 50, 50);
8454 mSecondWindow->assertNoEvents();
8455
8456 // Move to another window and release button, expect to drop item.
8457 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8458 injectMotionEvent(mDispatcher,
8459 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
8460 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008461 .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +08008462 .build()))
8463 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8464 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8465 mWindow->assertNoEvents();
8466 mSecondWindow->assertNoEvents();
8467 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
8468
8469 // nothing to the window.
8470 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8471 injectMotionEvent(mDispatcher,
8472 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS)
8473 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008474 .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +08008475 .build()))
8476 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8477 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8478 mWindow->assertNoEvents();
8479 mSecondWindow->assertNoEvents();
8480}
8481
Arthur Hung54745652022-04-20 07:17:41 +00008482TEST_F(InputDispatcherDragTests, DragAndDropOnInvalidWindow) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008483 startDrag();
Arthur Hung6d0571e2021-04-09 20:18:16 +08008484
8485 // Set second window invisible.
8486 mSecondWindow->setVisible(false);
8487 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}});
8488
8489 // Move on window.
8490 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8491 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8492 ADISPLAY_ID_DEFAULT, {50, 50}))
8493 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8494 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8495 mWindow->consumeDragEvent(false, 50, 50);
8496 mSecondWindow->assertNoEvents();
8497
8498 // Move to another window.
8499 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8500 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8501 ADISPLAY_ID_DEFAULT, {150, 50}))
8502 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8503 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8504 mWindow->consumeDragEvent(true, 150, 50);
8505 mSecondWindow->assertNoEvents();
8506
8507 // drop to another window.
8508 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8509 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8510 {150, 50}))
8511 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8512 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8513 mFakePolicy->assertDropTargetEquals(nullptr);
8514 mWindow->assertNoEvents();
8515 mSecondWindow->assertNoEvents();
8516}
8517
Arthur Hung54745652022-04-20 07:17:41 +00008518TEST_F(InputDispatcherDragTests, NoDragAndDropWhenMultiFingers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008519 // Ensure window could track pointerIds if it didn't support split touch.
8520 mWindow->setPreventSplitting(true);
8521
Arthur Hung54745652022-04-20 07:17:41 +00008522 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8523 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8524 {50, 50}))
8525 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8526 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8527
8528 const MotionEvent secondFingerDownEvent =
8529 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8530 .displayId(ADISPLAY_ID_DEFAULT)
8531 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008532 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
8533 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(75).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008534 .build();
8535 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8536 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8537 InputEventInjectionSync::WAIT_FOR_RESULT))
8538 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Harry Cutts33476232023-01-30 19:57:29 +00008539 mWindow->consumeMotionPointerDown(/*pointerIndex=*/1);
Arthur Hung54745652022-04-20 07:17:41 +00008540
8541 // Should not perform drag and drop when window has multi fingers.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008542 ASSERT_FALSE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +00008543}
8544
8545TEST_F(InputDispatcherDragTests, DragAndDropWhenSplitTouch) {
8546 // First down on second window.
8547 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8548 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8549 {150, 50}))
8550 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8551
8552 mSecondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8553
8554 // Second down on first window.
8555 const MotionEvent secondFingerDownEvent =
8556 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8557 .displayId(ADISPLAY_ID_DEFAULT)
8558 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008559 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
8560 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008561 .build();
8562 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8563 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8564 InputEventInjectionSync::WAIT_FOR_RESULT))
8565 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8566 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8567
8568 // Perform drag and drop from first window.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008569 ASSERT_TRUE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +00008570
8571 // Move on window.
8572 const MotionEvent secondFingerMoveEvent =
8573 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
8574 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008575 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
8576 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008577 .build();
8578 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8579 injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT,
8580 InputEventInjectionSync::WAIT_FOR_RESULT));
8581 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8582 mWindow->consumeDragEvent(false, 50, 50);
8583 mSecondWindow->consumeMotionMove();
8584
8585 // Release the drag pointer should perform drop.
8586 const MotionEvent secondFingerUpEvent =
8587 MotionEventBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
8588 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008589 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
8590 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008591 .build();
8592 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8593 injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT,
8594 InputEventInjectionSync::WAIT_FOR_RESULT));
8595 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8596 mFakePolicy->assertDropTargetEquals(mWindow->getToken());
8597 mWindow->assertNoEvents();
8598 mSecondWindow->consumeMotionMove();
8599}
8600
Arthur Hung3915c1f2022-05-31 07:17:17 +00008601TEST_F(InputDispatcherDragTests, DragAndDropWhenMultiDisplays) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008602 startDrag();
Arthur Hung3915c1f2022-05-31 07:17:17 +00008603
8604 // Update window of second display.
8605 sp<FakeWindowHandle> windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008606 sp<FakeWindowHandle>::make(mApp, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung3915c1f2022-05-31 07:17:17 +00008607 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
8608
8609 // Let second display has a touch state.
8610 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8611 injectMotionEvent(mDispatcher,
8612 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
8613 AINPUT_SOURCE_TOUCHSCREEN)
8614 .displayId(SECOND_DISPLAY_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008615 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
Arthur Hung3915c1f2022-05-31 07:17:17 +00008616 .build()));
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07008617 windowInSecondary->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN,
Harry Cutts33476232023-01-30 19:57:29 +00008618 SECOND_DISPLAY_ID, /*expectedFlag=*/0);
Arthur Hung3915c1f2022-05-31 07:17:17 +00008619 // Update window again.
8620 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
8621
8622 // Move on window.
8623 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8624 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8625 ADISPLAY_ID_DEFAULT, {50, 50}))
8626 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8627 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8628 mWindow->consumeDragEvent(false, 50, 50);
8629 mSecondWindow->assertNoEvents();
8630
8631 // Move to another window.
8632 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8633 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8634 ADISPLAY_ID_DEFAULT, {150, 50}))
8635 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8636 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8637 mWindow->consumeDragEvent(true, 150, 50);
8638 mSecondWindow->consumeDragEvent(false, 50, 50);
8639
8640 // drop to another window.
8641 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8642 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8643 {150, 50}))
8644 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8645 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8646 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
8647 mWindow->assertNoEvents();
8648 mSecondWindow->assertNoEvents();
8649}
8650
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008651TEST_F(InputDispatcherDragTests, MouseDragAndDrop) {
8652 startDrag(true, AINPUT_SOURCE_MOUSE);
8653 // Move on window.
8654 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8655 injectMotionEvent(mDispatcher,
8656 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
8657 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
8658 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008659 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008660 .x(50)
8661 .y(50))
8662 .build()))
8663 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8664 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8665 mWindow->consumeDragEvent(false, 50, 50);
8666 mSecondWindow->assertNoEvents();
8667
8668 // Move to another window.
8669 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8670 injectMotionEvent(mDispatcher,
8671 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
8672 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
8673 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008674 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008675 .x(150)
8676 .y(50))
8677 .build()))
8678 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8679 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8680 mWindow->consumeDragEvent(true, 150, 50);
8681 mSecondWindow->consumeDragEvent(false, 50, 50);
8682
8683 // drop to another window.
8684 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8685 injectMotionEvent(mDispatcher,
8686 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
8687 .buttonState(0)
8688 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008689 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008690 .x(150)
8691 .y(50))
8692 .build()))
8693 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8694 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8695 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
8696 mWindow->assertNoEvents();
8697 mSecondWindow->assertNoEvents();
8698}
8699
Vishnu Nair062a8672021-09-03 16:07:44 -07008700class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {};
8701
8702TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) {
8703 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008704 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
8705 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008706 window->setDropInput(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008707 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
8708 window->setFocusable(true);
8709 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
8710 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00008711 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008712
8713 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +00008714 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008715 window->assertNoEvents();
8716
Prabir Pradhan678438e2023-04-13 19:32:51 +00008717 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8718 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008719 window->assertNoEvents();
8720
8721 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008722 window->setDropInput(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008723 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
8724
Prabir Pradhan678438e2023-04-13 19:32:51 +00008725 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008726 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
8727
Prabir Pradhan678438e2023-04-13 19:32:51 +00008728 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8729 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008730 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8731 window->assertNoEvents();
8732}
8733
8734TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) {
8735 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
8736 std::make_shared<FakeApplicationHandle>();
8737 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008738 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
8739 ADISPLAY_ID_DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -07008740 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
8741 obscuringWindow->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008742 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008743 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008744 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
8745 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008746 window->setDropInputIfObscured(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008747 window->setOwnerInfo(222, 222);
8748 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
8749 window->setFocusable(true);
8750 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
8751 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00008752 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008753
8754 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +00008755 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008756 window->assertNoEvents();
8757
Prabir Pradhan678438e2023-04-13 19:32:51 +00008758 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8759 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008760 window->assertNoEvents();
8761
8762 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008763 window->setDropInputIfObscured(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008764 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
8765
Prabir Pradhan678438e2023-04-13 19:32:51 +00008766 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008767 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
8768
Prabir Pradhan678438e2023-04-13 19:32:51 +00008769 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8770 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008771 window->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
8772 window->assertNoEvents();
8773}
8774
8775TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) {
8776 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
8777 std::make_shared<FakeApplicationHandle>();
8778 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008779 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
8780 ADISPLAY_ID_DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -07008781 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
8782 obscuringWindow->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008783 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008784 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008785 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
8786 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008787 window->setDropInputIfObscured(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008788 window->setOwnerInfo(222, 222);
8789 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
8790 window->setFocusable(true);
8791 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
8792 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00008793 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008794
8795 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +00008796 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008797 window->assertNoEvents();
8798
Prabir Pradhan678438e2023-04-13 19:32:51 +00008799 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8800 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008801 window->assertNoEvents();
8802
8803 // When the window is no longer obscured because it went on top, it should get input
8804 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, obscuringWindow}}});
8805
Prabir Pradhan678438e2023-04-13 19:32:51 +00008806 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008807 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
8808
Prabir Pradhan678438e2023-04-13 19:32:51 +00008809 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8810 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008811 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8812 window->assertNoEvents();
8813}
8814
Antonio Kantekf16f2832021-09-28 04:39:20 +00008815class InputDispatcherTouchModeChangedTests : public InputDispatcherTest {
8816protected:
8817 std::shared_ptr<FakeApplicationHandle> mApp;
Antonio Kantek15beb512022-06-13 22:35:41 +00008818 std::shared_ptr<FakeApplicationHandle> mSecondaryApp;
Antonio Kantekf16f2832021-09-28 04:39:20 +00008819 sp<FakeWindowHandle> mWindow;
8820 sp<FakeWindowHandle> mSecondWindow;
Antonio Kantek15beb512022-06-13 22:35:41 +00008821 sp<FakeWindowHandle> mThirdWindow;
Antonio Kantekf16f2832021-09-28 04:39:20 +00008822
8823 void SetUp() override {
8824 InputDispatcherTest::SetUp();
8825
8826 mApp = std::make_shared<FakeApplicationHandle>();
Antonio Kantek15beb512022-06-13 22:35:41 +00008827 mSecondaryApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008828 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008829 mWindow->setFocusable(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00008830 setFocusedWindow(mWindow);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008831 mSecondWindow =
8832 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008833 mSecondWindow->setFocusable(true);
Antonio Kantek15beb512022-06-13 22:35:41 +00008834 mThirdWindow =
8835 sp<FakeWindowHandle>::make(mSecondaryApp, mDispatcher,
8836 "TestWindow3_SecondaryDisplay", SECOND_DISPLAY_ID);
8837 mThirdWindow->setFocusable(true);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008838
8839 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Antonio Kantek15beb512022-06-13 22:35:41 +00008840 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}},
8841 {SECOND_DISPLAY_ID, {mThirdWindow}}});
8842 mThirdWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008843 mWindow->consumeFocusEvent(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00008844
Antonio Kantek15beb512022-06-13 22:35:41 +00008845 // Set main display initial touch mode to InputDispatcher::kDefaultInTouchMode.
Prabir Pradhan5735a322022-04-11 17:23:34 +00008846 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, WINDOW_PID,
Harry Cutts33476232023-01-30 19:57:29 +00008847 WINDOW_UID, /*hasPermission=*/true, ADISPLAY_ID_DEFAULT)) {
Antonio Kantek48710e42022-03-24 14:19:30 -07008848 mWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
8849 mSecondWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +00008850 mThirdWindow->assertNoEvents();
8851 }
8852
8853 // Set secondary display initial touch mode to InputDispatcher::kDefaultInTouchMode.
8854 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, SECONDARY_WINDOW_PID,
Harry Cutts33476232023-01-30 19:57:29 +00008855 SECONDARY_WINDOW_UID, /*hasPermission=*/true,
Antonio Kantek15beb512022-06-13 22:35:41 +00008856 SECOND_DISPLAY_ID)) {
8857 mWindow->assertNoEvents();
8858 mSecondWindow->assertNoEvents();
8859 mThirdWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek48710e42022-03-24 14:19:30 -07008860 }
Antonio Kantekf16f2832021-09-28 04:39:20 +00008861 }
8862
Antonio Kantek15beb512022-06-13 22:35:41 +00008863 void changeAndVerifyTouchModeInMainDisplayOnly(bool inTouchMode, int32_t pid, int32_t uid,
8864 bool hasPermission) {
Antonio Kanteka042c022022-07-06 16:51:07 -07008865 ASSERT_TRUE(mDispatcher->setInTouchMode(inTouchMode, pid, uid, hasPermission,
8866 ADISPLAY_ID_DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +00008867 mWindow->consumeTouchModeEvent(inTouchMode);
8868 mSecondWindow->consumeTouchModeEvent(inTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +00008869 mThirdWindow->assertNoEvents();
Antonio Kantekf16f2832021-09-28 04:39:20 +00008870 }
8871};
8872
Antonio Kantek26defcf2022-02-08 01:12:27 +00008873TEST_F(InputDispatcherTouchModeChangedTests, FocusedWindowCanChangeTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -08008874 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek15beb512022-06-13 22:35:41 +00008875 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode,
8876 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008877 /* hasPermission=*/false);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008878}
8879
Antonio Kantek26defcf2022-02-08 01:12:27 +00008880TEST_F(InputDispatcherTouchModeChangedTests, NonFocusedWindowOwnerCannotChangeTouchMode) {
8881 const WindowInfo& windowInfo = *mWindow->getInfo();
8882 int32_t ownerPid = windowInfo.ownerPid;
8883 int32_t ownerUid = windowInfo.ownerUid;
8884 mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1);
8885 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, ownerPid,
Harry Cutts33476232023-01-30 19:57:29 +00008886 ownerUid, /*hasPermission=*/false,
Antonio Kanteka042c022022-07-06 16:51:07 -07008887 ADISPLAY_ID_DEFAULT));
Antonio Kantek26defcf2022-02-08 01:12:27 +00008888 mWindow->assertNoEvents();
8889 mSecondWindow->assertNoEvents();
8890}
8891
8892TEST_F(InputDispatcherTouchModeChangedTests, NonWindowOwnerMayChangeTouchModeOnPermissionGranted) {
8893 const WindowInfo& windowInfo = *mWindow->getInfo();
8894 int32_t ownerPid = windowInfo.ownerPid;
8895 int32_t ownerUid = windowInfo.ownerUid;
8896 mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1);
Antonio Kantek15beb512022-06-13 22:35:41 +00008897 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, ownerPid,
Harry Cutts33476232023-01-30 19:57:29 +00008898 ownerUid, /*hasPermission=*/true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00008899}
8900
Antonio Kantekf16f2832021-09-28 04:39:20 +00008901TEST_F(InputDispatcherTouchModeChangedTests, EventIsNotGeneratedIfNotChangingTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -08008902 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek26defcf2022-02-08 01:12:27 +00008903 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode,
8904 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008905 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +00008906 mWindow->assertNoEvents();
8907 mSecondWindow->assertNoEvents();
8908}
8909
Antonio Kantek15beb512022-06-13 22:35:41 +00008910TEST_F(InputDispatcherTouchModeChangedTests, ChangeTouchOnSecondaryDisplayOnly) {
8911 const WindowInfo& windowInfo = *mThirdWindow->getInfo();
8912 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
8913 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008914 /*hasPermission=*/true, SECOND_DISPLAY_ID));
Antonio Kantek15beb512022-06-13 22:35:41 +00008915 mWindow->assertNoEvents();
8916 mSecondWindow->assertNoEvents();
8917 mThirdWindow->consumeTouchModeEvent(!InputDispatcher::kDefaultInTouchMode);
8918}
8919
Antonio Kantek48710e42022-03-24 14:19:30 -07008920TEST_F(InputDispatcherTouchModeChangedTests, CanChangeTouchModeWhenOwningLastInteractedWindow) {
8921 // Interact with the window first.
8922 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
8923 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
8924 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
8925
8926 // Then remove focus.
8927 mWindow->setFocusable(false);
8928 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
8929
8930 // Assert that caller can switch touch mode by owning one of the last interacted window.
8931 const WindowInfo& windowInfo = *mWindow->getInfo();
8932 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
8933 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008934 /*hasPermission=*/false, ADISPLAY_ID_DEFAULT));
Antonio Kantek48710e42022-03-24 14:19:30 -07008935}
8936
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008937class InputDispatcherSpyWindowTest : public InputDispatcherTest {
8938public:
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008939 sp<FakeWindowHandle> createSpy() {
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008940 std::shared_ptr<FakeApplicationHandle> application =
8941 std::make_shared<FakeApplicationHandle>();
8942 std::string name = "Fake Spy ";
8943 name += std::to_string(mSpyCount++);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008944 sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher,
8945 name.c_str(), ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008946 spy->setSpy(true);
Prabir Pradhan5c85e052021-12-22 02:27:12 -08008947 spy->setTrustedOverlay(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008948 return spy;
8949 }
8950
8951 sp<FakeWindowHandle> createForeground() {
8952 std::shared_ptr<FakeApplicationHandle> application =
8953 std::make_shared<FakeApplicationHandle>();
8954 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008955 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
8956 ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008957 window->setFocusable(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008958 return window;
8959 }
8960
8961private:
8962 int mSpyCount{0};
8963};
8964
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08008965using InputDispatcherSpyWindowDeathTest = InputDispatcherSpyWindowTest;
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008966/**
Prabir Pradhan5c85e052021-12-22 02:27:12 -08008967 * Adding a spy window that is not a trusted overlay causes Dispatcher to abort.
8968 */
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08008969TEST_F(InputDispatcherSpyWindowDeathTest, UntrustedSpy_AbortsDispatcher) {
8970 ScopedSilentDeath _silentDeath;
8971
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008972 auto spy = createSpy();
Prabir Pradhan5c85e052021-12-22 02:27:12 -08008973 spy->setTrustedOverlay(false);
8974 ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}}),
8975 ".* not a trusted overlay");
8976}
8977
8978/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008979 * Input injection into a display with a spy window but no foreground windows should succeed.
8980 */
8981TEST_F(InputDispatcherSpyWindowTest, NoForegroundWindow) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008982 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008983 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}});
8984
8985 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8986 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8987 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8988 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8989}
8990
8991/**
8992 * Verify the order in which different input windows receive events. The touched foreground window
8993 * (if there is one) should always receive the event first. When there are multiple spy windows, the
8994 * spy windows will receive the event according to their Z-order, where the top-most spy window will
8995 * receive events before ones belows it.
8996 *
8997 * Here, we set up a scenario with four windows in the following Z order from the top:
8998 * spy1, spy2, window, spy3.
8999 * We then inject an event and verify that the foreground "window" receives it first, followed by
9000 * "spy1" and "spy2". The "spy3" does not receive the event because it is underneath the foreground
9001 * window.
9002 */
9003TEST_F(InputDispatcherSpyWindowTest, ReceivesInputInOrder) {
9004 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009005 auto spy1 = createSpy();
9006 auto spy2 = createSpy();
9007 auto spy3 = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009008 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window, spy3}}});
9009 const std::vector<sp<FakeWindowHandle>> channels{spy1, spy2, window, spy3};
9010 const size_t numChannels = channels.size();
9011
Michael Wright8e9a8562022-02-09 13:44:29 +00009012 base::unique_fd epollFd(epoll_create1(EPOLL_CLOEXEC));
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009013 if (!epollFd.ok()) {
9014 FAIL() << "Failed to create epoll fd";
9015 }
9016
9017 for (size_t i = 0; i < numChannels; i++) {
9018 struct epoll_event event = {.events = EPOLLIN, .data.u64 = i};
9019 if (epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, channels[i]->getChannelFd(), &event) < 0) {
9020 FAIL() << "Failed to add fd to epoll";
9021 }
9022 }
9023
9024 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9025 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9026 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9027
9028 std::vector<size_t> eventOrder;
9029 std::vector<struct epoll_event> events(numChannels);
9030 for (;;) {
9031 const int nFds = epoll_wait(epollFd.get(), events.data(), static_cast<int>(numChannels),
9032 (100ms).count());
9033 if (nFds < 0) {
9034 FAIL() << "Failed to call epoll_wait";
9035 }
9036 if (nFds == 0) {
9037 break; // epoll_wait timed out
9038 }
9039 for (int i = 0; i < nFds; i++) {
Colin Cross5b799302022-10-18 21:52:41 -07009040 ASSERT_EQ(static_cast<uint32_t>(EPOLLIN), events[i].events);
Siarhei Vishniakou31977182022-09-30 08:51:23 -07009041 eventOrder.push_back(static_cast<size_t>(events[i].data.u64));
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009042 channels[i]->consumeMotionDown();
9043 }
9044 }
9045
9046 // Verify the order in which the events were received.
9047 EXPECT_EQ(3u, eventOrder.size());
9048 EXPECT_EQ(2u, eventOrder[0]); // index 2: window
9049 EXPECT_EQ(0u, eventOrder[1]); // index 0: spy1
9050 EXPECT_EQ(1u, eventOrder[2]); // index 1: spy2
9051}
9052
9053/**
9054 * A spy window using the NOT_TOUCHABLE flag does not receive events.
9055 */
9056TEST_F(InputDispatcherSpyWindowTest, NotTouchable) {
9057 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009058 auto spy = createSpy();
9059 spy->setTouchable(false);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009060 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9061
9062 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9063 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9064 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9065 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
9066 spy->assertNoEvents();
9067}
9068
9069/**
9070 * A spy window will only receive gestures that originate within its touchable region. Gestures that
9071 * have their ACTION_DOWN outside of the touchable region of the spy window will not be dispatched
9072 * to the window.
9073 */
9074TEST_F(InputDispatcherSpyWindowTest, TouchableRegion) {
9075 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009076 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009077 spy->setTouchableRegion(Region{{0, 0, 20, 20}});
9078 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9079
9080 // Inject an event outside the spy window's touchable region.
9081 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9082 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9083 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9084 window->consumeMotionDown();
9085 spy->assertNoEvents();
9086 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9087 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9088 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9089 window->consumeMotionUp();
9090 spy->assertNoEvents();
9091
9092 // Inject an event inside the spy window's touchable region.
9093 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9094 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9095 {5, 10}))
9096 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9097 window->consumeMotionDown();
9098 spy->consumeMotionDown();
9099}
9100
9101/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009102 * A spy window can listen for touches outside its touchable region using the WATCH_OUTSIDE_TOUCHES
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08009103 * flag, but it will get zero-ed out coordinates if the foreground has a different owner.
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009104 */
9105TEST_F(InputDispatcherSpyWindowTest, WatchOutsideTouches) {
9106 auto window = createForeground();
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08009107 window->setOwnerInfo(12, 34);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009108 auto spy = createSpy();
9109 spy->setWatchOutsideTouch(true);
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08009110 spy->setOwnerInfo(56, 78);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009111 spy->setFrame(Rect{0, 0, 20, 20});
9112 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9113
9114 // Inject an event outside the spy window's frame and touchable region.
9115 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08009116 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9117 {100, 200}))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009118 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9119 window->consumeMotionDown();
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08009120 spy->consumeMotionOutsideWithZeroedCoords();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009121}
9122
9123/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009124 * Even when a spy window spans over multiple foreground windows, the spy should receive all
9125 * pointers that are down within its bounds.
9126 */
9127TEST_F(InputDispatcherSpyWindowTest, ReceivesMultiplePointers) {
9128 auto windowLeft = createForeground();
9129 windowLeft->setFrame({0, 0, 100, 200});
9130 auto windowRight = createForeground();
9131 windowRight->setFrame({100, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009132 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009133 spy->setFrame({0, 0, 200, 200});
9134 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, windowLeft, windowRight}}});
9135
9136 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9137 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9138 {50, 50}))
9139 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9140 windowLeft->consumeMotionDown();
9141 spy->consumeMotionDown();
9142
9143 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08009144 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009145 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009146 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
9147 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009148 .build();
9149 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9150 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9151 InputEventInjectionSync::WAIT_FOR_RESULT))
9152 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9153 windowRight->consumeMotionDown();
Harry Cutts33476232023-01-30 19:57:29 +00009154 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009155}
9156
9157/**
9158 * When the first pointer lands outside the spy window and the second pointer lands inside it, the
9159 * the spy should receive the second pointer with ACTION_DOWN.
9160 */
9161TEST_F(InputDispatcherSpyWindowTest, ReceivesSecondPointerAsDown) {
9162 auto window = createForeground();
9163 window->setFrame({0, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009164 auto spyRight = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009165 spyRight->setFrame({100, 0, 200, 200});
9166 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyRight, window}}});
9167
9168 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9169 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9170 {50, 50}))
9171 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9172 window->consumeMotionDown();
9173 spyRight->assertNoEvents();
9174
9175 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08009176 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009177 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009178 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
9179 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009180 .build();
9181 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9182 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9183 InputEventInjectionSync::WAIT_FOR_RESULT))
9184 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Harry Cutts33476232023-01-30 19:57:29 +00009185 window->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009186 spyRight->consumeMotionDown();
9187}
9188
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009189/**
9190 * The spy window should not be able to affect whether or not touches are split. Only the foreground
9191 * windows should be allowed to control split touch.
9192 */
9193TEST_F(InputDispatcherSpyWindowTest, SplitIfNoForegroundWindowTouched) {
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08009194 // This spy window prevents touch splitting. However, we still expect to split touches
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009195 // because a foreground window has not disabled splitting.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009196 auto spy = createSpy();
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08009197 spy->setPreventSplitting(true);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009198
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009199 auto window = createForeground();
9200 window->setFrame(Rect(0, 0, 100, 100));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009201
9202 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9203
9204 // First finger down, no window touched.
9205 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9206 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9207 {100, 200}))
9208 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9209 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
9210 window->assertNoEvents();
9211
9212 // Second finger down on window, the window should receive touch down.
9213 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08009214 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009215 .displayId(ADISPLAY_ID_DEFAULT)
9216 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009217 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
9218 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009219 .build();
9220 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9221 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9222 InputEventInjectionSync::WAIT_FOR_RESULT))
9223 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9224
9225 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Harry Cutts33476232023-01-30 19:57:29 +00009226 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009227}
9228
9229/**
9230 * A spy window will usually be implemented as an un-focusable window. Verify that these windows
9231 * do not receive key events.
9232 */
9233TEST_F(InputDispatcherSpyWindowTest, UnfocusableSpyDoesNotReceiveKeyEvents) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009234 auto spy = createSpy();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009235 spy->setFocusable(false);
9236
9237 auto window = createForeground();
9238 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9239 setFocusedWindow(window);
9240 window->consumeFocusEvent(true);
9241
9242 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
9243 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
9244 window->consumeKeyDown(ADISPLAY_ID_NONE);
9245
9246 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
9247 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
9248 window->consumeKeyUp(ADISPLAY_ID_NONE);
9249
9250 spy->assertNoEvents();
9251}
9252
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009253using InputDispatcherPilferPointersTest = InputDispatcherSpyWindowTest;
9254
9255/**
9256 * A spy window can pilfer pointers. When this happens, touch gestures used by the spy window that
9257 * are currently sent to any other windows - including other spy windows - will also be cancelled.
9258 */
9259TEST_F(InputDispatcherPilferPointersTest, PilferPointers) {
9260 auto window = createForeground();
9261 auto spy1 = createSpy();
9262 auto spy2 = createSpy();
9263 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window}}});
9264
9265 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9266 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9267 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9268 window->consumeMotionDown();
9269 spy1->consumeMotionDown();
9270 spy2->consumeMotionDown();
9271
9272 // Pilfer pointers from the second spy window.
9273 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy2->getToken()));
9274 spy2->assertNoEvents();
9275 spy1->consumeMotionCancel();
9276 window->consumeMotionCancel();
9277
9278 // The rest of the gesture should only be sent to the second spy window.
9279 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9280 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
9281 ADISPLAY_ID_DEFAULT))
9282 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9283 spy2->consumeMotionMove();
9284 spy1->assertNoEvents();
9285 window->assertNoEvents();
9286}
9287
9288/**
9289 * A spy window can pilfer pointers for a gesture even after the foreground window has been removed
9290 * in the middle of the gesture.
9291 */
9292TEST_F(InputDispatcherPilferPointersTest, CanPilferAfterWindowIsRemovedMidStream) {
9293 auto window = createForeground();
9294 auto spy = createSpy();
9295 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9296
9297 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9298 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9299 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9300 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
9301 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
9302
9303 window->releaseChannel();
9304
9305 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9306
9307 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9308 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9309 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9310 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
9311}
9312
9313/**
9314 * After a spy window pilfers pointers, new pointers that go down in its bounds should be sent to
9315 * the spy, but not to any other windows.
9316 */
9317TEST_F(InputDispatcherPilferPointersTest, ContinuesToReceiveGestureAfterPilfer) {
9318 auto spy = createSpy();
9319 auto window = createForeground();
9320
9321 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9322
9323 // First finger down on the window and the spy.
9324 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9325 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9326 {100, 200}))
9327 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9328 spy->consumeMotionDown();
9329 window->consumeMotionDown();
9330
9331 // Spy window pilfers the pointers.
9332 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9333 window->consumeMotionCancel();
9334
9335 // Second finger down on the window and spy, but the window should not receive the pointer down.
9336 const MotionEvent secondFingerDownEvent =
9337 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9338 .displayId(ADISPLAY_ID_DEFAULT)
9339 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009340 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
9341 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009342 .build();
9343 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9344 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9345 InputEventInjectionSync::WAIT_FOR_RESULT))
9346 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9347
Harry Cutts33476232023-01-30 19:57:29 +00009348 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009349
9350 // Third finger goes down outside all windows, so injection should fail.
9351 const MotionEvent thirdFingerDownEvent =
9352 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9353 .displayId(ADISPLAY_ID_DEFAULT)
9354 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009355 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
9356 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
9357 .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(-5).y(-5))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009358 .build();
9359 ASSERT_EQ(InputEventInjectionResult::FAILED,
9360 injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
9361 InputEventInjectionSync::WAIT_FOR_RESULT))
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08009362 << "Inject motion event should return InputEventInjectionResult::FAILED";
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009363
9364 spy->assertNoEvents();
9365 window->assertNoEvents();
9366}
9367
9368/**
9369 * After a spy window pilfers pointers, only the pointers used by the spy should be canceled
9370 */
9371TEST_F(InputDispatcherPilferPointersTest, PartiallyPilferRequiredPointers) {
9372 auto spy = createSpy();
9373 spy->setFrame(Rect(0, 0, 100, 100));
9374 auto window = createForeground();
9375 window->setFrame(Rect(0, 0, 200, 200));
9376
9377 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9378
9379 // First finger down on the window only
9380 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9381 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9382 {150, 150}))
9383 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9384 window->consumeMotionDown();
9385
9386 // Second finger down on the spy and window
9387 const MotionEvent secondFingerDownEvent =
9388 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9389 .displayId(ADISPLAY_ID_DEFAULT)
9390 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009391 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150))
9392 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009393 .build();
9394 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9395 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9396 InputEventInjectionSync::WAIT_FOR_RESULT))
9397 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9398 spy->consumeMotionDown();
9399 window->consumeMotionPointerDown(1);
9400
9401 // Third finger down on the spy and window
9402 const MotionEvent thirdFingerDownEvent =
9403 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9404 .displayId(ADISPLAY_ID_DEFAULT)
9405 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009406 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150))
9407 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10))
9408 .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009409 .build();
9410 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9411 injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
9412 InputEventInjectionSync::WAIT_FOR_RESULT))
9413 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9414 spy->consumeMotionPointerDown(1);
9415 window->consumeMotionPointerDown(2);
9416
9417 // Spy window pilfers the pointers.
9418 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9419 window->consumeMotionPointerUp(/* idx */ 2, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
9420 window->consumeMotionPointerUp(/* idx */ 1, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
9421
9422 spy->assertNoEvents();
9423 window->assertNoEvents();
9424}
9425
9426/**
9427 * After a spy window pilfers pointers, all pilfered pointers that have already been dispatched to
9428 * other windows should be canceled. If this results in the cancellation of all pointers for some
9429 * window, then that window should receive ACTION_CANCEL.
9430 */
9431TEST_F(InputDispatcherPilferPointersTest, PilferAllRequiredPointers) {
9432 auto spy = createSpy();
9433 spy->setFrame(Rect(0, 0, 100, 100));
9434 auto window = createForeground();
9435 window->setFrame(Rect(0, 0, 200, 200));
9436
9437 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9438
9439 // First finger down on both spy and window
9440 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9441 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9442 {10, 10}))
9443 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9444 window->consumeMotionDown();
9445 spy->consumeMotionDown();
9446
9447 // Second finger down on the spy and window
9448 const MotionEvent secondFingerDownEvent =
9449 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9450 .displayId(ADISPLAY_ID_DEFAULT)
9451 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009452 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10))
9453 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009454 .build();
9455 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9456 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9457 InputEventInjectionSync::WAIT_FOR_RESULT))
9458 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9459 spy->consumeMotionPointerDown(1);
9460 window->consumeMotionPointerDown(1);
9461
9462 // Spy window pilfers the pointers.
9463 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9464 window->consumeMotionCancel();
9465
9466 spy->assertNoEvents();
9467 window->assertNoEvents();
9468}
9469
9470/**
9471 * After a spy window pilfers pointers, new pointers that are not touching the spy window can still
9472 * be sent to other windows
9473 */
9474TEST_F(InputDispatcherPilferPointersTest, CanReceivePointersAfterPilfer) {
9475 auto spy = createSpy();
9476 spy->setFrame(Rect(0, 0, 100, 100));
9477 auto window = createForeground();
9478 window->setFrame(Rect(0, 0, 200, 200));
9479
9480 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9481
9482 // First finger down on both window and spy
9483 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9484 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9485 {10, 10}))
9486 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9487 window->consumeMotionDown();
9488 spy->consumeMotionDown();
9489
9490 // Spy window pilfers the pointers.
9491 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9492 window->consumeMotionCancel();
9493
9494 // Second finger down on the window only
9495 const MotionEvent secondFingerDownEvent =
9496 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9497 .displayId(ADISPLAY_ID_DEFAULT)
9498 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009499 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10))
9500 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009501 .build();
9502 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9503 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9504 InputEventInjectionSync::WAIT_FOR_RESULT))
9505 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9506 window->consumeMotionDown();
9507 window->assertNoEvents();
9508
9509 // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line
9510 spy->consumeMotionMove();
9511 spy->assertNoEvents();
9512}
9513
Prabir Pradhand65552b2021-10-07 11:23:50 -07009514class InputDispatcherStylusInterceptorTest : public InputDispatcherTest {
9515public:
9516 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupStylusOverlayScenario() {
9517 std::shared_ptr<FakeApplicationHandle> overlayApplication =
9518 std::make_shared<FakeApplicationHandle>();
9519 sp<FakeWindowHandle> overlay =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009520 sp<FakeWindowHandle>::make(overlayApplication, mDispatcher,
9521 "Stylus interceptor window", ADISPLAY_ID_DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009522 overlay->setFocusable(false);
9523 overlay->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009524 overlay->setTouchable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08009525 overlay->setInterceptsStylus(true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009526 overlay->setTrustedOverlay(true);
9527
9528 std::shared_ptr<FakeApplicationHandle> application =
9529 std::make_shared<FakeApplicationHandle>();
9530 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009531 sp<FakeWindowHandle>::make(application, mDispatcher, "Application window",
9532 ADISPLAY_ID_DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009533 window->setFocusable(true);
9534 window->setOwnerInfo(222, 222);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009535
9536 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
9537 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9538 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00009539 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009540 return {std::move(overlay), std::move(window)};
9541 }
9542
9543 void sendFingerEvent(int32_t action) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00009544 mDispatcher->notifyMotion(
Prabir Pradhand65552b2021-10-07 11:23:50 -07009545 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
Prabir Pradhan678438e2023-04-13 19:32:51 +00009546 ADISPLAY_ID_DEFAULT, {PointF{20, 20}}));
Prabir Pradhand65552b2021-10-07 11:23:50 -07009547 }
9548
9549 void sendStylusEvent(int32_t action) {
9550 NotifyMotionArgs motionArgs =
9551 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
9552 ADISPLAY_ID_DEFAULT, {PointF{30, 40}});
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009553 motionArgs.pointerProperties[0].toolType = ToolType::STYLUS;
Prabir Pradhan678438e2023-04-13 19:32:51 +00009554 mDispatcher->notifyMotion(motionArgs);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009555 }
9556};
9557
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08009558using InputDispatcherStylusInterceptorDeathTest = InputDispatcherStylusInterceptorTest;
9559
9560TEST_F(InputDispatcherStylusInterceptorDeathTest, UntrustedOverlay_AbortsDispatcher) {
9561 ScopedSilentDeath _silentDeath;
9562
Prabir Pradhand65552b2021-10-07 11:23:50 -07009563 auto [overlay, window] = setupStylusOverlayScenario();
9564 overlay->setTrustedOverlay(false);
9565 // Configuring an untrusted overlay as a stylus interceptor should cause Dispatcher to abort.
9566 ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}),
9567 ".* not a trusted overlay");
9568}
9569
9570TEST_F(InputDispatcherStylusInterceptorTest, ConsmesOnlyStylusEvents) {
9571 auto [overlay, window] = setupStylusOverlayScenario();
9572 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9573
9574 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
9575 overlay->consumeMotionDown();
9576 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
9577 overlay->consumeMotionUp();
9578
9579 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
9580 window->consumeMotionDown();
9581 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
9582 window->consumeMotionUp();
9583
9584 overlay->assertNoEvents();
9585 window->assertNoEvents();
9586}
9587
9588TEST_F(InputDispatcherStylusInterceptorTest, SpyWindowStylusInterceptor) {
9589 auto [overlay, window] = setupStylusOverlayScenario();
Prabir Pradhan51e7db02022-02-07 06:02:57 -08009590 overlay->setSpy(true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009591 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9592
9593 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
9594 overlay->consumeMotionDown();
9595 window->consumeMotionDown();
9596 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
9597 overlay->consumeMotionUp();
9598 window->consumeMotionUp();
9599
9600 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
9601 window->consumeMotionDown();
9602 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
9603 window->consumeMotionUp();
9604
9605 overlay->assertNoEvents();
9606 window->assertNoEvents();
9607}
9608
Prabir Pradhan6dfbf262022-03-14 15:24:30 +00009609/**
9610 * Set up a scenario to test the behavior used by the stylus handwriting detection feature.
9611 * The scenario is as follows:
9612 * - The stylus interceptor overlay is configured as a spy window.
9613 * - The stylus interceptor spy receives the start of a new stylus gesture.
9614 * - It pilfers pointers and then configures itself to no longer be a spy.
9615 * - The stylus interceptor continues to receive the rest of the gesture.
9616 */
9617TEST_F(InputDispatcherStylusInterceptorTest, StylusHandwritingScenario) {
9618 auto [overlay, window] = setupStylusOverlayScenario();
9619 overlay->setSpy(true);
9620 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9621
9622 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
9623 overlay->consumeMotionDown();
9624 window->consumeMotionDown();
9625
9626 // The interceptor pilfers the pointers.
9627 EXPECT_EQ(OK, mDispatcher->pilferPointers(overlay->getToken()));
9628 window->consumeMotionCancel();
9629
9630 // The interceptor configures itself so that it is no longer a spy.
9631 overlay->setSpy(false);
9632 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9633
9634 // It continues to receive the rest of the stylus gesture.
9635 sendStylusEvent(AMOTION_EVENT_ACTION_MOVE);
9636 overlay->consumeMotionMove();
9637 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
9638 overlay->consumeMotionUp();
9639
9640 window->assertNoEvents();
9641}
9642
Prabir Pradhan5735a322022-04-11 17:23:34 +00009643struct User {
9644 int32_t mPid;
9645 int32_t mUid;
9646 uint32_t mPolicyFlags{DEFAULT_POLICY_FLAGS};
9647 std::unique_ptr<InputDispatcher>& mDispatcher;
9648
9649 User(std::unique_ptr<InputDispatcher>& dispatcher, int32_t pid, int32_t uid)
9650 : mPid(pid), mUid(uid), mDispatcher(dispatcher) {}
9651
9652 InputEventInjectionResult injectTargetedMotion(int32_t action) const {
9653 return injectMotionEvent(mDispatcher, action, AINPUT_SOURCE_TOUCHSCREEN,
9654 ADISPLAY_ID_DEFAULT, {100, 200},
9655 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
9656 AMOTION_EVENT_INVALID_CURSOR_POSITION},
9657 INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT,
9658 systemTime(SYSTEM_TIME_MONOTONIC), {mUid}, mPolicyFlags);
9659 }
9660
9661 InputEventInjectionResult injectTargetedKey(int32_t action) const {
Harry Cutts33476232023-01-30 19:57:29 +00009662 return inputdispatcher::injectKey(mDispatcher, action, /*repeatCount=*/0, ADISPLAY_ID_NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +00009663 InputEventInjectionSync::WAIT_FOR_RESULT,
Harry Cutts33476232023-01-30 19:57:29 +00009664 INJECT_EVENT_TIMEOUT, /*allowKeyRepeat=*/false, {mUid},
Prabir Pradhan5735a322022-04-11 17:23:34 +00009665 mPolicyFlags);
9666 }
9667
9668 sp<FakeWindowHandle> createWindow() const {
9669 std::shared_ptr<FakeApplicationHandle> overlayApplication =
9670 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009671 sp<FakeWindowHandle> window =
9672 sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, "Owned Window",
9673 ADISPLAY_ID_DEFAULT);
Prabir Pradhan5735a322022-04-11 17:23:34 +00009674 window->setOwnerInfo(mPid, mUid);
9675 return window;
9676 }
9677};
9678
9679using InputDispatcherTargetedInjectionTest = InputDispatcherTest;
9680
9681TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedWindow) {
9682 auto owner = User(mDispatcher, 10, 11);
9683 auto window = owner.createWindow();
9684 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
9685
9686 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9687 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9688 window->consumeMotionDown();
9689
9690 setFocusedWindow(window);
9691 window->consumeFocusEvent(true);
9692
9693 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9694 owner.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
9695 window->consumeKeyDown(ADISPLAY_ID_NONE);
9696}
9697
9698TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedWindow) {
9699 auto owner = User(mDispatcher, 10, 11);
9700 auto window = owner.createWindow();
9701 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
9702
9703 auto rando = User(mDispatcher, 20, 21);
9704 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
9705 rando.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9706
9707 setFocusedWindow(window);
9708 window->consumeFocusEvent(true);
9709
9710 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
9711 rando.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
9712 window->assertNoEvents();
9713}
9714
9715TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedSpyWindow) {
9716 auto owner = User(mDispatcher, 10, 11);
9717 auto window = owner.createWindow();
9718 auto spy = owner.createWindow();
9719 spy->setSpy(true);
9720 spy->setTrustedOverlay(true);
9721 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9722
9723 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9724 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9725 spy->consumeMotionDown();
9726 window->consumeMotionDown();
9727}
9728
9729TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedSpyWindow) {
9730 auto owner = User(mDispatcher, 10, 11);
9731 auto window = owner.createWindow();
9732
9733 auto rando = User(mDispatcher, 20, 21);
9734 auto randosSpy = rando.createWindow();
9735 randosSpy->setSpy(true);
9736 randosSpy->setTrustedOverlay(true);
9737 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}});
9738
9739 // The event is targeted at owner's window, so injection should succeed, but the spy should
9740 // not receive the event.
9741 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9742 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9743 randosSpy->assertNoEvents();
9744 window->consumeMotionDown();
9745}
9746
9747TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoAnyWindowWhenNotTargeting) {
9748 auto owner = User(mDispatcher, 10, 11);
9749 auto window = owner.createWindow();
9750
9751 auto rando = User(mDispatcher, 20, 21);
9752 auto randosSpy = rando.createWindow();
9753 randosSpy->setSpy(true);
9754 randosSpy->setTrustedOverlay(true);
9755 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}});
9756
9757 // A user that has injection permission can inject into any window.
9758 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9759 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
9760 ADISPLAY_ID_DEFAULT));
9761 randosSpy->consumeMotionDown();
9762 window->consumeMotionDown();
9763
9764 setFocusedWindow(randosSpy);
9765 randosSpy->consumeFocusEvent(true);
9766
9767 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher));
9768 randosSpy->consumeKeyDown(ADISPLAY_ID_NONE);
9769 window->assertNoEvents();
9770}
9771
9772TEST_F(InputDispatcherTargetedInjectionTest, CanGenerateActionOutsideToOtherUids) {
9773 auto owner = User(mDispatcher, 10, 11);
9774 auto window = owner.createWindow();
9775
9776 auto rando = User(mDispatcher, 20, 21);
9777 auto randosWindow = rando.createWindow();
9778 randosWindow->setFrame(Rect{-10, -10, -5, -5});
9779 randosWindow->setWatchOutsideTouch(true);
9780 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosWindow, window}}});
9781
9782 // We allow generation of ACTION_OUTSIDE events into windows owned by different uids.
9783 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9784 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9785 window->consumeMotionDown();
9786 randosWindow->consumeMotionOutside();
9787}
9788
Garfield Tane84e6f92019-08-29 17:28:41 -07009789} // namespace android::inputdispatcher