blob: a2fe911e1c0a514a93c7b30a78805b3631c5ce69 [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 Vishniakou487c49b2022-12-02 15:48:57 -080061static constexpr int32_t ACTION_OUTSIDE = AMOTION_EVENT_ACTION_OUTSIDE;
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -080062static constexpr int32_t ACTION_CANCEL = AMOTION_EVENT_ACTION_CANCEL;
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -080063static constexpr int32_t POINTER_1_DOWN =
64 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +000065static constexpr int32_t POINTER_2_DOWN =
66 AMOTION_EVENT_ACTION_POINTER_DOWN | (2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +000067static constexpr int32_t POINTER_3_DOWN =
68 AMOTION_EVENT_ACTION_POINTER_DOWN | (3 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Arthur Hungc539dbb2022-12-08 07:45:36 +000069static constexpr int32_t POINTER_0_UP =
70 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -080071static constexpr int32_t POINTER_1_UP =
72 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
73
Antonio Kantek15beb512022-06-13 22:35:41 +000074// The default pid and uid for windows created on the primary display by the test.
Prabir Pradhan5735a322022-04-11 17:23:34 +000075static constexpr int32_t WINDOW_PID = 999;
76static constexpr int32_t WINDOW_UID = 1001;
77
Antonio Kantek15beb512022-06-13 22:35:41 +000078// The default pid and uid for the windows created on the secondary display by the test.
79static constexpr int32_t SECONDARY_WINDOW_PID = 1010;
80static constexpr int32_t SECONDARY_WINDOW_UID = 1012;
81
Prabir Pradhan5735a322022-04-11 17:23:34 +000082// The default policy flags to use for event injection by tests.
83static constexpr uint32_t DEFAULT_POLICY_FLAGS = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER;
Michael Wrightd02c5b62014-02-10 15:10:22 -080084
Siarhei Vishniakou58cfc602020-12-14 23:21:30 +000085// An arbitrary pid of the gesture monitor window
86static constexpr int32_t MONITOR_PID = 2001;
87
Siarhei Vishniakou289e9242022-02-15 14:50:16 -080088static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 1000ms;
89
Arthur Hungc539dbb2022-12-08 07:45:36 +000090static constexpr int expectedWallpaperFlags =
91 AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED;
92
chaviwd1c23182019-12-20 18:44:56 -080093struct PointF {
94 float x;
95 float y;
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -080096 auto operator<=>(const PointF&) const = default;
chaviwd1c23182019-12-20 18:44:56 -080097};
Michael Wrightd02c5b62014-02-10 15:10:22 -080098
Gang Wang342c9272020-01-13 13:15:04 -050099/**
100 * Return a DOWN key event with KEYCODE_A.
101 */
102static KeyEvent getTestKeyEvent() {
103 KeyEvent event;
104
Garfield Tanfbe732e2020-01-24 11:26:14 -0800105 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
106 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
107 ARBITRARY_TIME, ARBITRARY_TIME);
Gang Wang342c9272020-01-13 13:15:04 -0500108 return event;
109}
110
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000111static void assertMotionAction(int32_t expectedAction, int32_t receivedAction) {
112 ASSERT_EQ(expectedAction, receivedAction)
113 << "expected " << MotionEvent::actionToString(expectedAction) << ", got "
114 << MotionEvent::actionToString(receivedAction);
115}
116
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -0800117MATCHER_P(WithMotionAction, action, "MotionEvent with specified action") {
118 bool matches = action == arg.getAction();
119 if (!matches) {
120 *result_listener << "expected action " << MotionEvent::actionToString(action)
121 << ", but got " << MotionEvent::actionToString(arg.getAction());
122 }
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -0800123 if (action == AMOTION_EVENT_ACTION_DOWN) {
124 if (!matches) {
125 *result_listener << "; ";
126 }
127 *result_listener << "downTime should match eventTime for ACTION_DOWN events";
128 matches &= arg.getDownTime() == arg.getEventTime();
129 }
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -0800130 if (action == AMOTION_EVENT_ACTION_CANCEL) {
131 if (!matches) {
132 *result_listener << "; ";
133 }
134 *result_listener << "expected FLAG_CANCELED to be set with ACTION_CANCEL, but was not set";
135 matches &= (arg.getFlags() & AMOTION_EVENT_FLAG_CANCELED) != 0;
136 }
137 return matches;
138}
139
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -0800140MATCHER_P(WithDownTime, downTime, "InputEvent with specified downTime") {
141 return arg.getDownTime() == downTime;
142}
143
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -0800144MATCHER_P(WithDisplayId, displayId, "InputEvent with specified displayId") {
145 return arg.getDisplayId() == displayId;
146}
147
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -0800148MATCHER_P(WithSource, source, "InputEvent with specified source") {
149 *result_listener << "expected source " << inputEventSourceToString(source) << ", but got "
150 << inputEventSourceToString(arg.getSource());
151 return arg.getSource() == source;
152}
153
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -0800154MATCHER_P(WithFlags, flags, "InputEvent with specified flags") {
155 return arg.getFlags() == flags;
156}
157
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -0800158MATCHER_P2(WithCoords, x, y, "MotionEvent with specified coordinates") {
159 if (arg.getPointerCount() != 1) {
160 *result_listener << "Expected 1 pointer, got " << arg.getPointerCount();
161 return false;
162 }
Harry Cutts33476232023-01-30 19:57:29 +0000163 return arg.getX(/*pointerIndex=*/0) == x && arg.getY(/*pointerIndex=*/0) == y;
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -0800164}
165
166MATCHER_P(WithPointers, pointers, "MotionEvent with specified pointers") {
167 // Build a map for the received pointers, by pointer id
168 std::map<int32_t /*pointerId*/, PointF> actualPointers;
169 for (size_t pointerIndex = 0; pointerIndex < arg.getPointerCount(); pointerIndex++) {
170 const int32_t pointerId = arg.getPointerId(pointerIndex);
171 actualPointers[pointerId] = {arg.getX(pointerIndex), arg.getY(pointerIndex)};
172 }
173 return pointers == actualPointers;
174}
175
Michael Wrightd02c5b62014-02-10 15:10:22 -0800176// --- FakeInputDispatcherPolicy ---
177
178class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface {
179 InputDispatcherConfiguration mConfig;
180
Prabir Pradhanedd96402022-02-15 01:46:16 -0800181 using AnrResult = std::pair<sp<IBinder>, int32_t /*pid*/>;
182
Michael Wrightd02c5b62014-02-10 15:10:22 -0800183protected:
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000184 virtual ~FakeInputDispatcherPolicy() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800185
186public:
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000187 FakeInputDispatcherPolicy() {}
Jackal Guof9696682018-10-05 12:23:23 +0800188
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800189 void assertFilterInputEventWasCalled(const NotifyKeyArgs& args) {
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700190 assertFilterInputEventWasCalledInternal([&args](const InputEvent& event) {
191 ASSERT_EQ(event.getType(), AINPUT_EVENT_TYPE_KEY);
192 EXPECT_EQ(event.getDisplayId(), args.displayId);
193
194 const auto& keyEvent = static_cast<const KeyEvent&>(event);
195 EXPECT_EQ(keyEvent.getEventTime(), args.eventTime);
196 EXPECT_EQ(keyEvent.getAction(), args.action);
197 });
Jackal Guof9696682018-10-05 12:23:23 +0800198 }
199
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700200 void assertFilterInputEventWasCalled(const NotifyMotionArgs& args, vec2 point) {
201 assertFilterInputEventWasCalledInternal([&](const InputEvent& event) {
202 ASSERT_EQ(event.getType(), AINPUT_EVENT_TYPE_MOTION);
203 EXPECT_EQ(event.getDisplayId(), args.displayId);
204
205 const auto& motionEvent = static_cast<const MotionEvent&>(event);
206 EXPECT_EQ(motionEvent.getEventTime(), args.eventTime);
207 EXPECT_EQ(motionEvent.getAction(), args.action);
208 EXPECT_EQ(motionEvent.getX(0), point.x);
209 EXPECT_EQ(motionEvent.getY(0), point.y);
210 EXPECT_EQ(motionEvent.getRawX(0), point.x);
211 EXPECT_EQ(motionEvent.getRawY(0), point.y);
212 });
Jackal Guof9696682018-10-05 12:23:23 +0800213 }
214
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700215 void assertFilterInputEventWasNotCalled() {
216 std::scoped_lock lock(mLock);
217 ASSERT_EQ(nullptr, mFilteredEvent);
218 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800219
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800220 void assertNotifyConfigurationChangedWasCalled(nsecs_t when) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700221 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800222 ASSERT_TRUE(mConfigurationChangedTime)
223 << "Timed out waiting for configuration changed call";
224 ASSERT_EQ(*mConfigurationChangedTime, when);
225 mConfigurationChangedTime = std::nullopt;
226 }
227
228 void assertNotifySwitchWasCalled(const NotifySwitchArgs& args) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700229 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800230 ASSERT_TRUE(mLastNotifySwitch);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800231 // We do not check id because it is not exposed to the policy
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800232 EXPECT_EQ(args.eventTime, mLastNotifySwitch->eventTime);
233 EXPECT_EQ(args.policyFlags, mLastNotifySwitch->policyFlags);
234 EXPECT_EQ(args.switchValues, mLastNotifySwitch->switchValues);
235 EXPECT_EQ(args.switchMask, mLastNotifySwitch->switchMask);
236 mLastNotifySwitch = std::nullopt;
237 }
238
chaviwfd6d3512019-03-25 13:23:49 -0700239 void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700240 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800241 ASSERT_EQ(touchedToken, mOnPointerDownToken);
242 mOnPointerDownToken.clear();
243 }
244
245 void assertOnPointerDownWasNotCalled() {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700246 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800247 ASSERT_TRUE(mOnPointerDownToken == nullptr)
248 << "Expected onPointerDownOutsideFocus to not have been called";
chaviwfd6d3512019-03-25 13:23:49 -0700249 }
250
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700251 // This function must be called soon after the expected ANR timer starts,
252 // because we are also checking how much time has passed.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500253 void assertNotifyNoFocusedWindowAnrWasCalled(
Chris Yea209fde2020-07-22 13:54:51 -0700254 std::chrono::nanoseconds timeout,
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500255 const std::shared_ptr<InputApplicationHandle>& expectedApplication) {
Prabir Pradhanedd96402022-02-15 01:46:16 -0800256 std::unique_lock lock(mLock);
257 android::base::ScopedLockAssertion assumeLocked(mLock);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500258 std::shared_ptr<InputApplicationHandle> application;
Prabir Pradhanedd96402022-02-15 01:46:16 -0800259 ASSERT_NO_FATAL_FAILURE(
260 application = getAnrTokenLockedInterruptible(timeout, mAnrApplications, lock));
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500261 ASSERT_EQ(expectedApplication, application);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700262 }
263
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000264 void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout,
Prabir Pradhanedd96402022-02-15 01:46:16 -0800265 const sp<WindowInfoHandle>& window) {
266 LOG_ALWAYS_FATAL_IF(window == nullptr, "window should not be null");
267 assertNotifyWindowUnresponsiveWasCalled(timeout, window->getToken(),
268 window->getInfo()->ownerPid);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500269 }
270
Prabir Pradhanedd96402022-02-15 01:46:16 -0800271 void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout,
272 const sp<IBinder>& expectedToken,
273 int32_t expectedPid) {
274 std::unique_lock lock(mLock);
275 android::base::ScopedLockAssertion assumeLocked(mLock);
276 AnrResult result;
277 ASSERT_NO_FATAL_FAILURE(result =
278 getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock));
279 const auto& [token, pid] = result;
280 ASSERT_EQ(expectedToken, token);
281 ASSERT_EQ(expectedPid, pid);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500282 }
283
Prabir Pradhanedd96402022-02-15 01:46:16 -0800284 /** Wrap call with ASSERT_NO_FATAL_FAILURE() to ensure the return value is valid. */
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000285 sp<IBinder> getUnresponsiveWindowToken(std::chrono::nanoseconds timeout) {
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500286 std::unique_lock lock(mLock);
287 android::base::ScopedLockAssertion assumeLocked(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800288 AnrResult result = getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock);
289 const auto& [token, _] = result;
290 return token;
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000291 }
292
Prabir Pradhanedd96402022-02-15 01:46:16 -0800293 void assertNotifyWindowResponsiveWasCalled(const sp<IBinder>& expectedToken,
294 int32_t expectedPid) {
295 std::unique_lock lock(mLock);
296 android::base::ScopedLockAssertion assumeLocked(mLock);
297 AnrResult result;
298 ASSERT_NO_FATAL_FAILURE(
299 result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock));
300 const auto& [token, pid] = result;
301 ASSERT_EQ(expectedToken, token);
302 ASSERT_EQ(expectedPid, pid);
303 }
304
305 /** Wrap call with ASSERT_NO_FATAL_FAILURE() to ensure the return value is valid. */
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000306 sp<IBinder> getResponsiveWindowToken() {
307 std::unique_lock lock(mLock);
308 android::base::ScopedLockAssertion assumeLocked(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800309 AnrResult result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock);
310 const auto& [token, _] = result;
311 return token;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700312 }
313
314 void assertNotifyAnrWasNotCalled() {
315 std::scoped_lock lock(mLock);
316 ASSERT_TRUE(mAnrApplications.empty());
Prabir Pradhanedd96402022-02-15 01:46:16 -0800317 ASSERT_TRUE(mAnrWindows.empty());
318 ASSERT_TRUE(mResponsiveWindows.empty())
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500319 << "ANR was not called, but please also consume the 'connection is responsive' "
320 "signal";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700321 }
322
Garfield Tan1c7bc862020-01-28 13:24:04 -0800323 void setKeyRepeatConfiguration(nsecs_t timeout, nsecs_t delay) {
324 mConfig.keyRepeatTimeout = timeout;
325 mConfig.keyRepeatDelay = delay;
326 }
327
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000328 PointerCaptureRequest assertSetPointerCaptureCalled(bool enabled) {
Prabir Pradhan99987712020-11-10 18:43:05 -0800329 std::unique_lock lock(mLock);
330 base::ScopedLockAssertion assumeLocked(mLock);
331
332 if (!mPointerCaptureChangedCondition.wait_for(lock, 100ms,
333 [this, enabled]() REQUIRES(mLock) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000334 return mPointerCaptureRequest->enable ==
Prabir Pradhan99987712020-11-10 18:43:05 -0800335 enabled;
336 })) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000337 ADD_FAILURE() << "Timed out waiting for setPointerCapture(" << enabled
338 << ") to be called.";
339 return {};
Prabir Pradhan99987712020-11-10 18:43:05 -0800340 }
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000341 auto request = *mPointerCaptureRequest;
342 mPointerCaptureRequest.reset();
343 return request;
Prabir Pradhan99987712020-11-10 18:43:05 -0800344 }
345
346 void assertSetPointerCaptureNotCalled() {
347 std::unique_lock lock(mLock);
348 base::ScopedLockAssertion assumeLocked(mLock);
349
350 if (mPointerCaptureChangedCondition.wait_for(lock, 100ms) != std::cv_status::timeout) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000351 FAIL() << "Expected setPointerCapture(request) to not be called, but was called. "
Prabir Pradhan99987712020-11-10 18:43:05 -0800352 "enabled = "
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000353 << std::to_string(mPointerCaptureRequest->enable);
Prabir Pradhan99987712020-11-10 18:43:05 -0800354 }
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000355 mPointerCaptureRequest.reset();
Prabir Pradhan99987712020-11-10 18:43:05 -0800356 }
357
arthurhungf452d0b2021-01-06 00:19:52 +0800358 void assertDropTargetEquals(const sp<IBinder>& targetToken) {
359 std::scoped_lock lock(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800360 ASSERT_TRUE(mNotifyDropWindowWasCalled);
arthurhungf452d0b2021-01-06 00:19:52 +0800361 ASSERT_EQ(targetToken, mDropTargetWindowToken);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800362 mNotifyDropWindowWasCalled = false;
arthurhungf452d0b2021-01-06 00:19:52 +0800363 }
364
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800365 void assertNotifyInputChannelBrokenWasCalled(const sp<IBinder>& token) {
366 std::unique_lock lock(mLock);
367 base::ScopedLockAssertion assumeLocked(mLock);
368 std::optional<sp<IBinder>> receivedToken =
369 getItemFromStorageLockedInterruptible(100ms, mBrokenInputChannels, lock,
370 mNotifyInputChannelBroken);
371 ASSERT_TRUE(receivedToken.has_value());
372 ASSERT_EQ(token, *receivedToken);
373 }
374
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800375 /**
376 * Set policy timeout. A value of zero means next key will not be intercepted.
377 */
378 void setInterceptKeyTimeout(std::chrono::milliseconds timeout) {
379 mInterceptKeyTimeout = timeout;
380 }
381
Michael Wrightd02c5b62014-02-10 15:10:22 -0800382private:
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700383 std::mutex mLock;
384 std::unique_ptr<InputEvent> mFilteredEvent GUARDED_BY(mLock);
385 std::optional<nsecs_t> mConfigurationChangedTime GUARDED_BY(mLock);
386 sp<IBinder> mOnPointerDownToken GUARDED_BY(mLock);
387 std::optional<NotifySwitchArgs> mLastNotifySwitch GUARDED_BY(mLock);
Jackal Guof9696682018-10-05 12:23:23 +0800388
Prabir Pradhan99987712020-11-10 18:43:05 -0800389 std::condition_variable mPointerCaptureChangedCondition;
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000390
391 std::optional<PointerCaptureRequest> mPointerCaptureRequest GUARDED_BY(mLock);
Prabir Pradhan99987712020-11-10 18:43:05 -0800392
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700393 // ANR handling
Chris Yea209fde2020-07-22 13:54:51 -0700394 std::queue<std::shared_ptr<InputApplicationHandle>> mAnrApplications GUARDED_BY(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800395 std::queue<AnrResult> mAnrWindows GUARDED_BY(mLock);
396 std::queue<AnrResult> mResponsiveWindows GUARDED_BY(mLock);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700397 std::condition_variable mNotifyAnr;
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800398 std::queue<sp<IBinder>> mBrokenInputChannels GUARDED_BY(mLock);
399 std::condition_variable mNotifyInputChannelBroken;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700400
arthurhungf452d0b2021-01-06 00:19:52 +0800401 sp<IBinder> mDropTargetWindowToken GUARDED_BY(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800402 bool mNotifyDropWindowWasCalled GUARDED_BY(mLock) = false;
arthurhungf452d0b2021-01-06 00:19:52 +0800403
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800404 std::chrono::milliseconds mInterceptKeyTimeout = 0ms;
405
Prabir Pradhanedd96402022-02-15 01:46:16 -0800406 // All three ANR-related callbacks behave the same way, so we use this generic function to wait
407 // for a specific container to become non-empty. When the container is non-empty, return the
408 // first entry from the container and erase it.
409 template <class T>
410 T getAnrTokenLockedInterruptible(std::chrono::nanoseconds timeout, std::queue<T>& storage,
411 std::unique_lock<std::mutex>& lock) REQUIRES(mLock) {
412 // If there is an ANR, Dispatcher won't be idle because there are still events
413 // in the waitQueue that we need to check on. So we can't wait for dispatcher to be idle
414 // before checking if ANR was called.
415 // Since dispatcher is not guaranteed to call notifyNoFocusedWindowAnr right away, we need
416 // to provide it some time to act. 100ms seems reasonable.
417 std::chrono::duration timeToWait = timeout + 100ms; // provide some slack
418 const std::chrono::time_point start = std::chrono::steady_clock::now();
419 std::optional<T> token =
420 getItemFromStorageLockedInterruptible(timeToWait, storage, lock, mNotifyAnr);
421 if (!token.has_value()) {
422 ADD_FAILURE() << "Did not receive the ANR callback";
423 return {};
424 }
425
426 const std::chrono::duration waited = std::chrono::steady_clock::now() - start;
427 // Ensure that the ANR didn't get raised too early. We can't be too strict here because
428 // the dispatcher started counting before this function was called
429 if (std::chrono::abs(timeout - waited) > 100ms) {
430 ADD_FAILURE() << "ANR was raised too early or too late. Expected "
431 << std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count()
432 << "ms, but waited "
433 << std::chrono::duration_cast<std::chrono::milliseconds>(waited).count()
434 << "ms instead";
435 }
436 return *token;
437 }
438
439 template <class T>
440 std::optional<T> getItemFromStorageLockedInterruptible(std::chrono::nanoseconds timeout,
441 std::queue<T>& storage,
442 std::unique_lock<std::mutex>& lock,
443 std::condition_variable& condition)
444 REQUIRES(mLock) {
445 condition.wait_for(lock, timeout,
446 [&storage]() REQUIRES(mLock) { return !storage.empty(); });
447 if (storage.empty()) {
448 ADD_FAILURE() << "Did not receive the expected callback";
449 return std::nullopt;
450 }
451 T item = storage.front();
452 storage.pop();
453 return std::make_optional(item);
454 }
455
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600456 void notifyConfigurationChanged(nsecs_t when) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700457 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800458 mConfigurationChangedTime = when;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800459 }
460
Prabir Pradhanedd96402022-02-15 01:46:16 -0800461 void notifyWindowUnresponsive(const sp<IBinder>& connectionToken, std::optional<int32_t> pid,
462 const std::string&) override {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700463 std::scoped_lock lock(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800464 ASSERT_TRUE(pid.has_value());
465 mAnrWindows.push({connectionToken, *pid});
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700466 mNotifyAnr.notify_all();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500467 }
468
Prabir Pradhanedd96402022-02-15 01:46:16 -0800469 void notifyWindowResponsive(const sp<IBinder>& connectionToken,
470 std::optional<int32_t> pid) override {
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500471 std::scoped_lock lock(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800472 ASSERT_TRUE(pid.has_value());
473 mResponsiveWindows.push({connectionToken, *pid});
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500474 mNotifyAnr.notify_all();
475 }
476
477 void notifyNoFocusedWindowAnr(
478 const std::shared_ptr<InputApplicationHandle>& applicationHandle) override {
479 std::scoped_lock lock(mLock);
480 mAnrApplications.push(applicationHandle);
481 mNotifyAnr.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800482 }
483
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800484 void notifyInputChannelBroken(const sp<IBinder>& connectionToken) override {
485 std::scoped_lock lock(mLock);
486 mBrokenInputChannels.push(connectionToken);
487 mNotifyInputChannelBroken.notify_all();
488 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800489
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600490 void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {}
Robert Carr740167f2018-10-11 19:03:41 -0700491
Chris Yef59a2f42020-10-16 12:55:26 -0700492 void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType,
493 InputDeviceSensorAccuracy accuracy, nsecs_t timestamp,
494 const std::vector<float>& values) override {}
495
496 void notifySensorAccuracy(int deviceId, InputDeviceSensorType sensorType,
497 InputDeviceSensorAccuracy accuracy) override {}
Bernardo Rufino2e1f6512020-10-08 13:42:07 +0000498
Chris Yefb552902021-02-03 17:18:37 -0800499 void notifyVibratorState(int32_t deviceId, bool isOn) override {}
500
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600501 void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800502 *outConfig = mConfig;
503 }
504
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600505 bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700506 std::scoped_lock lock(mLock);
Jackal Guof9696682018-10-05 12:23:23 +0800507 switch (inputEvent->getType()) {
508 case AINPUT_EVENT_TYPE_KEY: {
509 const KeyEvent* keyEvent = static_cast<const KeyEvent*>(inputEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800510 mFilteredEvent = std::make_unique<KeyEvent>(*keyEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800511 break;
512 }
513
514 case AINPUT_EVENT_TYPE_MOTION: {
515 const MotionEvent* motionEvent = static_cast<const MotionEvent*>(inputEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800516 mFilteredEvent = std::make_unique<MotionEvent>(*motionEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800517 break;
518 }
519 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800520 return true;
521 }
522
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800523 void interceptKeyBeforeQueueing(const KeyEvent* inputEvent, uint32_t&) override {
524 if (inputEvent->getAction() == AKEY_EVENT_ACTION_UP) {
525 // Clear intercept state when we handled the event.
526 mInterceptKeyTimeout = 0ms;
527 }
528 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800529
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600530 void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800531
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600532 nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent*, uint32_t) override {
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800533 nsecs_t delay = std::chrono::nanoseconds(mInterceptKeyTimeout).count();
534 // Clear intercept state so we could dispatch the event in next wake.
535 mInterceptKeyTimeout = 0ms;
536 return delay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800537 }
538
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600539 bool dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent*, uint32_t, KeyEvent*) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800540 return false;
541 }
542
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600543 void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask,
544 uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700545 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800546 /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is
547 * essentially a passthrough for notifySwitch.
548 */
Harry Cutts33476232023-01-30 19:57:29 +0000549 mLastNotifySwitch = NotifySwitchArgs(/*id=*/1, when, policyFlags, switchValues, switchMask);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800550 }
551
Sean Stoutb4e0a592021-02-23 07:34:53 -0800552 void pokeUserActivity(nsecs_t, int32_t, int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800553
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600554 void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700555 std::scoped_lock lock(mLock);
chaviwfd6d3512019-03-25 13:23:49 -0700556 mOnPointerDownToken = newToken;
557 }
558
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000559 void setPointerCapture(const PointerCaptureRequest& request) override {
Prabir Pradhan99987712020-11-10 18:43:05 -0800560 std::scoped_lock lock(mLock);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000561 mPointerCaptureRequest = {request};
Prabir Pradhan99987712020-11-10 18:43:05 -0800562 mPointerCaptureChangedCondition.notify_all();
563 }
564
arthurhungf452d0b2021-01-06 00:19:52 +0800565 void notifyDropWindow(const sp<IBinder>& token, float x, float y) override {
566 std::scoped_lock lock(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800567 mNotifyDropWindowWasCalled = true;
arthurhungf452d0b2021-01-06 00:19:52 +0800568 mDropTargetWindowToken = token;
569 }
570
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700571 void assertFilterInputEventWasCalledInternal(
572 const std::function<void(const InputEvent&)>& verify) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700573 std::scoped_lock lock(mLock);
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -0800574 ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called.";
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700575 verify(*mFilteredEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800576 mFilteredEvent = nullptr;
Jackal Guof9696682018-10-05 12:23:23 +0800577 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800578};
579
Michael Wrightd02c5b62014-02-10 15:10:22 -0800580// --- InputDispatcherTest ---
581
582class InputDispatcherTest : public testing::Test {
583protected:
584 sp<FakeInputDispatcherPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700585 std::unique_ptr<InputDispatcher> mDispatcher;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800586
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000587 void SetUp() override {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700588 mFakePolicy = sp<FakeInputDispatcherPolicy>::make();
Siarhei Vishniakou289e9242022-02-15 14:50:16 -0800589 mDispatcher = std::make_unique<InputDispatcher>(mFakePolicy, STALE_EVENT_TIMEOUT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800590 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000591 // Start InputDispatcher thread
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700592 ASSERT_EQ(OK, mDispatcher->start());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800593 }
594
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000595 void TearDown() override {
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700596 ASSERT_EQ(OK, mDispatcher->stop());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800597 mFakePolicy.clear();
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700598 mDispatcher.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800599 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700600
601 /**
602 * Used for debugging when writing the test
603 */
604 void dumpDispatcherState() {
605 std::string dump;
606 mDispatcher->dump(dump);
607 std::stringstream ss(dump);
608 std::string to;
609
610 while (std::getline(ss, to, '\n')) {
611 ALOGE("%s", to.c_str());
612 }
613 }
Vishnu Nair958da932020-08-21 17:12:37 -0700614
chaviw3277faf2021-05-19 16:45:23 -0500615 void setFocusedWindow(const sp<WindowInfoHandle>& window,
616 const sp<WindowInfoHandle>& focusedWindow = nullptr) {
Vishnu Nair958da932020-08-21 17:12:37 -0700617 FocusRequest request;
618 request.token = window->getToken();
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +0000619 request.windowName = window->getName();
Vishnu Nair958da932020-08-21 17:12:37 -0700620 if (focusedWindow) {
621 request.focusedToken = focusedWindow->getToken();
622 }
623 request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
624 request.displayId = window->getInfo()->displayId;
625 mDispatcher->setFocusedWindow(request);
626 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800627};
628
Michael Wrightd02c5b62014-02-10 15:10:22 -0800629TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) {
630 KeyEvent event;
631
632 // Rejects undefined key actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800633 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
634 INVALID_HMAC,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600635 /*action*/ -1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME,
636 ARBITRARY_TIME);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800637 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000638 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000639 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800640 << "Should reject key events with undefined action.";
641
642 // Rejects ACTION_MULTIPLE since it is not supported despite being defined in the API.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800643 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
644 INVALID_HMAC, AKEY_EVENT_ACTION_MULTIPLE, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600645 ARBITRARY_TIME, ARBITRARY_TIME);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800646 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000647 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000648 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800649 << "Should reject key events with ACTION_MULTIPLE.";
650}
651
652TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) {
653 MotionEvent event;
654 PointerProperties pointerProperties[MAX_POINTERS + 1];
655 PointerCoords pointerCoords[MAX_POINTERS + 1];
Siarhei Vishniakou01747382022-01-20 13:23:27 -0800656 for (size_t i = 0; i <= MAX_POINTERS; i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800657 pointerProperties[i].clear();
658 pointerProperties[i].id = i;
659 pointerCoords[i].clear();
660 }
661
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800662 // Some constants commonly used below
663 constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN;
664 constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE;
665 constexpr int32_t metaState = AMETA_NONE;
666 constexpr MotionClassification classification = MotionClassification::NONE;
667
chaviw9eaa22c2020-07-01 16:21:27 -0700668 ui::Transform identityTransform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800669 // Rejects undefined motion actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800670 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
chaviw9eaa22c2020-07-01 16:21:27 -0700671 /*action*/ -1, 0, 0, edgeFlags, metaState, 0, classification,
672 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700673 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
674 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700675 /*pointerCount*/ 1, pointerProperties, pointerCoords);
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 motion events with undefined action.";
680
681 // Rejects pointer down with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800682 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -0800683 POINTER_1_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
684 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
685 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
686 ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500687 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800688 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000689 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000690 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800691 << "Should reject motion events with pointer down index too large.";
692
Garfield Tanfbe732e2020-01-24 11:26:14 -0800693 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700694 AMOTION_EVENT_ACTION_POINTER_DOWN |
695 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700696 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
697 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700698 identityTransform, ARBITRARY_TIME, ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500699 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800700 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000701 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000702 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800703 << "Should reject motion events with pointer down index too small.";
704
705 // Rejects pointer up with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800706 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -0800707 POINTER_1_UP, 0, 0, edgeFlags, metaState, 0, classification, identityTransform,
708 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
709 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
710 ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500711 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800712 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000713 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000714 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800715 << "Should reject motion events with pointer up index too large.";
716
Garfield Tanfbe732e2020-01-24 11:26:14 -0800717 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700718 AMOTION_EVENT_ACTION_POINTER_UP |
719 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700720 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
721 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700722 identityTransform, ARBITRARY_TIME, ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500723 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800724 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000725 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000726 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800727 << "Should reject motion events with pointer up index too small.";
728
729 // Rejects motion events with invalid number of pointers.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800730 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
731 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700732 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700733 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
734 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700735 /*pointerCount*/ 0, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800736 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000737 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000738 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800739 << "Should reject motion events with 0 pointers.";
740
Garfield Tanfbe732e2020-01-24 11:26:14 -0800741 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
742 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700743 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700744 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
745 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700746 /*pointerCount*/ MAX_POINTERS + 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800747 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000748 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000749 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800750 << "Should reject motion events with more than MAX_POINTERS pointers.";
751
752 // Rejects motion events with invalid pointer ids.
753 pointerProperties[0].id = -1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800754 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
755 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700756 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700757 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
758 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700759 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800760 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000761 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000762 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800763 << "Should reject motion events with pointer ids less than 0.";
764
765 pointerProperties[0].id = MAX_POINTER_ID + 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800766 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
767 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700768 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700769 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
770 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700771 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800772 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000773 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000774 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800775 << "Should reject motion events with pointer ids greater than MAX_POINTER_ID.";
776
777 // Rejects motion events with duplicate pointer ids.
778 pointerProperties[0].id = 1;
779 pointerProperties[1].id = 1;
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*/ 2, 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 duplicate pointer ids.";
790}
791
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800792/* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */
793
794TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) {
795 constexpr nsecs_t eventTime = 20;
Harry Cutts33476232023-01-30 19:57:29 +0000796 NotifyConfigurationChangedArgs args(/*id=*/10, eventTime);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800797 mDispatcher->notifyConfigurationChanged(&args);
798 ASSERT_TRUE(mDispatcher->waitForIdle());
799
800 mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime);
801}
802
803TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) {
Harry Cutts33476232023-01-30 19:57:29 +0000804 NotifySwitchArgs args(/*id=*/10, /*eventTime=*/20, /*policyFlags=*/0, /*switchValues=*/1,
805 /*switchMask=*/2);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800806 mDispatcher->notifySwitch(&args);
807
808 // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener
809 args.policyFlags |= POLICY_FLAG_TRUSTED;
810 mFakePolicy->assertNotifySwitchWasCalled(args);
811}
812
Arthur Hungb92218b2018-08-14 12:00:21 +0800813// --- InputDispatcherTest SetInputWindowTest ---
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700814static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms;
Siarhei Vishniakou1c494c52021-08-11 20:25:01 -0700815// Default input dispatching timeout if there is no focused application or paused window
816// from which to determine an appropriate dispatching timeout.
817static const std::chrono::duration DISPATCHING_TIMEOUT = std::chrono::milliseconds(
818 android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS *
819 android::base::HwTimeoutMultiplier());
Arthur Hungb92218b2018-08-14 12:00:21 +0800820
821class FakeApplicationHandle : public InputApplicationHandle {
822public:
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700823 FakeApplicationHandle() {
824 mInfo.name = "Fake Application";
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700825 mInfo.token = sp<BBinder>::make();
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500826 mInfo.dispatchingTimeoutMillis =
827 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700828 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800829 virtual ~FakeApplicationHandle() {}
830
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000831 virtual bool updateInfo() override { return true; }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700832
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500833 void setDispatchingTimeout(std::chrono::milliseconds timeout) {
834 mInfo.dispatchingTimeoutMillis = timeout.count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700835 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800836};
837
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800838class FakeInputReceiver {
Arthur Hungb92218b2018-08-14 12:00:21 +0800839public:
Garfield Tan15601662020-09-22 15:32:38 -0700840 explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name)
chaviwd1c23182019-12-20 18:44:56 -0800841 : mName(name) {
Garfield Tan15601662020-09-22 15:32:38 -0700842 mConsumer = std::make_unique<InputConsumer>(std::move(clientChannel));
chaviwd1c23182019-12-20 18:44:56 -0800843 }
844
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800845 InputEvent* consume() {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700846 InputEvent* event;
847 std::optional<uint32_t> consumeSeq = receiveEvent(&event);
848 if (!consumeSeq) {
849 return nullptr;
850 }
851 finishEvent(*consumeSeq);
852 return event;
853 }
854
855 /**
856 * Receive an event without acknowledging it.
857 * Return the sequence number that could later be used to send finished signal.
858 */
859 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Arthur Hungb92218b2018-08-14 12:00:21 +0800860 uint32_t consumeSeq;
861 InputEvent* event;
Arthur Hungb92218b2018-08-14 12:00:21 +0800862
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800863 std::chrono::time_point start = std::chrono::steady_clock::now();
864 status_t status = WOULD_BLOCK;
865 while (status == WOULD_BLOCK) {
Harry Cutts33476232023-01-30 19:57:29 +0000866 status = mConsumer->consume(&mEventFactory, /*consumeBatches=*/true, -1, &consumeSeq,
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800867 &event);
868 std::chrono::duration elapsed = std::chrono::steady_clock::now() - start;
869 if (elapsed > 100ms) {
870 break;
871 }
872 }
873
874 if (status == WOULD_BLOCK) {
875 // Just means there's no event available.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700876 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800877 }
878
879 if (status != OK) {
880 ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK.";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700881 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800882 }
883 if (event == nullptr) {
884 ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700885 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800886 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700887 if (outEvent != nullptr) {
888 *outEvent = event;
889 }
890 return consumeSeq;
891 }
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800892
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700893 /**
894 * To be used together with "receiveEvent" to complete the consumption of an event.
895 */
896 void finishEvent(uint32_t consumeSeq) {
897 const status_t status = mConsumer->sendFinishedSignal(consumeSeq, true);
898 ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK.";
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800899 }
900
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000901 void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) {
902 const status_t status = mConsumer->sendTimeline(inputEventId, timeline);
903 ASSERT_EQ(OK, status);
904 }
905
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000906 void consumeEvent(int32_t expectedEventType, int32_t expectedAction,
907 std::optional<int32_t> expectedDisplayId,
908 std::optional<int32_t> expectedFlags) {
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800909 InputEvent* event = consume();
910
911 ASSERT_NE(nullptr, event) << mName.c_str()
912 << ": consumer should have returned non-NULL event.";
Arthur Hungb92218b2018-08-14 12:00:21 +0800913 ASSERT_EQ(expectedEventType, event->getType())
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700914 << mName.c_str() << " expected " << inputEventTypeToString(expectedEventType)
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800915 << " event, got " << inputEventTypeToString(event->getType()) << " event";
Arthur Hungb92218b2018-08-14 12:00:21 +0800916
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000917 if (expectedDisplayId.has_value()) {
918 EXPECT_EQ(expectedDisplayId, event->getDisplayId());
919 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800920
Tiger Huang8664f8c2018-10-11 19:14:35 +0800921 switch (expectedEventType) {
922 case AINPUT_EVENT_TYPE_KEY: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800923 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event);
924 EXPECT_EQ(expectedAction, keyEvent.getAction());
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000925 if (expectedFlags.has_value()) {
926 EXPECT_EQ(expectedFlags.value(), keyEvent.getFlags());
927 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800928 break;
929 }
930 case AINPUT_EVENT_TYPE_MOTION: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800931 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000932 assertMotionAction(expectedAction, motionEvent.getAction());
933
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000934 if (expectedFlags.has_value()) {
935 EXPECT_EQ(expectedFlags.value(), motionEvent.getFlags());
936 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800937 break;
938 }
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100939 case AINPUT_EVENT_TYPE_FOCUS: {
940 FAIL() << "Use 'consumeFocusEvent' for FOCUS events";
941 }
Prabir Pradhan99987712020-11-10 18:43:05 -0800942 case AINPUT_EVENT_TYPE_CAPTURE: {
943 FAIL() << "Use 'consumeCaptureEvent' for CAPTURE events";
944 }
Antonio Kantekf16f2832021-09-28 04:39:20 +0000945 case AINPUT_EVENT_TYPE_TOUCH_MODE: {
946 FAIL() << "Use 'consumeTouchModeEvent' for TOUCH_MODE events";
947 }
arthurhungb89ccb02020-12-30 16:19:01 +0800948 case AINPUT_EVENT_TYPE_DRAG: {
949 FAIL() << "Use 'consumeDragEvent' for DRAG events";
950 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800951 default: {
952 FAIL() << mName.c_str() << ": invalid event type: " << expectedEventType;
953 }
954 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800955 }
956
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -0800957 MotionEvent* consumeMotion() {
958 InputEvent* event = consume();
959
960 if (event == nullptr) {
961 ADD_FAILURE() << mName << ": expected a MotionEvent, but didn't get one.";
962 return nullptr;
963 }
964
965 if (event->getType() != AINPUT_EVENT_TYPE_MOTION) {
966 ADD_FAILURE() << mName << " expected a MotionEvent, got "
967 << inputEventTypeToString(event->getType()) << " event";
968 return nullptr;
969 }
970 return static_cast<MotionEvent*>(event);
971 }
972
973 void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) {
974 MotionEvent* motionEvent = consumeMotion();
975 ASSERT_NE(nullptr, motionEvent) << "Did not get a motion event, but expected " << matcher;
976 ASSERT_THAT(*motionEvent, matcher);
977 }
978
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100979 void consumeFocusEvent(bool hasFocus, bool inTouchMode) {
980 InputEvent* event = consume();
981 ASSERT_NE(nullptr, event) << mName.c_str()
982 << ": consumer should have returned non-NULL event.";
983 ASSERT_EQ(AINPUT_EVENT_TYPE_FOCUS, event->getType())
984 << "Got " << inputEventTypeToString(event->getType())
985 << " event instead of FOCUS event";
986
987 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
988 << mName.c_str() << ": event displayId should always be NONE.";
989
990 FocusEvent* focusEvent = static_cast<FocusEvent*>(event);
991 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100992 }
993
Prabir Pradhan99987712020-11-10 18:43:05 -0800994 void consumeCaptureEvent(bool hasCapture) {
995 const InputEvent* event = consume();
996 ASSERT_NE(nullptr, event) << mName.c_str()
997 << ": consumer should have returned non-NULL event.";
998 ASSERT_EQ(AINPUT_EVENT_TYPE_CAPTURE, event->getType())
999 << "Got " << inputEventTypeToString(event->getType())
1000 << " event instead of CAPTURE event";
1001
1002 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1003 << mName.c_str() << ": event displayId should always be NONE.";
1004
1005 const auto& captureEvent = static_cast<const CaptureEvent&>(*event);
1006 EXPECT_EQ(hasCapture, captureEvent.getPointerCaptureEnabled());
1007 }
1008
arthurhungb89ccb02020-12-30 16:19:01 +08001009 void consumeDragEvent(bool isExiting, float x, float y) {
1010 const InputEvent* event = consume();
1011 ASSERT_NE(nullptr, event) << mName.c_str()
1012 << ": consumer should have returned non-NULL event.";
1013 ASSERT_EQ(AINPUT_EVENT_TYPE_DRAG, event->getType())
1014 << "Got " << inputEventTypeToString(event->getType())
1015 << " event instead of DRAG event";
1016
1017 EXPECT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1018 << mName.c_str() << ": event displayId should always be NONE.";
1019
1020 const auto& dragEvent = static_cast<const DragEvent&>(*event);
1021 EXPECT_EQ(isExiting, dragEvent.isExiting());
1022 EXPECT_EQ(x, dragEvent.getX());
1023 EXPECT_EQ(y, dragEvent.getY());
1024 }
1025
Antonio Kantekf16f2832021-09-28 04:39:20 +00001026 void consumeTouchModeEvent(bool inTouchMode) {
1027 const InputEvent* event = consume();
1028 ASSERT_NE(nullptr, event) << mName.c_str()
1029 << ": consumer should have returned non-NULL event.";
1030 ASSERT_EQ(AINPUT_EVENT_TYPE_TOUCH_MODE, event->getType())
1031 << "Got " << inputEventTypeToString(event->getType())
1032 << " event instead of TOUCH_MODE event";
1033
1034 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1035 << mName.c_str() << ": event displayId should always be NONE.";
1036 const auto& touchModeEvent = static_cast<const TouchModeEvent&>(*event);
1037 EXPECT_EQ(inTouchMode, touchModeEvent.isInTouchMode());
1038 }
1039
chaviwd1c23182019-12-20 18:44:56 -08001040 void assertNoEvents() {
1041 InputEvent* event = consume();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001042 if (event == nullptr) {
1043 return;
1044 }
1045 if (event->getType() == AINPUT_EVENT_TYPE_KEY) {
1046 KeyEvent& keyEvent = static_cast<KeyEvent&>(*event);
1047 ADD_FAILURE() << "Received key event "
1048 << KeyEvent::actionToString(keyEvent.getAction());
1049 } else if (event->getType() == AINPUT_EVENT_TYPE_MOTION) {
1050 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
1051 ADD_FAILURE() << "Received motion event "
1052 << MotionEvent::actionToString(motionEvent.getAction());
1053 } else if (event->getType() == AINPUT_EVENT_TYPE_FOCUS) {
1054 FocusEvent& focusEvent = static_cast<FocusEvent&>(*event);
1055 ADD_FAILURE() << "Received focus event, hasFocus = "
1056 << (focusEvent.getHasFocus() ? "true" : "false");
Prabir Pradhan99987712020-11-10 18:43:05 -08001057 } else if (event->getType() == AINPUT_EVENT_TYPE_CAPTURE) {
1058 const auto& captureEvent = static_cast<CaptureEvent&>(*event);
1059 ADD_FAILURE() << "Received capture event, pointerCaptureEnabled = "
1060 << (captureEvent.getPointerCaptureEnabled() ? "true" : "false");
Antonio Kantekf16f2832021-09-28 04:39:20 +00001061 } else if (event->getType() == AINPUT_EVENT_TYPE_TOUCH_MODE) {
1062 const auto& touchModeEvent = static_cast<TouchModeEvent&>(*event);
1063 ADD_FAILURE() << "Received touch mode event, inTouchMode = "
1064 << (touchModeEvent.isInTouchMode() ? "true" : "false");
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001065 }
1066 FAIL() << mName.c_str()
1067 << ": should not have received any events, so consume() should return NULL";
chaviwd1c23182019-12-20 18:44:56 -08001068 }
1069
1070 sp<IBinder> getToken() { return mConsumer->getChannel()->getConnectionToken(); }
1071
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001072 int getChannelFd() { return mConsumer->getChannel()->getFd().get(); }
1073
chaviwd1c23182019-12-20 18:44:56 -08001074protected:
1075 std::unique_ptr<InputConsumer> mConsumer;
1076 PreallocatedInputEventFactory mEventFactory;
1077
1078 std::string mName;
1079};
1080
chaviw3277faf2021-05-19 16:45:23 -05001081class FakeWindowHandle : public WindowInfoHandle {
chaviwd1c23182019-12-20 18:44:56 -08001082public:
1083 static const int32_t WIDTH = 600;
1084 static const int32_t HEIGHT = 800;
chaviwd1c23182019-12-20 18:44:56 -08001085
Chris Yea209fde2020-07-22 13:54:51 -07001086 FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001087 const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name,
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001088 int32_t displayId, std::optional<sp<IBinder>> token = std::nullopt)
chaviwd1c23182019-12-20 18:44:56 -08001089 : mName(name) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001090 if (token == std::nullopt) {
Garfield Tan15601662020-09-22 15:32:38 -07001091 base::Result<std::unique_ptr<InputChannel>> channel =
1092 dispatcher->createInputChannel(name);
1093 token = (*channel)->getConnectionToken();
1094 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
chaviwd1c23182019-12-20 18:44:56 -08001095 }
1096
1097 inputApplicationHandle->updateInfo();
1098 mInfo.applicationInfo = *inputApplicationHandle->getInfo();
1099
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001100 mInfo.token = *token;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001101 mInfo.id = sId++;
chaviwd1c23182019-12-20 18:44:56 -08001102 mInfo.name = name;
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -05001103 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001104 mInfo.alpha = 1.0;
chaviwd1c23182019-12-20 18:44:56 -08001105 mInfo.frameLeft = 0;
1106 mInfo.frameTop = 0;
1107 mInfo.frameRight = WIDTH;
1108 mInfo.frameBottom = HEIGHT;
chaviw1ff3d1e2020-07-01 15:53:47 -07001109 mInfo.transform.set(0, 0);
chaviwd1c23182019-12-20 18:44:56 -08001110 mInfo.globalScaleFactor = 1.0;
1111 mInfo.touchableRegion.clear();
1112 mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT));
Prabir Pradhan5735a322022-04-11 17:23:34 +00001113 mInfo.ownerPid = WINDOW_PID;
1114 mInfo.ownerUid = WINDOW_UID;
chaviwd1c23182019-12-20 18:44:56 -08001115 mInfo.displayId = displayId;
Prabir Pradhan51e7db02022-02-07 06:02:57 -08001116 mInfo.inputConfig = WindowInfo::InputConfig::DEFAULT;
chaviwd1c23182019-12-20 18:44:56 -08001117 }
1118
Arthur Hungabbb9d82021-09-01 14:52:30 +00001119 sp<FakeWindowHandle> clone(
1120 const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001121 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t displayId) {
Arthur Hungabbb9d82021-09-01 14:52:30 +00001122 sp<FakeWindowHandle> handle =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001123 sp<FakeWindowHandle>::make(inputApplicationHandle, dispatcher,
1124 mInfo.name + "(Mirror)", displayId, mInfo.token);
Arthur Hungabbb9d82021-09-01 14:52:30 +00001125 return handle;
1126 }
1127
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001128 void setTouchable(bool touchable) {
1129 mInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, !touchable);
1130 }
chaviwd1c23182019-12-20 18:44:56 -08001131
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001132 void setFocusable(bool focusable) {
1133 mInfo.setInputConfig(WindowInfo::InputConfig::NOT_FOCUSABLE, !focusable);
1134 }
1135
1136 void setVisible(bool visible) {
1137 mInfo.setInputConfig(WindowInfo::InputConfig::NOT_VISIBLE, !visible);
1138 }
Vishnu Nair958da932020-08-21 17:12:37 -07001139
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001140 void setDispatchingTimeout(std::chrono::nanoseconds timeout) {
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -05001141 mInfo.dispatchingTimeout = timeout;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001142 }
1143
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001144 void setPaused(bool paused) {
1145 mInfo.setInputConfig(WindowInfo::InputConfig::PAUSE_DISPATCHING, paused);
1146 }
1147
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08001148 void setPreventSplitting(bool preventSplitting) {
1149 mInfo.setInputConfig(WindowInfo::InputConfig::PREVENT_SPLITTING, preventSplitting);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001150 }
1151
1152 void setSlippery(bool slippery) {
1153 mInfo.setInputConfig(WindowInfo::InputConfig::SLIPPERY, slippery);
1154 }
1155
1156 void setWatchOutsideTouch(bool watchOutside) {
1157 mInfo.setInputConfig(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH, watchOutside);
1158 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001159
Prabir Pradhan51e7db02022-02-07 06:02:57 -08001160 void setSpy(bool spy) { mInfo.setInputConfig(WindowInfo::InputConfig::SPY, spy); }
1161
1162 void setInterceptsStylus(bool interceptsStylus) {
1163 mInfo.setInputConfig(WindowInfo::InputConfig::INTERCEPTS_STYLUS, interceptsStylus);
1164 }
1165
1166 void setDropInput(bool dropInput) {
1167 mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT, dropInput);
1168 }
1169
1170 void setDropInputIfObscured(bool dropInputIfObscured) {
1171 mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED, dropInputIfObscured);
1172 }
1173
1174 void setNoInputChannel(bool noInputChannel) {
1175 mInfo.setInputConfig(WindowInfo::InputConfig::NO_INPUT_CHANNEL, noInputChannel);
1176 }
1177
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001178 void setAlpha(float alpha) { mInfo.alpha = alpha; }
1179
chaviw3277faf2021-05-19 16:45:23 -05001180 void setTouchOcclusionMode(TouchOcclusionMode mode) { mInfo.touchOcclusionMode = mode; }
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001181
Bernardo Rufino7393d172021-02-26 13:56:11 +00001182 void setApplicationToken(sp<IBinder> token) { mInfo.applicationInfo.token = token; }
1183
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001184 void setFrame(const Rect& frame, const ui::Transform& displayTransform = ui::Transform()) {
chaviwd1c23182019-12-20 18:44:56 -08001185 mInfo.frameLeft = frame.left;
1186 mInfo.frameTop = frame.top;
1187 mInfo.frameRight = frame.right;
1188 mInfo.frameBottom = frame.bottom;
1189 mInfo.touchableRegion.clear();
1190 mInfo.addTouchableRegion(frame);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001191
1192 const Rect logicalDisplayFrame = displayTransform.transform(frame);
1193 ui::Transform translate;
1194 translate.set(-logicalDisplayFrame.left, -logicalDisplayFrame.top);
1195 mInfo.transform = translate * displayTransform;
chaviwd1c23182019-12-20 18:44:56 -08001196 }
1197
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001198 void setTouchableRegion(const Region& region) { mInfo.touchableRegion = region; }
1199
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001200 void setIsWallpaper(bool isWallpaper) {
1201 mInfo.setInputConfig(WindowInfo::InputConfig::IS_WALLPAPER, isWallpaper);
1202 }
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001203
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001204 void setDupTouchToWallpaper(bool hasWallpaper) {
1205 mInfo.setInputConfig(WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER, hasWallpaper);
1206 }
chaviwd1c23182019-12-20 18:44:56 -08001207
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001208 void setTrustedOverlay(bool trustedOverlay) {
1209 mInfo.setInputConfig(WindowInfo::InputConfig::TRUSTED_OVERLAY, trustedOverlay);
1210 }
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001211
chaviw9eaa22c2020-07-01 16:21:27 -07001212 void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) {
1213 mInfo.transform.set(dsdx, dtdx, dtdy, dsdy);
1214 }
1215
1216 void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); }
chaviwaf87b3e2019-10-01 16:59:28 -07001217
yunho.shinf4a80b82020-11-16 21:13:57 +09001218 void setWindowOffset(float offsetX, float offsetY) { mInfo.transform.set(offsetX, offsetY); }
1219
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001220 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
1221 consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId,
1222 expectedFlags);
1223 }
1224
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001225 void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
1226 consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, expectedDisplayId, expectedFlags);
1227 }
1228
Svet Ganov5d3bc372020-01-26 23:11:07 -08001229 void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001230 int32_t expectedFlags = 0) {
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08001231 consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(expectedDisplayId),
1232 WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED)));
Svet Ganov5d3bc372020-01-26 23:11:07 -08001233 }
1234
1235 void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001236 int32_t expectedFlags = 0) {
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08001237 consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
1238 WithDisplayId(expectedDisplayId), WithFlags(expectedFlags)));
Svet Ganov5d3bc372020-01-26 23:11:07 -08001239 }
1240
1241 void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001242 int32_t expectedFlags = 0) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001243 consumeAnyMotionDown(expectedDisplayId, expectedFlags);
1244 }
1245
1246 void consumeAnyMotionDown(std::optional<int32_t> expectedDisplayId = std::nullopt,
1247 std::optional<int32_t> expectedFlags = std::nullopt) {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001248 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId,
1249 expectedFlags);
1250 }
1251
Svet Ganov5d3bc372020-01-26 23:11:07 -08001252 void consumeMotionPointerDown(int32_t pointerIdx,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001253 int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1254 int32_t expectedFlags = 0) {
1255 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
1256 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001257 consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags);
1258 }
1259
1260 void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001261 int32_t expectedFlags = 0) {
1262 int32_t action = AMOTION_EVENT_ACTION_POINTER_UP |
1263 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001264 consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags);
1265 }
1266
1267 void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001268 int32_t expectedFlags = 0) {
Michael Wright3a240c42019-12-10 20:53:41 +00001269 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId,
1270 expectedFlags);
1271 }
1272
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05001273 void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1274 int32_t expectedFlags = 0) {
1275 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, expectedDisplayId,
1276 expectedFlags);
1277 }
1278
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08001279 void consumeMotionOutsideWithZeroedCoords(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1280 int32_t expectedFlags = 0) {
1281 InputEvent* event = consume();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08001282 ASSERT_NE(nullptr, event);
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08001283 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType());
1284 const MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
1285 EXPECT_EQ(AMOTION_EVENT_ACTION_OUTSIDE, motionEvent.getActionMasked());
1286 EXPECT_EQ(0.f, motionEvent.getRawPointerCoords(0)->getX());
1287 EXPECT_EQ(0.f, motionEvent.getRawPointerCoords(0)->getY());
1288 }
1289
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001290 void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) {
1291 ASSERT_NE(mInputReceiver, nullptr)
1292 << "Cannot consume events from a window with no receiver";
1293 mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode);
1294 }
1295
Prabir Pradhan99987712020-11-10 18:43:05 -08001296 void consumeCaptureEvent(bool hasCapture) {
1297 ASSERT_NE(mInputReceiver, nullptr)
1298 << "Cannot consume events from a window with no receiver";
1299 mInputReceiver->consumeCaptureEvent(hasCapture);
1300 }
1301
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08001302 void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) {
1303 MotionEvent* motionEvent = consumeMotion();
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08001304 ASSERT_NE(nullptr, motionEvent) << "Did not get a motion event, but expected " << matcher;
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08001305 ASSERT_THAT(*motionEvent, matcher);
1306 }
1307
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001308 void consumeEvent(int32_t expectedEventType, int32_t expectedAction,
1309 std::optional<int32_t> expectedDisplayId,
1310 std::optional<int32_t> expectedFlags) {
chaviwd1c23182019-12-20 18:44:56 -08001311 ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver";
1312 mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId,
1313 expectedFlags);
1314 }
1315
arthurhungb89ccb02020-12-30 16:19:01 +08001316 void consumeDragEvent(bool isExiting, float x, float y) {
1317 mInputReceiver->consumeDragEvent(isExiting, x, y);
1318 }
1319
Antonio Kantekf16f2832021-09-28 04:39:20 +00001320 void consumeTouchModeEvent(bool inTouchMode) {
1321 ASSERT_NE(mInputReceiver, nullptr)
1322 << "Cannot consume events from a window with no receiver";
1323 mInputReceiver->consumeTouchModeEvent(inTouchMode);
1324 }
1325
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001326 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001327 if (mInputReceiver == nullptr) {
1328 ADD_FAILURE() << "Invalid receive event on window with no receiver";
1329 return std::nullopt;
1330 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001331 return mInputReceiver->receiveEvent(outEvent);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001332 }
1333
1334 void finishEvent(uint32_t sequenceNum) {
1335 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
1336 mInputReceiver->finishEvent(sequenceNum);
1337 }
1338
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00001339 void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) {
1340 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
1341 mInputReceiver->sendTimeline(inputEventId, timeline);
1342 }
1343
chaviwaf87b3e2019-10-01 16:59:28 -07001344 InputEvent* consume() {
1345 if (mInputReceiver == nullptr) {
1346 return nullptr;
1347 }
1348 return mInputReceiver->consume();
1349 }
1350
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00001351 MotionEvent* consumeMotion() {
1352 InputEvent* event = consume();
1353 if (event == nullptr) {
1354 ADD_FAILURE() << "Consume failed : no event";
1355 return nullptr;
1356 }
1357 if (event->getType() != AINPUT_EVENT_TYPE_MOTION) {
1358 ADD_FAILURE() << "Instead of motion event, got "
1359 << inputEventTypeToString(event->getType());
1360 return nullptr;
1361 }
1362 return static_cast<MotionEvent*>(event);
1363 }
1364
Arthur Hungb92218b2018-08-14 12:00:21 +08001365 void assertNoEvents() {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001366 if (mInputReceiver == nullptr &&
Prabir Pradhan51e7db02022-02-07 06:02:57 -08001367 mInfo.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001368 return; // Can't receive events if the window does not have input channel
1369 }
1370 ASSERT_NE(nullptr, mInputReceiver)
1371 << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL";
chaviwd1c23182019-12-20 18:44:56 -08001372 mInputReceiver->assertNoEvents();
Arthur Hungb92218b2018-08-14 12:00:21 +08001373 }
1374
chaviwaf87b3e2019-10-01 16:59:28 -07001375 sp<IBinder> getToken() { return mInfo.token; }
1376
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001377 const std::string& getName() { return mName; }
1378
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001379 void setOwnerInfo(int32_t ownerPid, int32_t ownerUid) {
1380 mInfo.ownerPid = ownerPid;
1381 mInfo.ownerUid = ownerUid;
1382 }
1383
Prabir Pradhanedd96402022-02-15 01:46:16 -08001384 int32_t getPid() const { return mInfo.ownerPid; }
1385
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001386 void destroyReceiver() { mInputReceiver = nullptr; }
1387
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001388 int getChannelFd() { return mInputReceiver->getChannelFd(); }
1389
chaviwd1c23182019-12-20 18:44:56 -08001390private:
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001391 const std::string mName;
chaviwd1c23182019-12-20 18:44:56 -08001392 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001393 static std::atomic<int32_t> sId; // each window gets a unique id, like in surfaceflinger
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001394};
1395
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001396std::atomic<int32_t> FakeWindowHandle::sId{1};
1397
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001398static InputEventInjectionResult injectKey(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001399 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t repeatCount,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001400 int32_t displayId = ADISPLAY_ID_NONE,
1401 InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001402 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Prabir Pradhan5735a322022-04-11 17:23:34 +00001403 bool allowKeyRepeat = true, std::optional<int32_t> targetUid = {},
1404 uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
Arthur Hungb92218b2018-08-14 12:00:21 +08001405 KeyEvent event;
1406 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1407
1408 // Define a valid key down event.
Garfield Tanfbe732e2020-01-24 11:26:14 -08001409 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId,
Harry Cutts33476232023-01-30 19:57:29 +00001410 INVALID_HMAC, action, /*flags=*/0, AKEYCODE_A, KEY_A, AMETA_NONE, repeatCount,
1411 currentTime, currentTime);
Arthur Hungb92218b2018-08-14 12:00:21 +08001412
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001413 if (!allowKeyRepeat) {
1414 policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT;
1415 }
Arthur Hungb92218b2018-08-14 12:00:21 +08001416 // Inject event until dispatch out.
Prabir Pradhan5735a322022-04-11 17:23:34 +00001417 return dispatcher->injectInputEvent(&event, targetUid, syncMode, injectionTimeout, policyFlags);
Arthur Hungb92218b2018-08-14 12:00:21 +08001418}
1419
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001420static InputEventInjectionResult injectKeyDown(const std::unique_ptr<InputDispatcher>& dispatcher,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001421 int32_t displayId = ADISPLAY_ID_NONE) {
Harry Cutts33476232023-01-30 19:57:29 +00001422 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001423}
1424
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001425// Inject a down event that has key repeat disabled. This allows InputDispatcher to idle without
1426// sending a subsequent key up. When key repeat is enabled, the dispatcher cannot idle because it
1427// has to be woken up to process the repeating key.
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001428static InputEventInjectionResult injectKeyDownNoRepeat(
1429 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t displayId = ADISPLAY_ID_NONE) {
Harry Cutts33476232023-01-30 19:57:29 +00001430 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001431 InputEventInjectionSync::WAIT_FOR_RESULT, INJECT_EVENT_TIMEOUT,
Harry Cutts33476232023-01-30 19:57:29 +00001432 /*allowKeyRepeat=*/false);
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001433}
1434
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001435static InputEventInjectionResult injectKeyUp(const std::unique_ptr<InputDispatcher>& dispatcher,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001436 int32_t displayId = ADISPLAY_ID_NONE) {
Harry Cutts33476232023-01-30 19:57:29 +00001437 return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /*repeatCount=*/0, displayId);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001438}
1439
Garfield Tandf26e862020-07-01 20:18:19 -07001440class PointerBuilder {
1441public:
1442 PointerBuilder(int32_t id, int32_t toolType) {
1443 mProperties.clear();
1444 mProperties.id = id;
1445 mProperties.toolType = toolType;
1446 mCoords.clear();
1447 }
1448
1449 PointerBuilder& x(float x) { return axis(AMOTION_EVENT_AXIS_X, x); }
1450
1451 PointerBuilder& y(float y) { return axis(AMOTION_EVENT_AXIS_Y, y); }
1452
1453 PointerBuilder& axis(int32_t axis, float value) {
1454 mCoords.setAxisValue(axis, value);
1455 return *this;
1456 }
1457
1458 PointerProperties buildProperties() const { return mProperties; }
1459
1460 PointerCoords buildCoords() const { return mCoords; }
1461
1462private:
1463 PointerProperties mProperties;
1464 PointerCoords mCoords;
1465};
1466
1467class MotionEventBuilder {
1468public:
1469 MotionEventBuilder(int32_t action, int32_t source) {
1470 mAction = action;
1471 mSource = source;
1472 mEventTime = systemTime(SYSTEM_TIME_MONOTONIC);
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08001473 mDownTime = mEventTime;
Garfield Tandf26e862020-07-01 20:18:19 -07001474 }
1475
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08001476 MotionEventBuilder& deviceId(int32_t deviceId) {
1477 mDeviceId = deviceId;
1478 return *this;
1479 }
1480
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08001481 MotionEventBuilder& downTime(nsecs_t downTime) {
1482 mDownTime = downTime;
1483 return *this;
1484 }
1485
Garfield Tandf26e862020-07-01 20:18:19 -07001486 MotionEventBuilder& eventTime(nsecs_t eventTime) {
1487 mEventTime = eventTime;
1488 return *this;
1489 }
1490
1491 MotionEventBuilder& displayId(int32_t displayId) {
1492 mDisplayId = displayId;
1493 return *this;
1494 }
1495
1496 MotionEventBuilder& actionButton(int32_t actionButton) {
1497 mActionButton = actionButton;
1498 return *this;
1499 }
1500
arthurhung6d4bed92021-03-17 11:59:33 +08001501 MotionEventBuilder& buttonState(int32_t buttonState) {
1502 mButtonState = buttonState;
Garfield Tandf26e862020-07-01 20:18:19 -07001503 return *this;
1504 }
1505
1506 MotionEventBuilder& rawXCursorPosition(float rawXCursorPosition) {
1507 mRawXCursorPosition = rawXCursorPosition;
1508 return *this;
1509 }
1510
1511 MotionEventBuilder& rawYCursorPosition(float rawYCursorPosition) {
1512 mRawYCursorPosition = rawYCursorPosition;
1513 return *this;
1514 }
1515
1516 MotionEventBuilder& pointer(PointerBuilder pointer) {
1517 mPointers.push_back(pointer);
1518 return *this;
1519 }
1520
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08001521 MotionEventBuilder& addFlag(uint32_t flags) {
1522 mFlags |= flags;
1523 return *this;
1524 }
1525
Garfield Tandf26e862020-07-01 20:18:19 -07001526 MotionEvent build() {
1527 std::vector<PointerProperties> pointerProperties;
1528 std::vector<PointerCoords> pointerCoords;
1529 for (const PointerBuilder& pointer : mPointers) {
1530 pointerProperties.push_back(pointer.buildProperties());
1531 pointerCoords.push_back(pointer.buildCoords());
1532 }
1533
1534 // Set mouse cursor position for the most common cases to avoid boilerplate.
1535 if (mSource == AINPUT_SOURCE_MOUSE &&
1536 !MotionEvent::isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition) &&
1537 mPointers.size() == 1) {
1538 mRawXCursorPosition = pointerCoords[0].getX();
1539 mRawYCursorPosition = pointerCoords[0].getY();
1540 }
1541
1542 MotionEvent event;
chaviw9eaa22c2020-07-01 16:21:27 -07001543 ui::Transform identityTransform;
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08001544 event.initialize(InputEvent::nextId(), mDeviceId, mSource, mDisplayId, INVALID_HMAC,
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08001545 mAction, mActionButton, mFlags, /* edgeFlags */ 0, AMETA_NONE,
chaviw9eaa22c2020-07-01 16:21:27 -07001546 mButtonState, MotionClassification::NONE, identityTransform,
1547 /* xPrecision */ 0, /* yPrecision */ 0, mRawXCursorPosition,
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08001548 mRawYCursorPosition, identityTransform, mDownTime, mEventTime,
Prabir Pradhanb9b18502021-08-26 12:30:32 -07001549 mPointers.size(), pointerProperties.data(), pointerCoords.data());
Garfield Tandf26e862020-07-01 20:18:19 -07001550
1551 return event;
1552 }
1553
1554private:
1555 int32_t mAction;
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08001556 int32_t mDeviceId = DEVICE_ID;
Garfield Tandf26e862020-07-01 20:18:19 -07001557 int32_t mSource;
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08001558 nsecs_t mDownTime;
Garfield Tandf26e862020-07-01 20:18:19 -07001559 nsecs_t mEventTime;
1560 int32_t mDisplayId{ADISPLAY_ID_DEFAULT};
1561 int32_t mActionButton{0};
1562 int32_t mButtonState{0};
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08001563 int32_t mFlags{0};
Garfield Tandf26e862020-07-01 20:18:19 -07001564 float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
1565 float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
1566
1567 std::vector<PointerBuilder> mPointers;
1568};
1569
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001570static InputEventInjectionResult injectMotionEvent(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001571 const std::unique_ptr<InputDispatcher>& dispatcher, const MotionEvent& event,
Garfield Tandf26e862020-07-01 20:18:19 -07001572 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Prabir Pradhan5735a322022-04-11 17:23:34 +00001573 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT,
1574 std::optional<int32_t> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
1575 return dispatcher->injectInputEvent(&event, targetUid, injectionMode, injectionTimeout,
1576 policyFlags);
Garfield Tandf26e862020-07-01 20:18:19 -07001577}
1578
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001579static InputEventInjectionResult injectMotionEvent(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001580 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t source,
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001581 int32_t displayId, const PointF& position = {100, 200},
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001582 const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001583 AMOTION_EVENT_INVALID_CURSOR_POSITION},
1584 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001585 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Prabir Pradhan5735a322022-04-11 17:23:34 +00001586 nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC),
1587 std::optional<int32_t> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
Garfield Tandf26e862020-07-01 20:18:19 -07001588 MotionEvent event = MotionEventBuilder(action, source)
1589 .displayId(displayId)
1590 .eventTime(eventTime)
1591 .rawXCursorPosition(cursorPosition.x)
1592 .rawYCursorPosition(cursorPosition.y)
Harry Cutts33476232023-01-30 19:57:29 +00001593 .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER)
Garfield Tandf26e862020-07-01 20:18:19 -07001594 .x(position.x)
1595 .y(position.y))
1596 .build();
Arthur Hungb92218b2018-08-14 12:00:21 +08001597
1598 // Inject event until dispatch out.
Prabir Pradhan5735a322022-04-11 17:23:34 +00001599 return injectMotionEvent(dispatcher, event, injectionTimeout, injectionMode, targetUid,
1600 policyFlags);
Arthur Hungb92218b2018-08-14 12:00:21 +08001601}
1602
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001603static InputEventInjectionResult injectMotionDown(
1604 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t source, int32_t displayId,
1605 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001606 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location);
Garfield Tan00f511d2019-06-12 16:55:40 -07001607}
1608
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001609static InputEventInjectionResult injectMotionUp(const std::unique_ptr<InputDispatcher>& dispatcher,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001610 int32_t source, int32_t displayId,
1611 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001612 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location);
Michael Wright3a240c42019-12-10 20:53:41 +00001613}
1614
Jackal Guof9696682018-10-05 12:23:23 +08001615static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) {
1616 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1617 // Define a valid key event.
Harry Cutts33476232023-01-30 19:57:29 +00001618 NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001619 displayId, POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0, AKEYCODE_A,
1620 KEY_A, AMETA_NONE, currentTime);
Jackal Guof9696682018-10-05 12:23:23 +08001621
1622 return args;
1623}
1624
chaviwd1c23182019-12-20 18:44:56 -08001625static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId,
1626 const std::vector<PointF>& points) {
1627 size_t pointerCount = points.size();
chaviwaf87b3e2019-10-01 16:59:28 -07001628 if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) {
1629 EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer";
1630 }
1631
chaviwd1c23182019-12-20 18:44:56 -08001632 PointerProperties pointerProperties[pointerCount];
1633 PointerCoords pointerCoords[pointerCount];
Jackal Guof9696682018-10-05 12:23:23 +08001634
chaviwd1c23182019-12-20 18:44:56 -08001635 for (size_t i = 0; i < pointerCount; i++) {
1636 pointerProperties[i].clear();
1637 pointerProperties[i].id = i;
1638 pointerProperties[i].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jackal Guof9696682018-10-05 12:23:23 +08001639
chaviwd1c23182019-12-20 18:44:56 -08001640 pointerCoords[i].clear();
1641 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x);
1642 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y);
1643 }
Jackal Guof9696682018-10-05 12:23:23 +08001644
1645 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1646 // Define a valid motion event.
Harry Cutts33476232023-01-30 19:57:29 +00001647 NotifyMotionArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, source, displayId,
Garfield Tan00f511d2019-06-12 16:55:40 -07001648 POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0,
1649 AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
chaviwd1c23182019-12-20 18:44:56 -08001650 AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties,
1651 pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0,
Garfield Tan00f511d2019-06-12 16:55:40 -07001652 AMOTION_EVENT_INVALID_CURSOR_POSITION,
1653 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {});
Jackal Guof9696682018-10-05 12:23:23 +08001654
1655 return args;
1656}
1657
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08001658static NotifyMotionArgs generateTouchArgs(int32_t action, const std::vector<PointF>& points) {
1659 return generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, points);
1660}
1661
chaviwd1c23182019-12-20 18:44:56 -08001662static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) {
1663 return generateMotionArgs(action, source, displayId, {PointF{100, 200}});
1664}
1665
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001666static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs(
1667 const PointerCaptureRequest& request) {
Harry Cutts33476232023-01-30 19:57:29 +00001668 return NotifyPointerCaptureChangedArgs(/*id=*/0, systemTime(SYSTEM_TIME_MONOTONIC), request);
Prabir Pradhan99987712020-11-10 18:43:05 -08001669}
1670
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001671/**
1672 * When a window unexpectedly disposes of its input channel, policy should be notified about the
1673 * broken channel.
1674 */
1675TEST_F(InputDispatcherTest, WhenInputChannelBreaks_PolicyIsNotified) {
1676 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1677 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001678 sp<FakeWindowHandle>::make(application, mDispatcher,
1679 "Window that breaks its input channel", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001680
1681 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1682
1683 // Window closes its channel, but the window remains.
1684 window->destroyReceiver();
1685 mFakePolicy->assertNotifyInputChannelBrokenWasCalled(window->getInfo()->token);
1686}
1687
Arthur Hungb92218b2018-08-14 12:00:21 +08001688TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001689 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001690 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1691 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001692
Arthur Hung72d8dc32020-03-28 00:48:39 +00001693 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001694 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1695 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1696 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001697
1698 // Window should receive motion event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001699 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001700}
1701
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001702TEST_F(InputDispatcherTest, WhenDisplayNotSpecified_InjectMotionToDefaultDisplay) {
1703 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001704 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1705 "Fake Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001706
1707 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1708 // Inject a MotionEvent to an unknown display.
1709 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1710 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_NONE))
1711 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1712
1713 // Window should receive motion event.
1714 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1715}
1716
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001717/**
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08001718 * Calling setInputWindows once should not cause any issues.
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001719 * This test serves as a sanity check for the next test, where setInputWindows is
1720 * called twice.
1721 */
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08001722TEST_F(InputDispatcherTest, SetInputWindowOnceWithSingleTouchWindow) {
Chris Yea209fde2020-07-22 13:54:51 -07001723 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001724 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1725 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001726 window->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001727
1728 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001729 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001730 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1731 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001732 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001733
1734 // Window should receive motion event.
1735 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1736}
1737
1738/**
1739 * Calling setInputWindows twice, with the same info, should not cause any issues.
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001740 */
1741TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001742 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001743 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1744 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001745 window->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001746
1747 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1748 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001749 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001750 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1751 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001752 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001753
1754 // Window should receive motion event.
1755 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1756}
1757
Arthur Hungb92218b2018-08-14 12:00:21 +08001758// The foreground window should receive the first touch down event.
1759TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001760 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001761 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001762 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001763 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001764 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001765
Arthur Hung72d8dc32020-03-28 00:48:39 +00001766 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001767 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1768 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1769 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001770
1771 // Top window should receive the touch down event. Second window should not receive anything.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001772 windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001773 windowSecond->assertNoEvents();
1774}
1775
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001776/**
1777 * Two windows: A top window, and a wallpaper behind the window.
1778 * Touch goes to the top window, and then top window disappears. Ensure that wallpaper window
1779 * gets ACTION_CANCEL.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001780 * 1. foregroundWindow <-- dup touch to wallpaper
1781 * 2. wallpaperWindow <-- is wallpaper
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001782 */
1783TEST_F(InputDispatcherTest, WhenForegroundWindowDisappears_WallpaperTouchIsCanceled) {
1784 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1785 sp<FakeWindowHandle> foregroundWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001786 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001787 foregroundWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001788 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001789 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001790 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001791
1792 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}});
1793 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1794 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1795 {100, 200}))
1796 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1797
1798 // Both foreground window and its wallpaper should receive the touch down
1799 foregroundWindow->consumeMotionDown();
1800 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1801
1802 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1803 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
1804 ADISPLAY_ID_DEFAULT, {110, 200}))
1805 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1806
1807 foregroundWindow->consumeMotionMove();
1808 wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1809
1810 // Now the foreground window goes away, but the wallpaper stays
1811 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}});
1812 foregroundWindow->consumeMotionCancel();
1813 // Since the "parent" window of the wallpaper is gone, wallpaper should receive cancel, too.
1814 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1815}
1816
1817/**
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001818 * Same test as WhenForegroundWindowDisappears_WallpaperTouchIsCanceled above,
1819 * with the following differences:
1820 * After ACTION_DOWN, Wallpaper window hangs up its channel, which forces the dispatcher to
1821 * clean up the connection.
1822 * This later may crash dispatcher during ACTION_CANCEL synthesis, if the dispatcher is not careful.
1823 * Ensure that there's no crash in the dispatcher.
1824 */
1825TEST_F(InputDispatcherTest, WhenWallpaperDisappears_NoCrash) {
1826 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1827 sp<FakeWindowHandle> foregroundWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001828 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001829 foregroundWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001830 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001831 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001832 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001833
1834 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}});
1835 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1836 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1837 {100, 200}))
1838 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1839
1840 // Both foreground window and its wallpaper should receive the touch down
1841 foregroundWindow->consumeMotionDown();
1842 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1843
1844 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1845 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
1846 ADISPLAY_ID_DEFAULT, {110, 200}))
1847 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1848
1849 foregroundWindow->consumeMotionMove();
1850 wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1851
1852 // Wallpaper closes its channel, but the window remains.
1853 wallpaperWindow->destroyReceiver();
1854 mFakePolicy->assertNotifyInputChannelBrokenWasCalled(wallpaperWindow->getInfo()->token);
1855
1856 // Now the foreground window goes away, but the wallpaper stays, even though its channel
1857 // is no longer valid.
1858 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}});
1859 foregroundWindow->consumeMotionCancel();
1860}
1861
Arthur Hungc539dbb2022-12-08 07:45:36 +00001862class ShouldSplitTouchFixture : public InputDispatcherTest,
1863 public ::testing::WithParamInterface<bool> {};
1864INSTANTIATE_TEST_SUITE_P(InputDispatcherTest, ShouldSplitTouchFixture,
1865 ::testing::Values(true, false));
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001866/**
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001867 * A single window that receives touch (on top), and a wallpaper window underneath it.
1868 * The top window gets a multitouch gesture.
1869 * Ensure that wallpaper gets the same gesture.
1870 */
Arthur Hungc539dbb2022-12-08 07:45:36 +00001871TEST_P(ShouldSplitTouchFixture, WallpaperWindowReceivesMultiTouch) {
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001872 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Arthur Hungc539dbb2022-12-08 07:45:36 +00001873 sp<FakeWindowHandle> foregroundWindow =
1874 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
1875 foregroundWindow->setDupTouchToWallpaper(true);
1876 foregroundWindow->setPreventSplitting(GetParam());
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001877
1878 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001879 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001880 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001881
Arthur Hungc539dbb2022-12-08 07:45:36 +00001882 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}});
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001883
1884 // Touch down on top window
1885 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1886 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1887 {100, 100}))
1888 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1889
1890 // Both top window and its wallpaper should receive the touch down
Arthur Hungc539dbb2022-12-08 07:45:36 +00001891 foregroundWindow->consumeMotionDown();
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001892 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1893
1894 // Second finger down on the top window
1895 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08001896 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001897 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Harry Cutts33476232023-01-30 19:57:29 +00001898 .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(100).y(100))
1899 .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(150))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001900 .build();
1901 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1902 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
1903 InputEventInjectionSync::WAIT_FOR_RESULT))
1904 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1905
Harry Cutts33476232023-01-30 19:57:29 +00001906 foregroundWindow->consumeMotionPointerDown(/*pointerIndex=*/1);
1907 wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001908 expectedWallpaperFlags);
Arthur Hungc539dbb2022-12-08 07:45:36 +00001909
1910 const MotionEvent secondFingerUpEvent =
1911 MotionEventBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
1912 .displayId(ADISPLAY_ID_DEFAULT)
1913 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Harry Cutts33476232023-01-30 19:57:29 +00001914 .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(100).y(100))
1915 .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(150))
Arthur Hungc539dbb2022-12-08 07:45:36 +00001916 .build();
1917 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1918 injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT,
1919 InputEventInjectionSync::WAIT_FOR_RESULT))
1920 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1921 foregroundWindow->consumeMotionPointerUp(0);
1922 wallpaperWindow->consumeMotionPointerUp(0, ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1923
1924 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1925 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1926 {100, 100}))
1927 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1928 foregroundWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
1929 wallpaperWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001930}
1931
1932/**
1933 * Two windows: a window on the left and window on the right.
1934 * A third window, wallpaper, is behind both windows, and spans both top windows.
1935 * The first touch down goes to the left window. A second pointer touches down on the right window.
1936 * The touch is split, so both left and right windows should receive ACTION_DOWN.
1937 * The wallpaper will get the full event, so it should receive ACTION_DOWN followed by
1938 * ACTION_POINTER_DOWN(1).
1939 */
1940TEST_F(InputDispatcherTest, TwoWindows_SplitWallpaperTouch) {
1941 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1942 sp<FakeWindowHandle> leftWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001943 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001944 leftWindow->setFrame(Rect(0, 0, 200, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001945 leftWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001946
1947 sp<FakeWindowHandle> rightWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001948 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001949 rightWindow->setFrame(Rect(200, 0, 400, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001950 rightWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001951
1952 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001953 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001954 wallpaperWindow->setFrame(Rect(0, 0, 400, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001955 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001956
1957 mDispatcher->setInputWindows(
1958 {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}});
1959
1960 // Touch down on left window
1961 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1962 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1963 {100, 100}))
1964 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1965
1966 // Both foreground window and its wallpaper should receive the touch down
1967 leftWindow->consumeMotionDown();
1968 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1969
1970 // Second finger down on the right window
1971 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08001972 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001973 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Harry Cutts33476232023-01-30 19:57:29 +00001974 .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(100).y(100))
1975 .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(300).y(100))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001976 .build();
1977 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1978 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
1979 InputEventInjectionSync::WAIT_FOR_RESULT))
1980 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1981
1982 leftWindow->consumeMotionMove();
1983 // Since the touch is split, right window gets ACTION_DOWN
1984 rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Harry Cutts33476232023-01-30 19:57:29 +00001985 wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001986 expectedWallpaperFlags);
1987
1988 // Now, leftWindow, which received the first finger, disappears.
1989 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {rightWindow, wallpaperWindow}}});
1990 leftWindow->consumeMotionCancel();
1991 // Since a "parent" window of the wallpaper is gone, wallpaper should receive cancel, too.
1992 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1993
1994 // The pointer that's still down on the right window moves, and goes to the right window only.
1995 // As far as the dispatcher's concerned though, both pointers are still present.
1996 const MotionEvent secondFingerMoveEvent =
1997 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
1998 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Harry Cutts33476232023-01-30 19:57:29 +00001999 .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(100).y(100))
2000 .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(310).y(110))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002001 .build();
2002 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2003 injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT,
2004 InputEventInjectionSync::WAIT_FOR_RESULT));
2005 rightWindow->consumeMotionMove();
2006
2007 leftWindow->assertNoEvents();
2008 rightWindow->assertNoEvents();
2009 wallpaperWindow->assertNoEvents();
2010}
2011
Arthur Hungc539dbb2022-12-08 07:45:36 +00002012/**
2013 * Two windows: a window on the left with dup touch to wallpaper and window on the right without it.
2014 * The touch slips to the right window. so left window and wallpaper should receive ACTION_CANCEL
2015 * The right window should receive ACTION_DOWN.
2016 */
2017TEST_F(InputDispatcherTest, WallpaperWindowWhenSlippery) {
Arthur Hung74c248d2022-11-23 07:09:59 +00002018 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Arthur Hungc539dbb2022-12-08 07:45:36 +00002019 sp<FakeWindowHandle> leftWindow =
2020 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2021 leftWindow->setFrame(Rect(0, 0, 200, 200));
2022 leftWindow->setDupTouchToWallpaper(true);
2023 leftWindow->setSlippery(true);
2024
2025 sp<FakeWindowHandle> rightWindow =
2026 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2027 rightWindow->setFrame(Rect(200, 0, 400, 200));
Arthur Hung74c248d2022-11-23 07:09:59 +00002028
2029 sp<FakeWindowHandle> wallpaperWindow =
2030 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
2031 wallpaperWindow->setIsWallpaper(true);
Arthur Hung74c248d2022-11-23 07:09:59 +00002032
Arthur Hungc539dbb2022-12-08 07:45:36 +00002033 mDispatcher->setInputWindows(
2034 {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}});
Arthur Hung74c248d2022-11-23 07:09:59 +00002035
Arthur Hungc539dbb2022-12-08 07:45:36 +00002036 // Touch down on left window
Arthur Hung74c248d2022-11-23 07:09:59 +00002037 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2038 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Arthur Hungc539dbb2022-12-08 07:45:36 +00002039 {100, 100}))
Arthur Hung74c248d2022-11-23 07:09:59 +00002040 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungc539dbb2022-12-08 07:45:36 +00002041
2042 // Both foreground window and its wallpaper should receive the touch down
2043 leftWindow->consumeMotionDown();
Arthur Hung74c248d2022-11-23 07:09:59 +00002044 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2045
Arthur Hungc539dbb2022-12-08 07:45:36 +00002046 // Move to right window, the left window should receive cancel.
Arthur Hung74c248d2022-11-23 07:09:59 +00002047 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Arthur Hungc539dbb2022-12-08 07:45:36 +00002048 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
2049 ADISPLAY_ID_DEFAULT, {201, 100}))
Arthur Hung74c248d2022-11-23 07:09:59 +00002050 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2051
Arthur Hungc539dbb2022-12-08 07:45:36 +00002052 leftWindow->consumeMotionCancel();
2053 rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2054 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Arthur Hung74c248d2022-11-23 07:09:59 +00002055}
2056
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002057/**
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002058 * Two windows: a window on the left and a window on the right.
2059 * Mouse is hovered from the right window into the left window.
2060 * Next, we tap on the left window, where the cursor was last seen.
2061 * The second tap is done onto the right window.
2062 * The mouse and tap are from two different devices.
2063 * We technically don't need to set the downtime / eventtime for these events, but setting these
2064 * explicitly helps during debugging.
2065 * This test reproduces a crash where there is a mismatch between the downTime and eventTime.
2066 * In the buggy implementation, a tap on the right window would cause a crash.
2067 */
2068TEST_F(InputDispatcherTest, HoverFromLeftToRightAndTap) {
2069 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2070 sp<FakeWindowHandle> leftWindow =
2071 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2072 leftWindow->setFrame(Rect(0, 0, 200, 200));
2073
2074 sp<FakeWindowHandle> rightWindow =
2075 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2076 rightWindow->setFrame(Rect(200, 0, 400, 200));
2077
2078 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}});
2079 // All times need to start at the current time, otherwise the dispatcher will drop the events as
2080 // stale.
2081 const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC);
2082 const int32_t mouseDeviceId = 6;
2083 const int32_t touchDeviceId = 4;
2084 // Move the cursor from right
2085 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2086 injectMotionEvent(mDispatcher,
2087 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2088 AINPUT_SOURCE_MOUSE)
2089 .deviceId(mouseDeviceId)
2090 .downTime(baseTime + 10)
2091 .eventTime(baseTime + 20)
2092 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2093 .x(300)
2094 .y(100))
2095 .build()));
2096 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2097
2098 // .. to the left window
2099 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2100 injectMotionEvent(mDispatcher,
2101 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2102 AINPUT_SOURCE_MOUSE)
2103 .deviceId(mouseDeviceId)
2104 .downTime(baseTime + 10)
2105 .eventTime(baseTime + 30)
2106 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2107 .x(110)
2108 .y(100))
2109 .build()));
2110 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2111 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2112 // Now tap the left window
2113 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2114 injectMotionEvent(mDispatcher,
2115 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2116 AINPUT_SOURCE_TOUCHSCREEN)
2117 .deviceId(touchDeviceId)
2118 .downTime(baseTime + 40)
2119 .eventTime(baseTime + 40)
2120 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER)
2121 .x(100)
2122 .y(100))
2123 .build()));
2124 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2125 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2126
2127 // release tap
2128 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2129 injectMotionEvent(mDispatcher,
2130 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2131 AINPUT_SOURCE_TOUCHSCREEN)
2132 .deviceId(touchDeviceId)
2133 .downTime(baseTime + 40)
2134 .eventTime(baseTime + 50)
2135 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER)
2136 .x(100)
2137 .y(100))
2138 .build()));
2139 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2140
2141 // Tap the window on the right
2142 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2143 injectMotionEvent(mDispatcher,
2144 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2145 AINPUT_SOURCE_TOUCHSCREEN)
2146 .deviceId(touchDeviceId)
2147 .downTime(baseTime + 60)
2148 .eventTime(baseTime + 60)
2149 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER)
2150 .x(300)
2151 .y(100))
2152 .build()));
2153 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2154
2155 // release tap
2156 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2157 injectMotionEvent(mDispatcher,
2158 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2159 AINPUT_SOURCE_TOUCHSCREEN)
2160 .deviceId(touchDeviceId)
2161 .downTime(baseTime + 60)
2162 .eventTime(baseTime + 70)
2163 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER)
2164 .x(300)
2165 .y(100))
2166 .build()));
2167 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2168
2169 // No more events
2170 leftWindow->assertNoEvents();
2171 rightWindow->assertNoEvents();
2172}
2173
2174/**
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002175 * This test is similar to the test above, but the sequence of injected events is different.
2176 *
2177 * Two windows: a window on the left and a window on the right.
2178 * Mouse is hovered over the left window.
2179 * Next, we tap on the left window, where the cursor was last seen.
2180 *
2181 * After that, we inject one finger down onto the right window, and then a second finger down onto
2182 * the left window.
2183 * The touch is split, so this last gesture should cause 2 ACTION_DOWN events, one in the right
2184 * window (first), and then another on the left window (second).
2185 * This test reproduces a crash where there is a mismatch between the downTime and eventTime.
2186 * In the buggy implementation, second finger down on the left window would cause a crash.
2187 */
2188TEST_F(InputDispatcherTest, HoverTapAndSplitTouch) {
2189 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2190 sp<FakeWindowHandle> leftWindow =
2191 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2192 leftWindow->setFrame(Rect(0, 0, 200, 200));
2193
2194 sp<FakeWindowHandle> rightWindow =
2195 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2196 rightWindow->setFrame(Rect(200, 0, 400, 200));
2197
2198 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}});
2199
2200 const int32_t mouseDeviceId = 6;
2201 const int32_t touchDeviceId = 4;
2202 // Hover over the left window. Keep the cursor there.
2203 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2204 injectMotionEvent(mDispatcher,
2205 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
2206 AINPUT_SOURCE_MOUSE)
2207 .deviceId(mouseDeviceId)
2208 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2209 .x(50)
2210 .y(50))
2211 .build()));
2212 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2213
2214 // Tap on left window
2215 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2216 injectMotionEvent(mDispatcher,
2217 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2218 AINPUT_SOURCE_TOUCHSCREEN)
2219 .deviceId(touchDeviceId)
2220 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER)
2221 .x(100)
2222 .y(100))
2223 .build()));
2224
2225 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2226 injectMotionEvent(mDispatcher,
2227 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2228 AINPUT_SOURCE_TOUCHSCREEN)
2229 .deviceId(touchDeviceId)
2230 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER)
2231 .x(100)
2232 .y(100))
2233 .build()));
2234 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2235 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2236 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2237
2238 // First finger down on right window
2239 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2240 injectMotionEvent(mDispatcher,
2241 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2242 AINPUT_SOURCE_TOUCHSCREEN)
2243 .deviceId(touchDeviceId)
2244 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER)
2245 .x(300)
2246 .y(100))
2247 .build()));
2248 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2249
2250 // Second finger down on the left window
2251 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2252 injectMotionEvent(mDispatcher,
2253 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2254 .deviceId(touchDeviceId)
2255 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER)
2256 .x(300)
2257 .y(100))
2258 .pointer(PointerBuilder(1, AMOTION_EVENT_TOOL_TYPE_FINGER)
2259 .x(100)
2260 .y(100))
2261 .build()));
2262 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2263 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_MOVE));
2264
2265 // No more events
2266 leftWindow->assertNoEvents();
2267 rightWindow->assertNoEvents();
2268}
2269
2270/**
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002271 * On the display, have a single window, and also an area where there's no window.
2272 * First pointer touches the "no window" area of the screen. Second pointer touches the window.
2273 * Make sure that the window receives the second pointer, and first pointer is simply ignored.
2274 */
2275TEST_F(InputDispatcherTest, SplitWorksWhenEmptyAreaIsTouched) {
2276 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2277 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002278 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002279
2280 mDispatcher->setInputWindows({{DISPLAY_ID, {window}}});
2281 NotifyMotionArgs args;
2282
2283 // Touch down on the empty space
2284 mDispatcher->notifyMotion(&(args = generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{-1, -1}})));
2285
2286 mDispatcher->waitForIdle();
2287 window->assertNoEvents();
2288
2289 // Now touch down on the window with another pointer
2290 mDispatcher->notifyMotion(&(args = generateTouchArgs(POINTER_1_DOWN, {{-1, -1}, {10, 10}})));
2291 mDispatcher->waitForIdle();
2292 window->consumeMotionDown();
2293}
2294
2295/**
2296 * Same test as above, but instead of touching the empty space, the first touch goes to
2297 * non-touchable window.
2298 */
2299TEST_F(InputDispatcherTest, SplitWorksWhenNonTouchableWindowIsTouched) {
2300 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2301 sp<FakeWindowHandle> window1 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002302 sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002303 window1->setTouchableRegion(Region{{0, 0, 100, 100}});
2304 window1->setTouchable(false);
2305 sp<FakeWindowHandle> window2 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002306 sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002307 window2->setTouchableRegion(Region{{100, 0, 200, 100}});
2308
2309 mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}});
2310
2311 NotifyMotionArgs args;
2312 // Touch down on the non-touchable window
2313 mDispatcher->notifyMotion(&(args = generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}})));
2314
2315 mDispatcher->waitForIdle();
2316 window1->assertNoEvents();
2317 window2->assertNoEvents();
2318
2319 // Now touch down on the window with another pointer
2320 mDispatcher->notifyMotion(&(args = generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}})));
2321 mDispatcher->waitForIdle();
2322 window2->consumeMotionDown();
2323}
2324
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002325/**
2326 * When splitting touch events the downTime should be adjusted such that the downTime corresponds
2327 * to the event time of the first ACTION_DOWN sent to the particular window.
2328 */
2329TEST_F(InputDispatcherTest, SplitTouchesSendCorrectActionDownTime) {
2330 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2331 sp<FakeWindowHandle> window1 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002332 sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002333 window1->setTouchableRegion(Region{{0, 0, 100, 100}});
2334 sp<FakeWindowHandle> window2 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002335 sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002336 window2->setTouchableRegion(Region{{100, 0, 200, 100}});
2337
2338 mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}});
2339
2340 NotifyMotionArgs args;
2341 // Touch down on the first window
2342 mDispatcher->notifyMotion(&(args = generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}})));
2343
2344 mDispatcher->waitForIdle();
2345 InputEvent* inputEvent1 = window1->consume();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002346 ASSERT_NE(inputEvent1, nullptr);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002347 window2->assertNoEvents();
2348 MotionEvent& motionEvent1 = static_cast<MotionEvent&>(*inputEvent1);
2349 nsecs_t downTimeForWindow1 = motionEvent1.getDownTime();
2350 ASSERT_EQ(motionEvent1.getDownTime(), motionEvent1.getEventTime());
2351
2352 // Now touch down on the window with another pointer
2353 mDispatcher->notifyMotion(&(args = generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}})));
2354 mDispatcher->waitForIdle();
2355 InputEvent* inputEvent2 = window2->consume();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002356 ASSERT_NE(inputEvent2, nullptr);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002357 MotionEvent& motionEvent2 = static_cast<MotionEvent&>(*inputEvent2);
2358 nsecs_t downTimeForWindow2 = motionEvent2.getDownTime();
2359 ASSERT_NE(downTimeForWindow1, downTimeForWindow2);
2360 ASSERT_EQ(motionEvent2.getDownTime(), motionEvent2.getEventTime());
2361
2362 // Now move the pointer on the second window
2363 mDispatcher->notifyMotion(
2364 &(args = generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{50, 50}, {151, 51}})));
2365 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002366 window2->consumeMotionEvent(WithDownTime(downTimeForWindow2));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002367
2368 // Now add new touch down on the second window
2369 mDispatcher->notifyMotion(
2370 &(args = generateTouchArgs(POINTER_2_DOWN, {{50, 50}, {151, 51}, {150, 50}})));
2371 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002372 window2->consumeMotionEvent(WithDownTime(downTimeForWindow2));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002373
2374 // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line
2375 window1->consumeMotionMove();
2376 window1->assertNoEvents();
2377
2378 // Now move the pointer on the first window
2379 mDispatcher->notifyMotion(
2380 &(args = generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{51, 51}, {151, 51}})));
2381 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002382 window1->consumeMotionEvent(WithDownTime(downTimeForWindow1));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002383
2384 mDispatcher->notifyMotion(&(
2385 args = generateTouchArgs(POINTER_3_DOWN, {{51, 51}, {151, 51}, {150, 50}, {50, 50}})));
2386 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002387 window1->consumeMotionEvent(WithDownTime(downTimeForWindow1));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002388}
2389
Garfield Tandf26e862020-07-01 20:18:19 -07002390TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) {
Chris Yea209fde2020-07-22 13:54:51 -07002391 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07002392 sp<FakeWindowHandle> windowLeft =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002393 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07002394 windowLeft->setFrame(Rect(0, 0, 600, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07002395 sp<FakeWindowHandle> windowRight =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002396 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07002397 windowRight->setFrame(Rect(600, 0, 1200, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07002398
2399 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2400
2401 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
2402
2403 // Start cursor position in right window so that we can move the cursor to left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002404 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002405 injectMotionEvent(mDispatcher,
2406 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2407 AINPUT_SOURCE_MOUSE)
2408 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2409 .x(900)
2410 .y(400))
2411 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002412 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07002413
2414 // Move cursor into left window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002415 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002416 injectMotionEvent(mDispatcher,
2417 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2418 AINPUT_SOURCE_MOUSE)
2419 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2420 .x(300)
2421 .y(400))
2422 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002423 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2424 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07002425
2426 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002427 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002428 injectMotionEvent(mDispatcher,
2429 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2430 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2431 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2432 .x(300)
2433 .y(400))
2434 .build()));
2435 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2436
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002437 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002438 injectMotionEvent(mDispatcher,
2439 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
2440 AINPUT_SOURCE_MOUSE)
2441 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2442 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2443 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2444 .x(300)
2445 .y(400))
2446 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002447 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
Garfield Tandf26e862020-07-01 20:18:19 -07002448
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002449 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002450 injectMotionEvent(mDispatcher,
2451 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
2452 AINPUT_SOURCE_MOUSE)
2453 .buttonState(0)
2454 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2455 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2456 .x(300)
2457 .y(400))
2458 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002459 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE));
Garfield Tandf26e862020-07-01 20:18:19 -07002460
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002461 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002462 injectMotionEvent(mDispatcher,
2463 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
2464 .buttonState(0)
2465 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2466 .x(300)
2467 .y(400))
2468 .build()));
2469 windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT);
2470
2471 // Move mouse cursor back to right window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002472 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002473 injectMotionEvent(mDispatcher,
2474 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2475 AINPUT_SOURCE_MOUSE)
2476 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2477 .x(900)
2478 .y(400))
2479 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002480 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2481 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002482
2483 // No more events
2484 windowLeft->assertNoEvents();
2485 windowRight->assertNoEvents();
2486}
2487
2488TEST_F(InputDispatcherTest, HoverWithSpyWindows) {
2489 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2490
2491 sp<FakeWindowHandle> spyWindow =
2492 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
2493 spyWindow->setFrame(Rect(0, 0, 600, 800));
2494 spyWindow->setTrustedOverlay(true);
2495 spyWindow->setSpy(true);
2496 sp<FakeWindowHandle> window =
2497 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2498 window->setFrame(Rect(0, 0, 600, 800));
2499
2500 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2501 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
2502
2503 // Send mouse cursor to the window
2504 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2505 injectMotionEvent(mDispatcher,
2506 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
2507 AINPUT_SOURCE_MOUSE)
2508 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2509 .x(100)
2510 .y(100))
2511 .build()));
2512
2513 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2514 WithSource(AINPUT_SOURCE_MOUSE)));
2515 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2516 WithSource(AINPUT_SOURCE_MOUSE)));
2517
2518 window->assertNoEvents();
2519 spyWindow->assertNoEvents();
Garfield Tandf26e862020-07-01 20:18:19 -07002520}
2521
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08002522TEST_F(InputDispatcherTest, MouseAndTouchWithSpyWindows) {
2523 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2524
2525 sp<FakeWindowHandle> spyWindow =
2526 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
2527 spyWindow->setFrame(Rect(0, 0, 600, 800));
2528 spyWindow->setTrustedOverlay(true);
2529 spyWindow->setSpy(true);
2530 sp<FakeWindowHandle> window =
2531 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2532 window->setFrame(Rect(0, 0, 600, 800));
2533
2534 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2535 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
2536
2537 // Send mouse cursor to the window
2538 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2539 injectMotionEvent(mDispatcher,
2540 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
2541 AINPUT_SOURCE_MOUSE)
2542 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2543 .x(100)
2544 .y(100))
2545 .build()));
2546
2547 // Move mouse cursor
2548 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2549 injectMotionEvent(mDispatcher,
2550 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2551 AINPUT_SOURCE_MOUSE)
2552 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2553 .x(110)
2554 .y(110))
2555 .build()));
2556
2557 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2558 WithSource(AINPUT_SOURCE_MOUSE)));
2559 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2560 WithSource(AINPUT_SOURCE_MOUSE)));
2561 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2562 WithSource(AINPUT_SOURCE_MOUSE)));
2563 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2564 WithSource(AINPUT_SOURCE_MOUSE)));
2565 // Touch down on the window
2566 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2567 injectMotionEvent(mDispatcher,
2568 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2569 AINPUT_SOURCE_TOUCHSCREEN)
2570 .deviceId(SECOND_DEVICE_ID)
2571 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER)
2572 .x(200)
2573 .y(200))
2574 .build()));
2575 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2576 WithSource(AINPUT_SOURCE_MOUSE)));
2577 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2578 WithSource(AINPUT_SOURCE_MOUSE)));
2579 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2580 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
2581 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2582 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
2583
2584 // pilfer the motion, retaining the gesture on the spy window.
2585 EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken()));
2586 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
2587 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
2588
2589 // Touch UP on the window
2590 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2591 injectMotionEvent(mDispatcher,
2592 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2593 AINPUT_SOURCE_TOUCHSCREEN)
2594 .deviceId(SECOND_DEVICE_ID)
2595 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER)
2596 .x(200)
2597 .y(200))
2598 .build()));
2599 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
2600 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
2601
2602 // Previously, a touch was pilfered. However, that gesture was just finished. Now, we are going
2603 // to send a new gesture. It should again go to both windows (spy and the window below), just
2604 // like the first gesture did, before pilfering. The window configuration has not changed.
2605
2606 // One more tap - DOWN
2607 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2608 injectMotionEvent(mDispatcher,
2609 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2610 AINPUT_SOURCE_TOUCHSCREEN)
2611 .deviceId(SECOND_DEVICE_ID)
2612 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER)
2613 .x(250)
2614 .y(250))
2615 .build()));
2616 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2617 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
2618 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2619 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
2620
2621 // Touch UP on the window
2622 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2623 injectMotionEvent(mDispatcher,
2624 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2625 AINPUT_SOURCE_TOUCHSCREEN)
2626 .deviceId(SECOND_DEVICE_ID)
2627 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER)
2628 .x(250)
2629 .y(250))
2630 .build()));
2631 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
2632 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
2633 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
2634 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
2635
2636 window->assertNoEvents();
2637 spyWindow->assertNoEvents();
2638}
2639
Garfield Tandf26e862020-07-01 20:18:19 -07002640// This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected
2641// directly in this test.
2642TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) {
Chris Yea209fde2020-07-22 13:54:51 -07002643 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07002644 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002645 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07002646 window->setFrame(Rect(0, 0, 1200, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07002647
2648 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2649
2650 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2651
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002652 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002653 injectMotionEvent(mDispatcher,
2654 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
2655 AINPUT_SOURCE_MOUSE)
2656 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2657 .x(300)
2658 .y(400))
2659 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002660 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07002661 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002662 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002663 injectMotionEvent(mDispatcher,
2664 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2665 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2666 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2667 .x(300)
2668 .y(400))
2669 .build()));
2670 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2671
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002672 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002673 injectMotionEvent(mDispatcher,
2674 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
2675 AINPUT_SOURCE_MOUSE)
2676 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2677 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2678 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2679 .x(300)
2680 .y(400))
2681 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002682 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
Garfield Tandf26e862020-07-01 20:18:19 -07002683
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002684 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002685 injectMotionEvent(mDispatcher,
2686 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
2687 AINPUT_SOURCE_MOUSE)
2688 .buttonState(0)
2689 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2690 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2691 .x(300)
2692 .y(400))
2693 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002694 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE));
Garfield Tandf26e862020-07-01 20:18:19 -07002695
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002696 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002697 injectMotionEvent(mDispatcher,
2698 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
2699 .buttonState(0)
2700 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2701 .x(300)
2702 .y(400))
2703 .build()));
2704 window->consumeMotionUp(ADISPLAY_ID_DEFAULT);
2705
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002706 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002707 injectMotionEvent(mDispatcher,
2708 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT,
2709 AINPUT_SOURCE_MOUSE)
2710 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2711 .x(300)
2712 .y(400))
2713 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002714 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
Garfield Tandf26e862020-07-01 20:18:19 -07002715}
2716
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08002717/**
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00002718 * Hover over a window, and then remove that window. Make sure that HOVER_EXIT for that event
2719 * is generated.
2720 */
2721TEST_F(InputDispatcherTest, HoverExitIsSentToRemovedWindow) {
2722 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2723 sp<FakeWindowHandle> window =
2724 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2725 window->setFrame(Rect(0, 0, 1200, 800));
2726
2727 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2728
2729 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2730
2731 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2732 injectMotionEvent(mDispatcher,
2733 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
2734 AINPUT_SOURCE_MOUSE)
2735 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2736 .x(300)
2737 .y(400))
2738 .build()));
2739 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2740
2741 // Remove the window, but keep the channel.
2742 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
2743 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2744}
2745
2746/**
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08002747 * Inject a mouse hover event followed by a tap from touchscreen.
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00002748 * The tap causes a HOVER_EXIT event to be generated because the current event
2749 * stream's source has been switched.
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08002750 */
2751TEST_F(InputDispatcherTest, MouseHoverAndTouchTap) {
2752 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2753 sp<FakeWindowHandle> window =
2754 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2755 window->setFrame(Rect(0, 0, 100, 100));
2756
2757 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2758
2759 // Inject a hover_move from mouse.
2760 NotifyMotionArgs motionArgs =
2761 generateMotionArgs(AMOTION_EVENT_ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE,
2762 ADISPLAY_ID_DEFAULT, {{50, 50}});
2763 motionArgs.xCursorPosition = 50;
2764 motionArgs.yCursorPosition = 50;
2765 mDispatcher->notifyMotion(&motionArgs);
2766 ASSERT_NO_FATAL_FAILURE(
2767 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2768 WithSource(AINPUT_SOURCE_MOUSE))));
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08002769
2770 // Tap on the window
2771 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2772 ADISPLAY_ID_DEFAULT, {{10, 10}});
2773 mDispatcher->notifyMotion(&motionArgs);
2774 ASSERT_NO_FATAL_FAILURE(
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00002775 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2776 WithSource(AINPUT_SOURCE_MOUSE))));
2777
2778 ASSERT_NO_FATAL_FAILURE(
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08002779 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2780 WithSource(AINPUT_SOURCE_TOUCHSCREEN))));
2781
2782 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
2783 ADISPLAY_ID_DEFAULT, {{10, 10}});
2784 mDispatcher->notifyMotion(&motionArgs);
2785 ASSERT_NO_FATAL_FAILURE(
2786 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
2787 WithSource(AINPUT_SOURCE_TOUCHSCREEN))));
2788}
2789
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02002790TEST_F(InputDispatcherTest, HoverEnterMoveRemoveWindowsInSecondDisplay) {
2791 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2792 sp<FakeWindowHandle> windowDefaultDisplay =
2793 sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay",
2794 ADISPLAY_ID_DEFAULT);
2795 windowDefaultDisplay->setFrame(Rect(0, 0, 600, 800));
2796 sp<FakeWindowHandle> windowSecondDisplay =
2797 sp<FakeWindowHandle>::make(application, mDispatcher, "SecondDisplay",
2798 SECOND_DISPLAY_ID);
2799 windowSecondDisplay->setFrame(Rect(0, 0, 600, 800));
2800
2801 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}},
2802 {SECOND_DISPLAY_ID, {windowSecondDisplay}}});
2803
2804 // Set cursor position in window in default display and check that hover enter and move
2805 // events are generated.
2806 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2807 injectMotionEvent(mDispatcher,
2808 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2809 AINPUT_SOURCE_MOUSE)
2810 .displayId(ADISPLAY_ID_DEFAULT)
2811 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2812 .x(300)
2813 .y(600))
2814 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002815 windowDefaultDisplay->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02002816
2817 // Remove all windows in secondary display and check that no event happens on window in
2818 // primary display.
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002819 mDispatcher->setInputWindows(
2820 {{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}}, {SECOND_DISPLAY_ID, {}}});
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02002821 windowDefaultDisplay->assertNoEvents();
2822
2823 // Move cursor position in window in default display and check that only hover move
2824 // event is generated and not hover enter event.
2825 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}},
2826 {SECOND_DISPLAY_ID, {windowSecondDisplay}}});
2827 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2828 injectMotionEvent(mDispatcher,
2829 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2830 AINPUT_SOURCE_MOUSE)
2831 .displayId(ADISPLAY_ID_DEFAULT)
2832 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2833 .x(400)
2834 .y(700))
2835 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002836 windowDefaultDisplay->consumeMotionEvent(
2837 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2838 WithSource(AINPUT_SOURCE_MOUSE)));
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02002839 windowDefaultDisplay->assertNoEvents();
2840}
2841
Garfield Tan00f511d2019-06-12 16:55:40 -07002842TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) {
Chris Yea209fde2020-07-22 13:54:51 -07002843 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tan00f511d2019-06-12 16:55:40 -07002844
2845 sp<FakeWindowHandle> windowLeft =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002846 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07002847 windowLeft->setFrame(Rect(0, 0, 600, 800));
Garfield Tan00f511d2019-06-12 16:55:40 -07002848 sp<FakeWindowHandle> windowRight =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002849 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07002850 windowRight->setFrame(Rect(600, 0, 1200, 800));
Garfield Tan00f511d2019-06-12 16:55:40 -07002851
2852 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2853
Arthur Hung72d8dc32020-03-28 00:48:39 +00002854 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
Garfield Tan00f511d2019-06-12 16:55:40 -07002855
2856 // Inject an event with coordinate in the area of right window, with mouse cursor in the area of
2857 // left window. This event should be dispatched to the left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002858 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tan00f511d2019-06-12 16:55:40 -07002859 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07002860 ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400}));
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002861 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07002862 windowRight->assertNoEvents();
2863}
2864
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002865TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) {
Chris Yea209fde2020-07-22 13:54:51 -07002866 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002867 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
2868 "Fake Window", ADISPLAY_ID_DEFAULT);
Vishnu Nair47074b82020-08-14 11:54:47 -07002869 window->setFocusable(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002870
Arthur Hung72d8dc32020-03-28 00:48:39 +00002871 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002872 setFocusedWindow(window);
2873
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002874 window->consumeFocusEvent(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002875
2876 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
2877 mDispatcher->notifyKey(&keyArgs);
2878
2879 // Window should receive key down event.
2880 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2881
2882 // When device reset happens, that key stream should be terminated with FLAG_CANCELED
2883 // on the app side.
Harry Cutts33476232023-01-30 19:57:29 +00002884 NotifyDeviceResetArgs args(/*id=*/10, /*eventTime=*/20, DEVICE_ID);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002885 mDispatcher->notifyDeviceReset(&args);
2886 window->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
2887 AKEY_EVENT_FLAG_CANCELED);
2888}
2889
2890TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) {
Chris Yea209fde2020-07-22 13:54:51 -07002891 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002892 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
2893 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002894
Arthur Hung72d8dc32020-03-28 00:48:39 +00002895 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002896
2897 NotifyMotionArgs motionArgs =
2898 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2899 ADISPLAY_ID_DEFAULT);
2900 mDispatcher->notifyMotion(&motionArgs);
2901
2902 // Window should receive motion down event.
2903 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2904
2905 // When device reset happens, that motion stream should be terminated with ACTION_CANCEL
2906 // on the app side.
Harry Cutts33476232023-01-30 19:57:29 +00002907 NotifyDeviceResetArgs args(/*id=*/10, /*eventTime=*/20, DEVICE_ID);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002908 mDispatcher->notifyDeviceReset(&args);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08002909 window->consumeMotionEvent(
2910 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002911}
2912
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08002913TEST_F(InputDispatcherTest, InterceptKeyByPolicy) {
2914 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002915 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
2916 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08002917 window->setFocusable(true);
2918
2919 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2920 setFocusedWindow(window);
2921
2922 window->consumeFocusEvent(true);
2923
2924 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
2925 const std::chrono::milliseconds interceptKeyTimeout = 50ms;
2926 const nsecs_t injectTime = keyArgs.eventTime;
2927 mFakePolicy->setInterceptKeyTimeout(interceptKeyTimeout);
2928 mDispatcher->notifyKey(&keyArgs);
2929 // The dispatching time should be always greater than or equal to intercept key timeout.
2930 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2931 ASSERT_TRUE((systemTime(SYSTEM_TIME_MONOTONIC) - injectTime) >=
2932 std::chrono::nanoseconds(interceptKeyTimeout).count());
2933}
2934
2935TEST_F(InputDispatcherTest, InterceptKeyIfKeyUp) {
2936 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002937 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
2938 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08002939 window->setFocusable(true);
2940
2941 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2942 setFocusedWindow(window);
2943
2944 window->consumeFocusEvent(true);
2945
2946 NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
2947 NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
2948 mFakePolicy->setInterceptKeyTimeout(150ms);
2949 mDispatcher->notifyKey(&keyDown);
2950 mDispatcher->notifyKey(&keyUp);
2951
2952 // Window should receive key event immediately when same key up.
2953 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2954 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
2955}
2956
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07002957/**
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08002958 * Two windows. First is a regular window. Second does not overlap with the first, and has
2959 * WATCH_OUTSIDE_TOUCH.
2960 * Both windows are owned by the same UID.
2961 * Tap first window. Make sure that the second window receives ACTION_OUTSIDE with correct, non-zero
2962 * coordinates. The coordinates are not zeroed out because both windows are owned by the same UID.
2963 */
2964TEST_F(InputDispatcherTest, ActionOutsideForOwnedWindowHasValidCoordinates) {
2965 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2966 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
2967 "First Window", ADISPLAY_ID_DEFAULT);
2968 window->setFrame(Rect{0, 0, 100, 100});
2969
2970 sp<FakeWindowHandle> outsideWindow =
2971 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
2972 ADISPLAY_ID_DEFAULT);
2973 outsideWindow->setFrame(Rect{100, 100, 200, 200});
2974 outsideWindow->setWatchOutsideTouch(true);
2975 // outsideWindow must be above 'window' to receive ACTION_OUTSIDE events when 'window' is tapped
2976 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {outsideWindow, window}}});
2977
2978 // Tap on first window.
2979 NotifyMotionArgs motionArgs =
2980 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2981 ADISPLAY_ID_DEFAULT, {PointF{50, 50}});
2982 mDispatcher->notifyMotion(&motionArgs);
2983 window->consumeMotionDown();
2984 // The coordinates of the tap in 'outsideWindow' are relative to its top left corner.
2985 // Therefore, we should offset them by (100, 100) relative to the screen's top left corner.
2986 outsideWindow->consumeMotionEvent(
2987 AllOf(WithMotionAction(ACTION_OUTSIDE), WithCoords(-50, -50)));
2988}
2989
2990/**
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00002991 * This test documents the behavior of WATCH_OUTSIDE_TOUCH. The window will get ACTION_OUTSIDE when
2992 * a another pointer causes ACTION_DOWN to be sent to another window for the first time. Only one
2993 * ACTION_OUTSIDE event is sent per gesture.
2994 */
2995TEST_F(InputDispatcherTest, ActionOutsideSentOnlyWhenAWindowIsTouched) {
2996 // There are three windows that do not overlap. `window` wants to WATCH_OUTSIDE_TOUCH.
2997 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002998 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
2999 "First Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003000 window->setWatchOutsideTouch(true);
3001 window->setFrame(Rect{0, 0, 100, 100});
3002 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003003 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3004 ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003005 secondWindow->setFrame(Rect{100, 100, 200, 200});
3006 sp<FakeWindowHandle> thirdWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003007 sp<FakeWindowHandle>::make(application, mDispatcher, "Third Window",
3008 ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003009 thirdWindow->setFrame(Rect{200, 200, 300, 300});
3010 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, secondWindow, thirdWindow}}});
3011
3012 // First pointer lands outside all windows. `window` does not get ACTION_OUTSIDE.
3013 NotifyMotionArgs motionArgs =
3014 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3015 ADISPLAY_ID_DEFAULT, {PointF{-10, -10}});
3016 mDispatcher->notifyMotion(&motionArgs);
3017 window->assertNoEvents();
3018 secondWindow->assertNoEvents();
3019
3020 // The second pointer lands inside `secondWindow`, which should receive a DOWN event.
3021 // Now, `window` should get ACTION_OUTSIDE.
3022 motionArgs = generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3023 {PointF{-10, -10}, PointF{105, 105}});
3024 mDispatcher->notifyMotion(&motionArgs);
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08003025 const std::map<int32_t, PointF> expectedPointers{{0, PointF{-10, -10}}, {1, PointF{105, 105}}};
3026 window->consumeMotionEvent(
3027 AllOf(WithMotionAction(ACTION_OUTSIDE), WithPointers(expectedPointers)));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003028 secondWindow->consumeMotionDown();
3029 thirdWindow->assertNoEvents();
3030
3031 // The third pointer lands inside `thirdWindow`, which should receive a DOWN event. There is
3032 // no ACTION_OUTSIDE sent to `window` because one has already been sent for this gesture.
3033 motionArgs = generateMotionArgs(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3034 {PointF{-10, -10}, PointF{105, 105}, PointF{205, 205}});
3035 mDispatcher->notifyMotion(&motionArgs);
3036 window->assertNoEvents();
3037 secondWindow->consumeMotionMove();
3038 thirdWindow->consumeMotionDown();
3039}
3040
Prabir Pradhan814fe082022-07-22 20:22:18 +00003041TEST_F(InputDispatcherTest, OnWindowInfosChanged_RemoveAllWindowsOnDisplay) {
3042 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003043 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3044 "Fake Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan814fe082022-07-22 20:22:18 +00003045 window->setFocusable(true);
3046
3047 mDispatcher->onWindowInfosChanged({*window->getInfo()}, {});
3048 setFocusedWindow(window);
3049
3050 window->consumeFocusEvent(true);
3051
3052 NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
3053 NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
3054 mDispatcher->notifyKey(&keyDown);
3055 mDispatcher->notifyKey(&keyUp);
3056
3057 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3058 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
3059
3060 // All windows are removed from the display. Ensure that we can no longer dispatch to it.
3061 mDispatcher->onWindowInfosChanged({}, {});
3062
3063 window->consumeFocusEvent(false);
3064
3065 mDispatcher->notifyKey(&keyDown);
3066 mDispatcher->notifyKey(&keyUp);
3067 window->assertNoEvents();
3068}
3069
Arthur Hung96483742022-11-15 03:30:48 +00003070TEST_F(InputDispatcherTest, NonSplitTouchableWindowReceivesMultiTouch) {
3071 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3072 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3073 "Fake Window", ADISPLAY_ID_DEFAULT);
3074 // Ensure window is non-split and have some transform.
3075 window->setPreventSplitting(true);
3076 window->setWindowOffset(20, 40);
3077 mDispatcher->onWindowInfosChanged({*window->getInfo()}, {});
3078
3079 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3080 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3081 {50, 50}))
3082 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3083 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3084
3085 const MotionEvent secondFingerDownEvent =
3086 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3087 .displayId(ADISPLAY_ID_DEFAULT)
3088 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Harry Cutts33476232023-01-30 19:57:29 +00003089 .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
3090 .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(-30).y(-50))
Arthur Hung96483742022-11-15 03:30:48 +00003091 .build();
3092 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3093 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
3094 InputEventInjectionSync::WAIT_FOR_RESULT))
3095 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3096
3097 const MotionEvent* event = window->consumeMotion();
3098 EXPECT_EQ(POINTER_1_DOWN, event->getAction());
3099 EXPECT_EQ(70, event->getX(0)); // 50 + 20
3100 EXPECT_EQ(90, event->getY(0)); // 50 + 40
3101 EXPECT_EQ(-10, event->getX(1)); // -30 + 20
3102 EXPECT_EQ(-10, event->getY(1)); // -50 + 40
3103}
3104
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003105/**
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003106 * Ensure the correct coordinate spaces are used by InputDispatcher.
3107 *
3108 * InputDispatcher works in the display space, so its coordinate system is relative to the display
3109 * panel. Windows get events in the window space, and get raw coordinates in the logical display
3110 * space.
3111 */
3112class InputDispatcherDisplayProjectionTest : public InputDispatcherTest {
3113public:
3114 void SetUp() override {
3115 InputDispatcherTest::SetUp();
3116 mDisplayInfos.clear();
3117 mWindowInfos.clear();
3118 }
3119
3120 void addDisplayInfo(int displayId, const ui::Transform& transform) {
3121 gui::DisplayInfo info;
3122 info.displayId = displayId;
3123 info.transform = transform;
3124 mDisplayInfos.push_back(std::move(info));
3125 mDispatcher->onWindowInfosChanged(mWindowInfos, mDisplayInfos);
3126 }
3127
3128 void addWindow(const sp<WindowInfoHandle>& windowHandle) {
3129 mWindowInfos.push_back(*windowHandle->getInfo());
3130 mDispatcher->onWindowInfosChanged(mWindowInfos, mDisplayInfos);
3131 }
3132
3133 // Set up a test scenario where the display has a scaled projection and there are two windows
3134 // on the display.
3135 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupScaledDisplayScenario() {
3136 // The display has a projection that has a scale factor of 2 and 4 in the x and y directions
3137 // respectively.
3138 ui::Transform displayTransform;
3139 displayTransform.set(2, 0, 0, 4);
3140 addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform);
3141
3142 std::shared_ptr<FakeApplicationHandle> application =
3143 std::make_shared<FakeApplicationHandle>();
3144
3145 // Add two windows to the display. Their frames are represented in the display space.
3146 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003147 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
3148 ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003149 firstWindow->setFrame(Rect(0, 0, 100, 200), displayTransform);
3150 addWindow(firstWindow);
3151
3152 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003153 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3154 ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003155 secondWindow->setFrame(Rect(100, 200, 200, 400), displayTransform);
3156 addWindow(secondWindow);
3157 return {std::move(firstWindow), std::move(secondWindow)};
3158 }
3159
3160private:
3161 std::vector<gui::DisplayInfo> mDisplayInfos;
3162 std::vector<gui::WindowInfo> mWindowInfos;
3163};
3164
3165TEST_F(InputDispatcherDisplayProjectionTest, HitTestsInDisplaySpace) {
3166 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
3167 // Send down to the first window. The point is represented in the display space. The point is
3168 // selected so that if the hit test was done with the transform applied to it, then it would
3169 // end up in the incorrect window.
3170 NotifyMotionArgs downMotionArgs =
3171 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3172 ADISPLAY_ID_DEFAULT, {PointF{75, 55}});
3173 mDispatcher->notifyMotion(&downMotionArgs);
3174
3175 firstWindow->consumeMotionDown();
3176 secondWindow->assertNoEvents();
3177}
3178
3179// Ensure that when a MotionEvent is injected through the InputDispatcher::injectInputEvent() API,
3180// the event should be treated as being in the logical display space.
3181TEST_F(InputDispatcherDisplayProjectionTest, InjectionInLogicalDisplaySpace) {
3182 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
3183 // Send down to the first window. The point is represented in the logical display space. The
3184 // point is selected so that if the hit test was done in logical display space, then it would
3185 // end up in the incorrect window.
3186 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3187 PointF{75 * 2, 55 * 4});
3188
3189 firstWindow->consumeMotionDown();
3190 secondWindow->assertNoEvents();
3191}
3192
Prabir Pradhandaa2f142021-12-10 09:30:08 +00003193// Ensure that when a MotionEvent that has a custom transform is injected, the post-transformed
3194// event should be treated as being in the logical display space.
3195TEST_F(InputDispatcherDisplayProjectionTest, InjectionWithTransformInLogicalDisplaySpace) {
3196 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
3197
3198 const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0};
3199 ui::Transform injectedEventTransform;
3200 injectedEventTransform.set(matrix);
3201 const vec2 expectedPoint{75, 55}; // The injected point in the logical display space.
3202 const vec2 untransformedPoint = injectedEventTransform.inverse().transform(expectedPoint);
3203
3204 MotionEvent event = MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3205 .displayId(ADISPLAY_ID_DEFAULT)
3206 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Harry Cutts33476232023-01-30 19:57:29 +00003207 .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER)
Prabir Pradhandaa2f142021-12-10 09:30:08 +00003208 .x(untransformedPoint.x)
3209 .y(untransformedPoint.y))
3210 .build();
3211 event.transform(matrix);
3212
3213 injectMotionEvent(mDispatcher, event, INJECT_EVENT_TIMEOUT,
3214 InputEventInjectionSync::WAIT_FOR_RESULT);
3215
3216 firstWindow->consumeMotionDown();
3217 secondWindow->assertNoEvents();
3218}
3219
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003220TEST_F(InputDispatcherDisplayProjectionTest, WindowGetsEventsInCorrectCoordinateSpace) {
3221 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
3222
3223 // Send down to the second window.
3224 NotifyMotionArgs downMotionArgs =
3225 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3226 ADISPLAY_ID_DEFAULT, {PointF{150, 220}});
3227 mDispatcher->notifyMotion(&downMotionArgs);
3228
3229 firstWindow->assertNoEvents();
3230 const MotionEvent* event = secondWindow->consumeMotion();
3231 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, event->getAction());
3232
3233 // Ensure that the events from the "getRaw" API are in logical display coordinates.
3234 EXPECT_EQ(300, event->getRawX(0));
3235 EXPECT_EQ(880, event->getRawY(0));
3236
3237 // Ensure that the x and y values are in the window's coordinate space.
3238 // The left-top of the second window is at (100, 200) in display space, which is (200, 800) in
3239 // the logical display space. This will be the origin of the window space.
3240 EXPECT_EQ(100, event->getX(0));
3241 EXPECT_EQ(80, event->getY(0));
3242}
3243
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003244using TransferFunction = std::function<bool(const std::unique_ptr<InputDispatcher>& dispatcher,
3245 sp<IBinder>, sp<IBinder>)>;
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003246
3247class TransferTouchFixture : public InputDispatcherTest,
3248 public ::testing::WithParamInterface<TransferFunction> {};
3249
3250TEST_P(TransferTouchFixture, TransferTouch_OnePointer) {
Chris Yea209fde2020-07-22 13:54:51 -07003251 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08003252
3253 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003254 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003255 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
3256 ADISPLAY_ID_DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00003257 firstWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003258 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003259 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3260 ADISPLAY_ID_DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00003261 sp<FakeWindowHandle> wallpaper =
3262 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
3263 wallpaper->setIsWallpaper(true);
Svet Ganov5d3bc372020-01-26 23:11:07 -08003264 // Add the windows to the dispatcher
Arthur Hungc539dbb2022-12-08 07:45:36 +00003265 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow, wallpaper}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08003266
3267 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003268 NotifyMotionArgs downMotionArgs =
3269 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3270 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08003271 mDispatcher->notifyMotion(&downMotionArgs);
Arthur Hungc539dbb2022-12-08 07:45:36 +00003272
Svet Ganov5d3bc372020-01-26 23:11:07 -08003273 // Only the first window should get the down event
3274 firstWindow->consumeMotionDown();
3275 secondWindow->assertNoEvents();
Arthur Hungc539dbb2022-12-08 07:45:36 +00003276 wallpaper->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08003277
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003278 // Transfer touch to the second window
3279 TransferFunction f = GetParam();
3280 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
3281 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08003282 // The first window gets cancel and the second gets down
3283 firstWindow->consumeMotionCancel();
3284 secondWindow->consumeMotionDown();
Arthur Hungc539dbb2022-12-08 07:45:36 +00003285 wallpaper->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08003286
3287 // Send up event to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003288 NotifyMotionArgs upMotionArgs =
3289 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3290 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08003291 mDispatcher->notifyMotion(&upMotionArgs);
3292 // The first window gets no events and the second gets up
3293 firstWindow->assertNoEvents();
3294 secondWindow->consumeMotionUp();
Arthur Hungc539dbb2022-12-08 07:45:36 +00003295 wallpaper->assertNoEvents();
Svet Ganov5d3bc372020-01-26 23:11:07 -08003296}
3297
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07003298/**
3299 * When 'transferTouch' API is invoked, dispatcher needs to find the "best" window to take touch
3300 * from. When we have spy windows, there are several windows to choose from: either spy, or the
3301 * 'real' (non-spy) window. Always prefer the 'real' window because that's what would be most
3302 * natural to the user.
3303 * In this test, we are sending a pointer to both spy window and first window. We then try to
3304 * transfer touch to the second window. The dispatcher should identify the first window as the
3305 * one that should lose the gesture, and therefore the action should be to move the gesture from
3306 * the first window to the second.
3307 * The main goal here is to test the behaviour of 'transferTouch' API, but it's still valid to test
3308 * the other API, as well.
3309 */
3310TEST_P(TransferTouchFixture, TransferTouch_MultipleWindowsWithSpy) {
3311 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3312
3313 // Create a couple of windows + a spy window
3314 sp<FakeWindowHandle> spyWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003315 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07003316 spyWindow->setTrustedOverlay(true);
3317 spyWindow->setSpy(true);
3318 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003319 sp<FakeWindowHandle>::make(application, mDispatcher, "First", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07003320 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003321 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07003322
3323 // Add the windows to the dispatcher
3324 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, firstWindow, secondWindow}}});
3325
3326 // Send down to the first window
3327 NotifyMotionArgs downMotionArgs =
3328 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3329 ADISPLAY_ID_DEFAULT);
3330 mDispatcher->notifyMotion(&downMotionArgs);
3331 // Only the first window and spy should get the down event
3332 spyWindow->consumeMotionDown();
3333 firstWindow->consumeMotionDown();
3334
3335 // Transfer touch to the second window. Non-spy window should be preferred over the spy window
3336 // if f === 'transferTouch'.
3337 TransferFunction f = GetParam();
3338 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
3339 ASSERT_TRUE(success);
3340 // The first window gets cancel and the second gets down
3341 firstWindow->consumeMotionCancel();
3342 secondWindow->consumeMotionDown();
3343
3344 // Send up event to the second window
3345 NotifyMotionArgs upMotionArgs =
3346 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3347 ADISPLAY_ID_DEFAULT);
3348 mDispatcher->notifyMotion(&upMotionArgs);
3349 // The first window gets no events and the second+spy get up
3350 firstWindow->assertNoEvents();
3351 spyWindow->consumeMotionUp();
3352 secondWindow->consumeMotionUp();
3353}
3354
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003355TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07003356 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08003357
3358 PointF touchPoint = {10, 10};
3359
3360 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003361 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003362 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
3363 ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08003364 firstWindow->setPreventSplitting(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003365 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003366 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3367 ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08003368 secondWindow->setPreventSplitting(true);
Svet Ganov5d3bc372020-01-26 23:11:07 -08003369
3370 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00003371 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08003372
3373 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003374 NotifyMotionArgs downMotionArgs =
3375 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3376 ADISPLAY_ID_DEFAULT, {touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08003377 mDispatcher->notifyMotion(&downMotionArgs);
3378 // Only the first window should get the down event
3379 firstWindow->consumeMotionDown();
3380 secondWindow->assertNoEvents();
3381
3382 // Send pointer down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003383 NotifyMotionArgs pointerDownMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003384 generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003385 {touchPoint, touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08003386 mDispatcher->notifyMotion(&pointerDownMotionArgs);
3387 // Only the first window should get the pointer down event
3388 firstWindow->consumeMotionPointerDown(1);
3389 secondWindow->assertNoEvents();
3390
3391 // Transfer touch focus to the second window
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003392 TransferFunction f = GetParam();
3393 bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
3394 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08003395 // The first window gets cancel and the second gets down and pointer down
3396 firstWindow->consumeMotionCancel();
3397 secondWindow->consumeMotionDown();
3398 secondWindow->consumeMotionPointerDown(1);
3399
3400 // Send pointer up to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003401 NotifyMotionArgs pointerUpMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003402 generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003403 {touchPoint, touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08003404 mDispatcher->notifyMotion(&pointerUpMotionArgs);
3405 // The first window gets nothing and the second gets pointer up
3406 firstWindow->assertNoEvents();
3407 secondWindow->consumeMotionPointerUp(1);
3408
3409 // Send up event to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003410 NotifyMotionArgs upMotionArgs =
3411 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3412 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08003413 mDispatcher->notifyMotion(&upMotionArgs);
3414 // The first window gets nothing and the second gets up
3415 firstWindow->assertNoEvents();
3416 secondWindow->consumeMotionUp();
3417}
3418
Arthur Hungc539dbb2022-12-08 07:45:36 +00003419TEST_P(TransferTouchFixture, TransferTouch_MultipleWallpapers) {
3420 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3421
3422 // Create a couple of windows
3423 sp<FakeWindowHandle> firstWindow =
3424 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
3425 ADISPLAY_ID_DEFAULT);
3426 firstWindow->setDupTouchToWallpaper(true);
3427 sp<FakeWindowHandle> secondWindow =
3428 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3429 ADISPLAY_ID_DEFAULT);
3430 secondWindow->setDupTouchToWallpaper(true);
3431
3432 sp<FakeWindowHandle> wallpaper1 =
3433 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper1", ADISPLAY_ID_DEFAULT);
3434 wallpaper1->setIsWallpaper(true);
3435
3436 sp<FakeWindowHandle> wallpaper2 =
3437 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper2", ADISPLAY_ID_DEFAULT);
3438 wallpaper2->setIsWallpaper(true);
3439 // Add the windows to the dispatcher
3440 mDispatcher->setInputWindows(
3441 {{ADISPLAY_ID_DEFAULT, {firstWindow, wallpaper1, secondWindow, wallpaper2}}});
3442
3443 // Send down to the first window
3444 NotifyMotionArgs downMotionArgs =
3445 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3446 ADISPLAY_ID_DEFAULT);
3447 mDispatcher->notifyMotion(&downMotionArgs);
3448
3449 // Only the first window should get the down event
3450 firstWindow->consumeMotionDown();
3451 secondWindow->assertNoEvents();
3452 wallpaper1->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
3453 wallpaper2->assertNoEvents();
3454
3455 // Transfer touch focus to the second window
3456 TransferFunction f = GetParam();
3457 bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
3458 ASSERT_TRUE(success);
3459
3460 // The first window gets cancel and the second gets down
3461 firstWindow->consumeMotionCancel();
3462 secondWindow->consumeMotionDown();
3463 wallpaper1->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
3464 wallpaper2->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
3465
3466 // Send up event to the second window
3467 NotifyMotionArgs upMotionArgs =
3468 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3469 ADISPLAY_ID_DEFAULT);
3470 mDispatcher->notifyMotion(&upMotionArgs);
3471 // The first window gets no events and the second gets up
3472 firstWindow->assertNoEvents();
3473 secondWindow->consumeMotionUp();
3474 wallpaper1->assertNoEvents();
3475 wallpaper2->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
3476}
3477
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003478// For the cases of single pointer touch and two pointers non-split touch, the api's
3479// 'transferTouch' and 'transferTouchFocus' are equivalent in behaviour. They only differ
3480// for the case where there are multiple pointers split across several windows.
3481INSTANTIATE_TEST_SUITE_P(TransferFunctionTests, TransferTouchFixture,
3482 ::testing::Values(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003483 [&](const std::unique_ptr<InputDispatcher>& dispatcher,
3484 sp<IBinder> /*ignored*/, sp<IBinder> destChannelToken) {
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07003485 return dispatcher->transferTouch(destChannelToken,
3486 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003487 },
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003488 [&](const std::unique_ptr<InputDispatcher>& dispatcher,
3489 sp<IBinder> from, sp<IBinder> to) {
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003490 return dispatcher->transferTouchFocus(from, to,
Harry Cutts33476232023-01-30 19:57:29 +00003491 /*isDragAndDrop=*/false);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003492 }));
3493
Svet Ganov5d3bc372020-01-26 23:11:07 -08003494TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07003495 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08003496
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003497 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003498 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
3499 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08003500 firstWindow->setFrame(Rect(0, 0, 600, 400));
Svet Ganov5d3bc372020-01-26 23:11:07 -08003501
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003502 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003503 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3504 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08003505 secondWindow->setFrame(Rect(0, 400, 600, 800));
Svet Ganov5d3bc372020-01-26 23:11:07 -08003506
3507 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00003508 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08003509
3510 PointF pointInFirst = {300, 200};
3511 PointF pointInSecond = {300, 600};
3512
3513 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003514 NotifyMotionArgs firstDownMotionArgs =
3515 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3516 ADISPLAY_ID_DEFAULT, {pointInFirst});
Svet Ganov5d3bc372020-01-26 23:11:07 -08003517 mDispatcher->notifyMotion(&firstDownMotionArgs);
3518 // Only the first window should get the down event
3519 firstWindow->consumeMotionDown();
3520 secondWindow->assertNoEvents();
3521
3522 // Send down to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003523 NotifyMotionArgs secondDownMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003524 generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003525 {pointInFirst, pointInSecond});
Svet Ganov5d3bc372020-01-26 23:11:07 -08003526 mDispatcher->notifyMotion(&secondDownMotionArgs);
3527 // The first window gets a move and the second a down
3528 firstWindow->consumeMotionMove();
3529 secondWindow->consumeMotionDown();
3530
3531 // Transfer touch focus to the second window
3532 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
3533 // The first window gets cancel and the new gets pointer down (it already saw down)
3534 firstWindow->consumeMotionCancel();
3535 secondWindow->consumeMotionPointerDown(1);
3536
3537 // Send pointer up to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003538 NotifyMotionArgs pointerUpMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003539 generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003540 {pointInFirst, pointInSecond});
Svet Ganov5d3bc372020-01-26 23:11:07 -08003541 mDispatcher->notifyMotion(&pointerUpMotionArgs);
3542 // The first window gets nothing and the second gets pointer up
3543 firstWindow->assertNoEvents();
3544 secondWindow->consumeMotionPointerUp(1);
3545
3546 // Send up event to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003547 NotifyMotionArgs upMotionArgs =
3548 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3549 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08003550 mDispatcher->notifyMotion(&upMotionArgs);
3551 // The first window gets nothing and the second gets up
3552 firstWindow->assertNoEvents();
3553 secondWindow->consumeMotionUp();
3554}
3555
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003556// Same as TransferTouchFocus_TwoPointersSplitTouch, but using 'transferTouch' api.
3557// Unlike 'transferTouchFocus', calling 'transferTouch' when there are two windows receiving
3558// touch is not supported, so the touch should continue on those windows and the transferred-to
3559// window should get nothing.
3560TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) {
3561 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3562
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003563 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003564 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
3565 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003566 firstWindow->setFrame(Rect(0, 0, 600, 400));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003567
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003568 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003569 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3570 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003571 secondWindow->setFrame(Rect(0, 400, 600, 800));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003572
3573 // Add the windows to the dispatcher
3574 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
3575
3576 PointF pointInFirst = {300, 200};
3577 PointF pointInSecond = {300, 600};
3578
3579 // Send down to the first window
3580 NotifyMotionArgs firstDownMotionArgs =
3581 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3582 ADISPLAY_ID_DEFAULT, {pointInFirst});
3583 mDispatcher->notifyMotion(&firstDownMotionArgs);
3584 // Only the first window should get the down event
3585 firstWindow->consumeMotionDown();
3586 secondWindow->assertNoEvents();
3587
3588 // Send down to the second window
3589 NotifyMotionArgs secondDownMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003590 generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003591 {pointInFirst, pointInSecond});
3592 mDispatcher->notifyMotion(&secondDownMotionArgs);
3593 // The first window gets a move and the second a down
3594 firstWindow->consumeMotionMove();
3595 secondWindow->consumeMotionDown();
3596
3597 // Transfer touch focus to the second window
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07003598 const bool transferred =
3599 mDispatcher->transferTouch(secondWindow->getToken(), ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003600 // The 'transferTouch' call should not succeed, because there are 2 touched windows
3601 ASSERT_FALSE(transferred);
3602 firstWindow->assertNoEvents();
3603 secondWindow->assertNoEvents();
3604
3605 // The rest of the dispatch should proceed as normal
3606 // Send pointer up to the second window
3607 NotifyMotionArgs pointerUpMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003608 generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003609 {pointInFirst, pointInSecond});
3610 mDispatcher->notifyMotion(&pointerUpMotionArgs);
3611 // The first window gets MOVE and the second gets pointer up
3612 firstWindow->consumeMotionMove();
3613 secondWindow->consumeMotionUp();
3614
3615 // Send up event to the first window
3616 NotifyMotionArgs upMotionArgs =
3617 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3618 ADISPLAY_ID_DEFAULT);
3619 mDispatcher->notifyMotion(&upMotionArgs);
3620 // The first window gets nothing and the second gets up
3621 firstWindow->consumeMotionUp();
3622 secondWindow->assertNoEvents();
3623}
3624
Arthur Hungabbb9d82021-09-01 14:52:30 +00003625// This case will create two windows and one mirrored window on the default display and mirror
3626// two windows on the second display. It will test if 'transferTouchFocus' works fine if we put
3627// the windows info of second display before default display.
3628TEST_F(InputDispatcherTest, TransferTouchFocus_CloneSurface) {
3629 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3630 sp<FakeWindowHandle> firstWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003631 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00003632 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003633 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003634 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00003635 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003636
3637 sp<FakeWindowHandle> mirrorWindowInPrimary =
3638 firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT);
3639 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003640
3641 sp<FakeWindowHandle> firstWindowInSecondary =
3642 firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
3643 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003644
3645 sp<FakeWindowHandle> secondWindowInSecondary =
3646 secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
3647 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003648
3649 // Update window info, let it find window handle of second display first.
3650 mDispatcher->setInputWindows(
3651 {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}},
3652 {ADISPLAY_ID_DEFAULT,
3653 {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}});
3654
3655 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3656 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3657 {50, 50}))
3658 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3659
3660 // Window should receive motion event.
3661 firstWindowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3662
3663 // Transfer touch focus
3664 ASSERT_TRUE(mDispatcher->transferTouchFocus(firstWindowInPrimary->getToken(),
3665 secondWindowInPrimary->getToken()));
3666 // The first window gets cancel.
3667 firstWindowInPrimary->consumeMotionCancel();
3668 secondWindowInPrimary->consumeMotionDown();
3669
3670 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3671 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
3672 ADISPLAY_ID_DEFAULT, {150, 50}))
3673 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3674 firstWindowInPrimary->assertNoEvents();
3675 secondWindowInPrimary->consumeMotionMove();
3676
3677 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3678 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3679 {150, 50}))
3680 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3681 firstWindowInPrimary->assertNoEvents();
3682 secondWindowInPrimary->consumeMotionUp();
3683}
3684
3685// Same as TransferTouchFocus_CloneSurface, but this touch on the secondary display and use
3686// 'transferTouch' api.
3687TEST_F(InputDispatcherTest, TransferTouch_CloneSurface) {
3688 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3689 sp<FakeWindowHandle> firstWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003690 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00003691 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003692 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003693 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00003694 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003695
3696 sp<FakeWindowHandle> mirrorWindowInPrimary =
3697 firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT);
3698 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003699
3700 sp<FakeWindowHandle> firstWindowInSecondary =
3701 firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
3702 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003703
3704 sp<FakeWindowHandle> secondWindowInSecondary =
3705 secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
3706 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003707
3708 // Update window info, let it find window handle of second display first.
3709 mDispatcher->setInputWindows(
3710 {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}},
3711 {ADISPLAY_ID_DEFAULT,
3712 {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}});
3713
3714 // Touch on second display.
3715 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3716 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {50, 50}))
3717 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3718
3719 // Window should receive motion event.
3720 firstWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID);
3721
3722 // Transfer touch focus
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07003723 ASSERT_TRUE(mDispatcher->transferTouch(secondWindowInSecondary->getToken(), SECOND_DISPLAY_ID));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003724
3725 // The first window gets cancel.
3726 firstWindowInPrimary->consumeMotionCancel(SECOND_DISPLAY_ID);
3727 secondWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID);
3728
3729 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3730 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
3731 SECOND_DISPLAY_ID, {150, 50}))
3732 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3733 firstWindowInPrimary->assertNoEvents();
3734 secondWindowInPrimary->consumeMotionMove(SECOND_DISPLAY_ID);
3735
3736 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3737 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {150, 50}))
3738 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3739 firstWindowInPrimary->assertNoEvents();
3740 secondWindowInPrimary->consumeMotionUp(SECOND_DISPLAY_ID);
3741}
3742
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003743TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07003744 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003745 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3746 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003747
Vishnu Nair47074b82020-08-14 11:54:47 -07003748 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00003749 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003750 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003751
3752 window->consumeFocusEvent(true);
3753
3754 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
3755 mDispatcher->notifyKey(&keyArgs);
3756
3757 // Window should receive key down event.
3758 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3759}
3760
3761TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07003762 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003763 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3764 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003765
Arthur Hung72d8dc32020-03-28 00:48:39 +00003766 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003767
3768 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
3769 mDispatcher->notifyKey(&keyArgs);
3770 mDispatcher->waitForIdle();
3771
3772 window->assertNoEvents();
3773}
3774
3775// If a window is touchable, but does not have focus, it should receive motion events, but not keys
3776TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) {
Chris Yea209fde2020-07-22 13:54:51 -07003777 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003778 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3779 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003780
Arthur Hung72d8dc32020-03-28 00:48:39 +00003781 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003782
3783 // Send key
3784 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
3785 mDispatcher->notifyKey(&keyArgs);
3786 // Send motion
3787 NotifyMotionArgs motionArgs =
3788 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3789 ADISPLAY_ID_DEFAULT);
3790 mDispatcher->notifyMotion(&motionArgs);
3791
3792 // Window should receive only the motion event
3793 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3794 window->assertNoEvents(); // Key event or focus event will not be received
3795}
3796
arthurhungea3f4fc2020-12-21 23:18:53 +08003797TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) {
3798 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3799
arthurhungea3f4fc2020-12-21 23:18:53 +08003800 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003801 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
3802 ADISPLAY_ID_DEFAULT);
arthurhungea3f4fc2020-12-21 23:18:53 +08003803 firstWindow->setFrame(Rect(0, 0, 600, 400));
arthurhungea3f4fc2020-12-21 23:18:53 +08003804
arthurhungea3f4fc2020-12-21 23:18:53 +08003805 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003806 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3807 ADISPLAY_ID_DEFAULT);
arthurhungea3f4fc2020-12-21 23:18:53 +08003808 secondWindow->setFrame(Rect(0, 400, 600, 800));
arthurhungea3f4fc2020-12-21 23:18:53 +08003809
3810 // Add the windows to the dispatcher
3811 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
3812
3813 PointF pointInFirst = {300, 200};
3814 PointF pointInSecond = {300, 600};
3815
3816 // Send down to the first window
3817 NotifyMotionArgs firstDownMotionArgs =
3818 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3819 ADISPLAY_ID_DEFAULT, {pointInFirst});
3820 mDispatcher->notifyMotion(&firstDownMotionArgs);
3821 // Only the first window should get the down event
3822 firstWindow->consumeMotionDown();
3823 secondWindow->assertNoEvents();
3824
3825 // Send down to the second window
3826 NotifyMotionArgs secondDownMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003827 generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
arthurhungea3f4fc2020-12-21 23:18:53 +08003828 {pointInFirst, pointInSecond});
3829 mDispatcher->notifyMotion(&secondDownMotionArgs);
3830 // The first window gets a move and the second a down
3831 firstWindow->consumeMotionMove();
3832 secondWindow->consumeMotionDown();
3833
3834 // Send pointer cancel to the second window
3835 NotifyMotionArgs pointerUpMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003836 generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
arthurhungea3f4fc2020-12-21 23:18:53 +08003837 {pointInFirst, pointInSecond});
3838 pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED;
3839 mDispatcher->notifyMotion(&pointerUpMotionArgs);
3840 // The first window gets move and the second gets cancel.
3841 firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
3842 secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
3843
3844 // Send up event.
3845 NotifyMotionArgs upMotionArgs =
3846 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3847 ADISPLAY_ID_DEFAULT);
3848 mDispatcher->notifyMotion(&upMotionArgs);
3849 // The first window gets up and the second gets nothing.
3850 firstWindow->consumeMotionUp();
3851 secondWindow->assertNoEvents();
3852}
3853
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00003854TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) {
3855 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3856
3857 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003858 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00003859 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3860 std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline;
3861 graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2;
3862 graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3;
3863
Harry Cutts33476232023-01-30 19:57:29 +00003864 window->sendTimeline(/*inputEventId=*/1, graphicsTimeline);
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00003865 window->assertNoEvents();
3866 mDispatcher->waitForIdle();
3867}
3868
chaviwd1c23182019-12-20 18:44:56 -08003869class FakeMonitorReceiver {
Michael Wright3a240c42019-12-10 20:53:41 +00003870public:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003871 FakeMonitorReceiver(const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003872 int32_t displayId) {
Garfield Tan15601662020-09-22 15:32:38 -07003873 base::Result<std::unique_ptr<InputChannel>> channel =
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08003874 dispatcher->createInputMonitor(displayId, name, MONITOR_PID);
Garfield Tan15601662020-09-22 15:32:38 -07003875 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
Michael Wright3a240c42019-12-10 20:53:41 +00003876 }
3877
chaviwd1c23182019-12-20 18:44:56 -08003878 sp<IBinder> getToken() { return mInputReceiver->getToken(); }
3879
3880 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
3881 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN,
3882 expectedDisplayId, expectedFlags);
3883 }
3884
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003885 std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); }
3886
3887 void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); }
3888
chaviwd1c23182019-12-20 18:44:56 -08003889 void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
3890 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN,
3891 expectedDisplayId, expectedFlags);
3892 }
3893
Siarhei Vishniakouca205502021-07-16 21:31:58 +00003894 void consumeMotionMove(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
3895 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE,
3896 expectedDisplayId, expectedFlags);
3897 }
3898
chaviwd1c23182019-12-20 18:44:56 -08003899 void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
3900 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP,
3901 expectedDisplayId, expectedFlags);
3902 }
3903
Siarhei Vishniakouca205502021-07-16 21:31:58 +00003904 void consumeMotionCancel(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08003905 mInputReceiver->consumeMotionEvent(
3906 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
3907 WithDisplayId(expectedDisplayId),
3908 WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED)));
Siarhei Vishniakouca205502021-07-16 21:31:58 +00003909 }
3910
Arthur Hungfbfa5722021-11-16 02:45:54 +00003911 void consumeMotionPointerDown(int32_t pointerIdx) {
3912 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
3913 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
3914 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00003915 /*expectedFlags=*/0);
Arthur Hungfbfa5722021-11-16 02:45:54 +00003916 }
3917
Evan Rosky84f07f02021-04-16 10:42:42 -07003918 MotionEvent* consumeMotion() {
3919 InputEvent* event = mInputReceiver->consume();
3920 if (!event) {
3921 ADD_FAILURE() << "No event was produced";
3922 return nullptr;
3923 }
3924 if (event->getType() != AINPUT_EVENT_TYPE_MOTION) {
3925 ADD_FAILURE() << "Received event of type " << event->getType() << " instead of motion";
3926 return nullptr;
3927 }
3928 return static_cast<MotionEvent*>(event);
3929 }
3930
chaviwd1c23182019-12-20 18:44:56 -08003931 void assertNoEvents() { mInputReceiver->assertNoEvents(); }
3932
3933private:
3934 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Michael Wright3a240c42019-12-10 20:53:41 +00003935};
3936
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003937using InputDispatcherMonitorTest = InputDispatcherTest;
3938
Siarhei Vishniakouca205502021-07-16 21:31:58 +00003939/**
3940 * Two entities that receive touch: A window, and a global monitor.
3941 * The touch goes to the window, and then the window disappears.
3942 * The monitor does not get cancel right away. But if more events come in, the touch gets canceled
3943 * for the monitor, as well.
3944 * 1. foregroundWindow
3945 * 2. monitor <-- global monitor (doesn't observe z order, receives all events)
3946 */
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003947TEST_F(InputDispatcherMonitorTest, MonitorTouchIsCanceledWhenForegroundWindowDisappears) {
Siarhei Vishniakouca205502021-07-16 21:31:58 +00003948 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3949 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003950 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00003951
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003952 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00003953
3954 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3955 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3956 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3957 {100, 200}))
3958 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3959
3960 // Both the foreground window and the global monitor should receive the touch down
3961 window->consumeMotionDown();
3962 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
3963
3964 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3965 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
3966 ADISPLAY_ID_DEFAULT, {110, 200}))
3967 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3968
3969 window->consumeMotionMove();
3970 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
3971
3972 // Now the foreground window goes away
3973 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
3974 window->consumeMotionCancel();
3975 monitor.assertNoEvents(); // Global monitor does not get a cancel yet
3976
3977 // If more events come in, there will be no more foreground window to send them to. This will
3978 // cause a cancel for the monitor, as well.
3979 ASSERT_EQ(InputEventInjectionResult::FAILED,
3980 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
3981 ADISPLAY_ID_DEFAULT, {120, 200}))
3982 << "Injection should fail because the window was removed";
3983 window->assertNoEvents();
3984 // Global monitor now gets the cancel
3985 monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
3986}
3987
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003988TEST_F(InputDispatcherMonitorTest, ReceivesMotionEvents) {
Chris Yea209fde2020-07-22 13:54:51 -07003989 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003990 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3991 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00003992 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00003993
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003994 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00003995
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003996 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00003997 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003998 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00003999 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08004000 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00004001}
4002
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004003TEST_F(InputDispatcherMonitorTest, MonitorCannotPilferPointers) {
4004 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00004005
Chris Yea209fde2020-07-22 13:54:51 -07004006 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004007 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4008 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004009 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00004010
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004011 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00004012 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004013 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
chaviwd1c23182019-12-20 18:44:56 -08004014 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004015 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00004016
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004017 // Pilfer pointers from the monitor.
4018 // This should not do anything and the window should continue to receive events.
4019 EXPECT_NE(OK, mDispatcher->pilferPointers(monitor.getToken()));
Michael Wright3a240c42019-12-10 20:53:41 +00004020
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004021 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004022 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4023 ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004024 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004025
4026 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
4027 window->consumeMotionMove(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00004028}
4029
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004030TEST_F(InputDispatcherMonitorTest, NoWindowTransform) {
Evan Rosky84f07f02021-04-16 10:42:42 -07004031 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004032 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4033 "Fake Window", ADISPLAY_ID_DEFAULT);
Evan Rosky84f07f02021-04-16 10:42:42 -07004034 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4035 window->setWindowOffset(20, 40);
4036 window->setWindowTransform(0, 1, -1, 0);
4037
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004038 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Evan Rosky84f07f02021-04-16 10:42:42 -07004039
4040 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4041 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
4042 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4043 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4044 MotionEvent* event = monitor.consumeMotion();
4045 // Even though window has transform, gesture monitor must not.
4046 ASSERT_EQ(ui::Transform(), event->getTransform());
4047}
4048
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004049TEST_F(InputDispatcherMonitorTest, InjectionFailsWithNoWindow) {
Arthur Hungb3307ee2021-10-14 10:57:37 +00004050 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004051 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Arthur Hungb3307ee2021-10-14 10:57:37 +00004052
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004053 ASSERT_EQ(InputEventInjectionResult::FAILED,
Arthur Hungb3307ee2021-10-14 10:57:37 +00004054 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004055 << "Injection should fail if there is a monitor, but no touchable window";
4056 monitor.assertNoEvents();
Arthur Hungb3307ee2021-10-14 10:57:37 +00004057}
4058
chaviw81e2bb92019-12-18 15:03:51 -08004059TEST_F(InputDispatcherTest, TestMoveEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07004060 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004061 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4062 "Fake Window", ADISPLAY_ID_DEFAULT);
chaviw81e2bb92019-12-18 15:03:51 -08004063
Arthur Hung72d8dc32020-03-28 00:48:39 +00004064 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
chaviw81e2bb92019-12-18 15:03:51 -08004065
4066 NotifyMotionArgs motionArgs =
4067 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4068 ADISPLAY_ID_DEFAULT);
4069
4070 mDispatcher->notifyMotion(&motionArgs);
4071 // Window should receive motion down event.
4072 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4073
4074 motionArgs.action = AMOTION_EVENT_ACTION_MOVE;
Garfield Tanc51d1ba2020-01-28 13:24:04 -08004075 motionArgs.id += 1;
chaviw81e2bb92019-12-18 15:03:51 -08004076 motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
4077 motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
4078 motionArgs.pointerCoords[0].getX() - 10);
4079
4080 mDispatcher->notifyMotion(&motionArgs);
4081 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00004082 /*expectedFlags=*/0);
chaviw81e2bb92019-12-18 15:03:51 -08004083}
4084
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004085/**
4086 * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to
4087 * the device default right away. In the test scenario, we check both the default value,
4088 * and the action of enabling / disabling.
4089 */
4090TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) {
Chris Yea209fde2020-07-22 13:54:51 -07004091 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004092 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4093 "Test window", ADISPLAY_ID_DEFAULT);
Antonio Kantekea47acb2021-12-23 12:41:25 -08004094 const WindowInfo& windowInfo = *window->getInfo();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004095
4096 // Set focused application.
4097 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07004098 window->setFocusable(true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004099
4100 SCOPED_TRACE("Check default value of touch mode");
Arthur Hung72d8dc32020-03-28 00:48:39 +00004101 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004102 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00004103 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004104
4105 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07004106 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004107 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Harry Cutts33476232023-01-30 19:57:29 +00004108 window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004109
4110 SCOPED_TRACE("Disable touch mode");
Antonio Kantekea47acb2021-12-23 12:41:25 -08004111 mDispatcher->setInTouchMode(false, windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00004112 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00004113 window->consumeTouchModeEvent(false);
Vishnu Nair47074b82020-08-14 11:54:47 -07004114 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004115 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004116 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00004117 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/false);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004118
4119 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07004120 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004121 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Harry Cutts33476232023-01-30 19:57:29 +00004122 window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/false);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004123
4124 SCOPED_TRACE("Enable touch mode again");
Antonio Kantekea47acb2021-12-23 12:41:25 -08004125 mDispatcher->setInTouchMode(true, windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00004126 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00004127 window->consumeTouchModeEvent(true);
Vishnu Nair47074b82020-08-14 11:54:47 -07004128 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004129 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004130 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00004131 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004132
4133 window->assertNoEvents();
4134}
4135
Gang Wange9087892020-01-07 12:17:14 -05004136TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07004137 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004138 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4139 "Test window", ADISPLAY_ID_DEFAULT);
Gang Wange9087892020-01-07 12:17:14 -05004140
4141 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07004142 window->setFocusable(true);
Gang Wange9087892020-01-07 12:17:14 -05004143
Arthur Hung72d8dc32020-03-28 00:48:39 +00004144 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004145 setFocusedWindow(window);
4146
Harry Cutts33476232023-01-30 19:57:29 +00004147 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Gang Wange9087892020-01-07 12:17:14 -05004148
4149 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
4150 mDispatcher->notifyKey(&keyArgs);
4151
4152 InputEvent* event = window->consume();
4153 ASSERT_NE(event, nullptr);
4154
4155 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
4156 ASSERT_NE(verified, nullptr);
4157 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY);
4158
4159 ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos);
4160 ASSERT_EQ(keyArgs.deviceId, verified->deviceId);
4161 ASSERT_EQ(keyArgs.source, verified->source);
4162 ASSERT_EQ(keyArgs.displayId, verified->displayId);
4163
4164 const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified);
4165
4166 ASSERT_EQ(keyArgs.action, verifiedKey.action);
Gang Wange9087892020-01-07 12:17:14 -05004167 ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08004168 ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos);
Gang Wange9087892020-01-07 12:17:14 -05004169 ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode);
4170 ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode);
4171 ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState);
4172 ASSERT_EQ(0, verifiedKey.repeatCount);
4173}
4174
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08004175TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07004176 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004177 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4178 "Test window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08004179
4180 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
4181
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07004182 ui::Transform transform;
4183 transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
4184
4185 gui::DisplayInfo displayInfo;
4186 displayInfo.displayId = ADISPLAY_ID_DEFAULT;
4187 displayInfo.transform = transform;
4188
4189 mDispatcher->onWindowInfosChanged({*window->getInfo()}, {displayInfo});
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08004190
4191 NotifyMotionArgs motionArgs =
4192 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4193 ADISPLAY_ID_DEFAULT);
4194 mDispatcher->notifyMotion(&motionArgs);
4195
4196 InputEvent* event = window->consume();
4197 ASSERT_NE(event, nullptr);
4198
4199 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
4200 ASSERT_NE(verified, nullptr);
4201 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION);
4202
4203 EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos);
4204 EXPECT_EQ(motionArgs.deviceId, verified->deviceId);
4205 EXPECT_EQ(motionArgs.source, verified->source);
4206 EXPECT_EQ(motionArgs.displayId, verified->displayId);
4207
4208 const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified);
4209
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07004210 const vec2 rawXY =
4211 MotionEvent::calculateTransformedXY(motionArgs.source, transform,
4212 motionArgs.pointerCoords[0].getXYValue());
4213 EXPECT_EQ(rawXY.x, verifiedMotion.rawX);
4214 EXPECT_EQ(rawXY.y, verifiedMotion.rawY);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08004215 EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08004216 EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08004217 EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08004218 EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState);
4219 EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState);
4220}
4221
chaviw09c8d2d2020-08-24 15:48:26 -07004222/**
4223 * Ensure that separate calls to sign the same data are generating the same key.
4224 * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance
4225 * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky
4226 * tests.
4227 */
4228TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) {
4229 KeyEvent event = getTestKeyEvent();
4230 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
4231
4232 std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent);
4233 std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent);
4234 ASSERT_EQ(hmac1, hmac2);
4235}
4236
4237/**
4238 * Ensure that changes in VerifiedKeyEvent produce a different hmac.
4239 */
4240TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) {
4241 KeyEvent event = getTestKeyEvent();
4242 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
4243 std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent);
4244
4245 verifiedEvent.deviceId += 1;
4246 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
4247
4248 verifiedEvent.source += 1;
4249 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
4250
4251 verifiedEvent.eventTimeNanos += 1;
4252 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
4253
4254 verifiedEvent.displayId += 1;
4255 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
4256
4257 verifiedEvent.action += 1;
4258 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
4259
4260 verifiedEvent.downTimeNanos += 1;
4261 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
4262
4263 verifiedEvent.flags += 1;
4264 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
4265
4266 verifiedEvent.keyCode += 1;
4267 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
4268
4269 verifiedEvent.scanCode += 1;
4270 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
4271
4272 verifiedEvent.metaState += 1;
4273 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
4274
4275 verifiedEvent.repeatCount += 1;
4276 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
4277}
4278
Vishnu Nair958da932020-08-21 17:12:37 -07004279TEST_F(InputDispatcherTest, SetFocusedWindow) {
4280 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4281 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004282 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07004283 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004284 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07004285 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
4286
4287 // Top window is also focusable but is not granted focus.
4288 windowTop->setFocusable(true);
4289 windowSecond->setFocusable(true);
4290 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
4291 setFocusedWindow(windowSecond);
4292
4293 windowSecond->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004294 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4295 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004296
4297 // Focused window should receive event.
4298 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
4299 windowTop->assertNoEvents();
4300}
4301
4302TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) {
4303 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4304 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004305 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07004306 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
4307
4308 window->setFocusable(true);
4309 // Release channel for window is no longer valid.
4310 window->releaseChannel();
4311 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4312 setFocusedWindow(window);
4313
4314 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004315 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
4316 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07004317
4318 // window channel is invalid, so it should not receive any input event.
4319 window->assertNoEvents();
4320}
4321
4322TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) {
4323 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4324 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004325 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08004326 window->setFocusable(false);
Vishnu Nair958da932020-08-21 17:12:37 -07004327 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
4328
Vishnu Nair958da932020-08-21 17:12:37 -07004329 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4330 setFocusedWindow(window);
4331
4332 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004333 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
4334 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07004335
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08004336 // window is not focusable, so it should not receive any input event.
Vishnu Nair958da932020-08-21 17:12:37 -07004337 window->assertNoEvents();
4338}
4339
4340TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) {
4341 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4342 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004343 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07004344 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004345 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07004346 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
4347
4348 windowTop->setFocusable(true);
4349 windowSecond->setFocusable(true);
4350 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
4351 setFocusedWindow(windowTop);
4352 windowTop->consumeFocusEvent(true);
4353
4354 setFocusedWindow(windowSecond, windowTop);
4355 windowSecond->consumeFocusEvent(true);
4356 windowTop->consumeFocusEvent(false);
4357
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004358 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4359 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004360
4361 // Focused window should receive event.
4362 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
4363}
4364
4365TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestFocusTokenNotFocused) {
4366 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4367 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004368 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07004369 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004370 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07004371 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
4372
4373 windowTop->setFocusable(true);
4374 windowSecond->setFocusable(true);
4375 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
4376 setFocusedWindow(windowSecond, windowTop);
4377
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004378 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
4379 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07004380
4381 // Event should be dropped.
4382 windowTop->assertNoEvents();
4383 windowSecond->assertNoEvents();
4384}
4385
4386TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) {
4387 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4388 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004389 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07004390 sp<FakeWindowHandle> previousFocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004391 sp<FakeWindowHandle>::make(application, mDispatcher, "previousFocusedWindow",
4392 ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07004393 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
4394
4395 window->setFocusable(true);
4396 previousFocusedWindow->setFocusable(true);
4397 window->setVisible(false);
4398 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}});
4399 setFocusedWindow(previousFocusedWindow);
4400 previousFocusedWindow->consumeFocusEvent(true);
4401
4402 // Requesting focus on invisible window takes focus from currently focused window.
4403 setFocusedWindow(window);
4404 previousFocusedWindow->consumeFocusEvent(false);
4405
4406 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004407 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00004408 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
4409 InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07004410
4411 // Window does not get focus event or key down.
4412 window->assertNoEvents();
4413
4414 // Window becomes visible.
4415 window->setVisible(true);
4416 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4417
4418 // Window receives focus event.
4419 window->consumeFocusEvent(true);
4420 // Focused window receives key down.
4421 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4422}
4423
Vishnu Nair599f1412021-06-21 10:39:58 -07004424TEST_F(InputDispatcherTest, DisplayRemoved) {
4425 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4426 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004427 sp<FakeWindowHandle>::make(application, mDispatcher, "window", ADISPLAY_ID_DEFAULT);
Vishnu Nair599f1412021-06-21 10:39:58 -07004428 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
4429
4430 // window is granted focus.
4431 window->setFocusable(true);
4432 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4433 setFocusedWindow(window);
4434 window->consumeFocusEvent(true);
4435
4436 // When a display is removed window loses focus.
4437 mDispatcher->displayRemoved(ADISPLAY_ID_DEFAULT);
4438 window->consumeFocusEvent(false);
4439}
4440
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004441/**
4442 * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY,
4443 * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top
4444 * of the 'slipperyEnterWindow'.
4445 *
4446 * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such
4447 * a way so that the touched location is no longer covered by the top window.
4448 *
4449 * Next, inject a MOVE event. Because the top window already moved earlier, this event is now
4450 * positioned over the bottom (slipperyEnterWindow) only. And because the top window had
4451 * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive
4452 * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting
4453 * with ACTION_DOWN).
4454 * Thus, the touch has been transferred from the top window into the bottom window, because the top
4455 * window moved itself away from the touched location and had Flag::SLIPPERY.
4456 *
4457 * Even though the top window moved away from the touched location, it is still obscuring the bottom
4458 * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_
4459 * OBSCURED should be set for the MotionEvent that reaches the bottom window.
4460 *
4461 * In this test, we ensure that the event received by the bottom window has
4462 * FLAG_WINDOW_IS_PARTIALLY_OBSCURED.
4463 */
4464TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) {
Prabir Pradhan5735a322022-04-11 17:23:34 +00004465 constexpr int32_t SLIPPERY_PID = WINDOW_PID + 1;
4466 constexpr int32_t SLIPPERY_UID = WINDOW_UID + 1;
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004467
4468 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4469 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
4470
4471 sp<FakeWindowHandle> slipperyExitWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004472 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08004473 slipperyExitWindow->setSlippery(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004474 // Make sure this one overlaps the bottom window
4475 slipperyExitWindow->setFrame(Rect(25, 25, 75, 75));
4476 // Change the owner uid/pid of the window so that it is considered to be occluding the bottom
4477 // one. Windows with the same owner are not considered to be occluding each other.
4478 slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID);
4479
4480 sp<FakeWindowHandle> slipperyEnterWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004481 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004482 slipperyExitWindow->setFrame(Rect(0, 0, 100, 100));
4483
4484 mDispatcher->setInputWindows(
4485 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
4486
4487 // Use notifyMotion instead of injecting to avoid dealing with injection permissions
4488 NotifyMotionArgs args = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4489 ADISPLAY_ID_DEFAULT, {{50, 50}});
4490 mDispatcher->notifyMotion(&args);
4491 slipperyExitWindow->consumeMotionDown();
4492 slipperyExitWindow->setFrame(Rect(70, 70, 100, 100));
4493 mDispatcher->setInputWindows(
4494 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
4495
4496 args = generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4497 ADISPLAY_ID_DEFAULT, {{51, 51}});
4498 mDispatcher->notifyMotion(&args);
4499
4500 slipperyExitWindow->consumeMotionCancel();
4501
4502 slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT,
4503 AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
4504}
4505
Garfield Tan1c7bc862020-01-28 13:24:04 -08004506class InputDispatcherKeyRepeatTest : public InputDispatcherTest {
4507protected:
4508 static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms
4509 static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms
4510
Chris Yea209fde2020-07-22 13:54:51 -07004511 std::shared_ptr<FakeApplicationHandle> mApp;
Garfield Tan1c7bc862020-01-28 13:24:04 -08004512 sp<FakeWindowHandle> mWindow;
4513
4514 virtual void SetUp() override {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004515 mFakePolicy = sp<FakeInputDispatcherPolicy>::make();
Garfield Tan1c7bc862020-01-28 13:24:04 -08004516 mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004517 mDispatcher = std::make_unique<InputDispatcher>(mFakePolicy);
Garfield Tan1c7bc862020-01-28 13:24:04 -08004518 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
4519 ASSERT_EQ(OK, mDispatcher->start());
4520
4521 setUpWindow();
4522 }
4523
4524 void setUpWindow() {
Chris Yea209fde2020-07-22 13:54:51 -07004525 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004526 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Garfield Tan1c7bc862020-01-28 13:24:04 -08004527
Vishnu Nair47074b82020-08-14 11:54:47 -07004528 mWindow->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004529 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004530 setFocusedWindow(mWindow);
Garfield Tan1c7bc862020-01-28 13:24:04 -08004531 mWindow->consumeFocusEvent(true);
4532 }
4533
Chris Ye2ad95392020-09-01 13:44:44 -07004534 void sendAndConsumeKeyDown(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08004535 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07004536 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08004537 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event
4538 mDispatcher->notifyKey(&keyArgs);
4539
4540 // Window should receive key down event.
4541 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4542 }
4543
4544 void expectKeyRepeatOnce(int32_t repeatCount) {
4545 SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount));
4546 InputEvent* repeatEvent = mWindow->consume();
4547 ASSERT_NE(nullptr, repeatEvent);
4548
4549 uint32_t eventType = repeatEvent->getType();
4550 ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, eventType);
4551
4552 KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent);
4553 uint32_t eventAction = repeatKeyEvent->getAction();
4554 EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction);
4555 EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount());
4556 }
4557
Chris Ye2ad95392020-09-01 13:44:44 -07004558 void sendAndConsumeKeyUp(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08004559 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07004560 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08004561 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event
4562 mDispatcher->notifyKey(&keyArgs);
4563
4564 // Window should receive key down event.
4565 mWindow->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00004566 /*expectedFlags=*/0);
Garfield Tan1c7bc862020-01-28 13:24:04 -08004567 }
4568};
4569
4570TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) {
Harry Cutts33476232023-01-30 19:57:29 +00004571 sendAndConsumeKeyDown(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07004572 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
4573 expectKeyRepeatOnce(repeatCount);
4574 }
4575}
4576
4577TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) {
Harry Cutts33476232023-01-30 19:57:29 +00004578 sendAndConsumeKeyDown(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07004579 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
4580 expectKeyRepeatOnce(repeatCount);
4581 }
Harry Cutts33476232023-01-30 19:57:29 +00004582 sendAndConsumeKeyDown(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07004583 /* repeatCount will start from 1 for deviceId 2 */
Garfield Tan1c7bc862020-01-28 13:24:04 -08004584 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
4585 expectKeyRepeatOnce(repeatCount);
4586 }
4587}
4588
4589TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) {
Harry Cutts33476232023-01-30 19:57:29 +00004590 sendAndConsumeKeyDown(/*deviceId=*/1);
4591 expectKeyRepeatOnce(/*repeatCount=*/1);
4592 sendAndConsumeKeyUp(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07004593 mWindow->assertNoEvents();
4594}
4595
4596TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) {
Harry Cutts33476232023-01-30 19:57:29 +00004597 sendAndConsumeKeyDown(/*deviceId=*/1);
4598 expectKeyRepeatOnce(/*repeatCount=*/1);
4599 sendAndConsumeKeyDown(/*deviceId=*/2);
4600 expectKeyRepeatOnce(/*repeatCount=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07004601 // Stale key up from device 1.
Harry Cutts33476232023-01-30 19:57:29 +00004602 sendAndConsumeKeyUp(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07004603 // Device 2 is still down, keep repeating
Harry Cutts33476232023-01-30 19:57:29 +00004604 expectKeyRepeatOnce(/*repeatCount=*/2);
4605 expectKeyRepeatOnce(/*repeatCount=*/3);
Chris Ye2ad95392020-09-01 13:44:44 -07004606 // Device 2 key up
Harry Cutts33476232023-01-30 19:57:29 +00004607 sendAndConsumeKeyUp(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07004608 mWindow->assertNoEvents();
4609}
4610
4611TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) {
Harry Cutts33476232023-01-30 19:57:29 +00004612 sendAndConsumeKeyDown(/*deviceId=*/1);
4613 expectKeyRepeatOnce(/*repeatCount=*/1);
4614 sendAndConsumeKeyDown(/*deviceId=*/2);
4615 expectKeyRepeatOnce(/*repeatCount=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07004616 // Device 2 which holds the key repeating goes up, expect the repeating to stop.
Harry Cutts33476232023-01-30 19:57:29 +00004617 sendAndConsumeKeyUp(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07004618 // Device 1 still holds key down, but the repeating was already stopped
Garfield Tan1c7bc862020-01-28 13:24:04 -08004619 mWindow->assertNoEvents();
4620}
4621
liushenxiang42232912021-05-21 20:24:09 +08004622TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) {
4623 sendAndConsumeKeyDown(DEVICE_ID);
Harry Cutts33476232023-01-30 19:57:29 +00004624 expectKeyRepeatOnce(/*repeatCount=*/1);
4625 NotifyDeviceResetArgs args(/*id=*/10, /*eventTime=*/20, DEVICE_ID);
liushenxiang42232912021-05-21 20:24:09 +08004626 mDispatcher->notifyDeviceReset(&args);
4627 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT,
4628 AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS);
4629 mWindow->assertNoEvents();
4630}
4631
Garfield Tan1c7bc862020-01-28 13:24:04 -08004632TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) {
Harry Cutts33476232023-01-30 19:57:29 +00004633 sendAndConsumeKeyDown(/*deviceId=*/1);
Garfield Tan1c7bc862020-01-28 13:24:04 -08004634 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
4635 InputEvent* repeatEvent = mWindow->consume();
4636 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
4637 EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER,
4638 IdGenerator::getSource(repeatEvent->getId()));
4639 }
4640}
4641
4642TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) {
Harry Cutts33476232023-01-30 19:57:29 +00004643 sendAndConsumeKeyDown(/*deviceId=*/1);
Garfield Tan1c7bc862020-01-28 13:24:04 -08004644
4645 std::unordered_set<int32_t> idSet;
4646 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
4647 InputEvent* repeatEvent = mWindow->consume();
4648 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
4649 int32_t id = repeatEvent->getId();
4650 EXPECT_EQ(idSet.end(), idSet.find(id));
4651 idSet.insert(id);
4652 }
4653}
4654
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004655/* Test InputDispatcher for MultiDisplay */
4656class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest {
4657public:
Prabir Pradhan3608aad2019-10-02 17:08:26 -07004658 virtual void SetUp() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004659 InputDispatcherTest::SetUp();
Arthur Hungb92218b2018-08-14 12:00:21 +08004660
Chris Yea209fde2020-07-22 13:54:51 -07004661 application1 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004662 windowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004663 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08004664
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004665 // Set focus window for primary display, but focused display would be second one.
4666 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1);
Vishnu Nair47074b82020-08-14 11:54:47 -07004667 windowInPrimary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004668 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004669 setFocusedWindow(windowInPrimary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004670 windowInPrimary->consumeFocusEvent(true);
Arthur Hungb92218b2018-08-14 12:00:21 +08004671
Chris Yea209fde2020-07-22 13:54:51 -07004672 application2 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004673 windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004674 sp<FakeWindowHandle>::make(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004675 // Set focus to second display window.
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004676 // Set focus display to second one.
4677 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
4678 // Set focus window for second display.
4679 mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2);
Vishnu Nair47074b82020-08-14 11:54:47 -07004680 windowInSecondary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004681 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004682 setFocusedWindow(windowInSecondary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004683 windowInSecondary->consumeFocusEvent(true);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004684 }
4685
Prabir Pradhan3608aad2019-10-02 17:08:26 -07004686 virtual void TearDown() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004687 InputDispatcherTest::TearDown();
4688
Chris Yea209fde2020-07-22 13:54:51 -07004689 application1.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004690 windowInPrimary.clear();
Chris Yea209fde2020-07-22 13:54:51 -07004691 application2.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004692 windowInSecondary.clear();
4693 }
4694
4695protected:
Chris Yea209fde2020-07-22 13:54:51 -07004696 std::shared_ptr<FakeApplicationHandle> application1;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004697 sp<FakeWindowHandle> windowInPrimary;
Chris Yea209fde2020-07-22 13:54:51 -07004698 std::shared_ptr<FakeApplicationHandle> application2;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004699 sp<FakeWindowHandle> windowInSecondary;
4700};
4701
4702TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) {
4703 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004704 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4705 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
4706 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004707 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08004708 windowInSecondary->assertNoEvents();
4709
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004710 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004711 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4712 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
4713 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08004714 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004715 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +08004716}
4717
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004718TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) {
Tiger Huang721e26f2018-07-24 22:26:19 +08004719 // Test inject a key down with display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08004720 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4721 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004722 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004723 windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Tiger Huang721e26f2018-07-24 22:26:19 +08004724 windowInSecondary->assertNoEvents();
4725
4726 // Test inject a key down without display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08004727 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004728 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08004729 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004730 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hungb92218b2018-08-14 12:00:21 +08004731
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08004732 // Remove all windows in secondary display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00004733 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}});
Arthur Hungb92218b2018-08-14 12:00:21 +08004734
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004735 // Old focus should receive a cancel event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004736 windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE,
4737 AKEY_EVENT_FLAG_CANCELED);
Arthur Hungb92218b2018-08-14 12:00:21 +08004738
4739 // Test inject a key down, should timeout because of no target window.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08004740 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004741 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Arthur Hungb92218b2018-08-14 12:00:21 +08004742 windowInPrimary->assertNoEvents();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004743 windowInSecondary->consumeFocusEvent(false);
Arthur Hungb92218b2018-08-14 12:00:21 +08004744 windowInSecondary->assertNoEvents();
4745}
4746
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004747// Test per-display input monitors for motion event.
4748TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) {
chaviwd1c23182019-12-20 18:44:56 -08004749 FakeMonitorReceiver monitorInPrimary =
4750 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
4751 FakeMonitorReceiver monitorInSecondary =
4752 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004753
4754 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004755 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4756 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
4757 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004758 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08004759 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004760 windowInSecondary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08004761 monitorInSecondary.assertNoEvents();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004762
4763 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004764 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4765 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
4766 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004767 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08004768 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004769 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
chaviwd1c23182019-12-20 18:44:56 -08004770 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004771
4772 // Test inject a non-pointer motion event.
4773 // If specific a display, it will dispatch to the focused window of particular display,
4774 // or it will dispatch to the focused window of focused display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004775 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4776 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE))
4777 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004778 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08004779 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004780 windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08004781 monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004782}
4783
4784// Test per-display input monitors for key event.
4785TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) {
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004786 // Input monitor per display.
chaviwd1c23182019-12-20 18:44:56 -08004787 FakeMonitorReceiver monitorInPrimary =
4788 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
4789 FakeMonitorReceiver monitorInSecondary =
4790 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004791
4792 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004793 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4794 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004795 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08004796 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004797 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08004798 monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004799}
4800
Vishnu Nair958da932020-08-21 17:12:37 -07004801TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) {
4802 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004803 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07004804 secondWindowInPrimary->setFocusable(true);
4805 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}});
4806 setFocusedWindow(secondWindowInPrimary);
4807 windowInPrimary->consumeFocusEvent(false);
4808 secondWindowInPrimary->consumeFocusEvent(true);
4809
4810 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004811 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
4812 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004813 windowInPrimary->assertNoEvents();
4814 windowInSecondary->assertNoEvents();
4815 secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4816}
4817
Arthur Hungdfd528e2021-12-08 13:23:04 +00004818TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CancelTouch_MultiDisplay) {
4819 FakeMonitorReceiver monitorInPrimary =
4820 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
4821 FakeMonitorReceiver monitorInSecondary =
4822 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
4823
4824 // Test touch down on primary display.
4825 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4826 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
4827 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4828 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4829 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
4830
4831 // Test touch down on second display.
4832 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4833 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
4834 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4835 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
4836 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
4837
4838 // Trigger cancel touch.
4839 mDispatcher->cancelCurrentTouch();
4840 windowInPrimary->consumeMotionCancel(ADISPLAY_ID_DEFAULT);
4841 monitorInPrimary.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
4842 windowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID);
4843 monitorInSecondary.consumeMotionCancel(SECOND_DISPLAY_ID);
4844
4845 // Test inject a move motion event, no window/monitor should receive the event.
4846 ASSERT_EQ(InputEventInjectionResult::FAILED,
4847 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4848 ADISPLAY_ID_DEFAULT, {110, 200}))
4849 << "Inject motion event should return InputEventInjectionResult::FAILED";
4850 windowInPrimary->assertNoEvents();
4851 monitorInPrimary.assertNoEvents();
4852
4853 ASSERT_EQ(InputEventInjectionResult::FAILED,
4854 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4855 SECOND_DISPLAY_ID, {110, 200}))
4856 << "Inject motion event should return InputEventInjectionResult::FAILED";
4857 windowInSecondary->assertNoEvents();
4858 monitorInSecondary.assertNoEvents();
4859}
4860
Jackal Guof9696682018-10-05 12:23:23 +08004861class InputFilterTest : public InputDispatcherTest {
4862protected:
Prabir Pradhan81420cc2021-09-06 10:28:50 -07004863 void testNotifyMotion(int32_t displayId, bool expectToBeFiltered,
4864 const ui::Transform& transform = ui::Transform()) {
Jackal Guof9696682018-10-05 12:23:23 +08004865 NotifyMotionArgs motionArgs;
4866
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004867 motionArgs =
4868 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Jackal Guof9696682018-10-05 12:23:23 +08004869 mDispatcher->notifyMotion(&motionArgs);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004870 motionArgs =
4871 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Jackal Guof9696682018-10-05 12:23:23 +08004872 mDispatcher->notifyMotion(&motionArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004873 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08004874 if (expectToBeFiltered) {
Prabir Pradhan81420cc2021-09-06 10:28:50 -07004875 const auto xy = transform.transform(motionArgs.pointerCoords->getXYValue());
4876 mFakePolicy->assertFilterInputEventWasCalled(motionArgs, xy);
Jackal Guof9696682018-10-05 12:23:23 +08004877 } else {
4878 mFakePolicy->assertFilterInputEventWasNotCalled();
4879 }
4880 }
4881
4882 void testNotifyKey(bool expectToBeFiltered) {
4883 NotifyKeyArgs keyArgs;
4884
4885 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
4886 mDispatcher->notifyKey(&keyArgs);
4887 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP);
4888 mDispatcher->notifyKey(&keyArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004889 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08004890
4891 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08004892 mFakePolicy->assertFilterInputEventWasCalled(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08004893 } else {
4894 mFakePolicy->assertFilterInputEventWasNotCalled();
4895 }
4896 }
4897};
4898
4899// Test InputFilter for MotionEvent
4900TEST_F(InputFilterTest, MotionEvent_InputFilter) {
4901 // Since the InputFilter is disabled by default, check if touch events aren't filtered.
4902 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
4903 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
4904
4905 // Enable InputFilter
4906 mDispatcher->setInputFilterEnabled(true);
4907 // Test touch on both primary and second display, and check if both events are filtered.
4908 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true);
4909 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true);
4910
4911 // Disable InputFilter
4912 mDispatcher->setInputFilterEnabled(false);
4913 // Test touch on both primary and second display, and check if both events aren't filtered.
4914 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
4915 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
4916}
4917
4918// Test InputFilter for KeyEvent
4919TEST_F(InputFilterTest, KeyEvent_InputFilter) {
4920 // Since the InputFilter is disabled by default, check if key event aren't filtered.
4921 testNotifyKey(/*expectToBeFiltered*/ false);
4922
4923 // Enable InputFilter
4924 mDispatcher->setInputFilterEnabled(true);
4925 // Send a key event, and check if it is filtered.
4926 testNotifyKey(/*expectToBeFiltered*/ true);
4927
4928 // Disable InputFilter
4929 mDispatcher->setInputFilterEnabled(false);
4930 // Send a key event, and check if it isn't filtered.
4931 testNotifyKey(/*expectToBeFiltered*/ false);
4932}
4933
Prabir Pradhan81420cc2021-09-06 10:28:50 -07004934// Ensure that MotionEvents sent to the InputFilter through InputListener are converted to the
4935// logical display coordinate space.
4936TEST_F(InputFilterTest, MotionEvent_UsesLogicalDisplayCoordinates_notifyMotion) {
4937 ui::Transform firstDisplayTransform;
4938 firstDisplayTransform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
4939 ui::Transform secondDisplayTransform;
4940 secondDisplayTransform.set({-6.6, -5.5, -4.4, -3.3, -2.2, -1.1, 0, 0, 1});
4941
4942 std::vector<gui::DisplayInfo> displayInfos(2);
4943 displayInfos[0].displayId = ADISPLAY_ID_DEFAULT;
4944 displayInfos[0].transform = firstDisplayTransform;
4945 displayInfos[1].displayId = SECOND_DISPLAY_ID;
4946 displayInfos[1].transform = secondDisplayTransform;
4947
4948 mDispatcher->onWindowInfosChanged({}, displayInfos);
4949
4950 // Enable InputFilter
4951 mDispatcher->setInputFilterEnabled(true);
4952
4953 // Ensure the correct transforms are used for the displays.
4954 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true, firstDisplayTransform);
4955 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true, secondDisplayTransform);
4956}
4957
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004958class InputFilterInjectionPolicyTest : public InputDispatcherTest {
4959protected:
4960 virtual void SetUp() override {
4961 InputDispatcherTest::SetUp();
4962
4963 /**
4964 * We don't need to enable input filter to test the injected event policy, but we enabled it
4965 * here to make the tests more realistic, since this policy only matters when inputfilter is
4966 * on.
4967 */
4968 mDispatcher->setInputFilterEnabled(true);
4969
4970 std::shared_ptr<InputApplicationHandle> application =
4971 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004972 mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Test Window",
4973 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004974
4975 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
4976 mWindow->setFocusable(true);
4977 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
4978 setFocusedWindow(mWindow);
4979 mWindow->consumeFocusEvent(true);
4980 }
4981
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00004982 void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
4983 int32_t flags) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004984 KeyEvent event;
4985
4986 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
4987 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD,
4988 ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A,
Harry Cutts33476232023-01-30 19:57:29 +00004989 KEY_A, AMETA_NONE, /*repeatCount=*/0, eventTime, eventTime);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004990 const int32_t additionalPolicyFlags =
4991 POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT;
4992 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00004993 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004994 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
4995 policyFlags | additionalPolicyFlags));
4996
4997 InputEvent* received = mWindow->consume();
4998 ASSERT_NE(nullptr, received);
4999 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005000 ASSERT_EQ(received->getType(), AINPUT_EVENT_TYPE_KEY);
5001 KeyEvent& keyEvent = static_cast<KeyEvent&>(*received);
5002 ASSERT_EQ(flags, keyEvent.getFlags());
5003 }
5004
5005 void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
5006 int32_t flags) {
5007 MotionEvent event;
5008 PointerProperties pointerProperties[1];
5009 PointerCoords pointerCoords[1];
5010 pointerProperties[0].clear();
5011 pointerProperties[0].id = 0;
5012 pointerCoords[0].clear();
5013 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300);
5014 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400);
5015
5016 ui::Transform identityTransform;
5017 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
5018 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN,
5019 DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0,
5020 AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE,
5021 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -07005022 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, eventTime,
Evan Rosky09576692021-07-01 12:22:09 -07005023 eventTime,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005024 /*pointerCount*/ 1, pointerProperties, pointerCoords);
5025
5026 const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER;
5027 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00005028 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005029 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
5030 policyFlags | additionalPolicyFlags));
5031
5032 InputEvent* received = mWindow->consume();
5033 ASSERT_NE(nullptr, received);
5034 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
5035 ASSERT_EQ(received->getType(), AINPUT_EVENT_TYPE_MOTION);
5036 MotionEvent& motionEvent = static_cast<MotionEvent&>(*received);
5037 ASSERT_EQ(flags, motionEvent.getFlags());
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005038 }
5039
5040private:
5041 sp<FakeWindowHandle> mWindow;
5042};
5043
5044TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) {
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005045 // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input
5046 // filter. Without it, the event will no different from a regularly injected event, and the
5047 // injected device id will be overwritten.
Harry Cutts33476232023-01-30 19:57:29 +00005048 testInjectedKey(POLICY_FLAG_FILTERED, /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
5049 /*flags=*/0);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005050}
5051
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005052TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005053 testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Harry Cutts33476232023-01-30 19:57:29 +00005054 /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005055 AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
5056}
5057
5058TEST_F(InputFilterInjectionPolicyTest,
5059 MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
5060 testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Harry Cutts33476232023-01-30 19:57:29 +00005061 /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005062 AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005063}
5064
5065TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) {
Harry Cutts33476232023-01-30 19:57:29 +00005066 testInjectedKey(/*policyFlags=*/0, /*injectedDeviceId=*/3,
5067 /*resolvedDeviceId=*/VIRTUAL_KEYBOARD_ID, /*flags=*/0);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005068}
5069
chaviwfd6d3512019-03-25 13:23:49 -07005070class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest {
Prabir Pradhan3608aad2019-10-02 17:08:26 -07005071 virtual void SetUp() override {
chaviwfd6d3512019-03-25 13:23:49 -07005072 InputDispatcherTest::SetUp();
5073
Chris Yea209fde2020-07-22 13:54:51 -07005074 std::shared_ptr<FakeApplicationHandle> application =
5075 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005076 mUnfocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005077 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07005078 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
chaviwfd6d3512019-03-25 13:23:49 -07005079
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005080 mFocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005081 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005082 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
chaviwfd6d3512019-03-25 13:23:49 -07005083
5084 // Set focused application.
5085 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07005086 mFocusedWindow->setFocusable(true);
chaviwfd6d3512019-03-25 13:23:49 -07005087
5088 // Expect one focus window exist in display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00005089 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005090 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005091 mFocusedWindow->consumeFocusEvent(true);
chaviwfd6d3512019-03-25 13:23:49 -07005092 }
5093
Prabir Pradhan3608aad2019-10-02 17:08:26 -07005094 virtual void TearDown() override {
chaviwfd6d3512019-03-25 13:23:49 -07005095 InputDispatcherTest::TearDown();
5096
5097 mUnfocusedWindow.clear();
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005098 mFocusedWindow.clear();
chaviwfd6d3512019-03-25 13:23:49 -07005099 }
5100
5101protected:
5102 sp<FakeWindowHandle> mUnfocusedWindow;
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005103 sp<FakeWindowHandle> mFocusedWindow;
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07005104 static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60};
chaviwfd6d3512019-03-25 13:23:49 -07005105};
5106
5107// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
5108// DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received
5109// the onPointerDownOutsideFocus callback.
5110TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005111 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07005112 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5113 {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005114 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07005115 mUnfocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07005116
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08005117 ASSERT_TRUE(mDispatcher->waitForIdle());
chaviwfd6d3512019-03-25 13:23:49 -07005118 mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken());
5119}
5120
5121// Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action
5122// DOWN on the window that doesn't have focus. Ensure no window received the
5123// onPointerDownOutsideFocus callback.
5124TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005125 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07005126 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005127 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07005128 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07005129
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08005130 ASSERT_TRUE(mDispatcher->waitForIdle());
5131 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07005132}
5133
5134// Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't
5135// have focus. Ensure no window received the onPointerDownOutsideFocus callback.
5136TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005137 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5138 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005139 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07005140 mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07005141
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08005142 ASSERT_TRUE(mDispatcher->waitForIdle());
5143 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07005144}
5145
5146// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
5147// DOWN on the window that already has focus. Ensure no window received the
5148// onPointerDownOutsideFocus callback.
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005149TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005150 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005151 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07005152 FOCUSED_WINDOW_TOUCH_POINT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005153 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07005154 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07005155
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08005156 ASSERT_TRUE(mDispatcher->waitForIdle());
5157 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07005158}
5159
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08005160// Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag
5161// NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback.
5162TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) {
5163 const MotionEvent event =
5164 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
5165 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Harry Cutts33476232023-01-30 19:57:29 +00005166 .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(20).y(20))
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08005167 .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE)
5168 .build();
5169 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(mDispatcher, event))
5170 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5171 mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
5172
5173 ASSERT_TRUE(mDispatcher->waitForIdle());
5174 mFakePolicy->assertOnPointerDownWasNotCalled();
5175 // Ensure that the unfocused window did not receive any FOCUS events.
5176 mUnfocusedWindow->assertNoEvents();
5177}
5178
chaviwaf87b3e2019-10-01 16:59:28 -07005179// These tests ensures we can send touch events to a single client when there are multiple input
5180// windows that point to the same client token.
5181class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest {
5182 virtual void SetUp() override {
5183 InputDispatcherTest::SetUp();
5184
Chris Yea209fde2020-07-22 13:54:51 -07005185 std::shared_ptr<FakeApplicationHandle> application =
5186 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005187 mWindow1 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 1",
5188 ADISPLAY_ID_DEFAULT);
chaviwaf87b3e2019-10-01 16:59:28 -07005189 mWindow1->setFrame(Rect(0, 0, 100, 100));
5190
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005191 mWindow2 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 2",
5192 ADISPLAY_ID_DEFAULT, mWindow1->getToken());
chaviwaf87b3e2019-10-01 16:59:28 -07005193 mWindow2->setFrame(Rect(100, 100, 200, 200));
5194
Arthur Hung72d8dc32020-03-28 00:48:39 +00005195 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
chaviwaf87b3e2019-10-01 16:59:28 -07005196 }
5197
5198protected:
5199 sp<FakeWindowHandle> mWindow1;
5200 sp<FakeWindowHandle> mWindow2;
5201
5202 // Helper function to convert the point from screen coordinates into the window's space
chaviw3277faf2021-05-19 16:45:23 -05005203 static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) {
chaviw1ff3d1e2020-07-01 15:53:47 -07005204 vec2 vals = windowInfo->transform.transform(point.x, point.y);
5205 return {vals.x, vals.y};
chaviwaf87b3e2019-10-01 16:59:28 -07005206 }
5207
5208 void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction,
5209 const std::vector<PointF>& points) {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005210 const std::string name = window->getName();
chaviwaf87b3e2019-10-01 16:59:28 -07005211 InputEvent* event = window->consume();
5212
5213 ASSERT_NE(nullptr, event) << name.c_str()
5214 << ": consumer should have returned non-NULL event.";
5215
5216 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType())
5217 << name.c_str() << "expected " << inputEventTypeToString(AINPUT_EVENT_TYPE_MOTION)
5218 << " event, got " << inputEventTypeToString(event->getType()) << " event";
5219
5220 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005221 assertMotionAction(expectedAction, motionEvent.getAction());
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08005222 ASSERT_EQ(points.size(), motionEvent.getPointerCount());
chaviwaf87b3e2019-10-01 16:59:28 -07005223
5224 for (size_t i = 0; i < points.size(); i++) {
5225 float expectedX = points[i].x;
5226 float expectedY = points[i].y;
5227
5228 EXPECT_EQ(expectedX, motionEvent.getX(i))
5229 << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str()
5230 << ", got " << motionEvent.getX(i);
5231 EXPECT_EQ(expectedY, motionEvent.getY(i))
5232 << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str()
5233 << ", got " << motionEvent.getY(i);
5234 }
5235 }
chaviw9eaa22c2020-07-01 16:21:27 -07005236
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08005237 void touchAndAssertPositions(int32_t action, const std::vector<PointF>& touchedPoints,
chaviw9eaa22c2020-07-01 16:21:27 -07005238 std::vector<PointF> expectedPoints) {
5239 NotifyMotionArgs motionArgs = generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN,
5240 ADISPLAY_ID_DEFAULT, touchedPoints);
5241 mDispatcher->notifyMotion(&motionArgs);
5242
5243 // Always consume from window1 since it's the window that has the InputReceiver
5244 consumeMotionEvent(mWindow1, action, expectedPoints);
5245 }
chaviwaf87b3e2019-10-01 16:59:28 -07005246};
5247
5248TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) {
5249 // Touch Window 1
5250 PointF touchedPoint = {10, 10};
5251 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07005252 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07005253
5254 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07005255 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07005256
5257 // Touch Window 2
5258 touchedPoint = {150, 150};
5259 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07005260 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07005261}
5262
chaviw9eaa22c2020-07-01 16:21:27 -07005263TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) {
5264 // Set scale value for window2
chaviwaf87b3e2019-10-01 16:59:28 -07005265 mWindow2->setWindowScale(0.5f, 0.5f);
5266
5267 // Touch Window 1
5268 PointF touchedPoint = {10, 10};
5269 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07005270 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07005271 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07005272 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07005273
5274 // Touch Window 2
5275 touchedPoint = {150, 150};
5276 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07005277 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
5278 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07005279
chaviw9eaa22c2020-07-01 16:21:27 -07005280 // Update the transform so rotation is set
5281 mWindow2->setWindowTransform(0, -1, 1, 0);
5282 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
5283 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07005284}
5285
chaviw9eaa22c2020-07-01 16:21:27 -07005286TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00005287 mWindow2->setWindowScale(0.5f, 0.5f);
5288
5289 // Touch Window 1
5290 std::vector<PointF> touchedPoints = {PointF{10, 10}};
5291 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07005292 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00005293
5294 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07005295 touchedPoints.push_back(PointF{150, 150});
5296 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08005297 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00005298
chaviw9eaa22c2020-07-01 16:21:27 -07005299 // Release Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08005300 touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07005301 expectedPoints.pop_back();
Chavi Weingarten65f98b82020-01-16 18:56:50 +00005302
chaviw9eaa22c2020-07-01 16:21:27 -07005303 // Update the transform so rotation is set for Window 2
5304 mWindow2->setWindowTransform(0, -1, 1, 0);
5305 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08005306 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00005307}
5308
chaviw9eaa22c2020-07-01 16:21:27 -07005309TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00005310 mWindow2->setWindowScale(0.5f, 0.5f);
5311
5312 // Touch Window 1
5313 std::vector<PointF> touchedPoints = {PointF{10, 10}};
5314 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07005315 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00005316
5317 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07005318 touchedPoints.push_back(PointF{150, 150});
5319 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00005320
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08005321 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00005322
5323 // Move both windows
5324 touchedPoints = {{20, 20}, {175, 175}};
5325 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
5326 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
5327
chaviw9eaa22c2020-07-01 16:21:27 -07005328 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00005329
chaviw9eaa22c2020-07-01 16:21:27 -07005330 // Release Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08005331 touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07005332 expectedPoints.pop_back();
5333
5334 // Touch Window 2
5335 mWindow2->setWindowTransform(0, -1, 1, 0);
5336 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08005337 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07005338
5339 // Move both windows
5340 touchedPoints = {{20, 20}, {175, 175}};
5341 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
5342 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
5343
5344 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00005345}
5346
5347TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) {
5348 mWindow1->setWindowScale(0.5f, 0.5f);
5349
5350 // Touch Window 1
5351 std::vector<PointF> touchedPoints = {PointF{10, 10}};
5352 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07005353 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00005354
5355 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07005356 touchedPoints.push_back(PointF{150, 150});
5357 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00005358
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08005359 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00005360
5361 // Move both windows
5362 touchedPoints = {{20, 20}, {175, 175}};
5363 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
5364 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
5365
chaviw9eaa22c2020-07-01 16:21:27 -07005366 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00005367}
5368
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005369class InputDispatcherSingleWindowAnr : public InputDispatcherTest {
5370 virtual void SetUp() override {
5371 InputDispatcherTest::SetUp();
5372
Chris Yea209fde2020-07-22 13:54:51 -07005373 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005374 mApplication->setDispatchingTimeout(20ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005375 mWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "TestWindow",
5376 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005377 mWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05005378 mWindow->setDispatchingTimeout(30ms);
Vishnu Nair47074b82020-08-14 11:54:47 -07005379 mWindow->setFocusable(true);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005380
5381 // Set focused application.
5382 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
5383
5384 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005385 setFocusedWindow(mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005386 mWindow->consumeFocusEvent(true);
5387 }
5388
5389 virtual void TearDown() override {
5390 InputDispatcherTest::TearDown();
5391 mWindow.clear();
5392 }
5393
5394protected:
Chris Yea209fde2020-07-22 13:54:51 -07005395 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005396 sp<FakeWindowHandle> mWindow;
5397 static constexpr PointF WINDOW_LOCATION = {20, 20};
5398
5399 void tapOnWindow() {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005400 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005401 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5402 WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005403 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005404 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5405 WINDOW_LOCATION));
5406 }
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005407
5408 sp<FakeWindowHandle> addSpyWindow() {
5409 sp<FakeWindowHandle> spy =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005410 sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005411 spy->setTrustedOverlay(true);
5412 spy->setFocusable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08005413 spy->setSpy(true);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005414 spy->setDispatchingTimeout(30ms);
5415 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, mWindow}}});
5416 return spy;
5417 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005418};
5419
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005420// Send a tap and respond, which should not cause an ANR.
5421TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) {
5422 tapOnWindow();
5423 mWindow->consumeMotionDown();
5424 mWindow->consumeMotionUp();
5425 ASSERT_TRUE(mDispatcher->waitForIdle());
5426 mFakePolicy->assertNotifyAnrWasNotCalled();
5427}
5428
5429// Send a regular key and respond, which should not cause an ANR.
5430TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005431 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005432 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
5433 ASSERT_TRUE(mDispatcher->waitForIdle());
5434 mFakePolicy->assertNotifyAnrWasNotCalled();
5435}
5436
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05005437TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) {
5438 mWindow->setFocusable(false);
5439 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
5440 mWindow->consumeFocusEvent(false);
5441
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005442 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00005443 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
5444 InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms,
5445 /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005446 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05005447 // Key will not go to window because we have no focused window.
5448 // The 'no focused window' ANR timer should start instead.
5449
5450 // Now, the focused application goes away.
5451 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr);
5452 // The key should get dropped and there should be no ANR.
5453
5454 ASSERT_TRUE(mDispatcher->waitForIdle());
5455 mFakePolicy->assertNotifyAnrWasNotCalled();
5456}
5457
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005458// Send an event to the app and have the app not respond right away.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005459// When ANR is raised, policy will tell the dispatcher to cancel the events for that window.
5460// So InputDispatcher will enqueue ACTION_CANCEL event as well.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005461TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005462 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005463 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5464 WINDOW_LOCATION));
5465
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005466 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN
5467 ASSERT_TRUE(sequenceNum);
5468 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005469 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005470
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005471 mWindow->finishEvent(*sequenceNum);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08005472 mWindow->consumeMotionEvent(
5473 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005474 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08005475 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005476}
5477
5478// Send a key to the app and have the app not respond right away.
5479TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) {
5480 // Inject a key, and don't respond - expect that ANR is called.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005481 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005482 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent();
5483 ASSERT_TRUE(sequenceNum);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005484 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005485 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005486 ASSERT_TRUE(mDispatcher->waitForIdle());
5487}
5488
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005489// We have a focused application, but no focused window
5490TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) {
Vishnu Nair47074b82020-08-14 11:54:47 -07005491 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005492 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
5493 mWindow->consumeFocusEvent(false);
5494
5495 // taps on the window work as normal
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005496 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005497 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5498 WINDOW_LOCATION));
5499 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
5500 mDispatcher->waitForIdle();
5501 mFakePolicy->assertNotifyAnrWasNotCalled();
5502
5503 // Once a focused event arrives, we get an ANR for this application
5504 // We specify the injection timeout to be smaller than the application timeout, to ensure that
5505 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005506 const InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00005507 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
5508 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005509 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005510 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Vishnu Naire4df8752022-09-08 09:17:55 -07005511 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005512 ASSERT_TRUE(mDispatcher->waitForIdle());
5513}
5514
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08005515/**
5516 * Make sure the stale key is dropped before causing an ANR. So even if there's no focused window,
5517 * there will not be an ANR.
5518 */
5519TEST_F(InputDispatcherSingleWindowAnr, StaleKeyEventDoesNotAnr) {
5520 mWindow->setFocusable(false);
5521 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
5522 mWindow->consumeFocusEvent(false);
5523
5524 KeyEvent event;
5525 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC) -
5526 std::chrono::nanoseconds(STALE_EVENT_TIMEOUT).count();
5527
5528 // Define a valid key down event that is stale (too old).
5529 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
5530 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, /* flags */ 0, AKEYCODE_A, KEY_A,
Harry Cutts33476232023-01-30 19:57:29 +00005531 AMETA_NONE, /*repeatCount=*/1, eventTime, eventTime);
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08005532
5533 const int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER;
5534
5535 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00005536 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08005537 InputEventInjectionSync::WAIT_FOR_RESULT,
5538 INJECT_EVENT_TIMEOUT, policyFlags);
5539 ASSERT_EQ(InputEventInjectionResult::FAILED, result)
5540 << "Injection should fail because the event is stale";
5541
5542 ASSERT_TRUE(mDispatcher->waitForIdle());
5543 mFakePolicy->assertNotifyAnrWasNotCalled();
5544 mWindow->assertNoEvents();
5545}
5546
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005547// We have a focused application, but no focused window
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005548// Make sure that we don't notify policy twice about the same ANR.
5549TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) {
Vishnu Nair47074b82020-08-14 11:54:47 -07005550 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005551 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
5552 mWindow->consumeFocusEvent(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005553
5554 // Once a focused event arrives, we get an ANR for this application
5555 // We specify the injection timeout to be smaller than the application timeout, to ensure that
5556 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005557 const InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00005558 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
5559 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005560 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Vishnu Naire4df8752022-09-08 09:17:55 -07005561 const std::chrono::duration appTimeout =
5562 mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
5563 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005564
Vishnu Naire4df8752022-09-08 09:17:55 -07005565 std::this_thread::sleep_for(appTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005566 // ANR should not be raised again. It is up to policy to do that if it desires.
5567 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005568
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005569 // If we now get a focused window, the ANR should stop, but the policy handles that via
5570 // 'notifyFocusChanged' callback. This is implemented in the policy so we can't test it here.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005571 ASSERT_TRUE(mDispatcher->waitForIdle());
5572}
5573
5574// We have a focused application, but no focused window
5575TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) {
Vishnu Nair47074b82020-08-14 11:54:47 -07005576 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005577 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
5578 mWindow->consumeFocusEvent(false);
5579
5580 // Once a focused event arrives, we get an ANR for this application
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005581 const InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00005582 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005583 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
5584 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005585
Vishnu Naire4df8752022-09-08 09:17:55 -07005586 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
5587 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005588
5589 // Future focused events get dropped right away
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005590 ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005591 ASSERT_TRUE(mDispatcher->waitForIdle());
5592 mWindow->assertNoEvents();
5593}
5594
5595/**
5596 * Ensure that the implementation is valid. Since we are using multiset to keep track of the
5597 * ANR timeouts, we are allowing entries with identical timestamps in the same connection.
5598 * If we process 1 of the events, but ANR on the second event with the same timestamp,
5599 * the ANR mechanism should still work.
5600 *
5601 * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the
5602 * DOWN event, while not responding on the second one.
5603 */
5604TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) {
5605 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
5606 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5607 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
5608 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
5609 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005610 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005611
5612 // Now send ACTION_UP, with identical timestamp
5613 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
5614 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
5615 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
5616 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005617 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005618
5619 // We have now sent down and up. Let's consume first event and then ANR on the second.
5620 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5621 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005622 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005623}
5624
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005625// A spy window can receive an ANR
5626TEST_F(InputDispatcherSingleWindowAnr, SpyWindowAnr) {
5627 sp<FakeWindowHandle> spy = addSpyWindow();
5628
5629 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5630 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5631 WINDOW_LOCATION));
5632 mWindow->consumeMotionDown();
5633
5634 std::optional<uint32_t> sequenceNum = spy->receiveEvent(); // ACTION_DOWN
5635 ASSERT_TRUE(sequenceNum);
5636 const std::chrono::duration timeout = spy->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005637 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, spy);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005638
5639 spy->finishEvent(*sequenceNum);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08005640 spy->consumeMotionEvent(
5641 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005642 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08005643 mFakePolicy->assertNotifyWindowResponsiveWasCalled(spy->getToken(), mWindow->getPid());
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005644}
5645
5646// If an app is not responding to a key event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005647// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005648TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnKey) {
5649 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005650
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005651 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5652 injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005653 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005654 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005655
5656 // Stuck on the ACTION_UP
5657 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005658 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005659
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005660 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005661 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005662 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5663 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005664
5665 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion
5666 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08005667 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005668 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005669 spy->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005670}
5671
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005672// If an app is not responding to a motion event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005673// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005674TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnMotion) {
5675 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005676
5677 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005678 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5679 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005680
5681 mWindow->consumeMotionDown();
5682 // Stuck on the ACTION_UP
5683 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005684 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005685
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005686 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005687 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005688 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5689 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005690
5691 mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion
5692 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08005693 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005694 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005695 spy->assertNoEvents();
5696}
5697
5698TEST_F(InputDispatcherSingleWindowAnr, UnresponsiveMonitorAnr) {
5699 mDispatcher->setMonitorDispatchingTimeoutForTest(30ms);
5700
5701 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
5702
5703 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5704 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5705 WINDOW_LOCATION));
5706
5707 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5708 const std::optional<uint32_t> consumeSeq = monitor.receiveEvent();
5709 ASSERT_TRUE(consumeSeq);
5710
Prabir Pradhanedd96402022-02-15 01:46:16 -08005711 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(30ms, monitor.getToken(), MONITOR_PID);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005712
5713 monitor.finishEvent(*consumeSeq);
5714 monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
5715
5716 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08005717 mFakePolicy->assertNotifyWindowResponsiveWasCalled(monitor.getToken(), MONITOR_PID);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005718}
5719
5720// If a window is unresponsive, then you get anr. if the window later catches up and starts to
5721// process events, you don't get an anr. When the window later becomes unresponsive again, you
5722// get an ANR again.
5723// 1. tap -> block on ACTION_UP -> receive ANR
5724// 2. consume all pending events (= queue becomes healthy again)
5725// 3. tap again -> block on ACTION_UP again -> receive ANR second time
5726TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) {
5727 tapOnWindow();
5728
5729 mWindow->consumeMotionDown();
5730 // Block on ACTION_UP
5731 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005732 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005733 mWindow->consumeMotionUp(); // Now the connection should be healthy again
5734 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08005735 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005736 mWindow->assertNoEvents();
5737
5738 tapOnWindow();
5739 mWindow->consumeMotionDown();
Prabir Pradhanedd96402022-02-15 01:46:16 -08005740 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005741 mWindow->consumeMotionUp();
5742
5743 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08005744 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005745 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005746 mWindow->assertNoEvents();
5747}
5748
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005749// If a connection remains unresponsive for a while, make sure policy is only notified once about
5750// it.
5751TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005752 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005753 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5754 WINDOW_LOCATION));
5755
5756 const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005757 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005758 std::this_thread::sleep_for(windowTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005759 // 'notifyConnectionUnresponsive' should only be called once per connection
5760 mFakePolicy->assertNotifyAnrWasNotCalled();
5761 // When the ANR happened, dispatcher should abort the current event stream via ACTION_CANCEL
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005762 mWindow->consumeMotionDown();
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08005763 mWindow->consumeMotionEvent(
5764 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005765 mWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005766 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08005767 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005768 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005769}
5770
5771/**
5772 * If a window is processing a motion event, and then a key event comes in, the key event should
5773 * not to to the focused window until the motion is processed.
5774 *
5775 * Warning!!!
5776 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
5777 * and the injection timeout that we specify when injecting the key.
5778 * We must have the injection timeout (10ms) be smaller than
5779 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
5780 *
5781 * If that value changes, this test should also change.
5782 */
5783TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) {
5784 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
5785 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
5786
5787 tapOnWindow();
5788 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
5789 ASSERT_TRUE(downSequenceNum);
5790 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
5791 ASSERT_TRUE(upSequenceNum);
5792 // Don't finish the events yet, and send a key
5793 // Injection will "succeed" because we will eventually give up and send the key to the focused
5794 // window even if motions are still being processed. But because the injection timeout is short,
5795 // we will receive INJECTION_TIMED_OUT as the result.
5796
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005797 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00005798 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005799 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
5800 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005801 // Key will not be sent to the window, yet, because the window is still processing events
5802 // and the key remains pending, waiting for the touch events to be processed
5803 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
5804 ASSERT_FALSE(keySequenceNum);
5805
5806 std::this_thread::sleep_for(500ms);
5807 // if we wait long enough though, dispatcher will give up, and still send the key
5808 // to the focused window, even though we have not yet finished the motion event
5809 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5810 mWindow->finishEvent(*downSequenceNum);
5811 mWindow->finishEvent(*upSequenceNum);
5812}
5813
5814/**
5815 * If a window is processing a motion event, and then a key event comes in, the key event should
5816 * not go to the focused window until the motion is processed.
5817 * If then a new motion comes in, then the pending key event should be going to the currently
5818 * focused window right away.
5819 */
5820TEST_F(InputDispatcherSingleWindowAnr,
5821 PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) {
5822 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
5823 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
5824
5825 tapOnWindow();
5826 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
5827 ASSERT_TRUE(downSequenceNum);
5828 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
5829 ASSERT_TRUE(upSequenceNum);
5830 // Don't finish the events yet, and send a key
5831 // Injection is async, so it will succeed
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005832 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00005833 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
5834 InputEventInjectionSync::NONE));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005835 // At this point, key is still pending, and should not be sent to the application yet.
5836 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
5837 ASSERT_FALSE(keySequenceNum);
5838
5839 // Now tap down again. It should cause the pending key to go to the focused window right away.
5840 tapOnWindow();
5841 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd
5842 // the other events yet. We can finish events in any order.
5843 mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN
5844 mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP
5845 mWindow->consumeMotionDown();
5846 mWindow->consumeMotionUp();
5847 mWindow->assertNoEvents();
5848}
5849
5850class InputDispatcherMultiWindowAnr : public InputDispatcherTest {
5851 virtual void SetUp() override {
5852 InputDispatcherTest::SetUp();
5853
Chris Yea209fde2020-07-22 13:54:51 -07005854 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005855 mApplication->setDispatchingTimeout(10ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005856 mUnfocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Unfocused",
5857 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005858 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005859 // Adding FLAG_WATCH_OUTSIDE_TOUCH to receive ACTION_OUTSIDE when another window is tapped
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08005860 mUnfocusedWindow->setWatchOutsideTouch(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005861
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005862 mFocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Focused",
5863 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05005864 mFocusedWindow->setDispatchingTimeout(30ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005865 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005866
5867 // Set focused application.
5868 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
Vishnu Nair47074b82020-08-14 11:54:47 -07005869 mFocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005870
5871 // Expect one focus window exist in display.
5872 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005873 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005874 mFocusedWindow->consumeFocusEvent(true);
5875 }
5876
5877 virtual void TearDown() override {
5878 InputDispatcherTest::TearDown();
5879
5880 mUnfocusedWindow.clear();
5881 mFocusedWindow.clear();
5882 }
5883
5884protected:
Chris Yea209fde2020-07-22 13:54:51 -07005885 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005886 sp<FakeWindowHandle> mUnfocusedWindow;
5887 sp<FakeWindowHandle> mFocusedWindow;
5888 static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20};
5889 static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75};
5890 static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40};
5891
5892 void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); }
5893
5894 void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); }
5895
5896private:
5897 void tap(const PointF& location) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005898 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005899 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5900 location));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005901 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005902 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5903 location));
5904 }
5905};
5906
5907// If we have 2 windows that are both unresponsive, the one with the shortest timeout
5908// should be ANR'd first.
5909TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005910 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005911 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5912 FOCUSED_WINDOW_LOCATION))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005913 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005914 mFocusedWindow->consumeMotionDown();
5915 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00005916 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005917 // We consumed all events, so no ANR
5918 ASSERT_TRUE(mDispatcher->waitForIdle());
5919 mFakePolicy->assertNotifyAnrWasNotCalled();
5920
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005921 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005922 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5923 FOCUSED_WINDOW_LOCATION));
5924 std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent();
5925 ASSERT_TRUE(unfocusedSequenceNum);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005926
5927 const std::chrono::duration timeout =
5928 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005929 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005930 // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event
5931 // sequence to make it consistent
5932 mFocusedWindow->consumeMotionCancel();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005933 mUnfocusedWindow->finishEvent(*unfocusedSequenceNum);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005934 mFocusedWindow->consumeMotionDown();
5935 // This cancel is generated because the connection was unresponsive
5936 mFocusedWindow->consumeMotionCancel();
5937 mFocusedWindow->assertNoEvents();
5938 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005939 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08005940 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
5941 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005942 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005943}
5944
5945// If we have 2 windows with identical timeouts that are both unresponsive,
5946// it doesn't matter which order they should have ANR.
5947// But we should receive ANR for both.
5948TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) {
5949 // Set the timeout for unfocused window to match the focused window
5950 mUnfocusedWindow->setDispatchingTimeout(10ms);
5951 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
5952
5953 tapOnFocusedWindow();
5954 // we should have ACTION_DOWN/ACTION_UP on focused window and ACTION_OUTSIDE on unfocused window
Prabir Pradhanedd96402022-02-15 01:46:16 -08005955 sp<IBinder> anrConnectionToken1, anrConnectionToken2;
5956 ASSERT_NO_FATAL_FAILURE(anrConnectionToken1 = mFakePolicy->getUnresponsiveWindowToken(10ms));
5957 ASSERT_NO_FATAL_FAILURE(anrConnectionToken2 = mFakePolicy->getUnresponsiveWindowToken(0ms));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005958
5959 // We don't know which window will ANR first. But both of them should happen eventually.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005960 ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 ||
5961 mFocusedWindow->getToken() == anrConnectionToken2);
5962 ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 ||
5963 mUnfocusedWindow->getToken() == anrConnectionToken2);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005964
5965 ASSERT_TRUE(mDispatcher->waitForIdle());
5966 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005967
5968 mFocusedWindow->consumeMotionDown();
5969 mFocusedWindow->consumeMotionUp();
5970 mUnfocusedWindow->consumeMotionOutside();
5971
Prabir Pradhanedd96402022-02-15 01:46:16 -08005972 sp<IBinder> responsiveToken1, responsiveToken2;
5973 ASSERT_NO_FATAL_FAILURE(responsiveToken1 = mFakePolicy->getResponsiveWindowToken());
5974 ASSERT_NO_FATAL_FAILURE(responsiveToken2 = mFakePolicy->getResponsiveWindowToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005975
5976 // Both applications should be marked as responsive, in any order
5977 ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 ||
5978 mFocusedWindow->getToken() == responsiveToken2);
5979 ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 ||
5980 mUnfocusedWindow->getToken() == responsiveToken2);
5981 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005982}
5983
5984// If a window is already not responding, the second tap on the same window should be ignored.
5985// We should also log an error to account for the dropped event (not tested here).
5986// At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events.
5987TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) {
5988 tapOnFocusedWindow();
5989 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00005990 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005991 // Receive the events, but don't respond
5992 std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN
5993 ASSERT_TRUE(downEventSequenceNum);
5994 std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP
5995 ASSERT_TRUE(upEventSequenceNum);
5996 const std::chrono::duration timeout =
5997 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005998 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005999
6000 // Tap once again
6001 // We cannot use "tapOnFocusedWindow" because it asserts the injection result to be success
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006002 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006003 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6004 FOCUSED_WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006005 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006006 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6007 FOCUSED_WINDOW_LOCATION));
6008 // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a
6009 // valid touch target
6010 mUnfocusedWindow->assertNoEvents();
6011
6012 // Consume the first tap
6013 mFocusedWindow->finishEvent(*downEventSequenceNum);
6014 mFocusedWindow->finishEvent(*upEventSequenceNum);
6015 ASSERT_TRUE(mDispatcher->waitForIdle());
6016 // The second tap did not go to the focused window
6017 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006018 // Since all events are finished, connection should be deemed healthy again
Prabir Pradhanedd96402022-02-15 01:46:16 -08006019 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
6020 mFocusedWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006021 mFakePolicy->assertNotifyAnrWasNotCalled();
6022}
6023
6024// If you tap outside of all windows, there will not be ANR
6025TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006026 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006027 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6028 LOCATION_OUTSIDE_ALL_WINDOWS));
6029 ASSERT_TRUE(mDispatcher->waitForIdle());
6030 mFakePolicy->assertNotifyAnrWasNotCalled();
6031}
6032
6033// Since the focused window is paused, tapping on it should not produce any events
6034TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) {
6035 mFocusedWindow->setPaused(true);
6036 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
6037
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006038 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006039 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6040 FOCUSED_WINDOW_LOCATION));
6041
6042 std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT));
6043 ASSERT_TRUE(mDispatcher->waitForIdle());
6044 // Should not ANR because the window is paused, and touches shouldn't go to it
6045 mFakePolicy->assertNotifyAnrWasNotCalled();
6046
6047 mFocusedWindow->assertNoEvents();
6048 mUnfocusedWindow->assertNoEvents();
6049}
6050
6051/**
6052 * If a window is processing a motion event, and then a key event comes in, the key event should
6053 * not to to the focused window until the motion is processed.
6054 * If a different window becomes focused at this time, the key should go to that window instead.
6055 *
6056 * Warning!!!
6057 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
6058 * and the injection timeout that we specify when injecting the key.
6059 * We must have the injection timeout (10ms) be smaller than
6060 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
6061 *
6062 * If that value changes, this test should also change.
6063 */
6064TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) {
6065 // Set a long ANR timeout to prevent it from triggering
6066 mFocusedWindow->setDispatchingTimeout(2s);
6067 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
6068
6069 tapOnUnfocusedWindow();
6070 std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent();
6071 ASSERT_TRUE(downSequenceNum);
6072 std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent();
6073 ASSERT_TRUE(upSequenceNum);
6074 // Don't finish the events yet, and send a key
6075 // Injection will succeed because we will eventually give up and send the key to the focused
6076 // window even if motions are still being processed.
6077
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006078 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006079 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6080 InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006081 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006082 // Key will not be sent to the window, yet, because the window is still processing events
6083 // and the key remains pending, waiting for the touch events to be processed
6084 std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent();
6085 ASSERT_FALSE(keySequenceNum);
6086
6087 // Switch the focus to the "unfocused" window that we tapped. Expect the key to go there
Vishnu Nair47074b82020-08-14 11:54:47 -07006088 mFocusedWindow->setFocusable(false);
6089 mUnfocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006090 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07006091 setFocusedWindow(mUnfocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006092
6093 // Focus events should precede the key events
6094 mUnfocusedWindow->consumeFocusEvent(true);
6095 mFocusedWindow->consumeFocusEvent(false);
6096
6097 // Finish the tap events, which should unblock dispatcher
6098 mUnfocusedWindow->finishEvent(*downSequenceNum);
6099 mUnfocusedWindow->finishEvent(*upSequenceNum);
6100
6101 // Now that all queues are cleared and no backlog in the connections, the key event
6102 // can finally go to the newly focused "mUnfocusedWindow".
6103 mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
6104 mFocusedWindow->assertNoEvents();
6105 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006106 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006107}
6108
6109// When the touch stream is split across 2 windows, and one of them does not respond,
6110// then ANR should be raised and the touch should be canceled for the unresponsive window.
6111// The other window should not be affected by that.
6112TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) {
6113 // Touch Window 1
6114 NotifyMotionArgs motionArgs =
6115 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6116 ADISPLAY_ID_DEFAULT, {FOCUSED_WINDOW_LOCATION});
6117 mDispatcher->notifyMotion(&motionArgs);
6118 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00006119 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006120
6121 // Touch Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006122 motionArgs = generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6123 {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006124 mDispatcher->notifyMotion(&motionArgs);
6125
6126 const std::chrono::duration timeout =
6127 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006128 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006129
6130 mUnfocusedWindow->consumeMotionDown();
6131 mFocusedWindow->consumeMotionDown();
6132 // Focused window may or may not receive ACTION_MOVE
6133 // But it should definitely receive ACTION_CANCEL due to the ANR
6134 InputEvent* event;
6135 std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event);
6136 ASSERT_TRUE(moveOrCancelSequenceNum);
6137 mFocusedWindow->finishEvent(*moveOrCancelSequenceNum);
6138 ASSERT_NE(nullptr, event);
6139 ASSERT_EQ(event->getType(), AINPUT_EVENT_TYPE_MOTION);
6140 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
6141 if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) {
6142 mFocusedWindow->consumeMotionCancel();
6143 } else {
6144 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction());
6145 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006146 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006147 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
6148 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006149
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006150 mUnfocusedWindow->assertNoEvents();
6151 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006152 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006153}
6154
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05006155/**
6156 * If we have no focused window, and a key comes in, we start the ANR timer.
6157 * The focused application should add a focused window before the timer runs out to prevent ANR.
6158 *
6159 * If the user touches another application during this time, the key should be dropped.
6160 * Next, if a new focused window comes in, without toggling the focused application,
6161 * then no ANR should occur.
6162 *
6163 * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication',
6164 * but in some cases the policy may not update the focused application.
6165 */
6166TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) {
6167 std::shared_ptr<FakeApplicationHandle> focusedApplication =
6168 std::make_shared<FakeApplicationHandle>();
6169 focusedApplication->setDispatchingTimeout(60ms);
6170 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication);
6171 // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused.
6172 mFocusedWindow->setFocusable(false);
6173
6174 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
6175 mFocusedWindow->consumeFocusEvent(false);
6176
6177 // Send a key. The ANR timer should start because there is no focused window.
6178 // 'focusedApplication' will get blamed if this timer completes.
6179 // Key will not be sent anywhere because we have no focused window. It will remain pending.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006180 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006181 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6182 InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms,
6183 /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006184 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05006185
6186 // Wait until dispatcher starts the "no focused window" timer. If we don't wait here,
6187 // then the injected touches won't cause the focused event to get dropped.
6188 // The dispatcher only checks for whether the queue should be pruned upon queueing.
6189 // If we inject the touch right away and the ANR timer hasn't started, the touch event would
6190 // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'.
6191 // For this test, it means that the key would get delivered to the window once it becomes
6192 // focused.
6193 std::this_thread::sleep_for(10ms);
6194
6195 // Touch unfocused window. This should force the pending key to get dropped.
6196 NotifyMotionArgs motionArgs =
6197 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6198 ADISPLAY_ID_DEFAULT, {UNFOCUSED_WINDOW_LOCATION});
6199 mDispatcher->notifyMotion(&motionArgs);
6200
6201 // We do not consume the motion right away, because that would require dispatcher to first
6202 // process (== drop) the key event, and by that time, ANR will be raised.
6203 // Set the focused window first.
6204 mFocusedWindow->setFocusable(true);
6205 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
6206 setFocusedWindow(mFocusedWindow);
6207 mFocusedWindow->consumeFocusEvent(true);
6208 // We do not call "setFocusedApplication" here, even though the newly focused window belongs
6209 // to another application. This could be a bug / behaviour in the policy.
6210
6211 mUnfocusedWindow->consumeMotionDown();
6212
6213 ASSERT_TRUE(mDispatcher->waitForIdle());
6214 // Should not ANR because we actually have a focused window. It was just added too slowly.
6215 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled());
6216}
6217
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05006218// These tests ensure we cannot send touch events to a window that's positioned behind a window
6219// that has feature NO_INPUT_CHANNEL.
6220// Layout:
6221// Top (closest to user)
6222// mNoInputWindow (above all windows)
6223// mBottomWindow
6224// Bottom (furthest from user)
6225class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest {
6226 virtual void SetUp() override {
6227 InputDispatcherTest::SetUp();
6228
6229 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006230 mNoInputWindow =
6231 sp<FakeWindowHandle>::make(mApplication, mDispatcher,
6232 "Window without input channel", ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00006233 /*token=*/std::make_optional<sp<IBinder>>(nullptr));
Prabir Pradhan51e7db02022-02-07 06:02:57 -08006234 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05006235 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
6236 // It's perfectly valid for this window to not have an associated input channel
6237
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006238 mBottomWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Bottom window",
6239 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05006240 mBottomWindow->setFrame(Rect(0, 0, 100, 100));
6241
6242 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
6243 }
6244
6245protected:
6246 std::shared_ptr<FakeApplicationHandle> mApplication;
6247 sp<FakeWindowHandle> mNoInputWindow;
6248 sp<FakeWindowHandle> mBottomWindow;
6249};
6250
6251TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) {
6252 PointF touchedPoint = {10, 10};
6253
6254 NotifyMotionArgs motionArgs =
6255 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6256 ADISPLAY_ID_DEFAULT, {touchedPoint});
6257 mDispatcher->notifyMotion(&motionArgs);
6258
6259 mNoInputWindow->assertNoEvents();
6260 // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have
6261 // an input channel, it is not marked as FLAG_NOT_TOUCHABLE,
6262 // and therefore should prevent mBottomWindow from receiving touches
6263 mBottomWindow->assertNoEvents();
6264}
6265
6266/**
6267 * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel,
6268 * ensure that this window does not receive any touches, and blocks touches to windows underneath.
6269 */
6270TEST_F(InputDispatcherMultiWindowOcclusionTests,
6271 NoInputChannelFeature_DropsTouchesWithValidChannel) {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006272 mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher,
6273 "Window with input channel and NO_INPUT_CHANNEL",
6274 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05006275
Prabir Pradhan51e7db02022-02-07 06:02:57 -08006276 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05006277 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
6278 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
6279
6280 PointF touchedPoint = {10, 10};
6281
6282 NotifyMotionArgs motionArgs =
6283 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6284 ADISPLAY_ID_DEFAULT, {touchedPoint});
6285 mDispatcher->notifyMotion(&motionArgs);
6286
6287 mNoInputWindow->assertNoEvents();
6288 mBottomWindow->assertNoEvents();
6289}
6290
Vishnu Nair958da932020-08-21 17:12:37 -07006291class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest {
6292protected:
6293 std::shared_ptr<FakeApplicationHandle> mApp;
6294 sp<FakeWindowHandle> mWindow;
6295 sp<FakeWindowHandle> mMirror;
6296
6297 virtual void SetUp() override {
6298 InputDispatcherTest::SetUp();
6299 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006300 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
6301 mMirror = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindowMirror",
6302 ADISPLAY_ID_DEFAULT, mWindow->getToken());
Vishnu Nair958da932020-08-21 17:12:37 -07006303 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
6304 mWindow->setFocusable(true);
6305 mMirror->setFocusable(true);
6306 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
6307 }
6308};
6309
6310TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) {
6311 // Request focus on a mirrored window
6312 setFocusedWindow(mMirror);
6313
6314 // window gets focused
6315 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006316 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
6317 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07006318 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
6319}
6320
6321// A focused & mirrored window remains focused only if the window and its mirror are both
6322// focusable.
6323TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) {
6324 setFocusedWindow(mMirror);
6325
6326 // window gets focused
6327 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006328 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
6329 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07006330 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006331 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
6332 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07006333 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
6334
6335 mMirror->setFocusable(false);
6336 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
6337
6338 // window loses focus since one of the windows associated with the token in not focusable
6339 mWindow->consumeFocusEvent(false);
6340
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006341 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
6342 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07006343 mWindow->assertNoEvents();
6344}
6345
6346// A focused & mirrored window remains focused until the window and its mirror both become
6347// invisible.
6348TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) {
6349 setFocusedWindow(mMirror);
6350
6351 // window gets focused
6352 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006353 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
6354 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07006355 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006356 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
6357 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07006358 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
6359
6360 mMirror->setVisible(false);
6361 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
6362
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006363 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
6364 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07006365 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006366 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
6367 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07006368 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
6369
6370 mWindow->setVisible(false);
6371 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
6372
6373 // window loses focus only after all windows associated with the token become invisible.
6374 mWindow->consumeFocusEvent(false);
6375
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006376 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
6377 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07006378 mWindow->assertNoEvents();
6379}
6380
6381// A focused & mirrored window remains focused until both windows are removed.
6382TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) {
6383 setFocusedWindow(mMirror);
6384
6385 // window gets focused
6386 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006387 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
6388 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07006389 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006390 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
6391 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07006392 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
6393
6394 // single window is removed but the window token remains focused
6395 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}});
6396
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006397 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
6398 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07006399 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006400 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
6401 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07006402 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
6403
6404 // Both windows are removed
6405 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
6406 mWindow->consumeFocusEvent(false);
6407
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006408 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
6409 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07006410 mWindow->assertNoEvents();
6411}
6412
6413// Focus request can be pending until one window becomes visible.
6414TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) {
6415 // Request focus on an invisible mirror.
6416 mWindow->setVisible(false);
6417 mMirror->setVisible(false);
6418 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
6419 setFocusedWindow(mMirror);
6420
6421 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006422 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00006423 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6424 InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07006425
6426 mMirror->setVisible(true);
6427 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
6428
6429 // window gets focused
6430 mWindow->consumeFocusEvent(true);
6431 // window gets the pending key event
6432 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
6433}
Prabir Pradhan99987712020-11-10 18:43:05 -08006434
6435class InputDispatcherPointerCaptureTests : public InputDispatcherTest {
6436protected:
6437 std::shared_ptr<FakeApplicationHandle> mApp;
6438 sp<FakeWindowHandle> mWindow;
6439 sp<FakeWindowHandle> mSecondWindow;
6440
6441 void SetUp() override {
6442 InputDispatcherTest::SetUp();
6443 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006444 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -08006445 mWindow->setFocusable(true);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006446 mSecondWindow =
6447 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -08006448 mSecondWindow->setFocusable(true);
6449
6450 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
6451 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}});
6452
6453 setFocusedWindow(mWindow);
6454 mWindow->consumeFocusEvent(true);
6455 }
6456
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006457 void notifyPointerCaptureChanged(const PointerCaptureRequest& request) {
6458 const NotifyPointerCaptureChangedArgs args = generatePointerCaptureChangedArgs(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08006459 mDispatcher->notifyPointerCaptureChanged(&args);
6460 }
6461
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006462 PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window,
6463 bool enabled) {
Prabir Pradhan99987712020-11-10 18:43:05 -08006464 mDispatcher->requestPointerCapture(window->getToken(), enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006465 auto request = mFakePolicy->assertSetPointerCaptureCalled(enabled);
6466 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08006467 window->consumeCaptureEvent(enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006468 return request;
Prabir Pradhan99987712020-11-10 18:43:05 -08006469 }
6470};
6471
6472TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) {
6473 // Ensure that capture cannot be obtained for unfocused windows.
6474 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
6475 mFakePolicy->assertSetPointerCaptureNotCalled();
6476 mSecondWindow->assertNoEvents();
6477
6478 // Ensure that capture can be enabled from the focus window.
6479 requestAndVerifyPointerCapture(mWindow, true);
6480
6481 // Ensure that capture cannot be disabled from a window that does not have capture.
6482 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false);
6483 mFakePolicy->assertSetPointerCaptureNotCalled();
6484
6485 // Ensure that capture can be disabled from the window with capture.
6486 requestAndVerifyPointerCapture(mWindow, false);
6487}
6488
6489TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006490 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08006491
6492 setFocusedWindow(mSecondWindow);
6493
6494 // Ensure that the capture disabled event was sent first.
6495 mWindow->consumeCaptureEvent(false);
6496 mWindow->consumeFocusEvent(false);
6497 mSecondWindow->consumeFocusEvent(true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006498 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08006499
6500 // Ensure that additional state changes from InputReader are not sent to the window.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006501 notifyPointerCaptureChanged({});
6502 notifyPointerCaptureChanged(request);
6503 notifyPointerCaptureChanged({});
Prabir Pradhan99987712020-11-10 18:43:05 -08006504 mWindow->assertNoEvents();
6505 mSecondWindow->assertNoEvents();
6506 mFakePolicy->assertSetPointerCaptureNotCalled();
6507}
6508
6509TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006510 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08006511
6512 // InputReader unexpectedly disables and enables pointer capture.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006513 notifyPointerCaptureChanged({});
6514 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08006515
6516 // Ensure that Pointer Capture is disabled.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006517 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08006518 mWindow->consumeCaptureEvent(false);
6519 mWindow->assertNoEvents();
6520}
6521
Prabir Pradhan167e6d92021-02-04 16:18:17 -08006522TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) {
6523 requestAndVerifyPointerCapture(mWindow, true);
6524
6525 // The first window loses focus.
6526 setFocusedWindow(mSecondWindow);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006527 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08006528 mWindow->consumeCaptureEvent(false);
6529
6530 // Request Pointer Capture from the second window before the notification from InputReader
6531 // arrives.
6532 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006533 auto request = mFakePolicy->assertSetPointerCaptureCalled(true);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08006534
6535 // InputReader notifies Pointer Capture was disabled (because of the focus change).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006536 notifyPointerCaptureChanged({});
Prabir Pradhan167e6d92021-02-04 16:18:17 -08006537
6538 // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006539 notifyPointerCaptureChanged(request);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08006540
6541 mSecondWindow->consumeFocusEvent(true);
6542 mSecondWindow->consumeCaptureEvent(true);
6543}
6544
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006545TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) {
6546 // App repeatedly enables and disables capture.
6547 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
6548 auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
6549 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
6550 mFakePolicy->assertSetPointerCaptureCalled(false);
6551 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
6552 auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
6553
6554 // InputReader notifies that PointerCapture has been enabled for the first request. Since the
6555 // first request is now stale, this should do nothing.
6556 notifyPointerCaptureChanged(firstRequest);
6557 mWindow->assertNoEvents();
6558
6559 // InputReader notifies that the second request was enabled.
6560 notifyPointerCaptureChanged(secondRequest);
6561 mWindow->consumeCaptureEvent(true);
6562}
6563
Prabir Pradhan7092e262022-05-03 16:51:09 +00006564TEST_F(InputDispatcherPointerCaptureTests, RapidToggleRequests) {
6565 requestAndVerifyPointerCapture(mWindow, true);
6566
6567 // App toggles pointer capture off and on.
6568 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
6569 mFakePolicy->assertSetPointerCaptureCalled(false);
6570
6571 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
6572 auto enableRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
6573
6574 // InputReader notifies that the latest "enable" request was processed, while skipping over the
6575 // preceding "disable" request.
6576 notifyPointerCaptureChanged(enableRequest);
6577
6578 // Since pointer capture was never disabled during the rapid toggle, the window does not receive
6579 // any notifications.
6580 mWindow->assertNoEvents();
6581}
6582
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006583class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest {
6584protected:
6585 constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8;
Bernardo Rufino7393d172021-02-26 13:56:11 +00006586
6587 constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9;
6588 static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY);
6589
6590 constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7;
6591 static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
6592
6593 // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold
6594 constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5;
6595 static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
6596 static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) <
6597 MAXIMUM_OBSCURING_OPACITY);
6598
Bernardo Rufino6d52e542021-02-15 18:38:10 +00006599 static const int32_t TOUCHED_APP_UID = 10001;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006600 static const int32_t APP_B_UID = 10002;
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006601 static const int32_t APP_C_UID = 10003;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006602
6603 sp<FakeWindowHandle> mTouchWindow;
6604
6605 virtual void SetUp() override {
6606 InputDispatcherTest::SetUp();
Bernardo Rufino6d52e542021-02-15 18:38:10 +00006607 mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched");
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006608 mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY);
6609 }
6610
6611 virtual void TearDown() override {
6612 InputDispatcherTest::TearDown();
6613 mTouchWindow.clear();
6614 }
6615
chaviw3277faf2021-05-19 16:45:23 -05006616 sp<FakeWindowHandle> getOccludingWindow(int32_t uid, std::string name, TouchOcclusionMode mode,
6617 float alpha = 1.0f) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006618 sp<FakeWindowHandle> window = getWindow(uid, name);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08006619 window->setTouchable(false);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006620 window->setTouchOcclusionMode(mode);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006621 window->setAlpha(alpha);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006622 return window;
6623 }
6624
6625 sp<FakeWindowHandle> getWindow(int32_t uid, std::string name) {
6626 std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>();
6627 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006628 sp<FakeWindowHandle>::make(app, mDispatcher, name, ADISPLAY_ID_DEFAULT);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006629 // Generate an arbitrary PID based on the UID
6630 window->setOwnerInfo(1777 + (uid % 10000), uid);
6631 return window;
6632 }
6633
6634 void touch(const std::vector<PointF>& points = {PointF{100, 200}}) {
6635 NotifyMotionArgs args =
6636 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6637 ADISPLAY_ID_DEFAULT, points);
6638 mDispatcher->notifyMotion(&args);
6639 }
6640};
6641
6642TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006643 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006644 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006645 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006646
6647 touch();
6648
6649 mTouchWindow->assertNoEvents();
6650}
6651
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00006652TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufino7393d172021-02-26 13:56:11 +00006653 WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) {
6654 const sp<FakeWindowHandle>& w =
6655 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f);
6656 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6657
6658 touch();
6659
6660 mTouchWindow->assertNoEvents();
6661}
6662
6663TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00006664 WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) {
6665 const sp<FakeWindowHandle>& w =
6666 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
6667 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6668
6669 touch();
6670
6671 w->assertNoEvents();
6672}
6673
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006674TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006675 const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW);
6676 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006677
6678 touch();
6679
6680 mTouchWindow->consumeAnyMotionDown();
6681}
6682
6683TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006684 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006685 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006686 w->setFrame(Rect(0, 0, 50, 50));
6687 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006688
6689 touch({PointF{100, 100}});
6690
6691 mTouchWindow->consumeAnyMotionDown();
6692}
6693
6694TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006695 const sp<FakeWindowHandle>& w =
Bernardo Rufino6d52e542021-02-15 18:38:10 +00006696 getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006697 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6698
6699 touch();
6700
6701 mTouchWindow->consumeAnyMotionDown();
6702}
6703
6704TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) {
6705 const sp<FakeWindowHandle>& w =
6706 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
6707 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006708
6709 touch();
6710
6711 mTouchWindow->consumeAnyMotionDown();
6712}
6713
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00006714TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) {
6715 const sp<FakeWindowHandle>& w =
6716 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
6717 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6718
6719 touch();
6720
6721 w->assertNoEvents();
6722}
6723
6724/**
6725 * This is important to make sure apps can't indirectly learn the position of touches (outside vs
6726 * inside) while letting them pass-through. Note that even though touch passes through the occluding
6727 * window, the occluding window will still receive ACTION_OUTSIDE event.
6728 */
6729TEST_F(InputDispatcherUntrustedTouchesTest,
6730 WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) {
6731 const sp<FakeWindowHandle>& w =
6732 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08006733 w->setWatchOutsideTouch(true);
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00006734 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6735
6736 touch();
6737
6738 w->consumeMotionOutside();
6739}
6740
6741TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) {
6742 const sp<FakeWindowHandle>& w =
6743 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08006744 w->setWatchOutsideTouch(true);
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00006745 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6746
6747 touch();
6748
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08006749 w->consumeMotionOutsideWithZeroedCoords();
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00006750}
6751
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006752TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006753 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006754 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
6755 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006756 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6757
6758 touch();
6759
6760 mTouchWindow->consumeAnyMotionDown();
6761}
6762
6763TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) {
6764 const sp<FakeWindowHandle>& w =
6765 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
6766 MAXIMUM_OBSCURING_OPACITY);
6767 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006768
6769 touch();
6770
6771 mTouchWindow->consumeAnyMotionDown();
6772}
6773
6774TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006775 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006776 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
6777 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006778 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6779
6780 touch();
6781
6782 mTouchWindow->assertNoEvents();
6783}
6784
6785TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) {
6786 // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91
6787 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006788 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
6789 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006790 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006791 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
6792 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006793 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
6794
6795 touch();
6796
6797 mTouchWindow->assertNoEvents();
6798}
6799
6800TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) {
6801 // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75
6802 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006803 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
6804 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006805 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006806 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
6807 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006808 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
6809
6810 touch();
6811
6812 mTouchWindow->consumeAnyMotionDown();
6813}
6814
6815TEST_F(InputDispatcherUntrustedTouchesTest,
6816 WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) {
6817 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006818 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
6819 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006820 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006821 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
6822 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006823 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
6824
6825 touch();
6826
6827 mTouchWindow->consumeAnyMotionDown();
6828}
6829
6830TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) {
6831 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006832 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
6833 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006834 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006835 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
6836 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006837 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006838
6839 touch();
6840
6841 mTouchWindow->assertNoEvents();
6842}
6843
Bernardo Rufino6d52e542021-02-15 18:38:10 +00006844TEST_F(InputDispatcherUntrustedTouchesTest,
6845 WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) {
6846 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006847 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
6848 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00006849 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006850 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
6851 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00006852 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
6853
6854 touch();
6855
6856 mTouchWindow->assertNoEvents();
6857}
6858
6859TEST_F(InputDispatcherUntrustedTouchesTest,
6860 WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) {
6861 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006862 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
6863 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00006864 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006865 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
6866 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00006867 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
6868
6869 touch();
6870
6871 mTouchWindow->consumeAnyMotionDown();
6872}
6873
6874TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) {
6875 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006876 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
6877 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00006878 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6879
6880 touch();
6881
6882 mTouchWindow->consumeAnyMotionDown();
6883}
6884
6885TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) {
6886 const sp<FakeWindowHandle>& w =
6887 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED);
6888 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6889
6890 touch();
6891
6892 mTouchWindow->consumeAnyMotionDown();
6893}
6894
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00006895TEST_F(InputDispatcherUntrustedTouchesTest,
6896 OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) {
6897 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
6898 const sp<FakeWindowHandle>& w =
6899 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f);
6900 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6901
6902 touch();
6903
6904 mTouchWindow->assertNoEvents();
6905}
6906
6907TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) {
6908 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
6909 const sp<FakeWindowHandle>& w =
6910 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f);
6911 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6912
6913 touch();
6914
6915 mTouchWindow->consumeAnyMotionDown();
6916}
6917
6918TEST_F(InputDispatcherUntrustedTouchesTest,
6919 OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) {
6920 mDispatcher->setMaximumObscuringOpacityForTouch(1.0f);
6921 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006922 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
6923 OPACITY_ABOVE_THRESHOLD);
6924 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6925
6926 touch();
6927
6928 mTouchWindow->consumeAnyMotionDown();
6929}
6930
6931TEST_F(InputDispatcherUntrustedTouchesTest,
6932 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) {
6933 const sp<FakeWindowHandle>& w1 =
6934 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
6935 OPACITY_BELOW_THRESHOLD);
6936 const sp<FakeWindowHandle>& w2 =
6937 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
6938 OPACITY_BELOW_THRESHOLD);
6939 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
6940
6941 touch();
6942
6943 mTouchWindow->assertNoEvents();
6944}
6945
6946/**
6947 * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the
6948 * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold
6949 * (which alone would result in allowing touches) does not affect the blocking behavior.
6950 */
6951TEST_F(InputDispatcherUntrustedTouchesTest,
6952 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) {
6953 const sp<FakeWindowHandle>& wB =
6954 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
6955 OPACITY_BELOW_THRESHOLD);
6956 const sp<FakeWindowHandle>& wC =
6957 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
6958 OPACITY_BELOW_THRESHOLD);
6959 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
6960
6961 touch();
6962
6963 mTouchWindow->assertNoEvents();
6964}
6965
6966/**
6967 * This test is testing that a window from a different UID but with same application token doesn't
6968 * block the touch. Apps can share the application token for close UI collaboration for example.
6969 */
6970TEST_F(InputDispatcherUntrustedTouchesTest,
6971 WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) {
6972 const sp<FakeWindowHandle>& w =
6973 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
6974 w->setApplicationToken(mTouchWindow->getApplicationToken());
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00006975 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6976
6977 touch();
6978
6979 mTouchWindow->consumeAnyMotionDown();
6980}
6981
arthurhungb89ccb02020-12-30 16:19:01 +08006982class InputDispatcherDragTests : public InputDispatcherTest {
6983protected:
6984 std::shared_ptr<FakeApplicationHandle> mApp;
6985 sp<FakeWindowHandle> mWindow;
6986 sp<FakeWindowHandle> mSecondWindow;
6987 sp<FakeWindowHandle> mDragWindow;
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00006988 sp<FakeWindowHandle> mSpyWindow;
Arthur Hungb75c2aa2022-07-15 09:35:36 +00006989 // Mouse would force no-split, set the id as non-zero to verify if drag state could track it.
6990 static constexpr int32_t MOUSE_POINTER_ID = 1;
arthurhungb89ccb02020-12-30 16:19:01 +08006991
6992 void SetUp() override {
6993 InputDispatcherTest::SetUp();
6994 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006995 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +08006996 mWindow->setFrame(Rect(0, 0, 100, 100));
arthurhungb89ccb02020-12-30 16:19:01 +08006997
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006998 mSecondWindow =
6999 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +08007000 mSecondWindow->setFrame(Rect(100, 0, 200, 100));
arthurhungb89ccb02020-12-30 16:19:01 +08007001
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007002 mSpyWindow =
7003 sp<FakeWindowHandle>::make(mApp, mDispatcher, "SpyWindow", ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00007004 mSpyWindow->setSpy(true);
7005 mSpyWindow->setTrustedOverlay(true);
7006 mSpyWindow->setFrame(Rect(0, 0, 200, 100));
7007
arthurhungb89ccb02020-12-30 16:19:01 +08007008 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00007009 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mSpyWindow, mWindow, mSecondWindow}}});
arthurhungb89ccb02020-12-30 16:19:01 +08007010 }
7011
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007012 void injectDown(int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
7013 switch (fromSource) {
7014 case AINPUT_SOURCE_TOUCHSCREEN:
7015 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7016 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
7017 ADISPLAY_ID_DEFAULT, {50, 50}))
7018 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7019 break;
7020 case AINPUT_SOURCE_STYLUS:
7021 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7022 injectMotionEvent(
7023 mDispatcher,
7024 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
7025 AINPUT_SOURCE_STYLUS)
7026 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
7027 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS)
7028 .x(50)
7029 .y(50))
7030 .build()));
7031 break;
7032 case AINPUT_SOURCE_MOUSE:
7033 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7034 injectMotionEvent(
7035 mDispatcher,
7036 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
7037 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
7038 .pointer(PointerBuilder(MOUSE_POINTER_ID,
7039 AMOTION_EVENT_TOOL_TYPE_MOUSE)
7040 .x(50)
7041 .y(50))
7042 .build()));
7043 break;
7044 default:
7045 FAIL() << "Source " << fromSource << " doesn't support drag and drop";
7046 }
arthurhungb89ccb02020-12-30 16:19:01 +08007047
7048 // Window should receive motion event.
7049 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00007050 // Spy window should also receive motion event
7051 mSpyWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung54745652022-04-20 07:17:41 +00007052 }
7053
7054 // Start performing drag, we will create a drag window and transfer touch to it.
7055 // @param sendDown : if true, send a motion down on first window before perform drag and drop.
7056 // Returns true on success.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007057 bool startDrag(bool sendDown = true, int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
Arthur Hung54745652022-04-20 07:17:41 +00007058 if (sendDown) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007059 injectDown(fromSource);
Arthur Hung54745652022-04-20 07:17:41 +00007060 }
arthurhungb89ccb02020-12-30 16:19:01 +08007061
7062 // The drag window covers the entire display
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007063 mDragWindow =
7064 sp<FakeWindowHandle>::make(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00007065 mDragWindow->setTouchableRegion(Region{{0, 0, 0, 0}});
arthurhungb89ccb02020-12-30 16:19:01 +08007066 mDispatcher->setInputWindows(
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00007067 {{ADISPLAY_ID_DEFAULT, {mDragWindow, mSpyWindow, mWindow, mSecondWindow}}});
arthurhungb89ccb02020-12-30 16:19:01 +08007068
7069 // Transfer touch focus to the drag window
Arthur Hung54745652022-04-20 07:17:41 +00007070 bool transferred =
7071 mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(),
Harry Cutts33476232023-01-30 19:57:29 +00007072 /*isDragDrop=*/true);
Arthur Hung54745652022-04-20 07:17:41 +00007073 if (transferred) {
7074 mWindow->consumeMotionCancel();
7075 mDragWindow->consumeMotionDown();
7076 }
7077 return transferred;
arthurhungb89ccb02020-12-30 16:19:01 +08007078 }
7079};
7080
7081TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007082 startDrag();
arthurhungb89ccb02020-12-30 16:19:01 +08007083
7084 // Move on window.
7085 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7086 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
7087 ADISPLAY_ID_DEFAULT, {50, 50}))
7088 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7089 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7090 mWindow->consumeDragEvent(false, 50, 50);
7091 mSecondWindow->assertNoEvents();
7092
7093 // Move to another window.
7094 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7095 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
7096 ADISPLAY_ID_DEFAULT, {150, 50}))
7097 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7098 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7099 mWindow->consumeDragEvent(true, 150, 50);
7100 mSecondWindow->consumeDragEvent(false, 50, 50);
7101
7102 // Move back to original window.
7103 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7104 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
7105 ADISPLAY_ID_DEFAULT, {50, 50}))
7106 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7107 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7108 mWindow->consumeDragEvent(false, 50, 50);
7109 mSecondWindow->consumeDragEvent(true, -50, 50);
7110
7111 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7112 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {50, 50}))
7113 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7114 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
7115 mWindow->assertNoEvents();
7116 mSecondWindow->assertNoEvents();
7117}
7118
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00007119TEST_F(InputDispatcherDragTests, DragEnterAndPointerDownPilfersPointers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007120 startDrag();
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00007121
7122 // No cancel event after drag start
7123 mSpyWindow->assertNoEvents();
7124
7125 const MotionEvent secondFingerDownEvent =
7126 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7127 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Harry Cutts33476232023-01-30 19:57:29 +00007128 .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
7129 .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(60).y(60))
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00007130 .build();
7131 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7132 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
7133 InputEventInjectionSync::WAIT_FOR_RESULT))
7134 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7135
7136 // Receives cancel for first pointer after next pointer down
7137 mSpyWindow->consumeMotionCancel();
7138 mSpyWindow->consumeMotionDown();
7139
7140 mSpyWindow->assertNoEvents();
7141}
7142
arthurhungf452d0b2021-01-06 00:19:52 +08007143TEST_F(InputDispatcherDragTests, DragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007144 startDrag();
arthurhungf452d0b2021-01-06 00:19:52 +08007145
7146 // Move on window.
7147 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7148 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
7149 ADISPLAY_ID_DEFAULT, {50, 50}))
7150 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7151 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7152 mWindow->consumeDragEvent(false, 50, 50);
7153 mSecondWindow->assertNoEvents();
7154
7155 // Move to another window.
7156 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7157 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
7158 ADISPLAY_ID_DEFAULT, {150, 50}))
7159 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7160 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7161 mWindow->consumeDragEvent(true, 150, 50);
7162 mSecondWindow->consumeDragEvent(false, 50, 50);
7163
7164 // drop to another window.
7165 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7166 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7167 {150, 50}))
7168 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7169 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
7170 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
7171 mWindow->assertNoEvents();
7172 mSecondWindow->assertNoEvents();
7173}
7174
arthurhung6d4bed92021-03-17 11:59:33 +08007175TEST_F(InputDispatcherDragTests, StylusDragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007176 startDrag(true, AINPUT_SOURCE_STYLUS);
arthurhung6d4bed92021-03-17 11:59:33 +08007177
7178 // Move on window and keep button pressed.
7179 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7180 injectMotionEvent(mDispatcher,
7181 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
7182 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
7183 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS)
7184 .x(50)
7185 .y(50))
7186 .build()))
7187 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7188 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7189 mWindow->consumeDragEvent(false, 50, 50);
7190 mSecondWindow->assertNoEvents();
7191
7192 // Move to another window and release button, expect to drop item.
7193 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7194 injectMotionEvent(mDispatcher,
7195 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
7196 .buttonState(0)
7197 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS)
7198 .x(150)
7199 .y(50))
7200 .build()))
7201 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7202 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7203 mWindow->assertNoEvents();
7204 mSecondWindow->assertNoEvents();
7205 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
7206
7207 // nothing to the window.
7208 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7209 injectMotionEvent(mDispatcher,
7210 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS)
7211 .buttonState(0)
7212 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS)
7213 .x(150)
7214 .y(50))
7215 .build()))
7216 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7217 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
7218 mWindow->assertNoEvents();
7219 mSecondWindow->assertNoEvents();
7220}
7221
Arthur Hung54745652022-04-20 07:17:41 +00007222TEST_F(InputDispatcherDragTests, DragAndDropOnInvalidWindow) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007223 startDrag();
Arthur Hung6d0571e2021-04-09 20:18:16 +08007224
7225 // Set second window invisible.
7226 mSecondWindow->setVisible(false);
7227 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}});
7228
7229 // Move on window.
7230 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7231 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
7232 ADISPLAY_ID_DEFAULT, {50, 50}))
7233 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7234 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7235 mWindow->consumeDragEvent(false, 50, 50);
7236 mSecondWindow->assertNoEvents();
7237
7238 // Move to another window.
7239 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7240 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
7241 ADISPLAY_ID_DEFAULT, {150, 50}))
7242 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7243 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7244 mWindow->consumeDragEvent(true, 150, 50);
7245 mSecondWindow->assertNoEvents();
7246
7247 // drop to another window.
7248 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7249 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7250 {150, 50}))
7251 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7252 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
7253 mFakePolicy->assertDropTargetEquals(nullptr);
7254 mWindow->assertNoEvents();
7255 mSecondWindow->assertNoEvents();
7256}
7257
Arthur Hung54745652022-04-20 07:17:41 +00007258TEST_F(InputDispatcherDragTests, NoDragAndDropWhenMultiFingers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007259 // Ensure window could track pointerIds if it didn't support split touch.
7260 mWindow->setPreventSplitting(true);
7261
Arthur Hung54745652022-04-20 07:17:41 +00007262 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7263 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7264 {50, 50}))
7265 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7266 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7267
7268 const MotionEvent secondFingerDownEvent =
7269 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7270 .displayId(ADISPLAY_ID_DEFAULT)
7271 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Harry Cutts33476232023-01-30 19:57:29 +00007272 .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
7273 .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(75).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00007274 .build();
7275 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7276 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
7277 InputEventInjectionSync::WAIT_FOR_RESULT))
7278 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Harry Cutts33476232023-01-30 19:57:29 +00007279 mWindow->consumeMotionPointerDown(/*pointerIndex=*/1);
Arthur Hung54745652022-04-20 07:17:41 +00007280
7281 // Should not perform drag and drop when window has multi fingers.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007282 ASSERT_FALSE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +00007283}
7284
7285TEST_F(InputDispatcherDragTests, DragAndDropWhenSplitTouch) {
7286 // First down on second window.
7287 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7288 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7289 {150, 50}))
7290 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7291
7292 mSecondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7293
7294 // Second down on first window.
7295 const MotionEvent secondFingerDownEvent =
7296 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7297 .displayId(ADISPLAY_ID_DEFAULT)
7298 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Harry Cutts33476232023-01-30 19:57:29 +00007299 .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50))
7300 .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00007301 .build();
7302 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7303 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
7304 InputEventInjectionSync::WAIT_FOR_RESULT))
7305 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7306 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7307
7308 // Perform drag and drop from first window.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007309 ASSERT_TRUE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +00007310
7311 // Move on window.
7312 const MotionEvent secondFingerMoveEvent =
7313 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
7314 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Harry Cutts33476232023-01-30 19:57:29 +00007315 .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50))
7316 .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00007317 .build();
7318 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7319 injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT,
7320 InputEventInjectionSync::WAIT_FOR_RESULT));
7321 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7322 mWindow->consumeDragEvent(false, 50, 50);
7323 mSecondWindow->consumeMotionMove();
7324
7325 // Release the drag pointer should perform drop.
7326 const MotionEvent secondFingerUpEvent =
7327 MotionEventBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
7328 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Harry Cutts33476232023-01-30 19:57:29 +00007329 .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50))
7330 .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00007331 .build();
7332 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7333 injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT,
7334 InputEventInjectionSync::WAIT_FOR_RESULT));
7335 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
7336 mFakePolicy->assertDropTargetEquals(mWindow->getToken());
7337 mWindow->assertNoEvents();
7338 mSecondWindow->consumeMotionMove();
7339}
7340
Arthur Hung3915c1f2022-05-31 07:17:17 +00007341TEST_F(InputDispatcherDragTests, DragAndDropWhenMultiDisplays) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007342 startDrag();
Arthur Hung3915c1f2022-05-31 07:17:17 +00007343
7344 // Update window of second display.
7345 sp<FakeWindowHandle> windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007346 sp<FakeWindowHandle>::make(mApp, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung3915c1f2022-05-31 07:17:17 +00007347 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
7348
7349 // Let second display has a touch state.
7350 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7351 injectMotionEvent(mDispatcher,
7352 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
7353 AINPUT_SOURCE_TOUCHSCREEN)
7354 .displayId(SECOND_DISPLAY_ID)
7355 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER)
7356 .x(100)
7357 .y(100))
7358 .build()));
7359 windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN,
Harry Cutts33476232023-01-30 19:57:29 +00007360 SECOND_DISPLAY_ID, /*expectedFlag=*/0);
Arthur Hung3915c1f2022-05-31 07:17:17 +00007361 // Update window again.
7362 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
7363
7364 // Move on window.
7365 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7366 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
7367 ADISPLAY_ID_DEFAULT, {50, 50}))
7368 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7369 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7370 mWindow->consumeDragEvent(false, 50, 50);
7371 mSecondWindow->assertNoEvents();
7372
7373 // Move to another window.
7374 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7375 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
7376 ADISPLAY_ID_DEFAULT, {150, 50}))
7377 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7378 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7379 mWindow->consumeDragEvent(true, 150, 50);
7380 mSecondWindow->consumeDragEvent(false, 50, 50);
7381
7382 // drop to another window.
7383 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7384 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7385 {150, 50}))
7386 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7387 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
7388 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
7389 mWindow->assertNoEvents();
7390 mSecondWindow->assertNoEvents();
7391}
7392
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007393TEST_F(InputDispatcherDragTests, MouseDragAndDrop) {
7394 startDrag(true, AINPUT_SOURCE_MOUSE);
7395 // Move on window.
7396 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7397 injectMotionEvent(mDispatcher,
7398 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
7399 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
7400 .pointer(PointerBuilder(MOUSE_POINTER_ID,
7401 AMOTION_EVENT_TOOL_TYPE_MOUSE)
7402 .x(50)
7403 .y(50))
7404 .build()))
7405 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7406 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7407 mWindow->consumeDragEvent(false, 50, 50);
7408 mSecondWindow->assertNoEvents();
7409
7410 // Move to another window.
7411 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7412 injectMotionEvent(mDispatcher,
7413 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
7414 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
7415 .pointer(PointerBuilder(MOUSE_POINTER_ID,
7416 AMOTION_EVENT_TOOL_TYPE_MOUSE)
7417 .x(150)
7418 .y(50))
7419 .build()))
7420 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7421 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7422 mWindow->consumeDragEvent(true, 150, 50);
7423 mSecondWindow->consumeDragEvent(false, 50, 50);
7424
7425 // drop to another window.
7426 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7427 injectMotionEvent(mDispatcher,
7428 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
7429 .buttonState(0)
7430 .pointer(PointerBuilder(MOUSE_POINTER_ID,
7431 AMOTION_EVENT_TOOL_TYPE_MOUSE)
7432 .x(150)
7433 .y(50))
7434 .build()))
7435 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7436 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
7437 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
7438 mWindow->assertNoEvents();
7439 mSecondWindow->assertNoEvents();
7440}
7441
Vishnu Nair062a8672021-09-03 16:07:44 -07007442class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {};
7443
7444TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) {
7445 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007446 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
7447 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007448 window->setDropInput(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07007449 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
7450 window->setFocusable(true);
7451 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
7452 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00007453 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07007454
7455 // With the flag set, window should not get any input
7456 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
7457 mDispatcher->notifyKey(&keyArgs);
7458 window->assertNoEvents();
7459
7460 NotifyMotionArgs motionArgs =
7461 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
7462 ADISPLAY_ID_DEFAULT);
7463 mDispatcher->notifyMotion(&motionArgs);
7464 window->assertNoEvents();
7465
7466 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007467 window->setDropInput(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07007468 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
7469
7470 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
7471 mDispatcher->notifyKey(&keyArgs);
7472 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
7473
7474 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
7475 ADISPLAY_ID_DEFAULT);
7476 mDispatcher->notifyMotion(&motionArgs);
7477 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7478 window->assertNoEvents();
7479}
7480
7481TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) {
7482 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
7483 std::make_shared<FakeApplicationHandle>();
7484 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007485 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
7486 ADISPLAY_ID_DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -07007487 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
7488 obscuringWindow->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007489 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07007490 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007491 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
7492 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007493 window->setDropInputIfObscured(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07007494 window->setOwnerInfo(222, 222);
7495 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
7496 window->setFocusable(true);
7497 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
7498 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00007499 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07007500
7501 // With the flag set, window should not get any input
7502 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
7503 mDispatcher->notifyKey(&keyArgs);
7504 window->assertNoEvents();
7505
7506 NotifyMotionArgs motionArgs =
7507 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
7508 ADISPLAY_ID_DEFAULT);
7509 mDispatcher->notifyMotion(&motionArgs);
7510 window->assertNoEvents();
7511
7512 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007513 window->setDropInputIfObscured(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07007514 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
7515
7516 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
7517 mDispatcher->notifyKey(&keyArgs);
7518 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
7519
7520 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
7521 ADISPLAY_ID_DEFAULT);
7522 mDispatcher->notifyMotion(&motionArgs);
7523 window->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
7524 window->assertNoEvents();
7525}
7526
7527TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) {
7528 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
7529 std::make_shared<FakeApplicationHandle>();
7530 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007531 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
7532 ADISPLAY_ID_DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -07007533 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
7534 obscuringWindow->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007535 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07007536 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007537 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
7538 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007539 window->setDropInputIfObscured(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07007540 window->setOwnerInfo(222, 222);
7541 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
7542 window->setFocusable(true);
7543 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
7544 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00007545 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07007546
7547 // With the flag set, window should not get any input
7548 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
7549 mDispatcher->notifyKey(&keyArgs);
7550 window->assertNoEvents();
7551
7552 NotifyMotionArgs motionArgs =
7553 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
7554 ADISPLAY_ID_DEFAULT);
7555 mDispatcher->notifyMotion(&motionArgs);
7556 window->assertNoEvents();
7557
7558 // When the window is no longer obscured because it went on top, it should get input
7559 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, obscuringWindow}}});
7560
7561 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
7562 mDispatcher->notifyKey(&keyArgs);
7563 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
7564
7565 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
7566 ADISPLAY_ID_DEFAULT);
7567 mDispatcher->notifyMotion(&motionArgs);
7568 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7569 window->assertNoEvents();
7570}
7571
Antonio Kantekf16f2832021-09-28 04:39:20 +00007572class InputDispatcherTouchModeChangedTests : public InputDispatcherTest {
7573protected:
7574 std::shared_ptr<FakeApplicationHandle> mApp;
Antonio Kantek15beb512022-06-13 22:35:41 +00007575 std::shared_ptr<FakeApplicationHandle> mSecondaryApp;
Antonio Kantekf16f2832021-09-28 04:39:20 +00007576 sp<FakeWindowHandle> mWindow;
7577 sp<FakeWindowHandle> mSecondWindow;
Antonio Kantek15beb512022-06-13 22:35:41 +00007578 sp<FakeWindowHandle> mThirdWindow;
Antonio Kantekf16f2832021-09-28 04:39:20 +00007579
7580 void SetUp() override {
7581 InputDispatcherTest::SetUp();
7582
7583 mApp = std::make_shared<FakeApplicationHandle>();
Antonio Kantek15beb512022-06-13 22:35:41 +00007584 mSecondaryApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007585 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00007586 mWindow->setFocusable(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00007587 setFocusedWindow(mWindow);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007588 mSecondWindow =
7589 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00007590 mSecondWindow->setFocusable(true);
Antonio Kantek15beb512022-06-13 22:35:41 +00007591 mThirdWindow =
7592 sp<FakeWindowHandle>::make(mSecondaryApp, mDispatcher,
7593 "TestWindow3_SecondaryDisplay", SECOND_DISPLAY_ID);
7594 mThirdWindow->setFocusable(true);
Antonio Kantekf16f2832021-09-28 04:39:20 +00007595
7596 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Antonio Kantek15beb512022-06-13 22:35:41 +00007597 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}},
7598 {SECOND_DISPLAY_ID, {mThirdWindow}}});
7599 mThirdWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
Antonio Kantekf16f2832021-09-28 04:39:20 +00007600 mWindow->consumeFocusEvent(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00007601
Antonio Kantek15beb512022-06-13 22:35:41 +00007602 // Set main display initial touch mode to InputDispatcher::kDefaultInTouchMode.
Prabir Pradhan5735a322022-04-11 17:23:34 +00007603 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, WINDOW_PID,
Harry Cutts33476232023-01-30 19:57:29 +00007604 WINDOW_UID, /*hasPermission=*/true, ADISPLAY_ID_DEFAULT)) {
Antonio Kantek48710e42022-03-24 14:19:30 -07007605 mWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
7606 mSecondWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +00007607 mThirdWindow->assertNoEvents();
7608 }
7609
7610 // Set secondary display initial touch mode to InputDispatcher::kDefaultInTouchMode.
7611 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, SECONDARY_WINDOW_PID,
Harry Cutts33476232023-01-30 19:57:29 +00007612 SECONDARY_WINDOW_UID, /*hasPermission=*/true,
Antonio Kantek15beb512022-06-13 22:35:41 +00007613 SECOND_DISPLAY_ID)) {
7614 mWindow->assertNoEvents();
7615 mSecondWindow->assertNoEvents();
7616 mThirdWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek48710e42022-03-24 14:19:30 -07007617 }
Antonio Kantekf16f2832021-09-28 04:39:20 +00007618 }
7619
Antonio Kantek15beb512022-06-13 22:35:41 +00007620 void changeAndVerifyTouchModeInMainDisplayOnly(bool inTouchMode, int32_t pid, int32_t uid,
7621 bool hasPermission) {
Antonio Kanteka042c022022-07-06 16:51:07 -07007622 ASSERT_TRUE(mDispatcher->setInTouchMode(inTouchMode, pid, uid, hasPermission,
7623 ADISPLAY_ID_DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +00007624 mWindow->consumeTouchModeEvent(inTouchMode);
7625 mSecondWindow->consumeTouchModeEvent(inTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +00007626 mThirdWindow->assertNoEvents();
Antonio Kantekf16f2832021-09-28 04:39:20 +00007627 }
7628};
7629
Antonio Kantek26defcf2022-02-08 01:12:27 +00007630TEST_F(InputDispatcherTouchModeChangedTests, FocusedWindowCanChangeTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -08007631 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek15beb512022-06-13 22:35:41 +00007632 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode,
7633 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00007634 /* hasPermission=*/false);
Antonio Kantekf16f2832021-09-28 04:39:20 +00007635}
7636
Antonio Kantek26defcf2022-02-08 01:12:27 +00007637TEST_F(InputDispatcherTouchModeChangedTests, NonFocusedWindowOwnerCannotChangeTouchMode) {
7638 const WindowInfo& windowInfo = *mWindow->getInfo();
7639 int32_t ownerPid = windowInfo.ownerPid;
7640 int32_t ownerUid = windowInfo.ownerUid;
7641 mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1);
7642 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, ownerPid,
Harry Cutts33476232023-01-30 19:57:29 +00007643 ownerUid, /*hasPermission=*/false,
Antonio Kanteka042c022022-07-06 16:51:07 -07007644 ADISPLAY_ID_DEFAULT));
Antonio Kantek26defcf2022-02-08 01:12:27 +00007645 mWindow->assertNoEvents();
7646 mSecondWindow->assertNoEvents();
7647}
7648
7649TEST_F(InputDispatcherTouchModeChangedTests, NonWindowOwnerMayChangeTouchModeOnPermissionGranted) {
7650 const WindowInfo& windowInfo = *mWindow->getInfo();
7651 int32_t ownerPid = windowInfo.ownerPid;
7652 int32_t ownerUid = windowInfo.ownerUid;
7653 mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1);
Antonio Kantek15beb512022-06-13 22:35:41 +00007654 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, ownerPid,
Harry Cutts33476232023-01-30 19:57:29 +00007655 ownerUid, /*hasPermission=*/true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00007656}
7657
Antonio Kantekf16f2832021-09-28 04:39:20 +00007658TEST_F(InputDispatcherTouchModeChangedTests, EventIsNotGeneratedIfNotChangingTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -08007659 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek26defcf2022-02-08 01:12:27 +00007660 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode,
7661 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00007662 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +00007663 mWindow->assertNoEvents();
7664 mSecondWindow->assertNoEvents();
7665}
7666
Antonio Kantek15beb512022-06-13 22:35:41 +00007667TEST_F(InputDispatcherTouchModeChangedTests, ChangeTouchOnSecondaryDisplayOnly) {
7668 const WindowInfo& windowInfo = *mThirdWindow->getInfo();
7669 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
7670 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00007671 /*hasPermission=*/true, SECOND_DISPLAY_ID));
Antonio Kantek15beb512022-06-13 22:35:41 +00007672 mWindow->assertNoEvents();
7673 mSecondWindow->assertNoEvents();
7674 mThirdWindow->consumeTouchModeEvent(!InputDispatcher::kDefaultInTouchMode);
7675}
7676
Antonio Kantek48710e42022-03-24 14:19:30 -07007677TEST_F(InputDispatcherTouchModeChangedTests, CanChangeTouchModeWhenOwningLastInteractedWindow) {
7678 // Interact with the window first.
7679 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
7680 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
7681 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
7682
7683 // Then remove focus.
7684 mWindow->setFocusable(false);
7685 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
7686
7687 // Assert that caller can switch touch mode by owning one of the last interacted window.
7688 const WindowInfo& windowInfo = *mWindow->getInfo();
7689 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
7690 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00007691 /*hasPermission=*/false, ADISPLAY_ID_DEFAULT));
Antonio Kantek48710e42022-03-24 14:19:30 -07007692}
7693
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007694class InputDispatcherSpyWindowTest : public InputDispatcherTest {
7695public:
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007696 sp<FakeWindowHandle> createSpy() {
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007697 std::shared_ptr<FakeApplicationHandle> application =
7698 std::make_shared<FakeApplicationHandle>();
7699 std::string name = "Fake Spy ";
7700 name += std::to_string(mSpyCount++);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007701 sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher,
7702 name.c_str(), ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007703 spy->setSpy(true);
Prabir Pradhan5c85e052021-12-22 02:27:12 -08007704 spy->setTrustedOverlay(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007705 return spy;
7706 }
7707
7708 sp<FakeWindowHandle> createForeground() {
7709 std::shared_ptr<FakeApplicationHandle> application =
7710 std::make_shared<FakeApplicationHandle>();
7711 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007712 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
7713 ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007714 window->setFocusable(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007715 return window;
7716 }
7717
7718private:
7719 int mSpyCount{0};
7720};
7721
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08007722using InputDispatcherSpyWindowDeathTest = InputDispatcherSpyWindowTest;
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007723/**
Prabir Pradhan5c85e052021-12-22 02:27:12 -08007724 * Adding a spy window that is not a trusted overlay causes Dispatcher to abort.
7725 */
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08007726TEST_F(InputDispatcherSpyWindowDeathTest, UntrustedSpy_AbortsDispatcher) {
7727 ScopedSilentDeath _silentDeath;
7728
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007729 auto spy = createSpy();
Prabir Pradhan5c85e052021-12-22 02:27:12 -08007730 spy->setTrustedOverlay(false);
7731 ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}}),
7732 ".* not a trusted overlay");
7733}
7734
7735/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007736 * Input injection into a display with a spy window but no foreground windows should succeed.
7737 */
7738TEST_F(InputDispatcherSpyWindowTest, NoForegroundWindow) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007739 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007740 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}});
7741
7742 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7743 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
7744 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7745 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7746}
7747
7748/**
7749 * Verify the order in which different input windows receive events. The touched foreground window
7750 * (if there is one) should always receive the event first. When there are multiple spy windows, the
7751 * spy windows will receive the event according to their Z-order, where the top-most spy window will
7752 * receive events before ones belows it.
7753 *
7754 * Here, we set up a scenario with four windows in the following Z order from the top:
7755 * spy1, spy2, window, spy3.
7756 * We then inject an event and verify that the foreground "window" receives it first, followed by
7757 * "spy1" and "spy2". The "spy3" does not receive the event because it is underneath the foreground
7758 * window.
7759 */
7760TEST_F(InputDispatcherSpyWindowTest, ReceivesInputInOrder) {
7761 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007762 auto spy1 = createSpy();
7763 auto spy2 = createSpy();
7764 auto spy3 = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007765 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window, spy3}}});
7766 const std::vector<sp<FakeWindowHandle>> channels{spy1, spy2, window, spy3};
7767 const size_t numChannels = channels.size();
7768
Michael Wright8e9a8562022-02-09 13:44:29 +00007769 base::unique_fd epollFd(epoll_create1(EPOLL_CLOEXEC));
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007770 if (!epollFd.ok()) {
7771 FAIL() << "Failed to create epoll fd";
7772 }
7773
7774 for (size_t i = 0; i < numChannels; i++) {
7775 struct epoll_event event = {.events = EPOLLIN, .data.u64 = i};
7776 if (epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, channels[i]->getChannelFd(), &event) < 0) {
7777 FAIL() << "Failed to add fd to epoll";
7778 }
7779 }
7780
7781 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7782 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
7783 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7784
7785 std::vector<size_t> eventOrder;
7786 std::vector<struct epoll_event> events(numChannels);
7787 for (;;) {
7788 const int nFds = epoll_wait(epollFd.get(), events.data(), static_cast<int>(numChannels),
7789 (100ms).count());
7790 if (nFds < 0) {
7791 FAIL() << "Failed to call epoll_wait";
7792 }
7793 if (nFds == 0) {
7794 break; // epoll_wait timed out
7795 }
7796 for (int i = 0; i < nFds; i++) {
Colin Cross5b799302022-10-18 21:52:41 -07007797 ASSERT_EQ(static_cast<uint32_t>(EPOLLIN), events[i].events);
Siarhei Vishniakou31977182022-09-30 08:51:23 -07007798 eventOrder.push_back(static_cast<size_t>(events[i].data.u64));
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007799 channels[i]->consumeMotionDown();
7800 }
7801 }
7802
7803 // Verify the order in which the events were received.
7804 EXPECT_EQ(3u, eventOrder.size());
7805 EXPECT_EQ(2u, eventOrder[0]); // index 2: window
7806 EXPECT_EQ(0u, eventOrder[1]); // index 0: spy1
7807 EXPECT_EQ(1u, eventOrder[2]); // index 1: spy2
7808}
7809
7810/**
7811 * A spy window using the NOT_TOUCHABLE flag does not receive events.
7812 */
7813TEST_F(InputDispatcherSpyWindowTest, NotTouchable) {
7814 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007815 auto spy = createSpy();
7816 spy->setTouchable(false);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007817 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
7818
7819 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7820 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
7821 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7822 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7823 spy->assertNoEvents();
7824}
7825
7826/**
7827 * A spy window will only receive gestures that originate within its touchable region. Gestures that
7828 * have their ACTION_DOWN outside of the touchable region of the spy window will not be dispatched
7829 * to the window.
7830 */
7831TEST_F(InputDispatcherSpyWindowTest, TouchableRegion) {
7832 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007833 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007834 spy->setTouchableRegion(Region{{0, 0, 20, 20}});
7835 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
7836
7837 // Inject an event outside the spy window's touchable region.
7838 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7839 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
7840 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7841 window->consumeMotionDown();
7842 spy->assertNoEvents();
7843 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7844 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
7845 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7846 window->consumeMotionUp();
7847 spy->assertNoEvents();
7848
7849 // Inject an event inside the spy window's touchable region.
7850 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7851 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7852 {5, 10}))
7853 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7854 window->consumeMotionDown();
7855 spy->consumeMotionDown();
7856}
7857
7858/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007859 * A spy window can listen for touches outside its touchable region using the WATCH_OUTSIDE_TOUCHES
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08007860 * flag, but it will get zero-ed out coordinates if the foreground has a different owner.
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007861 */
7862TEST_F(InputDispatcherSpyWindowTest, WatchOutsideTouches) {
7863 auto window = createForeground();
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08007864 window->setOwnerInfo(12, 34);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007865 auto spy = createSpy();
7866 spy->setWatchOutsideTouch(true);
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08007867 spy->setOwnerInfo(56, 78);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007868 spy->setFrame(Rect{0, 0, 20, 20});
7869 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
7870
7871 // Inject an event outside the spy window's frame and touchable region.
7872 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08007873 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7874 {100, 200}))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007875 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7876 window->consumeMotionDown();
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08007877 spy->consumeMotionOutsideWithZeroedCoords();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007878}
7879
7880/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007881 * Even when a spy window spans over multiple foreground windows, the spy should receive all
7882 * pointers that are down within its bounds.
7883 */
7884TEST_F(InputDispatcherSpyWindowTest, ReceivesMultiplePointers) {
7885 auto windowLeft = createForeground();
7886 windowLeft->setFrame({0, 0, 100, 200});
7887 auto windowRight = createForeground();
7888 windowRight->setFrame({100, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007889 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007890 spy->setFrame({0, 0, 200, 200});
7891 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, windowLeft, windowRight}}});
7892
7893 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7894 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7895 {50, 50}))
7896 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7897 windowLeft->consumeMotionDown();
7898 spy->consumeMotionDown();
7899
7900 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08007901 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007902 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Harry Cutts33476232023-01-30 19:57:29 +00007903 .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
7904 .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007905 .build();
7906 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7907 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
7908 InputEventInjectionSync::WAIT_FOR_RESULT))
7909 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7910 windowRight->consumeMotionDown();
Harry Cutts33476232023-01-30 19:57:29 +00007911 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007912}
7913
7914/**
7915 * When the first pointer lands outside the spy window and the second pointer lands inside it, the
7916 * the spy should receive the second pointer with ACTION_DOWN.
7917 */
7918TEST_F(InputDispatcherSpyWindowTest, ReceivesSecondPointerAsDown) {
7919 auto window = createForeground();
7920 window->setFrame({0, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007921 auto spyRight = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007922 spyRight->setFrame({100, 0, 200, 200});
7923 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyRight, window}}});
7924
7925 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7926 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7927 {50, 50}))
7928 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7929 window->consumeMotionDown();
7930 spyRight->assertNoEvents();
7931
7932 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08007933 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007934 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Harry Cutts33476232023-01-30 19:57:29 +00007935 .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
7936 .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007937 .build();
7938 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7939 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
7940 InputEventInjectionSync::WAIT_FOR_RESULT))
7941 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Harry Cutts33476232023-01-30 19:57:29 +00007942 window->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007943 spyRight->consumeMotionDown();
7944}
7945
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007946/**
7947 * The spy window should not be able to affect whether or not touches are split. Only the foreground
7948 * windows should be allowed to control split touch.
7949 */
7950TEST_F(InputDispatcherSpyWindowTest, SplitIfNoForegroundWindowTouched) {
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08007951 // This spy window prevents touch splitting. However, we still expect to split touches
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007952 // because a foreground window has not disabled splitting.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007953 auto spy = createSpy();
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08007954 spy->setPreventSplitting(true);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007955
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007956 auto window = createForeground();
7957 window->setFrame(Rect(0, 0, 100, 100));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007958
7959 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
7960
7961 // First finger down, no window touched.
7962 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7963 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7964 {100, 200}))
7965 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7966 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7967 window->assertNoEvents();
7968
7969 // Second finger down on window, the window should receive touch down.
7970 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08007971 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007972 .displayId(ADISPLAY_ID_DEFAULT)
7973 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Harry Cutts33476232023-01-30 19:57:29 +00007974 .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(100).y(200))
7975 .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007976 .build();
7977 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7978 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
7979 InputEventInjectionSync::WAIT_FOR_RESULT))
7980 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7981
7982 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Harry Cutts33476232023-01-30 19:57:29 +00007983 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007984}
7985
7986/**
7987 * A spy window will usually be implemented as an un-focusable window. Verify that these windows
7988 * do not receive key events.
7989 */
7990TEST_F(InputDispatcherSpyWindowTest, UnfocusableSpyDoesNotReceiveKeyEvents) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007991 auto spy = createSpy();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007992 spy->setFocusable(false);
7993
7994 auto window = createForeground();
7995 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
7996 setFocusedWindow(window);
7997 window->consumeFocusEvent(true);
7998
7999 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
8000 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
8001 window->consumeKeyDown(ADISPLAY_ID_NONE);
8002
8003 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
8004 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
8005 window->consumeKeyUp(ADISPLAY_ID_NONE);
8006
8007 spy->assertNoEvents();
8008}
8009
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00008010using InputDispatcherPilferPointersTest = InputDispatcherSpyWindowTest;
8011
8012/**
8013 * A spy window can pilfer pointers. When this happens, touch gestures used by the spy window that
8014 * are currently sent to any other windows - including other spy windows - will also be cancelled.
8015 */
8016TEST_F(InputDispatcherPilferPointersTest, PilferPointers) {
8017 auto window = createForeground();
8018 auto spy1 = createSpy();
8019 auto spy2 = createSpy();
8020 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window}}});
8021
8022 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8023 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8024 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8025 window->consumeMotionDown();
8026 spy1->consumeMotionDown();
8027 spy2->consumeMotionDown();
8028
8029 // Pilfer pointers from the second spy window.
8030 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy2->getToken()));
8031 spy2->assertNoEvents();
8032 spy1->consumeMotionCancel();
8033 window->consumeMotionCancel();
8034
8035 // The rest of the gesture should only be sent to the second spy window.
8036 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8037 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8038 ADISPLAY_ID_DEFAULT))
8039 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8040 spy2->consumeMotionMove();
8041 spy1->assertNoEvents();
8042 window->assertNoEvents();
8043}
8044
8045/**
8046 * A spy window can pilfer pointers for a gesture even after the foreground window has been removed
8047 * in the middle of the gesture.
8048 */
8049TEST_F(InputDispatcherPilferPointersTest, CanPilferAfterWindowIsRemovedMidStream) {
8050 auto window = createForeground();
8051 auto spy = createSpy();
8052 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8053
8054 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8055 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8056 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8057 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8058 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8059
8060 window->releaseChannel();
8061
8062 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
8063
8064 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8065 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8066 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8067 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8068}
8069
8070/**
8071 * After a spy window pilfers pointers, new pointers that go down in its bounds should be sent to
8072 * the spy, but not to any other windows.
8073 */
8074TEST_F(InputDispatcherPilferPointersTest, ContinuesToReceiveGestureAfterPilfer) {
8075 auto spy = createSpy();
8076 auto window = createForeground();
8077
8078 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8079
8080 // First finger down on the window and the spy.
8081 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8082 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8083 {100, 200}))
8084 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8085 spy->consumeMotionDown();
8086 window->consumeMotionDown();
8087
8088 // Spy window pilfers the pointers.
8089 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
8090 window->consumeMotionCancel();
8091
8092 // Second finger down on the window and spy, but the window should not receive the pointer down.
8093 const MotionEvent secondFingerDownEvent =
8094 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8095 .displayId(ADISPLAY_ID_DEFAULT)
8096 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Harry Cutts33476232023-01-30 19:57:29 +00008097 .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(100).y(200))
8098 .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00008099 .build();
8100 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8101 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8102 InputEventInjectionSync::WAIT_FOR_RESULT))
8103 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8104
Harry Cutts33476232023-01-30 19:57:29 +00008105 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00008106
8107 // Third finger goes down outside all windows, so injection should fail.
8108 const MotionEvent thirdFingerDownEvent =
8109 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8110 .displayId(ADISPLAY_ID_DEFAULT)
8111 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Harry Cutts33476232023-01-30 19:57:29 +00008112 .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(100).y(200))
8113 .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
8114 .pointer(PointerBuilder(/*id=*/2, AMOTION_EVENT_TOOL_TYPE_FINGER).x(-5).y(-5))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00008115 .build();
8116 ASSERT_EQ(InputEventInjectionResult::FAILED,
8117 injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
8118 InputEventInjectionSync::WAIT_FOR_RESULT))
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08008119 << "Inject motion event should return InputEventInjectionResult::FAILED";
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00008120
8121 spy->assertNoEvents();
8122 window->assertNoEvents();
8123}
8124
8125/**
8126 * After a spy window pilfers pointers, only the pointers used by the spy should be canceled
8127 */
8128TEST_F(InputDispatcherPilferPointersTest, PartiallyPilferRequiredPointers) {
8129 auto spy = createSpy();
8130 spy->setFrame(Rect(0, 0, 100, 100));
8131 auto window = createForeground();
8132 window->setFrame(Rect(0, 0, 200, 200));
8133
8134 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8135
8136 // First finger down on the window only
8137 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8138 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8139 {150, 150}))
8140 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8141 window->consumeMotionDown();
8142
8143 // Second finger down on the spy and window
8144 const MotionEvent secondFingerDownEvent =
8145 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8146 .displayId(ADISPLAY_ID_DEFAULT)
8147 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Harry Cutts33476232023-01-30 19:57:29 +00008148 .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(150))
8149 .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(10).y(10))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00008150 .build();
8151 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8152 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8153 InputEventInjectionSync::WAIT_FOR_RESULT))
8154 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8155 spy->consumeMotionDown();
8156 window->consumeMotionPointerDown(1);
8157
8158 // Third finger down on the spy and window
8159 const MotionEvent thirdFingerDownEvent =
8160 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8161 .displayId(ADISPLAY_ID_DEFAULT)
8162 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Harry Cutts33476232023-01-30 19:57:29 +00008163 .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(150))
8164 .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(10).y(10))
8165 .pointer(PointerBuilder(/*id=*/2, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00008166 .build();
8167 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8168 injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
8169 InputEventInjectionSync::WAIT_FOR_RESULT))
8170 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8171 spy->consumeMotionPointerDown(1);
8172 window->consumeMotionPointerDown(2);
8173
8174 // Spy window pilfers the pointers.
8175 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
8176 window->consumeMotionPointerUp(/* idx */ 2, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
8177 window->consumeMotionPointerUp(/* idx */ 1, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
8178
8179 spy->assertNoEvents();
8180 window->assertNoEvents();
8181}
8182
8183/**
8184 * After a spy window pilfers pointers, all pilfered pointers that have already been dispatched to
8185 * other windows should be canceled. If this results in the cancellation of all pointers for some
8186 * window, then that window should receive ACTION_CANCEL.
8187 */
8188TEST_F(InputDispatcherPilferPointersTest, PilferAllRequiredPointers) {
8189 auto spy = createSpy();
8190 spy->setFrame(Rect(0, 0, 100, 100));
8191 auto window = createForeground();
8192 window->setFrame(Rect(0, 0, 200, 200));
8193
8194 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8195
8196 // First finger down on both spy and window
8197 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8198 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8199 {10, 10}))
8200 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8201 window->consumeMotionDown();
8202 spy->consumeMotionDown();
8203
8204 // Second finger down on the spy and window
8205 const MotionEvent secondFingerDownEvent =
8206 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8207 .displayId(ADISPLAY_ID_DEFAULT)
8208 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Harry Cutts33476232023-01-30 19:57:29 +00008209 .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(10).y(10))
8210 .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00008211 .build();
8212 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8213 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8214 InputEventInjectionSync::WAIT_FOR_RESULT))
8215 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8216 spy->consumeMotionPointerDown(1);
8217 window->consumeMotionPointerDown(1);
8218
8219 // Spy window pilfers the pointers.
8220 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
8221 window->consumeMotionCancel();
8222
8223 spy->assertNoEvents();
8224 window->assertNoEvents();
8225}
8226
8227/**
8228 * After a spy window pilfers pointers, new pointers that are not touching the spy window can still
8229 * be sent to other windows
8230 */
8231TEST_F(InputDispatcherPilferPointersTest, CanReceivePointersAfterPilfer) {
8232 auto spy = createSpy();
8233 spy->setFrame(Rect(0, 0, 100, 100));
8234 auto window = createForeground();
8235 window->setFrame(Rect(0, 0, 200, 200));
8236
8237 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8238
8239 // First finger down on both window and spy
8240 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8241 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8242 {10, 10}))
8243 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8244 window->consumeMotionDown();
8245 spy->consumeMotionDown();
8246
8247 // Spy window pilfers the pointers.
8248 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
8249 window->consumeMotionCancel();
8250
8251 // Second finger down on the window only
8252 const MotionEvent secondFingerDownEvent =
8253 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8254 .displayId(ADISPLAY_ID_DEFAULT)
8255 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Harry Cutts33476232023-01-30 19:57:29 +00008256 .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(10).y(10))
8257 .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(150))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00008258 .build();
8259 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8260 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8261 InputEventInjectionSync::WAIT_FOR_RESULT))
8262 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8263 window->consumeMotionDown();
8264 window->assertNoEvents();
8265
8266 // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line
8267 spy->consumeMotionMove();
8268 spy->assertNoEvents();
8269}
8270
Prabir Pradhand65552b2021-10-07 11:23:50 -07008271class InputDispatcherStylusInterceptorTest : public InputDispatcherTest {
8272public:
8273 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupStylusOverlayScenario() {
8274 std::shared_ptr<FakeApplicationHandle> overlayApplication =
8275 std::make_shared<FakeApplicationHandle>();
8276 sp<FakeWindowHandle> overlay =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008277 sp<FakeWindowHandle>::make(overlayApplication, mDispatcher,
8278 "Stylus interceptor window", ADISPLAY_ID_DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -07008279 overlay->setFocusable(false);
8280 overlay->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008281 overlay->setTouchable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008282 overlay->setInterceptsStylus(true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07008283 overlay->setTrustedOverlay(true);
8284
8285 std::shared_ptr<FakeApplicationHandle> application =
8286 std::make_shared<FakeApplicationHandle>();
8287 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008288 sp<FakeWindowHandle>::make(application, mDispatcher, "Application window",
8289 ADISPLAY_ID_DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -07008290 window->setFocusable(true);
8291 window->setOwnerInfo(222, 222);
Prabir Pradhand65552b2021-10-07 11:23:50 -07008292
8293 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
8294 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
8295 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00008296 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07008297 return {std::move(overlay), std::move(window)};
8298 }
8299
8300 void sendFingerEvent(int32_t action) {
8301 NotifyMotionArgs motionArgs =
8302 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
8303 ADISPLAY_ID_DEFAULT, {PointF{20, 20}});
8304 mDispatcher->notifyMotion(&motionArgs);
8305 }
8306
8307 void sendStylusEvent(int32_t action) {
8308 NotifyMotionArgs motionArgs =
8309 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
8310 ADISPLAY_ID_DEFAULT, {PointF{30, 40}});
8311 motionArgs.pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS;
8312 mDispatcher->notifyMotion(&motionArgs);
8313 }
8314};
8315
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08008316using InputDispatcherStylusInterceptorDeathTest = InputDispatcherStylusInterceptorTest;
8317
8318TEST_F(InputDispatcherStylusInterceptorDeathTest, UntrustedOverlay_AbortsDispatcher) {
8319 ScopedSilentDeath _silentDeath;
8320
Prabir Pradhand65552b2021-10-07 11:23:50 -07008321 auto [overlay, window] = setupStylusOverlayScenario();
8322 overlay->setTrustedOverlay(false);
8323 // Configuring an untrusted overlay as a stylus interceptor should cause Dispatcher to abort.
8324 ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}),
8325 ".* not a trusted overlay");
8326}
8327
8328TEST_F(InputDispatcherStylusInterceptorTest, ConsmesOnlyStylusEvents) {
8329 auto [overlay, window] = setupStylusOverlayScenario();
8330 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
8331
8332 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
8333 overlay->consumeMotionDown();
8334 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
8335 overlay->consumeMotionUp();
8336
8337 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
8338 window->consumeMotionDown();
8339 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
8340 window->consumeMotionUp();
8341
8342 overlay->assertNoEvents();
8343 window->assertNoEvents();
8344}
8345
8346TEST_F(InputDispatcherStylusInterceptorTest, SpyWindowStylusInterceptor) {
8347 auto [overlay, window] = setupStylusOverlayScenario();
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008348 overlay->setSpy(true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07008349 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
8350
8351 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
8352 overlay->consumeMotionDown();
8353 window->consumeMotionDown();
8354 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
8355 overlay->consumeMotionUp();
8356 window->consumeMotionUp();
8357
8358 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
8359 window->consumeMotionDown();
8360 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
8361 window->consumeMotionUp();
8362
8363 overlay->assertNoEvents();
8364 window->assertNoEvents();
8365}
8366
Prabir Pradhan6dfbf262022-03-14 15:24:30 +00008367/**
8368 * Set up a scenario to test the behavior used by the stylus handwriting detection feature.
8369 * The scenario is as follows:
8370 * - The stylus interceptor overlay is configured as a spy window.
8371 * - The stylus interceptor spy receives the start of a new stylus gesture.
8372 * - It pilfers pointers and then configures itself to no longer be a spy.
8373 * - The stylus interceptor continues to receive the rest of the gesture.
8374 */
8375TEST_F(InputDispatcherStylusInterceptorTest, StylusHandwritingScenario) {
8376 auto [overlay, window] = setupStylusOverlayScenario();
8377 overlay->setSpy(true);
8378 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
8379
8380 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
8381 overlay->consumeMotionDown();
8382 window->consumeMotionDown();
8383
8384 // The interceptor pilfers the pointers.
8385 EXPECT_EQ(OK, mDispatcher->pilferPointers(overlay->getToken()));
8386 window->consumeMotionCancel();
8387
8388 // The interceptor configures itself so that it is no longer a spy.
8389 overlay->setSpy(false);
8390 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
8391
8392 // It continues to receive the rest of the stylus gesture.
8393 sendStylusEvent(AMOTION_EVENT_ACTION_MOVE);
8394 overlay->consumeMotionMove();
8395 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
8396 overlay->consumeMotionUp();
8397
8398 window->assertNoEvents();
8399}
8400
Prabir Pradhan5735a322022-04-11 17:23:34 +00008401struct User {
8402 int32_t mPid;
8403 int32_t mUid;
8404 uint32_t mPolicyFlags{DEFAULT_POLICY_FLAGS};
8405 std::unique_ptr<InputDispatcher>& mDispatcher;
8406
8407 User(std::unique_ptr<InputDispatcher>& dispatcher, int32_t pid, int32_t uid)
8408 : mPid(pid), mUid(uid), mDispatcher(dispatcher) {}
8409
8410 InputEventInjectionResult injectTargetedMotion(int32_t action) const {
8411 return injectMotionEvent(mDispatcher, action, AINPUT_SOURCE_TOUCHSCREEN,
8412 ADISPLAY_ID_DEFAULT, {100, 200},
8413 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
8414 AMOTION_EVENT_INVALID_CURSOR_POSITION},
8415 INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT,
8416 systemTime(SYSTEM_TIME_MONOTONIC), {mUid}, mPolicyFlags);
8417 }
8418
8419 InputEventInjectionResult injectTargetedKey(int32_t action) const {
Harry Cutts33476232023-01-30 19:57:29 +00008420 return inputdispatcher::injectKey(mDispatcher, action, /*repeatCount=*/0, ADISPLAY_ID_NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +00008421 InputEventInjectionSync::WAIT_FOR_RESULT,
Harry Cutts33476232023-01-30 19:57:29 +00008422 INJECT_EVENT_TIMEOUT, /*allowKeyRepeat=*/false, {mUid},
Prabir Pradhan5735a322022-04-11 17:23:34 +00008423 mPolicyFlags);
8424 }
8425
8426 sp<FakeWindowHandle> createWindow() const {
8427 std::shared_ptr<FakeApplicationHandle> overlayApplication =
8428 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008429 sp<FakeWindowHandle> window =
8430 sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, "Owned Window",
8431 ADISPLAY_ID_DEFAULT);
Prabir Pradhan5735a322022-04-11 17:23:34 +00008432 window->setOwnerInfo(mPid, mUid);
8433 return window;
8434 }
8435};
8436
8437using InputDispatcherTargetedInjectionTest = InputDispatcherTest;
8438
8439TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedWindow) {
8440 auto owner = User(mDispatcher, 10, 11);
8441 auto window = owner.createWindow();
8442 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
8443
8444 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
8445 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
8446 window->consumeMotionDown();
8447
8448 setFocusedWindow(window);
8449 window->consumeFocusEvent(true);
8450
8451 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
8452 owner.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
8453 window->consumeKeyDown(ADISPLAY_ID_NONE);
8454}
8455
8456TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedWindow) {
8457 auto owner = User(mDispatcher, 10, 11);
8458 auto window = owner.createWindow();
8459 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
8460
8461 auto rando = User(mDispatcher, 20, 21);
8462 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
8463 rando.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
8464
8465 setFocusedWindow(window);
8466 window->consumeFocusEvent(true);
8467
8468 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
8469 rando.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
8470 window->assertNoEvents();
8471}
8472
8473TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedSpyWindow) {
8474 auto owner = User(mDispatcher, 10, 11);
8475 auto window = owner.createWindow();
8476 auto spy = owner.createWindow();
8477 spy->setSpy(true);
8478 spy->setTrustedOverlay(true);
8479 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8480
8481 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
8482 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
8483 spy->consumeMotionDown();
8484 window->consumeMotionDown();
8485}
8486
8487TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedSpyWindow) {
8488 auto owner = User(mDispatcher, 10, 11);
8489 auto window = owner.createWindow();
8490
8491 auto rando = User(mDispatcher, 20, 21);
8492 auto randosSpy = rando.createWindow();
8493 randosSpy->setSpy(true);
8494 randosSpy->setTrustedOverlay(true);
8495 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}});
8496
8497 // The event is targeted at owner's window, so injection should succeed, but the spy should
8498 // not receive the event.
8499 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
8500 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
8501 randosSpy->assertNoEvents();
8502 window->consumeMotionDown();
8503}
8504
8505TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoAnyWindowWhenNotTargeting) {
8506 auto owner = User(mDispatcher, 10, 11);
8507 auto window = owner.createWindow();
8508
8509 auto rando = User(mDispatcher, 20, 21);
8510 auto randosSpy = rando.createWindow();
8511 randosSpy->setSpy(true);
8512 randosSpy->setTrustedOverlay(true);
8513 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}});
8514
8515 // A user that has injection permission can inject into any window.
8516 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
8517 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
8518 ADISPLAY_ID_DEFAULT));
8519 randosSpy->consumeMotionDown();
8520 window->consumeMotionDown();
8521
8522 setFocusedWindow(randosSpy);
8523 randosSpy->consumeFocusEvent(true);
8524
8525 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher));
8526 randosSpy->consumeKeyDown(ADISPLAY_ID_NONE);
8527 window->assertNoEvents();
8528}
8529
8530TEST_F(InputDispatcherTargetedInjectionTest, CanGenerateActionOutsideToOtherUids) {
8531 auto owner = User(mDispatcher, 10, 11);
8532 auto window = owner.createWindow();
8533
8534 auto rando = User(mDispatcher, 20, 21);
8535 auto randosWindow = rando.createWindow();
8536 randosWindow->setFrame(Rect{-10, -10, -5, -5});
8537 randosWindow->setWatchOutsideTouch(true);
8538 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosWindow, window}}});
8539
8540 // We allow generation of ACTION_OUTSIDE events into windows owned by different uids.
8541 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
8542 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
8543 window->consumeMotionDown();
8544 randosWindow->consumeMotionOutside();
8545}
8546
Garfield Tane84e6f92019-08-29 17:28:41 -07008547} // namespace android::inputdispatcher