blob: 2035fa1de298b7bb2c3c2c6839e95886469d0ce9 [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 Vishniakou2e3e4432023-02-09 18:34:11 -08002271 * Start hovering with a stylus device, and then tap with a touch device. Ensure no crash occurs.
2272 * While the touch is down, new hover events from the stylus device should be ignored. After the
2273 * touch is gone, stylus hovering should start working again.
2274 */
2275TEST_F(InputDispatcherTest, StylusHoverAndTouchTap) {
2276 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2277 sp<FakeWindowHandle> window =
2278 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2279 window->setFrame(Rect(0, 0, 200, 200));
2280
2281 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2282
2283 const int32_t stylusDeviceId = 5;
2284 const int32_t touchDeviceId = 4;
2285 // Start hovering with stylus
2286 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2287 injectMotionEvent(mDispatcher,
2288 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
2289 AINPUT_SOURCE_STYLUS)
2290 .deviceId(stylusDeviceId)
2291 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS)
2292 .x(50)
2293 .y(50))
2294 .build()));
2295 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2296
2297 // Finger down on the window
2298 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2299 injectMotionEvent(mDispatcher,
2300 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2301 AINPUT_SOURCE_TOUCHSCREEN)
2302 .deviceId(touchDeviceId)
2303 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER)
2304 .x(100)
2305 .y(100))
2306 .build()));
2307 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2308 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2309
2310 // Try to continue hovering with stylus. Since we are already down, injection should fail
2311 ASSERT_EQ(InputEventInjectionResult::FAILED,
2312 injectMotionEvent(mDispatcher,
2313 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2314 AINPUT_SOURCE_STYLUS)
2315 .deviceId(stylusDeviceId)
2316 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS)
2317 .x(50)
2318 .y(50))
2319 .build()));
2320 // No event should be sent. This event should be ignored because a pointer from another device
2321 // is already down.
2322
2323 // Lift up the finger
2324 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2325 injectMotionEvent(mDispatcher,
2326 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2327 AINPUT_SOURCE_TOUCHSCREEN)
2328 .deviceId(touchDeviceId)
2329 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER)
2330 .x(100)
2331 .y(100))
2332 .build()));
2333 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2334
2335 // Now that the touch is gone, stylus hovering should start working again
2336 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2337 injectMotionEvent(mDispatcher,
2338 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2339 AINPUT_SOURCE_STYLUS)
2340 .deviceId(stylusDeviceId)
2341 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS)
2342 .x(50)
2343 .y(50))
2344 .build()));
2345 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2346 // No more events
2347 window->assertNoEvents();
2348}
2349
2350/**
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002351 * On the display, have a single window, and also an area where there's no window.
2352 * First pointer touches the "no window" area of the screen. Second pointer touches the window.
2353 * Make sure that the window receives the second pointer, and first pointer is simply ignored.
2354 */
2355TEST_F(InputDispatcherTest, SplitWorksWhenEmptyAreaIsTouched) {
2356 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2357 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002358 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002359
2360 mDispatcher->setInputWindows({{DISPLAY_ID, {window}}});
2361 NotifyMotionArgs args;
2362
2363 // Touch down on the empty space
2364 mDispatcher->notifyMotion(&(args = generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{-1, -1}})));
2365
2366 mDispatcher->waitForIdle();
2367 window->assertNoEvents();
2368
2369 // Now touch down on the window with another pointer
2370 mDispatcher->notifyMotion(&(args = generateTouchArgs(POINTER_1_DOWN, {{-1, -1}, {10, 10}})));
2371 mDispatcher->waitForIdle();
2372 window->consumeMotionDown();
2373}
2374
2375/**
2376 * Same test as above, but instead of touching the empty space, the first touch goes to
2377 * non-touchable window.
2378 */
2379TEST_F(InputDispatcherTest, SplitWorksWhenNonTouchableWindowIsTouched) {
2380 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2381 sp<FakeWindowHandle> window1 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002382 sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002383 window1->setTouchableRegion(Region{{0, 0, 100, 100}});
2384 window1->setTouchable(false);
2385 sp<FakeWindowHandle> window2 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002386 sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002387 window2->setTouchableRegion(Region{{100, 0, 200, 100}});
2388
2389 mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}});
2390
2391 NotifyMotionArgs args;
2392 // Touch down on the non-touchable window
2393 mDispatcher->notifyMotion(&(args = generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}})));
2394
2395 mDispatcher->waitForIdle();
2396 window1->assertNoEvents();
2397 window2->assertNoEvents();
2398
2399 // Now touch down on the window with another pointer
2400 mDispatcher->notifyMotion(&(args = generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}})));
2401 mDispatcher->waitForIdle();
2402 window2->consumeMotionDown();
2403}
2404
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002405/**
2406 * When splitting touch events the downTime should be adjusted such that the downTime corresponds
2407 * to the event time of the first ACTION_DOWN sent to the particular window.
2408 */
2409TEST_F(InputDispatcherTest, SplitTouchesSendCorrectActionDownTime) {
2410 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2411 sp<FakeWindowHandle> window1 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002412 sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002413 window1->setTouchableRegion(Region{{0, 0, 100, 100}});
2414 sp<FakeWindowHandle> window2 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002415 sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002416 window2->setTouchableRegion(Region{{100, 0, 200, 100}});
2417
2418 mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}});
2419
2420 NotifyMotionArgs args;
2421 // Touch down on the first window
2422 mDispatcher->notifyMotion(&(args = generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}})));
2423
2424 mDispatcher->waitForIdle();
2425 InputEvent* inputEvent1 = window1->consume();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002426 ASSERT_NE(inputEvent1, nullptr);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002427 window2->assertNoEvents();
2428 MotionEvent& motionEvent1 = static_cast<MotionEvent&>(*inputEvent1);
2429 nsecs_t downTimeForWindow1 = motionEvent1.getDownTime();
2430 ASSERT_EQ(motionEvent1.getDownTime(), motionEvent1.getEventTime());
2431
2432 // Now touch down on the window with another pointer
2433 mDispatcher->notifyMotion(&(args = generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}})));
2434 mDispatcher->waitForIdle();
2435 InputEvent* inputEvent2 = window2->consume();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002436 ASSERT_NE(inputEvent2, nullptr);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002437 MotionEvent& motionEvent2 = static_cast<MotionEvent&>(*inputEvent2);
2438 nsecs_t downTimeForWindow2 = motionEvent2.getDownTime();
2439 ASSERT_NE(downTimeForWindow1, downTimeForWindow2);
2440 ASSERT_EQ(motionEvent2.getDownTime(), motionEvent2.getEventTime());
2441
2442 // Now move the pointer on the second window
2443 mDispatcher->notifyMotion(
2444 &(args = generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{50, 50}, {151, 51}})));
2445 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002446 window2->consumeMotionEvent(WithDownTime(downTimeForWindow2));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002447
2448 // Now add new touch down on the second window
2449 mDispatcher->notifyMotion(
2450 &(args = generateTouchArgs(POINTER_2_DOWN, {{50, 50}, {151, 51}, {150, 50}})));
2451 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002452 window2->consumeMotionEvent(WithDownTime(downTimeForWindow2));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002453
2454 // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line
2455 window1->consumeMotionMove();
2456 window1->assertNoEvents();
2457
2458 // Now move the pointer on the first window
2459 mDispatcher->notifyMotion(
2460 &(args = generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{51, 51}, {151, 51}})));
2461 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002462 window1->consumeMotionEvent(WithDownTime(downTimeForWindow1));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002463
2464 mDispatcher->notifyMotion(&(
2465 args = generateTouchArgs(POINTER_3_DOWN, {{51, 51}, {151, 51}, {150, 50}, {50, 50}})));
2466 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002467 window1->consumeMotionEvent(WithDownTime(downTimeForWindow1));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002468}
2469
Garfield Tandf26e862020-07-01 20:18:19 -07002470TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) {
Chris Yea209fde2020-07-22 13:54:51 -07002471 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07002472 sp<FakeWindowHandle> windowLeft =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002473 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07002474 windowLeft->setFrame(Rect(0, 0, 600, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07002475 sp<FakeWindowHandle> windowRight =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002476 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07002477 windowRight->setFrame(Rect(600, 0, 1200, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07002478
2479 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2480
2481 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
2482
2483 // Start cursor position in right window so that we can move the cursor to left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002484 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002485 injectMotionEvent(mDispatcher,
2486 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2487 AINPUT_SOURCE_MOUSE)
2488 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2489 .x(900)
2490 .y(400))
2491 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002492 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07002493
2494 // Move cursor into left window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002495 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002496 injectMotionEvent(mDispatcher,
2497 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2498 AINPUT_SOURCE_MOUSE)
2499 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2500 .x(300)
2501 .y(400))
2502 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002503 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2504 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07002505
2506 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002507 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002508 injectMotionEvent(mDispatcher,
2509 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2510 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2511 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2512 .x(300)
2513 .y(400))
2514 .build()));
2515 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2516
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002517 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002518 injectMotionEvent(mDispatcher,
2519 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
2520 AINPUT_SOURCE_MOUSE)
2521 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2522 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2523 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2524 .x(300)
2525 .y(400))
2526 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002527 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
Garfield Tandf26e862020-07-01 20:18:19 -07002528
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002529 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002530 injectMotionEvent(mDispatcher,
2531 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
2532 AINPUT_SOURCE_MOUSE)
2533 .buttonState(0)
2534 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2535 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2536 .x(300)
2537 .y(400))
2538 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002539 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE));
Garfield Tandf26e862020-07-01 20:18:19 -07002540
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002541 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002542 injectMotionEvent(mDispatcher,
2543 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
2544 .buttonState(0)
2545 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2546 .x(300)
2547 .y(400))
2548 .build()));
2549 windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT);
2550
2551 // Move mouse cursor back to right window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002552 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002553 injectMotionEvent(mDispatcher,
2554 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2555 AINPUT_SOURCE_MOUSE)
2556 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2557 .x(900)
2558 .y(400))
2559 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002560 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2561 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002562
2563 // No more events
2564 windowLeft->assertNoEvents();
2565 windowRight->assertNoEvents();
2566}
2567
2568TEST_F(InputDispatcherTest, HoverWithSpyWindows) {
2569 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2570
2571 sp<FakeWindowHandle> spyWindow =
2572 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
2573 spyWindow->setFrame(Rect(0, 0, 600, 800));
2574 spyWindow->setTrustedOverlay(true);
2575 spyWindow->setSpy(true);
2576 sp<FakeWindowHandle> window =
2577 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2578 window->setFrame(Rect(0, 0, 600, 800));
2579
2580 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2581 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
2582
2583 // Send mouse cursor to the window
2584 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2585 injectMotionEvent(mDispatcher,
2586 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
2587 AINPUT_SOURCE_MOUSE)
2588 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2589 .x(100)
2590 .y(100))
2591 .build()));
2592
2593 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2594 WithSource(AINPUT_SOURCE_MOUSE)));
2595 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2596 WithSource(AINPUT_SOURCE_MOUSE)));
2597
2598 window->assertNoEvents();
2599 spyWindow->assertNoEvents();
Garfield Tandf26e862020-07-01 20:18:19 -07002600}
2601
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08002602TEST_F(InputDispatcherTest, MouseAndTouchWithSpyWindows) {
2603 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2604
2605 sp<FakeWindowHandle> spyWindow =
2606 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
2607 spyWindow->setFrame(Rect(0, 0, 600, 800));
2608 spyWindow->setTrustedOverlay(true);
2609 spyWindow->setSpy(true);
2610 sp<FakeWindowHandle> window =
2611 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2612 window->setFrame(Rect(0, 0, 600, 800));
2613
2614 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2615 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
2616
2617 // Send mouse cursor to the window
2618 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2619 injectMotionEvent(mDispatcher,
2620 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
2621 AINPUT_SOURCE_MOUSE)
2622 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2623 .x(100)
2624 .y(100))
2625 .build()));
2626
2627 // Move mouse cursor
2628 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2629 injectMotionEvent(mDispatcher,
2630 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2631 AINPUT_SOURCE_MOUSE)
2632 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2633 .x(110)
2634 .y(110))
2635 .build()));
2636
2637 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2638 WithSource(AINPUT_SOURCE_MOUSE)));
2639 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2640 WithSource(AINPUT_SOURCE_MOUSE)));
2641 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2642 WithSource(AINPUT_SOURCE_MOUSE)));
2643 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2644 WithSource(AINPUT_SOURCE_MOUSE)));
2645 // Touch down on the window
2646 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2647 injectMotionEvent(mDispatcher,
2648 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2649 AINPUT_SOURCE_TOUCHSCREEN)
2650 .deviceId(SECOND_DEVICE_ID)
2651 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER)
2652 .x(200)
2653 .y(200))
2654 .build()));
2655 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2656 WithSource(AINPUT_SOURCE_MOUSE)));
2657 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2658 WithSource(AINPUT_SOURCE_MOUSE)));
2659 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2660 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
2661 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2662 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
2663
2664 // pilfer the motion, retaining the gesture on the spy window.
2665 EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken()));
2666 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
2667 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
2668
2669 // Touch UP on the window
2670 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2671 injectMotionEvent(mDispatcher,
2672 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2673 AINPUT_SOURCE_TOUCHSCREEN)
2674 .deviceId(SECOND_DEVICE_ID)
2675 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER)
2676 .x(200)
2677 .y(200))
2678 .build()));
2679 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
2680 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
2681
2682 // Previously, a touch was pilfered. However, that gesture was just finished. Now, we are going
2683 // to send a new gesture. It should again go to both windows (spy and the window below), just
2684 // like the first gesture did, before pilfering. The window configuration has not changed.
2685
2686 // One more tap - DOWN
2687 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2688 injectMotionEvent(mDispatcher,
2689 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2690 AINPUT_SOURCE_TOUCHSCREEN)
2691 .deviceId(SECOND_DEVICE_ID)
2692 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER)
2693 .x(250)
2694 .y(250))
2695 .build()));
2696 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2697 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
2698 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2699 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
2700
2701 // Touch UP on the window
2702 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2703 injectMotionEvent(mDispatcher,
2704 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2705 AINPUT_SOURCE_TOUCHSCREEN)
2706 .deviceId(SECOND_DEVICE_ID)
2707 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER)
2708 .x(250)
2709 .y(250))
2710 .build()));
2711 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
2712 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
2713 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
2714 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
2715
2716 window->assertNoEvents();
2717 spyWindow->assertNoEvents();
2718}
2719
Garfield Tandf26e862020-07-01 20:18:19 -07002720// This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected
2721// directly in this test.
2722TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) {
Chris Yea209fde2020-07-22 13:54:51 -07002723 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07002724 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002725 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07002726 window->setFrame(Rect(0, 0, 1200, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07002727
2728 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2729
2730 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2731
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002732 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002733 injectMotionEvent(mDispatcher,
2734 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
2735 AINPUT_SOURCE_MOUSE)
2736 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2737 .x(300)
2738 .y(400))
2739 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002740 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07002741 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002742 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002743 injectMotionEvent(mDispatcher,
2744 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2745 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2746 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2747 .x(300)
2748 .y(400))
2749 .build()));
2750 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2751
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002752 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002753 injectMotionEvent(mDispatcher,
2754 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
2755 AINPUT_SOURCE_MOUSE)
2756 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2757 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2758 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2759 .x(300)
2760 .y(400))
2761 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002762 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
Garfield Tandf26e862020-07-01 20:18:19 -07002763
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002764 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002765 injectMotionEvent(mDispatcher,
2766 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
2767 AINPUT_SOURCE_MOUSE)
2768 .buttonState(0)
2769 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2770 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2771 .x(300)
2772 .y(400))
2773 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002774 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE));
Garfield Tandf26e862020-07-01 20:18:19 -07002775
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002776 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002777 injectMotionEvent(mDispatcher,
2778 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
2779 .buttonState(0)
2780 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2781 .x(300)
2782 .y(400))
2783 .build()));
2784 window->consumeMotionUp(ADISPLAY_ID_DEFAULT);
2785
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002786 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002787 injectMotionEvent(mDispatcher,
2788 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT,
2789 AINPUT_SOURCE_MOUSE)
2790 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2791 .x(300)
2792 .y(400))
2793 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002794 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
Garfield Tandf26e862020-07-01 20:18:19 -07002795}
2796
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08002797/**
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00002798 * Hover over a window, and then remove that window. Make sure that HOVER_EXIT for that event
2799 * is generated.
2800 */
2801TEST_F(InputDispatcherTest, HoverExitIsSentToRemovedWindow) {
2802 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2803 sp<FakeWindowHandle> window =
2804 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2805 window->setFrame(Rect(0, 0, 1200, 800));
2806
2807 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2808
2809 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2810
2811 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2812 injectMotionEvent(mDispatcher,
2813 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
2814 AINPUT_SOURCE_MOUSE)
2815 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2816 .x(300)
2817 .y(400))
2818 .build()));
2819 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2820
2821 // Remove the window, but keep the channel.
2822 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
2823 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2824}
2825
2826/**
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08002827 * Inject a mouse hover event followed by a tap from touchscreen.
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00002828 * The tap causes a HOVER_EXIT event to be generated because the current event
2829 * stream's source has been switched.
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08002830 */
2831TEST_F(InputDispatcherTest, MouseHoverAndTouchTap) {
2832 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2833 sp<FakeWindowHandle> window =
2834 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2835 window->setFrame(Rect(0, 0, 100, 100));
2836
2837 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2838
2839 // Inject a hover_move from mouse.
2840 NotifyMotionArgs motionArgs =
2841 generateMotionArgs(AMOTION_EVENT_ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE,
2842 ADISPLAY_ID_DEFAULT, {{50, 50}});
2843 motionArgs.xCursorPosition = 50;
2844 motionArgs.yCursorPosition = 50;
2845 mDispatcher->notifyMotion(&motionArgs);
2846 ASSERT_NO_FATAL_FAILURE(
2847 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2848 WithSource(AINPUT_SOURCE_MOUSE))));
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08002849
2850 // Tap on the window
2851 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2852 ADISPLAY_ID_DEFAULT, {{10, 10}});
2853 mDispatcher->notifyMotion(&motionArgs);
2854 ASSERT_NO_FATAL_FAILURE(
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00002855 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2856 WithSource(AINPUT_SOURCE_MOUSE))));
2857
2858 ASSERT_NO_FATAL_FAILURE(
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08002859 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2860 WithSource(AINPUT_SOURCE_TOUCHSCREEN))));
2861
2862 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
2863 ADISPLAY_ID_DEFAULT, {{10, 10}});
2864 mDispatcher->notifyMotion(&motionArgs);
2865 ASSERT_NO_FATAL_FAILURE(
2866 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
2867 WithSource(AINPUT_SOURCE_TOUCHSCREEN))));
2868}
2869
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02002870TEST_F(InputDispatcherTest, HoverEnterMoveRemoveWindowsInSecondDisplay) {
2871 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2872 sp<FakeWindowHandle> windowDefaultDisplay =
2873 sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay",
2874 ADISPLAY_ID_DEFAULT);
2875 windowDefaultDisplay->setFrame(Rect(0, 0, 600, 800));
2876 sp<FakeWindowHandle> windowSecondDisplay =
2877 sp<FakeWindowHandle>::make(application, mDispatcher, "SecondDisplay",
2878 SECOND_DISPLAY_ID);
2879 windowSecondDisplay->setFrame(Rect(0, 0, 600, 800));
2880
2881 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}},
2882 {SECOND_DISPLAY_ID, {windowSecondDisplay}}});
2883
2884 // Set cursor position in window in default display and check that hover enter and move
2885 // events are generated.
2886 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2887 injectMotionEvent(mDispatcher,
2888 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2889 AINPUT_SOURCE_MOUSE)
2890 .displayId(ADISPLAY_ID_DEFAULT)
2891 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2892 .x(300)
2893 .y(600))
2894 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002895 windowDefaultDisplay->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02002896
2897 // Remove all windows in secondary display and check that no event happens on window in
2898 // primary display.
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002899 mDispatcher->setInputWindows(
2900 {{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}}, {SECOND_DISPLAY_ID, {}}});
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02002901 windowDefaultDisplay->assertNoEvents();
2902
2903 // Move cursor position in window in default display and check that only hover move
2904 // event is generated and not hover enter event.
2905 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}},
2906 {SECOND_DISPLAY_ID, {windowSecondDisplay}}});
2907 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2908 injectMotionEvent(mDispatcher,
2909 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2910 AINPUT_SOURCE_MOUSE)
2911 .displayId(ADISPLAY_ID_DEFAULT)
2912 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2913 .x(400)
2914 .y(700))
2915 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002916 windowDefaultDisplay->consumeMotionEvent(
2917 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2918 WithSource(AINPUT_SOURCE_MOUSE)));
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02002919 windowDefaultDisplay->assertNoEvents();
2920}
2921
Garfield Tan00f511d2019-06-12 16:55:40 -07002922TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) {
Chris Yea209fde2020-07-22 13:54:51 -07002923 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tan00f511d2019-06-12 16:55:40 -07002924
2925 sp<FakeWindowHandle> windowLeft =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002926 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07002927 windowLeft->setFrame(Rect(0, 0, 600, 800));
Garfield Tan00f511d2019-06-12 16:55:40 -07002928 sp<FakeWindowHandle> windowRight =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002929 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07002930 windowRight->setFrame(Rect(600, 0, 1200, 800));
Garfield Tan00f511d2019-06-12 16:55:40 -07002931
2932 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2933
Arthur Hung72d8dc32020-03-28 00:48:39 +00002934 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
Garfield Tan00f511d2019-06-12 16:55:40 -07002935
2936 // Inject an event with coordinate in the area of right window, with mouse cursor in the area of
2937 // left window. This event should be dispatched to the left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002938 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tan00f511d2019-06-12 16:55:40 -07002939 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07002940 ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400}));
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002941 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07002942 windowRight->assertNoEvents();
2943}
2944
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002945TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) {
Chris Yea209fde2020-07-22 13:54:51 -07002946 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002947 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
2948 "Fake Window", ADISPLAY_ID_DEFAULT);
Vishnu Nair47074b82020-08-14 11:54:47 -07002949 window->setFocusable(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002950
Arthur Hung72d8dc32020-03-28 00:48:39 +00002951 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002952 setFocusedWindow(window);
2953
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002954 window->consumeFocusEvent(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002955
2956 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
2957 mDispatcher->notifyKey(&keyArgs);
2958
2959 // Window should receive key down event.
2960 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2961
2962 // When device reset happens, that key stream should be terminated with FLAG_CANCELED
2963 // on the app side.
Harry Cutts33476232023-01-30 19:57:29 +00002964 NotifyDeviceResetArgs args(/*id=*/10, /*eventTime=*/20, DEVICE_ID);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002965 mDispatcher->notifyDeviceReset(&args);
2966 window->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
2967 AKEY_EVENT_FLAG_CANCELED);
2968}
2969
2970TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) {
Chris Yea209fde2020-07-22 13:54:51 -07002971 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002972 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
2973 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002974
Arthur Hung72d8dc32020-03-28 00:48:39 +00002975 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002976
2977 NotifyMotionArgs motionArgs =
2978 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2979 ADISPLAY_ID_DEFAULT);
2980 mDispatcher->notifyMotion(&motionArgs);
2981
2982 // Window should receive motion down event.
2983 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2984
2985 // When device reset happens, that motion stream should be terminated with ACTION_CANCEL
2986 // on the app side.
Harry Cutts33476232023-01-30 19:57:29 +00002987 NotifyDeviceResetArgs args(/*id=*/10, /*eventTime=*/20, DEVICE_ID);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002988 mDispatcher->notifyDeviceReset(&args);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08002989 window->consumeMotionEvent(
2990 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002991}
2992
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08002993TEST_F(InputDispatcherTest, InterceptKeyByPolicy) {
2994 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002995 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
2996 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08002997 window->setFocusable(true);
2998
2999 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3000 setFocusedWindow(window);
3001
3002 window->consumeFocusEvent(true);
3003
3004 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
3005 const std::chrono::milliseconds interceptKeyTimeout = 50ms;
3006 const nsecs_t injectTime = keyArgs.eventTime;
3007 mFakePolicy->setInterceptKeyTimeout(interceptKeyTimeout);
3008 mDispatcher->notifyKey(&keyArgs);
3009 // The dispatching time should be always greater than or equal to intercept key timeout.
3010 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3011 ASSERT_TRUE((systemTime(SYSTEM_TIME_MONOTONIC) - injectTime) >=
3012 std::chrono::nanoseconds(interceptKeyTimeout).count());
3013}
3014
3015TEST_F(InputDispatcherTest, InterceptKeyIfKeyUp) {
3016 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003017 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3018 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003019 window->setFocusable(true);
3020
3021 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3022 setFocusedWindow(window);
3023
3024 window->consumeFocusEvent(true);
3025
3026 NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
3027 NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
3028 mFakePolicy->setInterceptKeyTimeout(150ms);
3029 mDispatcher->notifyKey(&keyDown);
3030 mDispatcher->notifyKey(&keyUp);
3031
3032 // Window should receive key event immediately when same key up.
3033 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3034 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
3035}
3036
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003037/**
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08003038 * Two windows. First is a regular window. Second does not overlap with the first, and has
3039 * WATCH_OUTSIDE_TOUCH.
3040 * Both windows are owned by the same UID.
3041 * Tap first window. Make sure that the second window receives ACTION_OUTSIDE with correct, non-zero
3042 * coordinates. The coordinates are not zeroed out because both windows are owned by the same UID.
3043 */
3044TEST_F(InputDispatcherTest, ActionOutsideForOwnedWindowHasValidCoordinates) {
3045 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3046 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3047 "First Window", ADISPLAY_ID_DEFAULT);
3048 window->setFrame(Rect{0, 0, 100, 100});
3049
3050 sp<FakeWindowHandle> outsideWindow =
3051 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3052 ADISPLAY_ID_DEFAULT);
3053 outsideWindow->setFrame(Rect{100, 100, 200, 200});
3054 outsideWindow->setWatchOutsideTouch(true);
3055 // outsideWindow must be above 'window' to receive ACTION_OUTSIDE events when 'window' is tapped
3056 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {outsideWindow, window}}});
3057
3058 // Tap on first window.
3059 NotifyMotionArgs motionArgs =
3060 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3061 ADISPLAY_ID_DEFAULT, {PointF{50, 50}});
3062 mDispatcher->notifyMotion(&motionArgs);
3063 window->consumeMotionDown();
3064 // The coordinates of the tap in 'outsideWindow' are relative to its top left corner.
3065 // Therefore, we should offset them by (100, 100) relative to the screen's top left corner.
3066 outsideWindow->consumeMotionEvent(
3067 AllOf(WithMotionAction(ACTION_OUTSIDE), WithCoords(-50, -50)));
3068}
3069
3070/**
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003071 * This test documents the behavior of WATCH_OUTSIDE_TOUCH. The window will get ACTION_OUTSIDE when
3072 * a another pointer causes ACTION_DOWN to be sent to another window for the first time. Only one
3073 * ACTION_OUTSIDE event is sent per gesture.
3074 */
3075TEST_F(InputDispatcherTest, ActionOutsideSentOnlyWhenAWindowIsTouched) {
3076 // There are three windows that do not overlap. `window` wants to WATCH_OUTSIDE_TOUCH.
3077 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003078 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3079 "First Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003080 window->setWatchOutsideTouch(true);
3081 window->setFrame(Rect{0, 0, 100, 100});
3082 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003083 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3084 ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003085 secondWindow->setFrame(Rect{100, 100, 200, 200});
3086 sp<FakeWindowHandle> thirdWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003087 sp<FakeWindowHandle>::make(application, mDispatcher, "Third Window",
3088 ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003089 thirdWindow->setFrame(Rect{200, 200, 300, 300});
3090 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, secondWindow, thirdWindow}}});
3091
3092 // First pointer lands outside all windows. `window` does not get ACTION_OUTSIDE.
3093 NotifyMotionArgs motionArgs =
3094 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3095 ADISPLAY_ID_DEFAULT, {PointF{-10, -10}});
3096 mDispatcher->notifyMotion(&motionArgs);
3097 window->assertNoEvents();
3098 secondWindow->assertNoEvents();
3099
3100 // The second pointer lands inside `secondWindow`, which should receive a DOWN event.
3101 // Now, `window` should get ACTION_OUTSIDE.
3102 motionArgs = generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3103 {PointF{-10, -10}, PointF{105, 105}});
3104 mDispatcher->notifyMotion(&motionArgs);
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08003105 const std::map<int32_t, PointF> expectedPointers{{0, PointF{-10, -10}}, {1, PointF{105, 105}}};
3106 window->consumeMotionEvent(
3107 AllOf(WithMotionAction(ACTION_OUTSIDE), WithPointers(expectedPointers)));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003108 secondWindow->consumeMotionDown();
3109 thirdWindow->assertNoEvents();
3110
3111 // The third pointer lands inside `thirdWindow`, which should receive a DOWN event. There is
3112 // no ACTION_OUTSIDE sent to `window` because one has already been sent for this gesture.
3113 motionArgs = generateMotionArgs(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3114 {PointF{-10, -10}, PointF{105, 105}, PointF{205, 205}});
3115 mDispatcher->notifyMotion(&motionArgs);
3116 window->assertNoEvents();
3117 secondWindow->consumeMotionMove();
3118 thirdWindow->consumeMotionDown();
3119}
3120
Prabir Pradhan814fe082022-07-22 20:22:18 +00003121TEST_F(InputDispatcherTest, OnWindowInfosChanged_RemoveAllWindowsOnDisplay) {
3122 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003123 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3124 "Fake Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan814fe082022-07-22 20:22:18 +00003125 window->setFocusable(true);
3126
3127 mDispatcher->onWindowInfosChanged({*window->getInfo()}, {});
3128 setFocusedWindow(window);
3129
3130 window->consumeFocusEvent(true);
3131
3132 NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
3133 NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
3134 mDispatcher->notifyKey(&keyDown);
3135 mDispatcher->notifyKey(&keyUp);
3136
3137 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3138 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
3139
3140 // All windows are removed from the display. Ensure that we can no longer dispatch to it.
3141 mDispatcher->onWindowInfosChanged({}, {});
3142
3143 window->consumeFocusEvent(false);
3144
3145 mDispatcher->notifyKey(&keyDown);
3146 mDispatcher->notifyKey(&keyUp);
3147 window->assertNoEvents();
3148}
3149
Arthur Hung96483742022-11-15 03:30:48 +00003150TEST_F(InputDispatcherTest, NonSplitTouchableWindowReceivesMultiTouch) {
3151 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3152 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3153 "Fake Window", ADISPLAY_ID_DEFAULT);
3154 // Ensure window is non-split and have some transform.
3155 window->setPreventSplitting(true);
3156 window->setWindowOffset(20, 40);
3157 mDispatcher->onWindowInfosChanged({*window->getInfo()}, {});
3158
3159 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3160 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3161 {50, 50}))
3162 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3163 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3164
3165 const MotionEvent secondFingerDownEvent =
3166 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3167 .displayId(ADISPLAY_ID_DEFAULT)
3168 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Harry Cutts33476232023-01-30 19:57:29 +00003169 .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
3170 .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(-30).y(-50))
Arthur Hung96483742022-11-15 03:30:48 +00003171 .build();
3172 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3173 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
3174 InputEventInjectionSync::WAIT_FOR_RESULT))
3175 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3176
3177 const MotionEvent* event = window->consumeMotion();
3178 EXPECT_EQ(POINTER_1_DOWN, event->getAction());
3179 EXPECT_EQ(70, event->getX(0)); // 50 + 20
3180 EXPECT_EQ(90, event->getY(0)); // 50 + 40
3181 EXPECT_EQ(-10, event->getX(1)); // -30 + 20
3182 EXPECT_EQ(-10, event->getY(1)); // -50 + 40
3183}
3184
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003185/**
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003186 * Ensure the correct coordinate spaces are used by InputDispatcher.
3187 *
3188 * InputDispatcher works in the display space, so its coordinate system is relative to the display
3189 * panel. Windows get events in the window space, and get raw coordinates in the logical display
3190 * space.
3191 */
3192class InputDispatcherDisplayProjectionTest : public InputDispatcherTest {
3193public:
3194 void SetUp() override {
3195 InputDispatcherTest::SetUp();
3196 mDisplayInfos.clear();
3197 mWindowInfos.clear();
3198 }
3199
3200 void addDisplayInfo(int displayId, const ui::Transform& transform) {
3201 gui::DisplayInfo info;
3202 info.displayId = displayId;
3203 info.transform = transform;
3204 mDisplayInfos.push_back(std::move(info));
3205 mDispatcher->onWindowInfosChanged(mWindowInfos, mDisplayInfos);
3206 }
3207
3208 void addWindow(const sp<WindowInfoHandle>& windowHandle) {
3209 mWindowInfos.push_back(*windowHandle->getInfo());
3210 mDispatcher->onWindowInfosChanged(mWindowInfos, mDisplayInfos);
3211 }
3212
3213 // Set up a test scenario where the display has a scaled projection and there are two windows
3214 // on the display.
3215 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupScaledDisplayScenario() {
3216 // The display has a projection that has a scale factor of 2 and 4 in the x and y directions
3217 // respectively.
3218 ui::Transform displayTransform;
3219 displayTransform.set(2, 0, 0, 4);
3220 addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform);
3221
3222 std::shared_ptr<FakeApplicationHandle> application =
3223 std::make_shared<FakeApplicationHandle>();
3224
3225 // Add two windows to the display. Their frames are represented in the display space.
3226 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003227 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
3228 ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003229 firstWindow->setFrame(Rect(0, 0, 100, 200), displayTransform);
3230 addWindow(firstWindow);
3231
3232 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003233 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3234 ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003235 secondWindow->setFrame(Rect(100, 200, 200, 400), displayTransform);
3236 addWindow(secondWindow);
3237 return {std::move(firstWindow), std::move(secondWindow)};
3238 }
3239
3240private:
3241 std::vector<gui::DisplayInfo> mDisplayInfos;
3242 std::vector<gui::WindowInfo> mWindowInfos;
3243};
3244
3245TEST_F(InputDispatcherDisplayProjectionTest, HitTestsInDisplaySpace) {
3246 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
3247 // Send down to the first window. The point is represented in the display space. The point is
3248 // selected so that if the hit test was done with the transform applied to it, then it would
3249 // end up in the incorrect window.
3250 NotifyMotionArgs downMotionArgs =
3251 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3252 ADISPLAY_ID_DEFAULT, {PointF{75, 55}});
3253 mDispatcher->notifyMotion(&downMotionArgs);
3254
3255 firstWindow->consumeMotionDown();
3256 secondWindow->assertNoEvents();
3257}
3258
3259// Ensure that when a MotionEvent is injected through the InputDispatcher::injectInputEvent() API,
3260// the event should be treated as being in the logical display space.
3261TEST_F(InputDispatcherDisplayProjectionTest, InjectionInLogicalDisplaySpace) {
3262 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
3263 // Send down to the first window. The point is represented in the logical display space. The
3264 // point is selected so that if the hit test was done in logical display space, then it would
3265 // end up in the incorrect window.
3266 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3267 PointF{75 * 2, 55 * 4});
3268
3269 firstWindow->consumeMotionDown();
3270 secondWindow->assertNoEvents();
3271}
3272
Prabir Pradhandaa2f142021-12-10 09:30:08 +00003273// Ensure that when a MotionEvent that has a custom transform is injected, the post-transformed
3274// event should be treated as being in the logical display space.
3275TEST_F(InputDispatcherDisplayProjectionTest, InjectionWithTransformInLogicalDisplaySpace) {
3276 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
3277
3278 const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0};
3279 ui::Transform injectedEventTransform;
3280 injectedEventTransform.set(matrix);
3281 const vec2 expectedPoint{75, 55}; // The injected point in the logical display space.
3282 const vec2 untransformedPoint = injectedEventTransform.inverse().transform(expectedPoint);
3283
3284 MotionEvent event = MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3285 .displayId(ADISPLAY_ID_DEFAULT)
3286 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Harry Cutts33476232023-01-30 19:57:29 +00003287 .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER)
Prabir Pradhandaa2f142021-12-10 09:30:08 +00003288 .x(untransformedPoint.x)
3289 .y(untransformedPoint.y))
3290 .build();
3291 event.transform(matrix);
3292
3293 injectMotionEvent(mDispatcher, event, INJECT_EVENT_TIMEOUT,
3294 InputEventInjectionSync::WAIT_FOR_RESULT);
3295
3296 firstWindow->consumeMotionDown();
3297 secondWindow->assertNoEvents();
3298}
3299
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003300TEST_F(InputDispatcherDisplayProjectionTest, WindowGetsEventsInCorrectCoordinateSpace) {
3301 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
3302
3303 // Send down to the second window.
3304 NotifyMotionArgs downMotionArgs =
3305 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3306 ADISPLAY_ID_DEFAULT, {PointF{150, 220}});
3307 mDispatcher->notifyMotion(&downMotionArgs);
3308
3309 firstWindow->assertNoEvents();
3310 const MotionEvent* event = secondWindow->consumeMotion();
3311 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, event->getAction());
3312
3313 // Ensure that the events from the "getRaw" API are in logical display coordinates.
3314 EXPECT_EQ(300, event->getRawX(0));
3315 EXPECT_EQ(880, event->getRawY(0));
3316
3317 // Ensure that the x and y values are in the window's coordinate space.
3318 // The left-top of the second window is at (100, 200) in display space, which is (200, 800) in
3319 // the logical display space. This will be the origin of the window space.
3320 EXPECT_EQ(100, event->getX(0));
3321 EXPECT_EQ(80, event->getY(0));
3322}
3323
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003324using TransferFunction = std::function<bool(const std::unique_ptr<InputDispatcher>& dispatcher,
3325 sp<IBinder>, sp<IBinder>)>;
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003326
3327class TransferTouchFixture : public InputDispatcherTest,
3328 public ::testing::WithParamInterface<TransferFunction> {};
3329
3330TEST_P(TransferTouchFixture, TransferTouch_OnePointer) {
Chris Yea209fde2020-07-22 13:54:51 -07003331 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08003332
3333 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003334 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003335 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
3336 ADISPLAY_ID_DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00003337 firstWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003338 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003339 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3340 ADISPLAY_ID_DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00003341 sp<FakeWindowHandle> wallpaper =
3342 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
3343 wallpaper->setIsWallpaper(true);
Svet Ganov5d3bc372020-01-26 23:11:07 -08003344 // Add the windows to the dispatcher
Arthur Hungc539dbb2022-12-08 07:45:36 +00003345 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow, wallpaper}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08003346
3347 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003348 NotifyMotionArgs downMotionArgs =
3349 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3350 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08003351 mDispatcher->notifyMotion(&downMotionArgs);
Arthur Hungc539dbb2022-12-08 07:45:36 +00003352
Svet Ganov5d3bc372020-01-26 23:11:07 -08003353 // Only the first window should get the down event
3354 firstWindow->consumeMotionDown();
3355 secondWindow->assertNoEvents();
Arthur Hungc539dbb2022-12-08 07:45:36 +00003356 wallpaper->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08003357
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003358 // Transfer touch to the second window
3359 TransferFunction f = GetParam();
3360 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
3361 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08003362 // The first window gets cancel and the second gets down
3363 firstWindow->consumeMotionCancel();
3364 secondWindow->consumeMotionDown();
Arthur Hungc539dbb2022-12-08 07:45:36 +00003365 wallpaper->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08003366
3367 // Send up event to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003368 NotifyMotionArgs upMotionArgs =
3369 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3370 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08003371 mDispatcher->notifyMotion(&upMotionArgs);
3372 // The first window gets no events and the second gets up
3373 firstWindow->assertNoEvents();
3374 secondWindow->consumeMotionUp();
Arthur Hungc539dbb2022-12-08 07:45:36 +00003375 wallpaper->assertNoEvents();
Svet Ganov5d3bc372020-01-26 23:11:07 -08003376}
3377
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07003378/**
3379 * When 'transferTouch' API is invoked, dispatcher needs to find the "best" window to take touch
3380 * from. When we have spy windows, there are several windows to choose from: either spy, or the
3381 * 'real' (non-spy) window. Always prefer the 'real' window because that's what would be most
3382 * natural to the user.
3383 * In this test, we are sending a pointer to both spy window and first window. We then try to
3384 * transfer touch to the second window. The dispatcher should identify the first window as the
3385 * one that should lose the gesture, and therefore the action should be to move the gesture from
3386 * the first window to the second.
3387 * The main goal here is to test the behaviour of 'transferTouch' API, but it's still valid to test
3388 * the other API, as well.
3389 */
3390TEST_P(TransferTouchFixture, TransferTouch_MultipleWindowsWithSpy) {
3391 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3392
3393 // Create a couple of windows + a spy window
3394 sp<FakeWindowHandle> spyWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003395 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07003396 spyWindow->setTrustedOverlay(true);
3397 spyWindow->setSpy(true);
3398 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003399 sp<FakeWindowHandle>::make(application, mDispatcher, "First", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07003400 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003401 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07003402
3403 // Add the windows to the dispatcher
3404 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, firstWindow, secondWindow}}});
3405
3406 // Send down to the first window
3407 NotifyMotionArgs downMotionArgs =
3408 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3409 ADISPLAY_ID_DEFAULT);
3410 mDispatcher->notifyMotion(&downMotionArgs);
3411 // Only the first window and spy should get the down event
3412 spyWindow->consumeMotionDown();
3413 firstWindow->consumeMotionDown();
3414
3415 // Transfer touch to the second window. Non-spy window should be preferred over the spy window
3416 // if f === 'transferTouch'.
3417 TransferFunction f = GetParam();
3418 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
3419 ASSERT_TRUE(success);
3420 // The first window gets cancel and the second gets down
3421 firstWindow->consumeMotionCancel();
3422 secondWindow->consumeMotionDown();
3423
3424 // Send up event to the second window
3425 NotifyMotionArgs upMotionArgs =
3426 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3427 ADISPLAY_ID_DEFAULT);
3428 mDispatcher->notifyMotion(&upMotionArgs);
3429 // The first window gets no events and the second+spy get up
3430 firstWindow->assertNoEvents();
3431 spyWindow->consumeMotionUp();
3432 secondWindow->consumeMotionUp();
3433}
3434
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003435TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07003436 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08003437
3438 PointF touchPoint = {10, 10};
3439
3440 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003441 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003442 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
3443 ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08003444 firstWindow->setPreventSplitting(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003445 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003446 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3447 ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08003448 secondWindow->setPreventSplitting(true);
Svet Ganov5d3bc372020-01-26 23:11:07 -08003449
3450 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00003451 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08003452
3453 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003454 NotifyMotionArgs downMotionArgs =
3455 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3456 ADISPLAY_ID_DEFAULT, {touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08003457 mDispatcher->notifyMotion(&downMotionArgs);
3458 // Only the first window should get the down event
3459 firstWindow->consumeMotionDown();
3460 secondWindow->assertNoEvents();
3461
3462 // Send pointer down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003463 NotifyMotionArgs pointerDownMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003464 generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003465 {touchPoint, touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08003466 mDispatcher->notifyMotion(&pointerDownMotionArgs);
3467 // Only the first window should get the pointer down event
3468 firstWindow->consumeMotionPointerDown(1);
3469 secondWindow->assertNoEvents();
3470
3471 // Transfer touch focus to the second window
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003472 TransferFunction f = GetParam();
3473 bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
3474 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08003475 // The first window gets cancel and the second gets down and pointer down
3476 firstWindow->consumeMotionCancel();
3477 secondWindow->consumeMotionDown();
3478 secondWindow->consumeMotionPointerDown(1);
3479
3480 // Send pointer up to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003481 NotifyMotionArgs pointerUpMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003482 generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003483 {touchPoint, touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08003484 mDispatcher->notifyMotion(&pointerUpMotionArgs);
3485 // The first window gets nothing and the second gets pointer up
3486 firstWindow->assertNoEvents();
3487 secondWindow->consumeMotionPointerUp(1);
3488
3489 // Send up event to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003490 NotifyMotionArgs upMotionArgs =
3491 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3492 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08003493 mDispatcher->notifyMotion(&upMotionArgs);
3494 // The first window gets nothing and the second gets up
3495 firstWindow->assertNoEvents();
3496 secondWindow->consumeMotionUp();
3497}
3498
Arthur Hungc539dbb2022-12-08 07:45:36 +00003499TEST_P(TransferTouchFixture, TransferTouch_MultipleWallpapers) {
3500 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3501
3502 // Create a couple of windows
3503 sp<FakeWindowHandle> firstWindow =
3504 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
3505 ADISPLAY_ID_DEFAULT);
3506 firstWindow->setDupTouchToWallpaper(true);
3507 sp<FakeWindowHandle> secondWindow =
3508 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3509 ADISPLAY_ID_DEFAULT);
3510 secondWindow->setDupTouchToWallpaper(true);
3511
3512 sp<FakeWindowHandle> wallpaper1 =
3513 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper1", ADISPLAY_ID_DEFAULT);
3514 wallpaper1->setIsWallpaper(true);
3515
3516 sp<FakeWindowHandle> wallpaper2 =
3517 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper2", ADISPLAY_ID_DEFAULT);
3518 wallpaper2->setIsWallpaper(true);
3519 // Add the windows to the dispatcher
3520 mDispatcher->setInputWindows(
3521 {{ADISPLAY_ID_DEFAULT, {firstWindow, wallpaper1, secondWindow, wallpaper2}}});
3522
3523 // Send down to the first window
3524 NotifyMotionArgs downMotionArgs =
3525 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3526 ADISPLAY_ID_DEFAULT);
3527 mDispatcher->notifyMotion(&downMotionArgs);
3528
3529 // Only the first window should get the down event
3530 firstWindow->consumeMotionDown();
3531 secondWindow->assertNoEvents();
3532 wallpaper1->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
3533 wallpaper2->assertNoEvents();
3534
3535 // Transfer touch focus to the second window
3536 TransferFunction f = GetParam();
3537 bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
3538 ASSERT_TRUE(success);
3539
3540 // The first window gets cancel and the second gets down
3541 firstWindow->consumeMotionCancel();
3542 secondWindow->consumeMotionDown();
3543 wallpaper1->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
3544 wallpaper2->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
3545
3546 // Send up event to the second window
3547 NotifyMotionArgs upMotionArgs =
3548 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3549 ADISPLAY_ID_DEFAULT);
3550 mDispatcher->notifyMotion(&upMotionArgs);
3551 // The first window gets no events and the second gets up
3552 firstWindow->assertNoEvents();
3553 secondWindow->consumeMotionUp();
3554 wallpaper1->assertNoEvents();
3555 wallpaper2->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
3556}
3557
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003558// For the cases of single pointer touch and two pointers non-split touch, the api's
3559// 'transferTouch' and 'transferTouchFocus' are equivalent in behaviour. They only differ
3560// for the case where there are multiple pointers split across several windows.
3561INSTANTIATE_TEST_SUITE_P(TransferFunctionTests, TransferTouchFixture,
3562 ::testing::Values(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003563 [&](const std::unique_ptr<InputDispatcher>& dispatcher,
3564 sp<IBinder> /*ignored*/, sp<IBinder> destChannelToken) {
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07003565 return dispatcher->transferTouch(destChannelToken,
3566 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003567 },
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003568 [&](const std::unique_ptr<InputDispatcher>& dispatcher,
3569 sp<IBinder> from, sp<IBinder> to) {
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003570 return dispatcher->transferTouchFocus(from, to,
Harry Cutts33476232023-01-30 19:57:29 +00003571 /*isDragAndDrop=*/false);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003572 }));
3573
Svet Ganov5d3bc372020-01-26 23:11:07 -08003574TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07003575 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08003576
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003577 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003578 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
3579 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08003580 firstWindow->setFrame(Rect(0, 0, 600, 400));
Svet Ganov5d3bc372020-01-26 23:11:07 -08003581
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003582 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003583 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3584 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08003585 secondWindow->setFrame(Rect(0, 400, 600, 800));
Svet Ganov5d3bc372020-01-26 23:11:07 -08003586
3587 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00003588 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08003589
3590 PointF pointInFirst = {300, 200};
3591 PointF pointInSecond = {300, 600};
3592
3593 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003594 NotifyMotionArgs firstDownMotionArgs =
3595 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3596 ADISPLAY_ID_DEFAULT, {pointInFirst});
Svet Ganov5d3bc372020-01-26 23:11:07 -08003597 mDispatcher->notifyMotion(&firstDownMotionArgs);
3598 // Only the first window should get the down event
3599 firstWindow->consumeMotionDown();
3600 secondWindow->assertNoEvents();
3601
3602 // Send down to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003603 NotifyMotionArgs secondDownMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003604 generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003605 {pointInFirst, pointInSecond});
Svet Ganov5d3bc372020-01-26 23:11:07 -08003606 mDispatcher->notifyMotion(&secondDownMotionArgs);
3607 // The first window gets a move and the second a down
3608 firstWindow->consumeMotionMove();
3609 secondWindow->consumeMotionDown();
3610
3611 // Transfer touch focus to the second window
3612 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
3613 // The first window gets cancel and the new gets pointer down (it already saw down)
3614 firstWindow->consumeMotionCancel();
3615 secondWindow->consumeMotionPointerDown(1);
3616
3617 // Send pointer up to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003618 NotifyMotionArgs pointerUpMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003619 generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003620 {pointInFirst, pointInSecond});
Svet Ganov5d3bc372020-01-26 23:11:07 -08003621 mDispatcher->notifyMotion(&pointerUpMotionArgs);
3622 // The first window gets nothing and the second gets pointer up
3623 firstWindow->assertNoEvents();
3624 secondWindow->consumeMotionPointerUp(1);
3625
3626 // Send up event to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003627 NotifyMotionArgs upMotionArgs =
3628 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3629 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08003630 mDispatcher->notifyMotion(&upMotionArgs);
3631 // The first window gets nothing and the second gets up
3632 firstWindow->assertNoEvents();
3633 secondWindow->consumeMotionUp();
3634}
3635
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003636// Same as TransferTouchFocus_TwoPointersSplitTouch, but using 'transferTouch' api.
3637// Unlike 'transferTouchFocus', calling 'transferTouch' when there are two windows receiving
3638// touch is not supported, so the touch should continue on those windows and the transferred-to
3639// window should get nothing.
3640TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) {
3641 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3642
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003643 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003644 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
3645 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003646 firstWindow->setFrame(Rect(0, 0, 600, 400));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003647
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003648 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003649 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3650 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003651 secondWindow->setFrame(Rect(0, 400, 600, 800));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003652
3653 // Add the windows to the dispatcher
3654 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
3655
3656 PointF pointInFirst = {300, 200};
3657 PointF pointInSecond = {300, 600};
3658
3659 // Send down to the first window
3660 NotifyMotionArgs firstDownMotionArgs =
3661 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3662 ADISPLAY_ID_DEFAULT, {pointInFirst});
3663 mDispatcher->notifyMotion(&firstDownMotionArgs);
3664 // Only the first window should get the down event
3665 firstWindow->consumeMotionDown();
3666 secondWindow->assertNoEvents();
3667
3668 // Send down to the second window
3669 NotifyMotionArgs secondDownMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003670 generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003671 {pointInFirst, pointInSecond});
3672 mDispatcher->notifyMotion(&secondDownMotionArgs);
3673 // The first window gets a move and the second a down
3674 firstWindow->consumeMotionMove();
3675 secondWindow->consumeMotionDown();
3676
3677 // Transfer touch focus to the second window
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07003678 const bool transferred =
3679 mDispatcher->transferTouch(secondWindow->getToken(), ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003680 // The 'transferTouch' call should not succeed, because there are 2 touched windows
3681 ASSERT_FALSE(transferred);
3682 firstWindow->assertNoEvents();
3683 secondWindow->assertNoEvents();
3684
3685 // The rest of the dispatch should proceed as normal
3686 // Send pointer up to the second window
3687 NotifyMotionArgs pointerUpMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003688 generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003689 {pointInFirst, pointInSecond});
3690 mDispatcher->notifyMotion(&pointerUpMotionArgs);
3691 // The first window gets MOVE and the second gets pointer up
3692 firstWindow->consumeMotionMove();
3693 secondWindow->consumeMotionUp();
3694
3695 // Send up event to the first window
3696 NotifyMotionArgs upMotionArgs =
3697 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3698 ADISPLAY_ID_DEFAULT);
3699 mDispatcher->notifyMotion(&upMotionArgs);
3700 // The first window gets nothing and the second gets up
3701 firstWindow->consumeMotionUp();
3702 secondWindow->assertNoEvents();
3703}
3704
Arthur Hungabbb9d82021-09-01 14:52:30 +00003705// This case will create two windows and one mirrored window on the default display and mirror
3706// two windows on the second display. It will test if 'transferTouchFocus' works fine if we put
3707// the windows info of second display before default display.
3708TEST_F(InputDispatcherTest, TransferTouchFocus_CloneSurface) {
3709 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3710 sp<FakeWindowHandle> firstWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003711 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00003712 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003713 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003714 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00003715 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003716
3717 sp<FakeWindowHandle> mirrorWindowInPrimary =
3718 firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT);
3719 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003720
3721 sp<FakeWindowHandle> firstWindowInSecondary =
3722 firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
3723 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003724
3725 sp<FakeWindowHandle> secondWindowInSecondary =
3726 secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
3727 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003728
3729 // Update window info, let it find window handle of second display first.
3730 mDispatcher->setInputWindows(
3731 {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}},
3732 {ADISPLAY_ID_DEFAULT,
3733 {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}});
3734
3735 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3736 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3737 {50, 50}))
3738 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3739
3740 // Window should receive motion event.
3741 firstWindowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3742
3743 // Transfer touch focus
3744 ASSERT_TRUE(mDispatcher->transferTouchFocus(firstWindowInPrimary->getToken(),
3745 secondWindowInPrimary->getToken()));
3746 // The first window gets cancel.
3747 firstWindowInPrimary->consumeMotionCancel();
3748 secondWindowInPrimary->consumeMotionDown();
3749
3750 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3751 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
3752 ADISPLAY_ID_DEFAULT, {150, 50}))
3753 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3754 firstWindowInPrimary->assertNoEvents();
3755 secondWindowInPrimary->consumeMotionMove();
3756
3757 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3758 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3759 {150, 50}))
3760 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3761 firstWindowInPrimary->assertNoEvents();
3762 secondWindowInPrimary->consumeMotionUp();
3763}
3764
3765// Same as TransferTouchFocus_CloneSurface, but this touch on the secondary display and use
3766// 'transferTouch' api.
3767TEST_F(InputDispatcherTest, TransferTouch_CloneSurface) {
3768 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3769 sp<FakeWindowHandle> firstWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003770 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00003771 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003772 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003773 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00003774 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003775
3776 sp<FakeWindowHandle> mirrorWindowInPrimary =
3777 firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT);
3778 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003779
3780 sp<FakeWindowHandle> firstWindowInSecondary =
3781 firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
3782 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003783
3784 sp<FakeWindowHandle> secondWindowInSecondary =
3785 secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
3786 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003787
3788 // Update window info, let it find window handle of second display first.
3789 mDispatcher->setInputWindows(
3790 {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}},
3791 {ADISPLAY_ID_DEFAULT,
3792 {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}});
3793
3794 // Touch on second display.
3795 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3796 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {50, 50}))
3797 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3798
3799 // Window should receive motion event.
3800 firstWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID);
3801
3802 // Transfer touch focus
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07003803 ASSERT_TRUE(mDispatcher->transferTouch(secondWindowInSecondary->getToken(), SECOND_DISPLAY_ID));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003804
3805 // The first window gets cancel.
3806 firstWindowInPrimary->consumeMotionCancel(SECOND_DISPLAY_ID);
3807 secondWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID);
3808
3809 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3810 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
3811 SECOND_DISPLAY_ID, {150, 50}))
3812 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3813 firstWindowInPrimary->assertNoEvents();
3814 secondWindowInPrimary->consumeMotionMove(SECOND_DISPLAY_ID);
3815
3816 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3817 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {150, 50}))
3818 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3819 firstWindowInPrimary->assertNoEvents();
3820 secondWindowInPrimary->consumeMotionUp(SECOND_DISPLAY_ID);
3821}
3822
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003823TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07003824 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003825 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3826 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003827
Vishnu Nair47074b82020-08-14 11:54:47 -07003828 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00003829 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003830 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003831
3832 window->consumeFocusEvent(true);
3833
3834 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
3835 mDispatcher->notifyKey(&keyArgs);
3836
3837 // Window should receive key down event.
3838 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3839}
3840
3841TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07003842 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003843 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3844 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003845
Arthur Hung72d8dc32020-03-28 00:48:39 +00003846 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003847
3848 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
3849 mDispatcher->notifyKey(&keyArgs);
3850 mDispatcher->waitForIdle();
3851
3852 window->assertNoEvents();
3853}
3854
3855// If a window is touchable, but does not have focus, it should receive motion events, but not keys
3856TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) {
Chris Yea209fde2020-07-22 13:54:51 -07003857 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003858 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3859 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003860
Arthur Hung72d8dc32020-03-28 00:48:39 +00003861 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003862
3863 // Send key
3864 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
3865 mDispatcher->notifyKey(&keyArgs);
3866 // Send motion
3867 NotifyMotionArgs motionArgs =
3868 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3869 ADISPLAY_ID_DEFAULT);
3870 mDispatcher->notifyMotion(&motionArgs);
3871
3872 // Window should receive only the motion event
3873 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3874 window->assertNoEvents(); // Key event or focus event will not be received
3875}
3876
arthurhungea3f4fc2020-12-21 23:18:53 +08003877TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) {
3878 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3879
arthurhungea3f4fc2020-12-21 23:18:53 +08003880 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003881 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
3882 ADISPLAY_ID_DEFAULT);
arthurhungea3f4fc2020-12-21 23:18:53 +08003883 firstWindow->setFrame(Rect(0, 0, 600, 400));
arthurhungea3f4fc2020-12-21 23:18:53 +08003884
arthurhungea3f4fc2020-12-21 23:18:53 +08003885 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003886 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3887 ADISPLAY_ID_DEFAULT);
arthurhungea3f4fc2020-12-21 23:18:53 +08003888 secondWindow->setFrame(Rect(0, 400, 600, 800));
arthurhungea3f4fc2020-12-21 23:18:53 +08003889
3890 // Add the windows to the dispatcher
3891 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
3892
3893 PointF pointInFirst = {300, 200};
3894 PointF pointInSecond = {300, 600};
3895
3896 // Send down to the first window
3897 NotifyMotionArgs firstDownMotionArgs =
3898 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3899 ADISPLAY_ID_DEFAULT, {pointInFirst});
3900 mDispatcher->notifyMotion(&firstDownMotionArgs);
3901 // Only the first window should get the down event
3902 firstWindow->consumeMotionDown();
3903 secondWindow->assertNoEvents();
3904
3905 // Send down to the second window
3906 NotifyMotionArgs secondDownMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003907 generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
arthurhungea3f4fc2020-12-21 23:18:53 +08003908 {pointInFirst, pointInSecond});
3909 mDispatcher->notifyMotion(&secondDownMotionArgs);
3910 // The first window gets a move and the second a down
3911 firstWindow->consumeMotionMove();
3912 secondWindow->consumeMotionDown();
3913
3914 // Send pointer cancel to the second window
3915 NotifyMotionArgs pointerUpMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003916 generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
arthurhungea3f4fc2020-12-21 23:18:53 +08003917 {pointInFirst, pointInSecond});
3918 pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED;
3919 mDispatcher->notifyMotion(&pointerUpMotionArgs);
3920 // The first window gets move and the second gets cancel.
3921 firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
3922 secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
3923
3924 // Send up event.
3925 NotifyMotionArgs upMotionArgs =
3926 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3927 ADISPLAY_ID_DEFAULT);
3928 mDispatcher->notifyMotion(&upMotionArgs);
3929 // The first window gets up and the second gets nothing.
3930 firstWindow->consumeMotionUp();
3931 secondWindow->assertNoEvents();
3932}
3933
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00003934TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) {
3935 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3936
3937 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003938 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00003939 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3940 std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline;
3941 graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2;
3942 graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3;
3943
Harry Cutts33476232023-01-30 19:57:29 +00003944 window->sendTimeline(/*inputEventId=*/1, graphicsTimeline);
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00003945 window->assertNoEvents();
3946 mDispatcher->waitForIdle();
3947}
3948
chaviwd1c23182019-12-20 18:44:56 -08003949class FakeMonitorReceiver {
Michael Wright3a240c42019-12-10 20:53:41 +00003950public:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003951 FakeMonitorReceiver(const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003952 int32_t displayId) {
Garfield Tan15601662020-09-22 15:32:38 -07003953 base::Result<std::unique_ptr<InputChannel>> channel =
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08003954 dispatcher->createInputMonitor(displayId, name, MONITOR_PID);
Garfield Tan15601662020-09-22 15:32:38 -07003955 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
Michael Wright3a240c42019-12-10 20:53:41 +00003956 }
3957
chaviwd1c23182019-12-20 18:44:56 -08003958 sp<IBinder> getToken() { return mInputReceiver->getToken(); }
3959
3960 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
3961 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN,
3962 expectedDisplayId, expectedFlags);
3963 }
3964
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003965 std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); }
3966
3967 void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); }
3968
chaviwd1c23182019-12-20 18:44:56 -08003969 void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
3970 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN,
3971 expectedDisplayId, expectedFlags);
3972 }
3973
Siarhei Vishniakouca205502021-07-16 21:31:58 +00003974 void consumeMotionMove(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
3975 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE,
3976 expectedDisplayId, expectedFlags);
3977 }
3978
chaviwd1c23182019-12-20 18:44:56 -08003979 void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
3980 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP,
3981 expectedDisplayId, expectedFlags);
3982 }
3983
Siarhei Vishniakouca205502021-07-16 21:31:58 +00003984 void consumeMotionCancel(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08003985 mInputReceiver->consumeMotionEvent(
3986 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
3987 WithDisplayId(expectedDisplayId),
3988 WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED)));
Siarhei Vishniakouca205502021-07-16 21:31:58 +00003989 }
3990
Arthur Hungfbfa5722021-11-16 02:45:54 +00003991 void consumeMotionPointerDown(int32_t pointerIdx) {
3992 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
3993 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
3994 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00003995 /*expectedFlags=*/0);
Arthur Hungfbfa5722021-11-16 02:45:54 +00003996 }
3997
Evan Rosky84f07f02021-04-16 10:42:42 -07003998 MotionEvent* consumeMotion() {
3999 InputEvent* event = mInputReceiver->consume();
4000 if (!event) {
4001 ADD_FAILURE() << "No event was produced";
4002 return nullptr;
4003 }
4004 if (event->getType() != AINPUT_EVENT_TYPE_MOTION) {
4005 ADD_FAILURE() << "Received event of type " << event->getType() << " instead of motion";
4006 return nullptr;
4007 }
4008 return static_cast<MotionEvent*>(event);
4009 }
4010
chaviwd1c23182019-12-20 18:44:56 -08004011 void assertNoEvents() { mInputReceiver->assertNoEvents(); }
4012
4013private:
4014 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Michael Wright3a240c42019-12-10 20:53:41 +00004015};
4016
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004017using InputDispatcherMonitorTest = InputDispatcherTest;
4018
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004019/**
4020 * Two entities that receive touch: A window, and a global monitor.
4021 * The touch goes to the window, and then the window disappears.
4022 * The monitor does not get cancel right away. But if more events come in, the touch gets canceled
4023 * for the monitor, as well.
4024 * 1. foregroundWindow
4025 * 2. monitor <-- global monitor (doesn't observe z order, receives all events)
4026 */
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004027TEST_F(InputDispatcherMonitorTest, MonitorTouchIsCanceledWhenForegroundWindowDisappears) {
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004028 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4029 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004030 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004031
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004032 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004033
4034 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4035 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4036 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4037 {100, 200}))
4038 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4039
4040 // Both the foreground window and the global monitor should receive the touch down
4041 window->consumeMotionDown();
4042 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
4043
4044 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4045 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4046 ADISPLAY_ID_DEFAULT, {110, 200}))
4047 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4048
4049 window->consumeMotionMove();
4050 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
4051
4052 // Now the foreground window goes away
4053 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
4054 window->consumeMotionCancel();
4055 monitor.assertNoEvents(); // Global monitor does not get a cancel yet
4056
4057 // If more events come in, there will be no more foreground window to send them to. This will
4058 // cause a cancel for the monitor, as well.
4059 ASSERT_EQ(InputEventInjectionResult::FAILED,
4060 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4061 ADISPLAY_ID_DEFAULT, {120, 200}))
4062 << "Injection should fail because the window was removed";
4063 window->assertNoEvents();
4064 // Global monitor now gets the cancel
4065 monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
4066}
4067
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004068TEST_F(InputDispatcherMonitorTest, ReceivesMotionEvents) {
Chris Yea209fde2020-07-22 13:54:51 -07004069 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004070 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4071 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004072 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00004073
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004074 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00004075
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004076 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00004077 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004078 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00004079 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08004080 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00004081}
4082
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004083TEST_F(InputDispatcherMonitorTest, MonitorCannotPilferPointers) {
4084 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00004085
Chris Yea209fde2020-07-22 13:54:51 -07004086 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004087 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4088 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004089 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00004090
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004091 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00004092 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004093 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
chaviwd1c23182019-12-20 18:44:56 -08004094 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004095 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00004096
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004097 // Pilfer pointers from the monitor.
4098 // This should not do anything and the window should continue to receive events.
4099 EXPECT_NE(OK, mDispatcher->pilferPointers(monitor.getToken()));
Michael Wright3a240c42019-12-10 20:53:41 +00004100
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004101 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004102 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4103 ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004104 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004105
4106 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
4107 window->consumeMotionMove(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00004108}
4109
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004110TEST_F(InputDispatcherMonitorTest, NoWindowTransform) {
Evan Rosky84f07f02021-04-16 10:42:42 -07004111 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004112 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4113 "Fake Window", ADISPLAY_ID_DEFAULT);
Evan Rosky84f07f02021-04-16 10:42:42 -07004114 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4115 window->setWindowOffset(20, 40);
4116 window->setWindowTransform(0, 1, -1, 0);
4117
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004118 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Evan Rosky84f07f02021-04-16 10:42:42 -07004119
4120 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4121 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
4122 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4123 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4124 MotionEvent* event = monitor.consumeMotion();
4125 // Even though window has transform, gesture monitor must not.
4126 ASSERT_EQ(ui::Transform(), event->getTransform());
4127}
4128
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004129TEST_F(InputDispatcherMonitorTest, InjectionFailsWithNoWindow) {
Arthur Hungb3307ee2021-10-14 10:57:37 +00004130 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004131 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Arthur Hungb3307ee2021-10-14 10:57:37 +00004132
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004133 ASSERT_EQ(InputEventInjectionResult::FAILED,
Arthur Hungb3307ee2021-10-14 10:57:37 +00004134 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004135 << "Injection should fail if there is a monitor, but no touchable window";
4136 monitor.assertNoEvents();
Arthur Hungb3307ee2021-10-14 10:57:37 +00004137}
4138
chaviw81e2bb92019-12-18 15:03:51 -08004139TEST_F(InputDispatcherTest, TestMoveEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07004140 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004141 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4142 "Fake Window", ADISPLAY_ID_DEFAULT);
chaviw81e2bb92019-12-18 15:03:51 -08004143
Arthur Hung72d8dc32020-03-28 00:48:39 +00004144 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
chaviw81e2bb92019-12-18 15:03:51 -08004145
4146 NotifyMotionArgs motionArgs =
4147 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4148 ADISPLAY_ID_DEFAULT);
4149
4150 mDispatcher->notifyMotion(&motionArgs);
4151 // Window should receive motion down event.
4152 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4153
4154 motionArgs.action = AMOTION_EVENT_ACTION_MOVE;
Garfield Tanc51d1ba2020-01-28 13:24:04 -08004155 motionArgs.id += 1;
chaviw81e2bb92019-12-18 15:03:51 -08004156 motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
4157 motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
4158 motionArgs.pointerCoords[0].getX() - 10);
4159
4160 mDispatcher->notifyMotion(&motionArgs);
4161 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00004162 /*expectedFlags=*/0);
chaviw81e2bb92019-12-18 15:03:51 -08004163}
4164
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004165/**
4166 * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to
4167 * the device default right away. In the test scenario, we check both the default value,
4168 * and the action of enabling / disabling.
4169 */
4170TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) {
Chris Yea209fde2020-07-22 13:54:51 -07004171 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004172 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4173 "Test window", ADISPLAY_ID_DEFAULT);
Antonio Kantekea47acb2021-12-23 12:41:25 -08004174 const WindowInfo& windowInfo = *window->getInfo();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004175
4176 // Set focused application.
4177 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07004178 window->setFocusable(true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004179
4180 SCOPED_TRACE("Check default value of touch mode");
Arthur Hung72d8dc32020-03-28 00:48:39 +00004181 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004182 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00004183 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004184
4185 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07004186 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004187 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Harry Cutts33476232023-01-30 19:57:29 +00004188 window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004189
4190 SCOPED_TRACE("Disable touch mode");
Antonio Kantekea47acb2021-12-23 12:41:25 -08004191 mDispatcher->setInTouchMode(false, windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00004192 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00004193 window->consumeTouchModeEvent(false);
Vishnu Nair47074b82020-08-14 11:54:47 -07004194 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004195 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004196 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00004197 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/false);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004198
4199 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07004200 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004201 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Harry Cutts33476232023-01-30 19:57:29 +00004202 window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/false);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004203
4204 SCOPED_TRACE("Enable touch mode again");
Antonio Kantekea47acb2021-12-23 12:41:25 -08004205 mDispatcher->setInTouchMode(true, windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00004206 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00004207 window->consumeTouchModeEvent(true);
Vishnu Nair47074b82020-08-14 11:54:47 -07004208 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004209 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004210 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00004211 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004212
4213 window->assertNoEvents();
4214}
4215
Gang Wange9087892020-01-07 12:17:14 -05004216TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07004217 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004218 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4219 "Test window", ADISPLAY_ID_DEFAULT);
Gang Wange9087892020-01-07 12:17:14 -05004220
4221 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07004222 window->setFocusable(true);
Gang Wange9087892020-01-07 12:17:14 -05004223
Arthur Hung72d8dc32020-03-28 00:48:39 +00004224 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004225 setFocusedWindow(window);
4226
Harry Cutts33476232023-01-30 19:57:29 +00004227 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Gang Wange9087892020-01-07 12:17:14 -05004228
4229 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
4230 mDispatcher->notifyKey(&keyArgs);
4231
4232 InputEvent* event = window->consume();
4233 ASSERT_NE(event, nullptr);
4234
4235 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
4236 ASSERT_NE(verified, nullptr);
4237 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY);
4238
4239 ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos);
4240 ASSERT_EQ(keyArgs.deviceId, verified->deviceId);
4241 ASSERT_EQ(keyArgs.source, verified->source);
4242 ASSERT_EQ(keyArgs.displayId, verified->displayId);
4243
4244 const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified);
4245
4246 ASSERT_EQ(keyArgs.action, verifiedKey.action);
Gang Wange9087892020-01-07 12:17:14 -05004247 ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08004248 ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos);
Gang Wange9087892020-01-07 12:17:14 -05004249 ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode);
4250 ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode);
4251 ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState);
4252 ASSERT_EQ(0, verifiedKey.repeatCount);
4253}
4254
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08004255TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07004256 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004257 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4258 "Test window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08004259
4260 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
4261
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07004262 ui::Transform transform;
4263 transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
4264
4265 gui::DisplayInfo displayInfo;
4266 displayInfo.displayId = ADISPLAY_ID_DEFAULT;
4267 displayInfo.transform = transform;
4268
4269 mDispatcher->onWindowInfosChanged({*window->getInfo()}, {displayInfo});
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08004270
4271 NotifyMotionArgs motionArgs =
4272 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4273 ADISPLAY_ID_DEFAULT);
4274 mDispatcher->notifyMotion(&motionArgs);
4275
4276 InputEvent* event = window->consume();
4277 ASSERT_NE(event, nullptr);
4278
4279 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
4280 ASSERT_NE(verified, nullptr);
4281 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION);
4282
4283 EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos);
4284 EXPECT_EQ(motionArgs.deviceId, verified->deviceId);
4285 EXPECT_EQ(motionArgs.source, verified->source);
4286 EXPECT_EQ(motionArgs.displayId, verified->displayId);
4287
4288 const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified);
4289
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07004290 const vec2 rawXY =
4291 MotionEvent::calculateTransformedXY(motionArgs.source, transform,
4292 motionArgs.pointerCoords[0].getXYValue());
4293 EXPECT_EQ(rawXY.x, verifiedMotion.rawX);
4294 EXPECT_EQ(rawXY.y, verifiedMotion.rawY);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08004295 EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08004296 EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08004297 EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08004298 EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState);
4299 EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState);
4300}
4301
chaviw09c8d2d2020-08-24 15:48:26 -07004302/**
4303 * Ensure that separate calls to sign the same data are generating the same key.
4304 * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance
4305 * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky
4306 * tests.
4307 */
4308TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) {
4309 KeyEvent event = getTestKeyEvent();
4310 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
4311
4312 std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent);
4313 std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent);
4314 ASSERT_EQ(hmac1, hmac2);
4315}
4316
4317/**
4318 * Ensure that changes in VerifiedKeyEvent produce a different hmac.
4319 */
4320TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) {
4321 KeyEvent event = getTestKeyEvent();
4322 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
4323 std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent);
4324
4325 verifiedEvent.deviceId += 1;
4326 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
4327
4328 verifiedEvent.source += 1;
4329 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
4330
4331 verifiedEvent.eventTimeNanos += 1;
4332 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
4333
4334 verifiedEvent.displayId += 1;
4335 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
4336
4337 verifiedEvent.action += 1;
4338 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
4339
4340 verifiedEvent.downTimeNanos += 1;
4341 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
4342
4343 verifiedEvent.flags += 1;
4344 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
4345
4346 verifiedEvent.keyCode += 1;
4347 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
4348
4349 verifiedEvent.scanCode += 1;
4350 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
4351
4352 verifiedEvent.metaState += 1;
4353 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
4354
4355 verifiedEvent.repeatCount += 1;
4356 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
4357}
4358
Vishnu Nair958da932020-08-21 17:12:37 -07004359TEST_F(InputDispatcherTest, SetFocusedWindow) {
4360 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4361 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004362 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07004363 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004364 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07004365 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
4366
4367 // Top window is also focusable but is not granted focus.
4368 windowTop->setFocusable(true);
4369 windowSecond->setFocusable(true);
4370 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
4371 setFocusedWindow(windowSecond);
4372
4373 windowSecond->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004374 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4375 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004376
4377 // Focused window should receive event.
4378 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
4379 windowTop->assertNoEvents();
4380}
4381
4382TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) {
4383 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4384 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004385 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07004386 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
4387
4388 window->setFocusable(true);
4389 // Release channel for window is no longer valid.
4390 window->releaseChannel();
4391 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4392 setFocusedWindow(window);
4393
4394 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004395 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
4396 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07004397
4398 // window channel is invalid, so it should not receive any input event.
4399 window->assertNoEvents();
4400}
4401
4402TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) {
4403 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4404 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004405 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08004406 window->setFocusable(false);
Vishnu Nair958da932020-08-21 17:12:37 -07004407 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
4408
Vishnu Nair958da932020-08-21 17:12:37 -07004409 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4410 setFocusedWindow(window);
4411
4412 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004413 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
4414 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07004415
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08004416 // window is not focusable, so it should not receive any input event.
Vishnu Nair958da932020-08-21 17:12:37 -07004417 window->assertNoEvents();
4418}
4419
4420TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) {
4421 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4422 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004423 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07004424 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004425 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07004426 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
4427
4428 windowTop->setFocusable(true);
4429 windowSecond->setFocusable(true);
4430 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
4431 setFocusedWindow(windowTop);
4432 windowTop->consumeFocusEvent(true);
4433
4434 setFocusedWindow(windowSecond, windowTop);
4435 windowSecond->consumeFocusEvent(true);
4436 windowTop->consumeFocusEvent(false);
4437
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004438 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4439 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004440
4441 // Focused window should receive event.
4442 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
4443}
4444
4445TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestFocusTokenNotFocused) {
4446 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4447 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004448 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07004449 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004450 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07004451 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
4452
4453 windowTop->setFocusable(true);
4454 windowSecond->setFocusable(true);
4455 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
4456 setFocusedWindow(windowSecond, windowTop);
4457
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004458 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
4459 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07004460
4461 // Event should be dropped.
4462 windowTop->assertNoEvents();
4463 windowSecond->assertNoEvents();
4464}
4465
4466TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) {
4467 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4468 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004469 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07004470 sp<FakeWindowHandle> previousFocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004471 sp<FakeWindowHandle>::make(application, mDispatcher, "previousFocusedWindow",
4472 ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07004473 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
4474
4475 window->setFocusable(true);
4476 previousFocusedWindow->setFocusable(true);
4477 window->setVisible(false);
4478 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}});
4479 setFocusedWindow(previousFocusedWindow);
4480 previousFocusedWindow->consumeFocusEvent(true);
4481
4482 // Requesting focus on invisible window takes focus from currently focused window.
4483 setFocusedWindow(window);
4484 previousFocusedWindow->consumeFocusEvent(false);
4485
4486 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004487 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00004488 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
4489 InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07004490
4491 // Window does not get focus event or key down.
4492 window->assertNoEvents();
4493
4494 // Window becomes visible.
4495 window->setVisible(true);
4496 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4497
4498 // Window receives focus event.
4499 window->consumeFocusEvent(true);
4500 // Focused window receives key down.
4501 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4502}
4503
Vishnu Nair599f1412021-06-21 10:39:58 -07004504TEST_F(InputDispatcherTest, DisplayRemoved) {
4505 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4506 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004507 sp<FakeWindowHandle>::make(application, mDispatcher, "window", ADISPLAY_ID_DEFAULT);
Vishnu Nair599f1412021-06-21 10:39:58 -07004508 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
4509
4510 // window is granted focus.
4511 window->setFocusable(true);
4512 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4513 setFocusedWindow(window);
4514 window->consumeFocusEvent(true);
4515
4516 // When a display is removed window loses focus.
4517 mDispatcher->displayRemoved(ADISPLAY_ID_DEFAULT);
4518 window->consumeFocusEvent(false);
4519}
4520
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004521/**
4522 * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY,
4523 * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top
4524 * of the 'slipperyEnterWindow'.
4525 *
4526 * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such
4527 * a way so that the touched location is no longer covered by the top window.
4528 *
4529 * Next, inject a MOVE event. Because the top window already moved earlier, this event is now
4530 * positioned over the bottom (slipperyEnterWindow) only. And because the top window had
4531 * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive
4532 * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting
4533 * with ACTION_DOWN).
4534 * Thus, the touch has been transferred from the top window into the bottom window, because the top
4535 * window moved itself away from the touched location and had Flag::SLIPPERY.
4536 *
4537 * Even though the top window moved away from the touched location, it is still obscuring the bottom
4538 * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_
4539 * OBSCURED should be set for the MotionEvent that reaches the bottom window.
4540 *
4541 * In this test, we ensure that the event received by the bottom window has
4542 * FLAG_WINDOW_IS_PARTIALLY_OBSCURED.
4543 */
4544TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) {
Prabir Pradhan5735a322022-04-11 17:23:34 +00004545 constexpr int32_t SLIPPERY_PID = WINDOW_PID + 1;
4546 constexpr int32_t SLIPPERY_UID = WINDOW_UID + 1;
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004547
4548 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4549 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
4550
4551 sp<FakeWindowHandle> slipperyExitWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004552 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08004553 slipperyExitWindow->setSlippery(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004554 // Make sure this one overlaps the bottom window
4555 slipperyExitWindow->setFrame(Rect(25, 25, 75, 75));
4556 // Change the owner uid/pid of the window so that it is considered to be occluding the bottom
4557 // one. Windows with the same owner are not considered to be occluding each other.
4558 slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID);
4559
4560 sp<FakeWindowHandle> slipperyEnterWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004561 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004562 slipperyExitWindow->setFrame(Rect(0, 0, 100, 100));
4563
4564 mDispatcher->setInputWindows(
4565 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
4566
4567 // Use notifyMotion instead of injecting to avoid dealing with injection permissions
4568 NotifyMotionArgs args = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4569 ADISPLAY_ID_DEFAULT, {{50, 50}});
4570 mDispatcher->notifyMotion(&args);
4571 slipperyExitWindow->consumeMotionDown();
4572 slipperyExitWindow->setFrame(Rect(70, 70, 100, 100));
4573 mDispatcher->setInputWindows(
4574 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
4575
4576 args = generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4577 ADISPLAY_ID_DEFAULT, {{51, 51}});
4578 mDispatcher->notifyMotion(&args);
4579
4580 slipperyExitWindow->consumeMotionCancel();
4581
4582 slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT,
4583 AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
4584}
4585
Garfield Tan1c7bc862020-01-28 13:24:04 -08004586class InputDispatcherKeyRepeatTest : public InputDispatcherTest {
4587protected:
4588 static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms
4589 static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms
4590
Chris Yea209fde2020-07-22 13:54:51 -07004591 std::shared_ptr<FakeApplicationHandle> mApp;
Garfield Tan1c7bc862020-01-28 13:24:04 -08004592 sp<FakeWindowHandle> mWindow;
4593
4594 virtual void SetUp() override {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004595 mFakePolicy = sp<FakeInputDispatcherPolicy>::make();
Garfield Tan1c7bc862020-01-28 13:24:04 -08004596 mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004597 mDispatcher = std::make_unique<InputDispatcher>(mFakePolicy);
Garfield Tan1c7bc862020-01-28 13:24:04 -08004598 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
4599 ASSERT_EQ(OK, mDispatcher->start());
4600
4601 setUpWindow();
4602 }
4603
4604 void setUpWindow() {
Chris Yea209fde2020-07-22 13:54:51 -07004605 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004606 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Garfield Tan1c7bc862020-01-28 13:24:04 -08004607
Vishnu Nair47074b82020-08-14 11:54:47 -07004608 mWindow->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004609 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004610 setFocusedWindow(mWindow);
Garfield Tan1c7bc862020-01-28 13:24:04 -08004611 mWindow->consumeFocusEvent(true);
4612 }
4613
Chris Ye2ad95392020-09-01 13:44:44 -07004614 void sendAndConsumeKeyDown(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08004615 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07004616 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08004617 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event
4618 mDispatcher->notifyKey(&keyArgs);
4619
4620 // Window should receive key down event.
4621 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4622 }
4623
4624 void expectKeyRepeatOnce(int32_t repeatCount) {
4625 SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount));
4626 InputEvent* repeatEvent = mWindow->consume();
4627 ASSERT_NE(nullptr, repeatEvent);
4628
4629 uint32_t eventType = repeatEvent->getType();
4630 ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, eventType);
4631
4632 KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent);
4633 uint32_t eventAction = repeatKeyEvent->getAction();
4634 EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction);
4635 EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount());
4636 }
4637
Chris Ye2ad95392020-09-01 13:44:44 -07004638 void sendAndConsumeKeyUp(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08004639 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07004640 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08004641 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event
4642 mDispatcher->notifyKey(&keyArgs);
4643
4644 // Window should receive key down event.
4645 mWindow->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00004646 /*expectedFlags=*/0);
Garfield Tan1c7bc862020-01-28 13:24:04 -08004647 }
4648};
4649
4650TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) {
Harry Cutts33476232023-01-30 19:57:29 +00004651 sendAndConsumeKeyDown(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07004652 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
4653 expectKeyRepeatOnce(repeatCount);
4654 }
4655}
4656
4657TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) {
Harry Cutts33476232023-01-30 19:57:29 +00004658 sendAndConsumeKeyDown(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07004659 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
4660 expectKeyRepeatOnce(repeatCount);
4661 }
Harry Cutts33476232023-01-30 19:57:29 +00004662 sendAndConsumeKeyDown(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07004663 /* repeatCount will start from 1 for deviceId 2 */
Garfield Tan1c7bc862020-01-28 13:24:04 -08004664 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
4665 expectKeyRepeatOnce(repeatCount);
4666 }
4667}
4668
4669TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) {
Harry Cutts33476232023-01-30 19:57:29 +00004670 sendAndConsumeKeyDown(/*deviceId=*/1);
4671 expectKeyRepeatOnce(/*repeatCount=*/1);
4672 sendAndConsumeKeyUp(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07004673 mWindow->assertNoEvents();
4674}
4675
4676TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) {
Harry Cutts33476232023-01-30 19:57:29 +00004677 sendAndConsumeKeyDown(/*deviceId=*/1);
4678 expectKeyRepeatOnce(/*repeatCount=*/1);
4679 sendAndConsumeKeyDown(/*deviceId=*/2);
4680 expectKeyRepeatOnce(/*repeatCount=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07004681 // Stale key up from device 1.
Harry Cutts33476232023-01-30 19:57:29 +00004682 sendAndConsumeKeyUp(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07004683 // Device 2 is still down, keep repeating
Harry Cutts33476232023-01-30 19:57:29 +00004684 expectKeyRepeatOnce(/*repeatCount=*/2);
4685 expectKeyRepeatOnce(/*repeatCount=*/3);
Chris Ye2ad95392020-09-01 13:44:44 -07004686 // Device 2 key up
Harry Cutts33476232023-01-30 19:57:29 +00004687 sendAndConsumeKeyUp(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07004688 mWindow->assertNoEvents();
4689}
4690
4691TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) {
Harry Cutts33476232023-01-30 19:57:29 +00004692 sendAndConsumeKeyDown(/*deviceId=*/1);
4693 expectKeyRepeatOnce(/*repeatCount=*/1);
4694 sendAndConsumeKeyDown(/*deviceId=*/2);
4695 expectKeyRepeatOnce(/*repeatCount=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07004696 // Device 2 which holds the key repeating goes up, expect the repeating to stop.
Harry Cutts33476232023-01-30 19:57:29 +00004697 sendAndConsumeKeyUp(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07004698 // Device 1 still holds key down, but the repeating was already stopped
Garfield Tan1c7bc862020-01-28 13:24:04 -08004699 mWindow->assertNoEvents();
4700}
4701
liushenxiang42232912021-05-21 20:24:09 +08004702TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) {
4703 sendAndConsumeKeyDown(DEVICE_ID);
Harry Cutts33476232023-01-30 19:57:29 +00004704 expectKeyRepeatOnce(/*repeatCount=*/1);
4705 NotifyDeviceResetArgs args(/*id=*/10, /*eventTime=*/20, DEVICE_ID);
liushenxiang42232912021-05-21 20:24:09 +08004706 mDispatcher->notifyDeviceReset(&args);
4707 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT,
4708 AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS);
4709 mWindow->assertNoEvents();
4710}
4711
Garfield Tan1c7bc862020-01-28 13:24:04 -08004712TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) {
Harry Cutts33476232023-01-30 19:57:29 +00004713 sendAndConsumeKeyDown(/*deviceId=*/1);
Garfield Tan1c7bc862020-01-28 13:24:04 -08004714 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
4715 InputEvent* repeatEvent = mWindow->consume();
4716 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
4717 EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER,
4718 IdGenerator::getSource(repeatEvent->getId()));
4719 }
4720}
4721
4722TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) {
Harry Cutts33476232023-01-30 19:57:29 +00004723 sendAndConsumeKeyDown(/*deviceId=*/1);
Garfield Tan1c7bc862020-01-28 13:24:04 -08004724
4725 std::unordered_set<int32_t> idSet;
4726 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
4727 InputEvent* repeatEvent = mWindow->consume();
4728 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
4729 int32_t id = repeatEvent->getId();
4730 EXPECT_EQ(idSet.end(), idSet.find(id));
4731 idSet.insert(id);
4732 }
4733}
4734
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004735/* Test InputDispatcher for MultiDisplay */
4736class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest {
4737public:
Prabir Pradhan3608aad2019-10-02 17:08:26 -07004738 virtual void SetUp() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004739 InputDispatcherTest::SetUp();
Arthur Hungb92218b2018-08-14 12:00:21 +08004740
Chris Yea209fde2020-07-22 13:54:51 -07004741 application1 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004742 windowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004743 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08004744
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004745 // Set focus window for primary display, but focused display would be second one.
4746 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1);
Vishnu Nair47074b82020-08-14 11:54:47 -07004747 windowInPrimary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004748 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004749 setFocusedWindow(windowInPrimary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004750 windowInPrimary->consumeFocusEvent(true);
Arthur Hungb92218b2018-08-14 12:00:21 +08004751
Chris Yea209fde2020-07-22 13:54:51 -07004752 application2 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004753 windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004754 sp<FakeWindowHandle>::make(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004755 // Set focus to second display window.
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004756 // Set focus display to second one.
4757 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
4758 // Set focus window for second display.
4759 mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2);
Vishnu Nair47074b82020-08-14 11:54:47 -07004760 windowInSecondary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004761 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004762 setFocusedWindow(windowInSecondary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004763 windowInSecondary->consumeFocusEvent(true);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004764 }
4765
Prabir Pradhan3608aad2019-10-02 17:08:26 -07004766 virtual void TearDown() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004767 InputDispatcherTest::TearDown();
4768
Chris Yea209fde2020-07-22 13:54:51 -07004769 application1.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004770 windowInPrimary.clear();
Chris Yea209fde2020-07-22 13:54:51 -07004771 application2.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004772 windowInSecondary.clear();
4773 }
4774
4775protected:
Chris Yea209fde2020-07-22 13:54:51 -07004776 std::shared_ptr<FakeApplicationHandle> application1;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004777 sp<FakeWindowHandle> windowInPrimary;
Chris Yea209fde2020-07-22 13:54:51 -07004778 std::shared_ptr<FakeApplicationHandle> application2;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004779 sp<FakeWindowHandle> windowInSecondary;
4780};
4781
4782TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) {
4783 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004784 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4785 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
4786 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004787 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08004788 windowInSecondary->assertNoEvents();
4789
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004790 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004791 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4792 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
4793 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08004794 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004795 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +08004796}
4797
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004798TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) {
Tiger Huang721e26f2018-07-24 22:26:19 +08004799 // Test inject a key down with display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08004800 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4801 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004802 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004803 windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Tiger Huang721e26f2018-07-24 22:26:19 +08004804 windowInSecondary->assertNoEvents();
4805
4806 // Test inject a key down without display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08004807 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004808 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08004809 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004810 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hungb92218b2018-08-14 12:00:21 +08004811
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08004812 // Remove all windows in secondary display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00004813 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}});
Arthur Hungb92218b2018-08-14 12:00:21 +08004814
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004815 // Old focus should receive a cancel event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004816 windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE,
4817 AKEY_EVENT_FLAG_CANCELED);
Arthur Hungb92218b2018-08-14 12:00:21 +08004818
4819 // Test inject a key down, should timeout because of no target window.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08004820 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004821 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Arthur Hungb92218b2018-08-14 12:00:21 +08004822 windowInPrimary->assertNoEvents();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004823 windowInSecondary->consumeFocusEvent(false);
Arthur Hungb92218b2018-08-14 12:00:21 +08004824 windowInSecondary->assertNoEvents();
4825}
4826
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004827// Test per-display input monitors for motion event.
4828TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) {
chaviwd1c23182019-12-20 18:44:56 -08004829 FakeMonitorReceiver monitorInPrimary =
4830 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
4831 FakeMonitorReceiver monitorInSecondary =
4832 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004833
4834 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004835 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4836 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
4837 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004838 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08004839 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004840 windowInSecondary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08004841 monitorInSecondary.assertNoEvents();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004842
4843 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004844 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4845 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
4846 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004847 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08004848 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004849 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
chaviwd1c23182019-12-20 18:44:56 -08004850 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004851
4852 // Test inject a non-pointer motion event.
4853 // If specific a display, it will dispatch to the focused window of particular display,
4854 // or it will dispatch to the focused window of focused display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004855 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4856 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE))
4857 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004858 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08004859 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004860 windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08004861 monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004862}
4863
4864// Test per-display input monitors for key event.
4865TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) {
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004866 // Input monitor per display.
chaviwd1c23182019-12-20 18:44:56 -08004867 FakeMonitorReceiver monitorInPrimary =
4868 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
4869 FakeMonitorReceiver monitorInSecondary =
4870 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004871
4872 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004873 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4874 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004875 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08004876 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004877 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08004878 monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004879}
4880
Vishnu Nair958da932020-08-21 17:12:37 -07004881TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) {
4882 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004883 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07004884 secondWindowInPrimary->setFocusable(true);
4885 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}});
4886 setFocusedWindow(secondWindowInPrimary);
4887 windowInPrimary->consumeFocusEvent(false);
4888 secondWindowInPrimary->consumeFocusEvent(true);
4889
4890 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004891 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
4892 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004893 windowInPrimary->assertNoEvents();
4894 windowInSecondary->assertNoEvents();
4895 secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4896}
4897
Arthur Hungdfd528e2021-12-08 13:23:04 +00004898TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CancelTouch_MultiDisplay) {
4899 FakeMonitorReceiver monitorInPrimary =
4900 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
4901 FakeMonitorReceiver monitorInSecondary =
4902 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
4903
4904 // Test touch down on primary display.
4905 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4906 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
4907 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4908 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4909 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
4910
4911 // Test touch down on second display.
4912 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4913 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
4914 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4915 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
4916 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
4917
4918 // Trigger cancel touch.
4919 mDispatcher->cancelCurrentTouch();
4920 windowInPrimary->consumeMotionCancel(ADISPLAY_ID_DEFAULT);
4921 monitorInPrimary.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
4922 windowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID);
4923 monitorInSecondary.consumeMotionCancel(SECOND_DISPLAY_ID);
4924
4925 // Test inject a move motion event, no window/monitor should receive the event.
4926 ASSERT_EQ(InputEventInjectionResult::FAILED,
4927 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4928 ADISPLAY_ID_DEFAULT, {110, 200}))
4929 << "Inject motion event should return InputEventInjectionResult::FAILED";
4930 windowInPrimary->assertNoEvents();
4931 monitorInPrimary.assertNoEvents();
4932
4933 ASSERT_EQ(InputEventInjectionResult::FAILED,
4934 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4935 SECOND_DISPLAY_ID, {110, 200}))
4936 << "Inject motion event should return InputEventInjectionResult::FAILED";
4937 windowInSecondary->assertNoEvents();
4938 monitorInSecondary.assertNoEvents();
4939}
4940
Jackal Guof9696682018-10-05 12:23:23 +08004941class InputFilterTest : public InputDispatcherTest {
4942protected:
Prabir Pradhan81420cc2021-09-06 10:28:50 -07004943 void testNotifyMotion(int32_t displayId, bool expectToBeFiltered,
4944 const ui::Transform& transform = ui::Transform()) {
Jackal Guof9696682018-10-05 12:23:23 +08004945 NotifyMotionArgs motionArgs;
4946
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004947 motionArgs =
4948 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Jackal Guof9696682018-10-05 12:23:23 +08004949 mDispatcher->notifyMotion(&motionArgs);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004950 motionArgs =
4951 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Jackal Guof9696682018-10-05 12:23:23 +08004952 mDispatcher->notifyMotion(&motionArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004953 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08004954 if (expectToBeFiltered) {
Prabir Pradhan81420cc2021-09-06 10:28:50 -07004955 const auto xy = transform.transform(motionArgs.pointerCoords->getXYValue());
4956 mFakePolicy->assertFilterInputEventWasCalled(motionArgs, xy);
Jackal Guof9696682018-10-05 12:23:23 +08004957 } else {
4958 mFakePolicy->assertFilterInputEventWasNotCalled();
4959 }
4960 }
4961
4962 void testNotifyKey(bool expectToBeFiltered) {
4963 NotifyKeyArgs keyArgs;
4964
4965 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
4966 mDispatcher->notifyKey(&keyArgs);
4967 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP);
4968 mDispatcher->notifyKey(&keyArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004969 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08004970
4971 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08004972 mFakePolicy->assertFilterInputEventWasCalled(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08004973 } else {
4974 mFakePolicy->assertFilterInputEventWasNotCalled();
4975 }
4976 }
4977};
4978
4979// Test InputFilter for MotionEvent
4980TEST_F(InputFilterTest, MotionEvent_InputFilter) {
4981 // Since the InputFilter is disabled by default, check if touch events aren't filtered.
4982 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
4983 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
4984
4985 // Enable InputFilter
4986 mDispatcher->setInputFilterEnabled(true);
4987 // Test touch on both primary and second display, and check if both events are filtered.
4988 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true);
4989 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true);
4990
4991 // Disable InputFilter
4992 mDispatcher->setInputFilterEnabled(false);
4993 // Test touch on both primary and second display, and check if both events aren't filtered.
4994 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
4995 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
4996}
4997
4998// Test InputFilter for KeyEvent
4999TEST_F(InputFilterTest, KeyEvent_InputFilter) {
5000 // Since the InputFilter is disabled by default, check if key event aren't filtered.
5001 testNotifyKey(/*expectToBeFiltered*/ false);
5002
5003 // Enable InputFilter
5004 mDispatcher->setInputFilterEnabled(true);
5005 // Send a key event, and check if it is filtered.
5006 testNotifyKey(/*expectToBeFiltered*/ true);
5007
5008 // Disable InputFilter
5009 mDispatcher->setInputFilterEnabled(false);
5010 // Send a key event, and check if it isn't filtered.
5011 testNotifyKey(/*expectToBeFiltered*/ false);
5012}
5013
Prabir Pradhan81420cc2021-09-06 10:28:50 -07005014// Ensure that MotionEvents sent to the InputFilter through InputListener are converted to the
5015// logical display coordinate space.
5016TEST_F(InputFilterTest, MotionEvent_UsesLogicalDisplayCoordinates_notifyMotion) {
5017 ui::Transform firstDisplayTransform;
5018 firstDisplayTransform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
5019 ui::Transform secondDisplayTransform;
5020 secondDisplayTransform.set({-6.6, -5.5, -4.4, -3.3, -2.2, -1.1, 0, 0, 1});
5021
5022 std::vector<gui::DisplayInfo> displayInfos(2);
5023 displayInfos[0].displayId = ADISPLAY_ID_DEFAULT;
5024 displayInfos[0].transform = firstDisplayTransform;
5025 displayInfos[1].displayId = SECOND_DISPLAY_ID;
5026 displayInfos[1].transform = secondDisplayTransform;
5027
5028 mDispatcher->onWindowInfosChanged({}, displayInfos);
5029
5030 // Enable InputFilter
5031 mDispatcher->setInputFilterEnabled(true);
5032
5033 // Ensure the correct transforms are used for the displays.
5034 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true, firstDisplayTransform);
5035 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true, secondDisplayTransform);
5036}
5037
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005038class InputFilterInjectionPolicyTest : public InputDispatcherTest {
5039protected:
5040 virtual void SetUp() override {
5041 InputDispatcherTest::SetUp();
5042
5043 /**
5044 * We don't need to enable input filter to test the injected event policy, but we enabled it
5045 * here to make the tests more realistic, since this policy only matters when inputfilter is
5046 * on.
5047 */
5048 mDispatcher->setInputFilterEnabled(true);
5049
5050 std::shared_ptr<InputApplicationHandle> application =
5051 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005052 mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Test Window",
5053 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005054
5055 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5056 mWindow->setFocusable(true);
5057 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
5058 setFocusedWindow(mWindow);
5059 mWindow->consumeFocusEvent(true);
5060 }
5061
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005062 void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
5063 int32_t flags) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005064 KeyEvent event;
5065
5066 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
5067 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD,
5068 ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A,
Harry Cutts33476232023-01-30 19:57:29 +00005069 KEY_A, AMETA_NONE, /*repeatCount=*/0, eventTime, eventTime);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005070 const int32_t additionalPolicyFlags =
5071 POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT;
5072 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00005073 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005074 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
5075 policyFlags | additionalPolicyFlags));
5076
5077 InputEvent* received = mWindow->consume();
5078 ASSERT_NE(nullptr, received);
5079 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005080 ASSERT_EQ(received->getType(), AINPUT_EVENT_TYPE_KEY);
5081 KeyEvent& keyEvent = static_cast<KeyEvent&>(*received);
5082 ASSERT_EQ(flags, keyEvent.getFlags());
5083 }
5084
5085 void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
5086 int32_t flags) {
5087 MotionEvent event;
5088 PointerProperties pointerProperties[1];
5089 PointerCoords pointerCoords[1];
5090 pointerProperties[0].clear();
5091 pointerProperties[0].id = 0;
5092 pointerCoords[0].clear();
5093 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300);
5094 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400);
5095
5096 ui::Transform identityTransform;
5097 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
5098 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN,
5099 DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0,
5100 AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE,
5101 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -07005102 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, eventTime,
Evan Rosky09576692021-07-01 12:22:09 -07005103 eventTime,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005104 /*pointerCount*/ 1, pointerProperties, pointerCoords);
5105
5106 const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER;
5107 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00005108 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005109 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
5110 policyFlags | additionalPolicyFlags));
5111
5112 InputEvent* received = mWindow->consume();
5113 ASSERT_NE(nullptr, received);
5114 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
5115 ASSERT_EQ(received->getType(), AINPUT_EVENT_TYPE_MOTION);
5116 MotionEvent& motionEvent = static_cast<MotionEvent&>(*received);
5117 ASSERT_EQ(flags, motionEvent.getFlags());
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005118 }
5119
5120private:
5121 sp<FakeWindowHandle> mWindow;
5122};
5123
5124TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) {
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005125 // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input
5126 // filter. Without it, the event will no different from a regularly injected event, and the
5127 // injected device id will be overwritten.
Harry Cutts33476232023-01-30 19:57:29 +00005128 testInjectedKey(POLICY_FLAG_FILTERED, /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
5129 /*flags=*/0);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005130}
5131
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005132TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005133 testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Harry Cutts33476232023-01-30 19:57:29 +00005134 /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005135 AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
5136}
5137
5138TEST_F(InputFilterInjectionPolicyTest,
5139 MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
5140 testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Harry Cutts33476232023-01-30 19:57:29 +00005141 /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005142 AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005143}
5144
5145TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) {
Harry Cutts33476232023-01-30 19:57:29 +00005146 testInjectedKey(/*policyFlags=*/0, /*injectedDeviceId=*/3,
5147 /*resolvedDeviceId=*/VIRTUAL_KEYBOARD_ID, /*flags=*/0);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005148}
5149
chaviwfd6d3512019-03-25 13:23:49 -07005150class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest {
Prabir Pradhan3608aad2019-10-02 17:08:26 -07005151 virtual void SetUp() override {
chaviwfd6d3512019-03-25 13:23:49 -07005152 InputDispatcherTest::SetUp();
5153
Chris Yea209fde2020-07-22 13:54:51 -07005154 std::shared_ptr<FakeApplicationHandle> application =
5155 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005156 mUnfocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005157 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07005158 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
chaviwfd6d3512019-03-25 13:23:49 -07005159
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005160 mFocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005161 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005162 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
chaviwfd6d3512019-03-25 13:23:49 -07005163
5164 // Set focused application.
5165 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07005166 mFocusedWindow->setFocusable(true);
chaviwfd6d3512019-03-25 13:23:49 -07005167
5168 // Expect one focus window exist in display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00005169 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005170 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005171 mFocusedWindow->consumeFocusEvent(true);
chaviwfd6d3512019-03-25 13:23:49 -07005172 }
5173
Prabir Pradhan3608aad2019-10-02 17:08:26 -07005174 virtual void TearDown() override {
chaviwfd6d3512019-03-25 13:23:49 -07005175 InputDispatcherTest::TearDown();
5176
5177 mUnfocusedWindow.clear();
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005178 mFocusedWindow.clear();
chaviwfd6d3512019-03-25 13:23:49 -07005179 }
5180
5181protected:
5182 sp<FakeWindowHandle> mUnfocusedWindow;
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005183 sp<FakeWindowHandle> mFocusedWindow;
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07005184 static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60};
chaviwfd6d3512019-03-25 13:23:49 -07005185};
5186
5187// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
5188// DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received
5189// the onPointerDownOutsideFocus callback.
5190TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005191 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07005192 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5193 {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005194 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07005195 mUnfocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07005196
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08005197 ASSERT_TRUE(mDispatcher->waitForIdle());
chaviwfd6d3512019-03-25 13:23:49 -07005198 mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken());
5199}
5200
5201// Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action
5202// DOWN on the window that doesn't have focus. Ensure no window received the
5203// onPointerDownOutsideFocus callback.
5204TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005205 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07005206 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005207 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07005208 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07005209
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08005210 ASSERT_TRUE(mDispatcher->waitForIdle());
5211 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07005212}
5213
5214// Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't
5215// have focus. Ensure no window received the onPointerDownOutsideFocus callback.
5216TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005217 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5218 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005219 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07005220 mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07005221
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08005222 ASSERT_TRUE(mDispatcher->waitForIdle());
5223 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07005224}
5225
5226// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
5227// DOWN on the window that already has focus. Ensure no window received the
5228// onPointerDownOutsideFocus callback.
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005229TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005230 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005231 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07005232 FOCUSED_WINDOW_TOUCH_POINT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005233 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07005234 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07005235
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08005236 ASSERT_TRUE(mDispatcher->waitForIdle());
5237 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07005238}
5239
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08005240// Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag
5241// NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback.
5242TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) {
5243 const MotionEvent event =
5244 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
5245 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Harry Cutts33476232023-01-30 19:57:29 +00005246 .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(20).y(20))
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08005247 .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE)
5248 .build();
5249 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(mDispatcher, event))
5250 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5251 mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
5252
5253 ASSERT_TRUE(mDispatcher->waitForIdle());
5254 mFakePolicy->assertOnPointerDownWasNotCalled();
5255 // Ensure that the unfocused window did not receive any FOCUS events.
5256 mUnfocusedWindow->assertNoEvents();
5257}
5258
chaviwaf87b3e2019-10-01 16:59:28 -07005259// These tests ensures we can send touch events to a single client when there are multiple input
5260// windows that point to the same client token.
5261class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest {
5262 virtual void SetUp() override {
5263 InputDispatcherTest::SetUp();
5264
Chris Yea209fde2020-07-22 13:54:51 -07005265 std::shared_ptr<FakeApplicationHandle> application =
5266 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005267 mWindow1 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 1",
5268 ADISPLAY_ID_DEFAULT);
chaviwaf87b3e2019-10-01 16:59:28 -07005269 mWindow1->setFrame(Rect(0, 0, 100, 100));
5270
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005271 mWindow2 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 2",
5272 ADISPLAY_ID_DEFAULT, mWindow1->getToken());
chaviwaf87b3e2019-10-01 16:59:28 -07005273 mWindow2->setFrame(Rect(100, 100, 200, 200));
5274
Arthur Hung72d8dc32020-03-28 00:48:39 +00005275 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
chaviwaf87b3e2019-10-01 16:59:28 -07005276 }
5277
5278protected:
5279 sp<FakeWindowHandle> mWindow1;
5280 sp<FakeWindowHandle> mWindow2;
5281
5282 // Helper function to convert the point from screen coordinates into the window's space
chaviw3277faf2021-05-19 16:45:23 -05005283 static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) {
chaviw1ff3d1e2020-07-01 15:53:47 -07005284 vec2 vals = windowInfo->transform.transform(point.x, point.y);
5285 return {vals.x, vals.y};
chaviwaf87b3e2019-10-01 16:59:28 -07005286 }
5287
5288 void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction,
5289 const std::vector<PointF>& points) {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005290 const std::string name = window->getName();
chaviwaf87b3e2019-10-01 16:59:28 -07005291 InputEvent* event = window->consume();
5292
5293 ASSERT_NE(nullptr, event) << name.c_str()
5294 << ": consumer should have returned non-NULL event.";
5295
5296 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType())
5297 << name.c_str() << "expected " << inputEventTypeToString(AINPUT_EVENT_TYPE_MOTION)
5298 << " event, got " << inputEventTypeToString(event->getType()) << " event";
5299
5300 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005301 assertMotionAction(expectedAction, motionEvent.getAction());
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08005302 ASSERT_EQ(points.size(), motionEvent.getPointerCount());
chaviwaf87b3e2019-10-01 16:59:28 -07005303
5304 for (size_t i = 0; i < points.size(); i++) {
5305 float expectedX = points[i].x;
5306 float expectedY = points[i].y;
5307
5308 EXPECT_EQ(expectedX, motionEvent.getX(i))
5309 << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str()
5310 << ", got " << motionEvent.getX(i);
5311 EXPECT_EQ(expectedY, motionEvent.getY(i))
5312 << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str()
5313 << ", got " << motionEvent.getY(i);
5314 }
5315 }
chaviw9eaa22c2020-07-01 16:21:27 -07005316
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08005317 void touchAndAssertPositions(int32_t action, const std::vector<PointF>& touchedPoints,
chaviw9eaa22c2020-07-01 16:21:27 -07005318 std::vector<PointF> expectedPoints) {
5319 NotifyMotionArgs motionArgs = generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN,
5320 ADISPLAY_ID_DEFAULT, touchedPoints);
5321 mDispatcher->notifyMotion(&motionArgs);
5322
5323 // Always consume from window1 since it's the window that has the InputReceiver
5324 consumeMotionEvent(mWindow1, action, expectedPoints);
5325 }
chaviwaf87b3e2019-10-01 16:59:28 -07005326};
5327
5328TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) {
5329 // Touch Window 1
5330 PointF touchedPoint = {10, 10};
5331 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07005332 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07005333
5334 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07005335 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07005336
5337 // Touch Window 2
5338 touchedPoint = {150, 150};
5339 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07005340 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07005341}
5342
chaviw9eaa22c2020-07-01 16:21:27 -07005343TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) {
5344 // Set scale value for window2
chaviwaf87b3e2019-10-01 16:59:28 -07005345 mWindow2->setWindowScale(0.5f, 0.5f);
5346
5347 // Touch Window 1
5348 PointF touchedPoint = {10, 10};
5349 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07005350 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07005351 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07005352 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07005353
5354 // Touch Window 2
5355 touchedPoint = {150, 150};
5356 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07005357 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
5358 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07005359
chaviw9eaa22c2020-07-01 16:21:27 -07005360 // Update the transform so rotation is set
5361 mWindow2->setWindowTransform(0, -1, 1, 0);
5362 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
5363 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07005364}
5365
chaviw9eaa22c2020-07-01 16:21:27 -07005366TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00005367 mWindow2->setWindowScale(0.5f, 0.5f);
5368
5369 // Touch Window 1
5370 std::vector<PointF> touchedPoints = {PointF{10, 10}};
5371 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07005372 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00005373
5374 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07005375 touchedPoints.push_back(PointF{150, 150});
5376 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08005377 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00005378
chaviw9eaa22c2020-07-01 16:21:27 -07005379 // Release Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08005380 touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07005381 expectedPoints.pop_back();
Chavi Weingarten65f98b82020-01-16 18:56:50 +00005382
chaviw9eaa22c2020-07-01 16:21:27 -07005383 // Update the transform so rotation is set for Window 2
5384 mWindow2->setWindowTransform(0, -1, 1, 0);
5385 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08005386 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00005387}
5388
chaviw9eaa22c2020-07-01 16:21:27 -07005389TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00005390 mWindow2->setWindowScale(0.5f, 0.5f);
5391
5392 // Touch Window 1
5393 std::vector<PointF> touchedPoints = {PointF{10, 10}};
5394 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07005395 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00005396
5397 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07005398 touchedPoints.push_back(PointF{150, 150});
5399 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00005400
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08005401 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00005402
5403 // Move both windows
5404 touchedPoints = {{20, 20}, {175, 175}};
5405 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
5406 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
5407
chaviw9eaa22c2020-07-01 16:21:27 -07005408 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00005409
chaviw9eaa22c2020-07-01 16:21:27 -07005410 // Release Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08005411 touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07005412 expectedPoints.pop_back();
5413
5414 // Touch Window 2
5415 mWindow2->setWindowTransform(0, -1, 1, 0);
5416 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08005417 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07005418
5419 // Move both windows
5420 touchedPoints = {{20, 20}, {175, 175}};
5421 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
5422 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
5423
5424 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00005425}
5426
5427TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) {
5428 mWindow1->setWindowScale(0.5f, 0.5f);
5429
5430 // Touch Window 1
5431 std::vector<PointF> touchedPoints = {PointF{10, 10}};
5432 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07005433 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00005434
5435 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07005436 touchedPoints.push_back(PointF{150, 150});
5437 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00005438
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08005439 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00005440
5441 // Move both windows
5442 touchedPoints = {{20, 20}, {175, 175}};
5443 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
5444 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
5445
chaviw9eaa22c2020-07-01 16:21:27 -07005446 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00005447}
5448
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005449class InputDispatcherSingleWindowAnr : public InputDispatcherTest {
5450 virtual void SetUp() override {
5451 InputDispatcherTest::SetUp();
5452
Chris Yea209fde2020-07-22 13:54:51 -07005453 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005454 mApplication->setDispatchingTimeout(20ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005455 mWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "TestWindow",
5456 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005457 mWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05005458 mWindow->setDispatchingTimeout(30ms);
Vishnu Nair47074b82020-08-14 11:54:47 -07005459 mWindow->setFocusable(true);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005460
5461 // Set focused application.
5462 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
5463
5464 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005465 setFocusedWindow(mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005466 mWindow->consumeFocusEvent(true);
5467 }
5468
5469 virtual void TearDown() override {
5470 InputDispatcherTest::TearDown();
5471 mWindow.clear();
5472 }
5473
5474protected:
Chris Yea209fde2020-07-22 13:54:51 -07005475 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005476 sp<FakeWindowHandle> mWindow;
5477 static constexpr PointF WINDOW_LOCATION = {20, 20};
5478
5479 void tapOnWindow() {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005480 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005481 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5482 WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005483 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005484 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5485 WINDOW_LOCATION));
5486 }
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005487
5488 sp<FakeWindowHandle> addSpyWindow() {
5489 sp<FakeWindowHandle> spy =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005490 sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005491 spy->setTrustedOverlay(true);
5492 spy->setFocusable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08005493 spy->setSpy(true);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005494 spy->setDispatchingTimeout(30ms);
5495 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, mWindow}}});
5496 return spy;
5497 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005498};
5499
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005500// Send a tap and respond, which should not cause an ANR.
5501TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) {
5502 tapOnWindow();
5503 mWindow->consumeMotionDown();
5504 mWindow->consumeMotionUp();
5505 ASSERT_TRUE(mDispatcher->waitForIdle());
5506 mFakePolicy->assertNotifyAnrWasNotCalled();
5507}
5508
5509// Send a regular key and respond, which should not cause an ANR.
5510TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005511 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005512 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
5513 ASSERT_TRUE(mDispatcher->waitForIdle());
5514 mFakePolicy->assertNotifyAnrWasNotCalled();
5515}
5516
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05005517TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) {
5518 mWindow->setFocusable(false);
5519 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
5520 mWindow->consumeFocusEvent(false);
5521
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005522 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00005523 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
5524 InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms,
5525 /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005526 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05005527 // Key will not go to window because we have no focused window.
5528 // The 'no focused window' ANR timer should start instead.
5529
5530 // Now, the focused application goes away.
5531 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr);
5532 // The key should get dropped and there should be no ANR.
5533
5534 ASSERT_TRUE(mDispatcher->waitForIdle());
5535 mFakePolicy->assertNotifyAnrWasNotCalled();
5536}
5537
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005538// Send an event to the app and have the app not respond right away.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005539// When ANR is raised, policy will tell the dispatcher to cancel the events for that window.
5540// So InputDispatcher will enqueue ACTION_CANCEL event as well.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005541TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005542 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005543 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5544 WINDOW_LOCATION));
5545
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005546 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN
5547 ASSERT_TRUE(sequenceNum);
5548 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005549 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005550
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005551 mWindow->finishEvent(*sequenceNum);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08005552 mWindow->consumeMotionEvent(
5553 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005554 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08005555 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005556}
5557
5558// Send a key to the app and have the app not respond right away.
5559TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) {
5560 // Inject a key, and don't respond - expect that ANR is called.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005561 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005562 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent();
5563 ASSERT_TRUE(sequenceNum);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005564 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005565 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005566 ASSERT_TRUE(mDispatcher->waitForIdle());
5567}
5568
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005569// We have a focused application, but no focused window
5570TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) {
Vishnu Nair47074b82020-08-14 11:54:47 -07005571 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005572 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
5573 mWindow->consumeFocusEvent(false);
5574
5575 // taps on the window work as normal
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005576 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005577 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5578 WINDOW_LOCATION));
5579 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
5580 mDispatcher->waitForIdle();
5581 mFakePolicy->assertNotifyAnrWasNotCalled();
5582
5583 // Once a focused event arrives, we get an ANR for this application
5584 // We specify the injection timeout to be smaller than the application timeout, to ensure that
5585 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005586 const InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00005587 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
5588 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005589 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005590 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Vishnu Naire4df8752022-09-08 09:17:55 -07005591 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005592 ASSERT_TRUE(mDispatcher->waitForIdle());
5593}
5594
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08005595/**
5596 * Make sure the stale key is dropped before causing an ANR. So even if there's no focused window,
5597 * there will not be an ANR.
5598 */
5599TEST_F(InputDispatcherSingleWindowAnr, StaleKeyEventDoesNotAnr) {
5600 mWindow->setFocusable(false);
5601 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
5602 mWindow->consumeFocusEvent(false);
5603
5604 KeyEvent event;
5605 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC) -
5606 std::chrono::nanoseconds(STALE_EVENT_TIMEOUT).count();
5607
5608 // Define a valid key down event that is stale (too old).
5609 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
5610 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, /* flags */ 0, AKEYCODE_A, KEY_A,
Harry Cutts33476232023-01-30 19:57:29 +00005611 AMETA_NONE, /*repeatCount=*/1, eventTime, eventTime);
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08005612
5613 const int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER;
5614
5615 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00005616 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08005617 InputEventInjectionSync::WAIT_FOR_RESULT,
5618 INJECT_EVENT_TIMEOUT, policyFlags);
5619 ASSERT_EQ(InputEventInjectionResult::FAILED, result)
5620 << "Injection should fail because the event is stale";
5621
5622 ASSERT_TRUE(mDispatcher->waitForIdle());
5623 mFakePolicy->assertNotifyAnrWasNotCalled();
5624 mWindow->assertNoEvents();
5625}
5626
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005627// We have a focused application, but no focused window
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005628// Make sure that we don't notify policy twice about the same ANR.
5629TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) {
Vishnu Nair47074b82020-08-14 11:54:47 -07005630 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005631 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
5632 mWindow->consumeFocusEvent(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005633
5634 // Once a focused event arrives, we get an ANR for this application
5635 // We specify the injection timeout to be smaller than the application timeout, to ensure that
5636 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005637 const InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00005638 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
5639 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005640 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Vishnu Naire4df8752022-09-08 09:17:55 -07005641 const std::chrono::duration appTimeout =
5642 mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
5643 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005644
Vishnu Naire4df8752022-09-08 09:17:55 -07005645 std::this_thread::sleep_for(appTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005646 // ANR should not be raised again. It is up to policy to do that if it desires.
5647 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005648
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005649 // If we now get a focused window, the ANR should stop, but the policy handles that via
5650 // 'notifyFocusChanged' callback. This is implemented in the policy so we can't test it here.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005651 ASSERT_TRUE(mDispatcher->waitForIdle());
5652}
5653
5654// We have a focused application, but no focused window
5655TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) {
Vishnu Nair47074b82020-08-14 11:54:47 -07005656 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005657 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
5658 mWindow->consumeFocusEvent(false);
5659
5660 // Once a focused event arrives, we get an ANR for this application
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005661 const InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00005662 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005663 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
5664 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005665
Vishnu Naire4df8752022-09-08 09:17:55 -07005666 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
5667 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005668
5669 // Future focused events get dropped right away
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005670 ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005671 ASSERT_TRUE(mDispatcher->waitForIdle());
5672 mWindow->assertNoEvents();
5673}
5674
5675/**
5676 * Ensure that the implementation is valid. Since we are using multiset to keep track of the
5677 * ANR timeouts, we are allowing entries with identical timestamps in the same connection.
5678 * If we process 1 of the events, but ANR on the second event with the same timestamp,
5679 * the ANR mechanism should still work.
5680 *
5681 * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the
5682 * DOWN event, while not responding on the second one.
5683 */
5684TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) {
5685 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
5686 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5687 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
5688 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
5689 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005690 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005691
5692 // Now send ACTION_UP, with identical timestamp
5693 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
5694 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
5695 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
5696 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005697 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005698
5699 // We have now sent down and up. Let's consume first event and then ANR on the second.
5700 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5701 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005702 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005703}
5704
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005705// A spy window can receive an ANR
5706TEST_F(InputDispatcherSingleWindowAnr, SpyWindowAnr) {
5707 sp<FakeWindowHandle> spy = addSpyWindow();
5708
5709 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5710 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5711 WINDOW_LOCATION));
5712 mWindow->consumeMotionDown();
5713
5714 std::optional<uint32_t> sequenceNum = spy->receiveEvent(); // ACTION_DOWN
5715 ASSERT_TRUE(sequenceNum);
5716 const std::chrono::duration timeout = spy->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005717 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, spy);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005718
5719 spy->finishEvent(*sequenceNum);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08005720 spy->consumeMotionEvent(
5721 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005722 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08005723 mFakePolicy->assertNotifyWindowResponsiveWasCalled(spy->getToken(), mWindow->getPid());
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005724}
5725
5726// If an app is not responding to a key event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005727// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005728TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnKey) {
5729 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005730
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005731 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5732 injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005733 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005734 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005735
5736 // Stuck on the ACTION_UP
5737 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005738 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005739
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005740 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005741 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005742 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5743 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005744
5745 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion
5746 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08005747 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005748 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005749 spy->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005750}
5751
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005752// If an app is not responding to a motion event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005753// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005754TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnMotion) {
5755 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005756
5757 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005758 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5759 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005760
5761 mWindow->consumeMotionDown();
5762 // Stuck on the ACTION_UP
5763 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005764 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005765
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005766 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005767 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005768 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5769 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005770
5771 mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion
5772 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08005773 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005774 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005775 spy->assertNoEvents();
5776}
5777
5778TEST_F(InputDispatcherSingleWindowAnr, UnresponsiveMonitorAnr) {
5779 mDispatcher->setMonitorDispatchingTimeoutForTest(30ms);
5780
5781 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
5782
5783 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5784 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5785 WINDOW_LOCATION));
5786
5787 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5788 const std::optional<uint32_t> consumeSeq = monitor.receiveEvent();
5789 ASSERT_TRUE(consumeSeq);
5790
Prabir Pradhanedd96402022-02-15 01:46:16 -08005791 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(30ms, monitor.getToken(), MONITOR_PID);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005792
5793 monitor.finishEvent(*consumeSeq);
5794 monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
5795
5796 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08005797 mFakePolicy->assertNotifyWindowResponsiveWasCalled(monitor.getToken(), MONITOR_PID);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005798}
5799
5800// If a window is unresponsive, then you get anr. if the window later catches up and starts to
5801// process events, you don't get an anr. When the window later becomes unresponsive again, you
5802// get an ANR again.
5803// 1. tap -> block on ACTION_UP -> receive ANR
5804// 2. consume all pending events (= queue becomes healthy again)
5805// 3. tap again -> block on ACTION_UP again -> receive ANR second time
5806TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) {
5807 tapOnWindow();
5808
5809 mWindow->consumeMotionDown();
5810 // Block on ACTION_UP
5811 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005812 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005813 mWindow->consumeMotionUp(); // Now the connection should be healthy again
5814 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08005815 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005816 mWindow->assertNoEvents();
5817
5818 tapOnWindow();
5819 mWindow->consumeMotionDown();
Prabir Pradhanedd96402022-02-15 01:46:16 -08005820 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005821 mWindow->consumeMotionUp();
5822
5823 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08005824 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005825 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005826 mWindow->assertNoEvents();
5827}
5828
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005829// If a connection remains unresponsive for a while, make sure policy is only notified once about
5830// it.
5831TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005832 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005833 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5834 WINDOW_LOCATION));
5835
5836 const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005837 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005838 std::this_thread::sleep_for(windowTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005839 // 'notifyConnectionUnresponsive' should only be called once per connection
5840 mFakePolicy->assertNotifyAnrWasNotCalled();
5841 // When the ANR happened, dispatcher should abort the current event stream via ACTION_CANCEL
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005842 mWindow->consumeMotionDown();
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08005843 mWindow->consumeMotionEvent(
5844 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005845 mWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005846 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08005847 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005848 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005849}
5850
5851/**
5852 * If a window is processing a motion event, and then a key event comes in, the key event should
5853 * not to to the focused window until the motion is processed.
5854 *
5855 * Warning!!!
5856 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
5857 * and the injection timeout that we specify when injecting the key.
5858 * We must have the injection timeout (10ms) be smaller than
5859 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
5860 *
5861 * If that value changes, this test should also change.
5862 */
5863TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) {
5864 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
5865 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
5866
5867 tapOnWindow();
5868 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
5869 ASSERT_TRUE(downSequenceNum);
5870 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
5871 ASSERT_TRUE(upSequenceNum);
5872 // Don't finish the events yet, and send a key
5873 // Injection will "succeed" because we will eventually give up and send the key to the focused
5874 // window even if motions are still being processed. But because the injection timeout is short,
5875 // we will receive INJECTION_TIMED_OUT as the result.
5876
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005877 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00005878 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005879 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
5880 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005881 // Key will not be sent to the window, yet, because the window is still processing events
5882 // and the key remains pending, waiting for the touch events to be processed
5883 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
5884 ASSERT_FALSE(keySequenceNum);
5885
5886 std::this_thread::sleep_for(500ms);
5887 // if we wait long enough though, dispatcher will give up, and still send the key
5888 // to the focused window, even though we have not yet finished the motion event
5889 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5890 mWindow->finishEvent(*downSequenceNum);
5891 mWindow->finishEvent(*upSequenceNum);
5892}
5893
5894/**
5895 * If a window is processing a motion event, and then a key event comes in, the key event should
5896 * not go to the focused window until the motion is processed.
5897 * If then a new motion comes in, then the pending key event should be going to the currently
5898 * focused window right away.
5899 */
5900TEST_F(InputDispatcherSingleWindowAnr,
5901 PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) {
5902 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
5903 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
5904
5905 tapOnWindow();
5906 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
5907 ASSERT_TRUE(downSequenceNum);
5908 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
5909 ASSERT_TRUE(upSequenceNum);
5910 // Don't finish the events yet, and send a key
5911 // Injection is async, so it will succeed
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005912 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00005913 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
5914 InputEventInjectionSync::NONE));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005915 // At this point, key is still pending, and should not be sent to the application yet.
5916 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
5917 ASSERT_FALSE(keySequenceNum);
5918
5919 // Now tap down again. It should cause the pending key to go to the focused window right away.
5920 tapOnWindow();
5921 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd
5922 // the other events yet. We can finish events in any order.
5923 mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN
5924 mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP
5925 mWindow->consumeMotionDown();
5926 mWindow->consumeMotionUp();
5927 mWindow->assertNoEvents();
5928}
5929
5930class InputDispatcherMultiWindowAnr : public InputDispatcherTest {
5931 virtual void SetUp() override {
5932 InputDispatcherTest::SetUp();
5933
Chris Yea209fde2020-07-22 13:54:51 -07005934 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005935 mApplication->setDispatchingTimeout(10ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005936 mUnfocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Unfocused",
5937 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005938 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005939 // Adding FLAG_WATCH_OUTSIDE_TOUCH to receive ACTION_OUTSIDE when another window is tapped
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08005940 mUnfocusedWindow->setWatchOutsideTouch(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005941
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005942 mFocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Focused",
5943 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05005944 mFocusedWindow->setDispatchingTimeout(30ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005945 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005946
5947 // Set focused application.
5948 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
Vishnu Nair47074b82020-08-14 11:54:47 -07005949 mFocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005950
5951 // Expect one focus window exist in display.
5952 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005953 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005954 mFocusedWindow->consumeFocusEvent(true);
5955 }
5956
5957 virtual void TearDown() override {
5958 InputDispatcherTest::TearDown();
5959
5960 mUnfocusedWindow.clear();
5961 mFocusedWindow.clear();
5962 }
5963
5964protected:
Chris Yea209fde2020-07-22 13:54:51 -07005965 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005966 sp<FakeWindowHandle> mUnfocusedWindow;
5967 sp<FakeWindowHandle> mFocusedWindow;
5968 static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20};
5969 static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75};
5970 static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40};
5971
5972 void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); }
5973
5974 void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); }
5975
5976private:
5977 void tap(const PointF& location) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005978 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005979 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5980 location));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005981 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005982 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5983 location));
5984 }
5985};
5986
5987// If we have 2 windows that are both unresponsive, the one with the shortest timeout
5988// should be ANR'd first.
5989TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005990 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005991 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5992 FOCUSED_WINDOW_LOCATION))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005993 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005994 mFocusedWindow->consumeMotionDown();
5995 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00005996 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005997 // We consumed all events, so no ANR
5998 ASSERT_TRUE(mDispatcher->waitForIdle());
5999 mFakePolicy->assertNotifyAnrWasNotCalled();
6000
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006001 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006002 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6003 FOCUSED_WINDOW_LOCATION));
6004 std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent();
6005 ASSERT_TRUE(unfocusedSequenceNum);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006006
6007 const std::chrono::duration timeout =
6008 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006009 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006010 // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event
6011 // sequence to make it consistent
6012 mFocusedWindow->consumeMotionCancel();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006013 mUnfocusedWindow->finishEvent(*unfocusedSequenceNum);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006014 mFocusedWindow->consumeMotionDown();
6015 // This cancel is generated because the connection was unresponsive
6016 mFocusedWindow->consumeMotionCancel();
6017 mFocusedWindow->assertNoEvents();
6018 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006019 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006020 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
6021 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006022 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006023}
6024
6025// If we have 2 windows with identical timeouts that are both unresponsive,
6026// it doesn't matter which order they should have ANR.
6027// But we should receive ANR for both.
6028TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) {
6029 // Set the timeout for unfocused window to match the focused window
6030 mUnfocusedWindow->setDispatchingTimeout(10ms);
6031 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
6032
6033 tapOnFocusedWindow();
6034 // we should have ACTION_DOWN/ACTION_UP on focused window and ACTION_OUTSIDE on unfocused window
Prabir Pradhanedd96402022-02-15 01:46:16 -08006035 sp<IBinder> anrConnectionToken1, anrConnectionToken2;
6036 ASSERT_NO_FATAL_FAILURE(anrConnectionToken1 = mFakePolicy->getUnresponsiveWindowToken(10ms));
6037 ASSERT_NO_FATAL_FAILURE(anrConnectionToken2 = mFakePolicy->getUnresponsiveWindowToken(0ms));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006038
6039 // We don't know which window will ANR first. But both of them should happen eventually.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006040 ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 ||
6041 mFocusedWindow->getToken() == anrConnectionToken2);
6042 ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 ||
6043 mUnfocusedWindow->getToken() == anrConnectionToken2);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006044
6045 ASSERT_TRUE(mDispatcher->waitForIdle());
6046 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006047
6048 mFocusedWindow->consumeMotionDown();
6049 mFocusedWindow->consumeMotionUp();
6050 mUnfocusedWindow->consumeMotionOutside();
6051
Prabir Pradhanedd96402022-02-15 01:46:16 -08006052 sp<IBinder> responsiveToken1, responsiveToken2;
6053 ASSERT_NO_FATAL_FAILURE(responsiveToken1 = mFakePolicy->getResponsiveWindowToken());
6054 ASSERT_NO_FATAL_FAILURE(responsiveToken2 = mFakePolicy->getResponsiveWindowToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006055
6056 // Both applications should be marked as responsive, in any order
6057 ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 ||
6058 mFocusedWindow->getToken() == responsiveToken2);
6059 ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 ||
6060 mUnfocusedWindow->getToken() == responsiveToken2);
6061 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006062}
6063
6064// If a window is already not responding, the second tap on the same window should be ignored.
6065// We should also log an error to account for the dropped event (not tested here).
6066// At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events.
6067TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) {
6068 tapOnFocusedWindow();
6069 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00006070 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006071 // Receive the events, but don't respond
6072 std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN
6073 ASSERT_TRUE(downEventSequenceNum);
6074 std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP
6075 ASSERT_TRUE(upEventSequenceNum);
6076 const std::chrono::duration timeout =
6077 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006078 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006079
6080 // Tap once again
6081 // We cannot use "tapOnFocusedWindow" because it asserts the injection result to be success
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006082 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006083 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6084 FOCUSED_WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006085 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006086 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6087 FOCUSED_WINDOW_LOCATION));
6088 // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a
6089 // valid touch target
6090 mUnfocusedWindow->assertNoEvents();
6091
6092 // Consume the first tap
6093 mFocusedWindow->finishEvent(*downEventSequenceNum);
6094 mFocusedWindow->finishEvent(*upEventSequenceNum);
6095 ASSERT_TRUE(mDispatcher->waitForIdle());
6096 // The second tap did not go to the focused window
6097 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006098 // Since all events are finished, connection should be deemed healthy again
Prabir Pradhanedd96402022-02-15 01:46:16 -08006099 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
6100 mFocusedWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006101 mFakePolicy->assertNotifyAnrWasNotCalled();
6102}
6103
6104// If you tap outside of all windows, there will not be ANR
6105TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006106 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006107 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6108 LOCATION_OUTSIDE_ALL_WINDOWS));
6109 ASSERT_TRUE(mDispatcher->waitForIdle());
6110 mFakePolicy->assertNotifyAnrWasNotCalled();
6111}
6112
6113// Since the focused window is paused, tapping on it should not produce any events
6114TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) {
6115 mFocusedWindow->setPaused(true);
6116 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
6117
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006118 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006119 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6120 FOCUSED_WINDOW_LOCATION));
6121
6122 std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT));
6123 ASSERT_TRUE(mDispatcher->waitForIdle());
6124 // Should not ANR because the window is paused, and touches shouldn't go to it
6125 mFakePolicy->assertNotifyAnrWasNotCalled();
6126
6127 mFocusedWindow->assertNoEvents();
6128 mUnfocusedWindow->assertNoEvents();
6129}
6130
6131/**
6132 * If a window is processing a motion event, and then a key event comes in, the key event should
6133 * not to to the focused window until the motion is processed.
6134 * If a different window becomes focused at this time, the key should go to that window instead.
6135 *
6136 * Warning!!!
6137 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
6138 * and the injection timeout that we specify when injecting the key.
6139 * We must have the injection timeout (10ms) be smaller than
6140 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
6141 *
6142 * If that value changes, this test should also change.
6143 */
6144TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) {
6145 // Set a long ANR timeout to prevent it from triggering
6146 mFocusedWindow->setDispatchingTimeout(2s);
6147 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
6148
6149 tapOnUnfocusedWindow();
6150 std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent();
6151 ASSERT_TRUE(downSequenceNum);
6152 std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent();
6153 ASSERT_TRUE(upSequenceNum);
6154 // Don't finish the events yet, and send a key
6155 // Injection will succeed because we will eventually give up and send the key to the focused
6156 // window even if motions are still being processed.
6157
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006158 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006159 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6160 InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006161 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006162 // Key will not be sent to the window, yet, because the window is still processing events
6163 // and the key remains pending, waiting for the touch events to be processed
6164 std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent();
6165 ASSERT_FALSE(keySequenceNum);
6166
6167 // Switch the focus to the "unfocused" window that we tapped. Expect the key to go there
Vishnu Nair47074b82020-08-14 11:54:47 -07006168 mFocusedWindow->setFocusable(false);
6169 mUnfocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006170 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07006171 setFocusedWindow(mUnfocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006172
6173 // Focus events should precede the key events
6174 mUnfocusedWindow->consumeFocusEvent(true);
6175 mFocusedWindow->consumeFocusEvent(false);
6176
6177 // Finish the tap events, which should unblock dispatcher
6178 mUnfocusedWindow->finishEvent(*downSequenceNum);
6179 mUnfocusedWindow->finishEvent(*upSequenceNum);
6180
6181 // Now that all queues are cleared and no backlog in the connections, the key event
6182 // can finally go to the newly focused "mUnfocusedWindow".
6183 mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
6184 mFocusedWindow->assertNoEvents();
6185 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006186 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006187}
6188
6189// When the touch stream is split across 2 windows, and one of them does not respond,
6190// then ANR should be raised and the touch should be canceled for the unresponsive window.
6191// The other window should not be affected by that.
6192TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) {
6193 // Touch Window 1
6194 NotifyMotionArgs motionArgs =
6195 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6196 ADISPLAY_ID_DEFAULT, {FOCUSED_WINDOW_LOCATION});
6197 mDispatcher->notifyMotion(&motionArgs);
6198 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00006199 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006200
6201 // Touch Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006202 motionArgs = generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6203 {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006204 mDispatcher->notifyMotion(&motionArgs);
6205
6206 const std::chrono::duration timeout =
6207 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006208 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006209
6210 mUnfocusedWindow->consumeMotionDown();
6211 mFocusedWindow->consumeMotionDown();
6212 // Focused window may or may not receive ACTION_MOVE
6213 // But it should definitely receive ACTION_CANCEL due to the ANR
6214 InputEvent* event;
6215 std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event);
6216 ASSERT_TRUE(moveOrCancelSequenceNum);
6217 mFocusedWindow->finishEvent(*moveOrCancelSequenceNum);
6218 ASSERT_NE(nullptr, event);
6219 ASSERT_EQ(event->getType(), AINPUT_EVENT_TYPE_MOTION);
6220 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
6221 if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) {
6222 mFocusedWindow->consumeMotionCancel();
6223 } else {
6224 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction());
6225 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006226 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006227 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
6228 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006229
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006230 mUnfocusedWindow->assertNoEvents();
6231 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006232 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006233}
6234
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05006235/**
6236 * If we have no focused window, and a key comes in, we start the ANR timer.
6237 * The focused application should add a focused window before the timer runs out to prevent ANR.
6238 *
6239 * If the user touches another application during this time, the key should be dropped.
6240 * Next, if a new focused window comes in, without toggling the focused application,
6241 * then no ANR should occur.
6242 *
6243 * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication',
6244 * but in some cases the policy may not update the focused application.
6245 */
6246TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) {
6247 std::shared_ptr<FakeApplicationHandle> focusedApplication =
6248 std::make_shared<FakeApplicationHandle>();
6249 focusedApplication->setDispatchingTimeout(60ms);
6250 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication);
6251 // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused.
6252 mFocusedWindow->setFocusable(false);
6253
6254 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
6255 mFocusedWindow->consumeFocusEvent(false);
6256
6257 // Send a key. The ANR timer should start because there is no focused window.
6258 // 'focusedApplication' will get blamed if this timer completes.
6259 // Key will not be sent anywhere because we have no focused window. It will remain pending.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006260 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006261 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6262 InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms,
6263 /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006264 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05006265
6266 // Wait until dispatcher starts the "no focused window" timer. If we don't wait here,
6267 // then the injected touches won't cause the focused event to get dropped.
6268 // The dispatcher only checks for whether the queue should be pruned upon queueing.
6269 // If we inject the touch right away and the ANR timer hasn't started, the touch event would
6270 // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'.
6271 // For this test, it means that the key would get delivered to the window once it becomes
6272 // focused.
6273 std::this_thread::sleep_for(10ms);
6274
6275 // Touch unfocused window. This should force the pending key to get dropped.
6276 NotifyMotionArgs motionArgs =
6277 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6278 ADISPLAY_ID_DEFAULT, {UNFOCUSED_WINDOW_LOCATION});
6279 mDispatcher->notifyMotion(&motionArgs);
6280
6281 // We do not consume the motion right away, because that would require dispatcher to first
6282 // process (== drop) the key event, and by that time, ANR will be raised.
6283 // Set the focused window first.
6284 mFocusedWindow->setFocusable(true);
6285 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
6286 setFocusedWindow(mFocusedWindow);
6287 mFocusedWindow->consumeFocusEvent(true);
6288 // We do not call "setFocusedApplication" here, even though the newly focused window belongs
6289 // to another application. This could be a bug / behaviour in the policy.
6290
6291 mUnfocusedWindow->consumeMotionDown();
6292
6293 ASSERT_TRUE(mDispatcher->waitForIdle());
6294 // Should not ANR because we actually have a focused window. It was just added too slowly.
6295 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled());
6296}
6297
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05006298// These tests ensure we cannot send touch events to a window that's positioned behind a window
6299// that has feature NO_INPUT_CHANNEL.
6300// Layout:
6301// Top (closest to user)
6302// mNoInputWindow (above all windows)
6303// mBottomWindow
6304// Bottom (furthest from user)
6305class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest {
6306 virtual void SetUp() override {
6307 InputDispatcherTest::SetUp();
6308
6309 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006310 mNoInputWindow =
6311 sp<FakeWindowHandle>::make(mApplication, mDispatcher,
6312 "Window without input channel", ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00006313 /*token=*/std::make_optional<sp<IBinder>>(nullptr));
Prabir Pradhan51e7db02022-02-07 06:02:57 -08006314 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05006315 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
6316 // It's perfectly valid for this window to not have an associated input channel
6317
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006318 mBottomWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Bottom window",
6319 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05006320 mBottomWindow->setFrame(Rect(0, 0, 100, 100));
6321
6322 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
6323 }
6324
6325protected:
6326 std::shared_ptr<FakeApplicationHandle> mApplication;
6327 sp<FakeWindowHandle> mNoInputWindow;
6328 sp<FakeWindowHandle> mBottomWindow;
6329};
6330
6331TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) {
6332 PointF touchedPoint = {10, 10};
6333
6334 NotifyMotionArgs motionArgs =
6335 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6336 ADISPLAY_ID_DEFAULT, {touchedPoint});
6337 mDispatcher->notifyMotion(&motionArgs);
6338
6339 mNoInputWindow->assertNoEvents();
6340 // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have
6341 // an input channel, it is not marked as FLAG_NOT_TOUCHABLE,
6342 // and therefore should prevent mBottomWindow from receiving touches
6343 mBottomWindow->assertNoEvents();
6344}
6345
6346/**
6347 * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel,
6348 * ensure that this window does not receive any touches, and blocks touches to windows underneath.
6349 */
6350TEST_F(InputDispatcherMultiWindowOcclusionTests,
6351 NoInputChannelFeature_DropsTouchesWithValidChannel) {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006352 mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher,
6353 "Window with input channel and NO_INPUT_CHANNEL",
6354 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05006355
Prabir Pradhan51e7db02022-02-07 06:02:57 -08006356 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05006357 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
6358 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
6359
6360 PointF touchedPoint = {10, 10};
6361
6362 NotifyMotionArgs motionArgs =
6363 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6364 ADISPLAY_ID_DEFAULT, {touchedPoint});
6365 mDispatcher->notifyMotion(&motionArgs);
6366
6367 mNoInputWindow->assertNoEvents();
6368 mBottomWindow->assertNoEvents();
6369}
6370
Vishnu Nair958da932020-08-21 17:12:37 -07006371class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest {
6372protected:
6373 std::shared_ptr<FakeApplicationHandle> mApp;
6374 sp<FakeWindowHandle> mWindow;
6375 sp<FakeWindowHandle> mMirror;
6376
6377 virtual void SetUp() override {
6378 InputDispatcherTest::SetUp();
6379 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006380 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
6381 mMirror = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindowMirror",
6382 ADISPLAY_ID_DEFAULT, mWindow->getToken());
Vishnu Nair958da932020-08-21 17:12:37 -07006383 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
6384 mWindow->setFocusable(true);
6385 mMirror->setFocusable(true);
6386 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
6387 }
6388};
6389
6390TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) {
6391 // Request focus on a mirrored window
6392 setFocusedWindow(mMirror);
6393
6394 // window gets focused
6395 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006396 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
6397 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07006398 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
6399}
6400
6401// A focused & mirrored window remains focused only if the window and its mirror are both
6402// focusable.
6403TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) {
6404 setFocusedWindow(mMirror);
6405
6406 // window gets focused
6407 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006408 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
6409 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07006410 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006411 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
6412 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07006413 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
6414
6415 mMirror->setFocusable(false);
6416 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
6417
6418 // window loses focus since one of the windows associated with the token in not focusable
6419 mWindow->consumeFocusEvent(false);
6420
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006421 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
6422 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07006423 mWindow->assertNoEvents();
6424}
6425
6426// A focused & mirrored window remains focused until the window and its mirror both become
6427// invisible.
6428TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) {
6429 setFocusedWindow(mMirror);
6430
6431 // window gets focused
6432 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006433 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
6434 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07006435 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006436 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
6437 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07006438 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
6439
6440 mMirror->setVisible(false);
6441 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
6442
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006443 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
6444 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07006445 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006446 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
6447 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07006448 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
6449
6450 mWindow->setVisible(false);
6451 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
6452
6453 // window loses focus only after all windows associated with the token become invisible.
6454 mWindow->consumeFocusEvent(false);
6455
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006456 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
6457 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07006458 mWindow->assertNoEvents();
6459}
6460
6461// A focused & mirrored window remains focused until both windows are removed.
6462TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) {
6463 setFocusedWindow(mMirror);
6464
6465 // window gets focused
6466 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006467 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
6468 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07006469 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006470 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
6471 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07006472 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
6473
6474 // single window is removed but the window token remains focused
6475 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}});
6476
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006477 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
6478 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07006479 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006480 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
6481 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07006482 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
6483
6484 // Both windows are removed
6485 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
6486 mWindow->consumeFocusEvent(false);
6487
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006488 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
6489 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07006490 mWindow->assertNoEvents();
6491}
6492
6493// Focus request can be pending until one window becomes visible.
6494TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) {
6495 // Request focus on an invisible mirror.
6496 mWindow->setVisible(false);
6497 mMirror->setVisible(false);
6498 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
6499 setFocusedWindow(mMirror);
6500
6501 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006502 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00006503 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6504 InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07006505
6506 mMirror->setVisible(true);
6507 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
6508
6509 // window gets focused
6510 mWindow->consumeFocusEvent(true);
6511 // window gets the pending key event
6512 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
6513}
Prabir Pradhan99987712020-11-10 18:43:05 -08006514
6515class InputDispatcherPointerCaptureTests : public InputDispatcherTest {
6516protected:
6517 std::shared_ptr<FakeApplicationHandle> mApp;
6518 sp<FakeWindowHandle> mWindow;
6519 sp<FakeWindowHandle> mSecondWindow;
6520
6521 void SetUp() override {
6522 InputDispatcherTest::SetUp();
6523 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006524 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -08006525 mWindow->setFocusable(true);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006526 mSecondWindow =
6527 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -08006528 mSecondWindow->setFocusable(true);
6529
6530 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
6531 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}});
6532
6533 setFocusedWindow(mWindow);
6534 mWindow->consumeFocusEvent(true);
6535 }
6536
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006537 void notifyPointerCaptureChanged(const PointerCaptureRequest& request) {
6538 const NotifyPointerCaptureChangedArgs args = generatePointerCaptureChangedArgs(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08006539 mDispatcher->notifyPointerCaptureChanged(&args);
6540 }
6541
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006542 PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window,
6543 bool enabled) {
Prabir Pradhan99987712020-11-10 18:43:05 -08006544 mDispatcher->requestPointerCapture(window->getToken(), enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006545 auto request = mFakePolicy->assertSetPointerCaptureCalled(enabled);
6546 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08006547 window->consumeCaptureEvent(enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006548 return request;
Prabir Pradhan99987712020-11-10 18:43:05 -08006549 }
6550};
6551
6552TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) {
6553 // Ensure that capture cannot be obtained for unfocused windows.
6554 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
6555 mFakePolicy->assertSetPointerCaptureNotCalled();
6556 mSecondWindow->assertNoEvents();
6557
6558 // Ensure that capture can be enabled from the focus window.
6559 requestAndVerifyPointerCapture(mWindow, true);
6560
6561 // Ensure that capture cannot be disabled from a window that does not have capture.
6562 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false);
6563 mFakePolicy->assertSetPointerCaptureNotCalled();
6564
6565 // Ensure that capture can be disabled from the window with capture.
6566 requestAndVerifyPointerCapture(mWindow, false);
6567}
6568
6569TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006570 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08006571
6572 setFocusedWindow(mSecondWindow);
6573
6574 // Ensure that the capture disabled event was sent first.
6575 mWindow->consumeCaptureEvent(false);
6576 mWindow->consumeFocusEvent(false);
6577 mSecondWindow->consumeFocusEvent(true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006578 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08006579
6580 // Ensure that additional state changes from InputReader are not sent to the window.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006581 notifyPointerCaptureChanged({});
6582 notifyPointerCaptureChanged(request);
6583 notifyPointerCaptureChanged({});
Prabir Pradhan99987712020-11-10 18:43:05 -08006584 mWindow->assertNoEvents();
6585 mSecondWindow->assertNoEvents();
6586 mFakePolicy->assertSetPointerCaptureNotCalled();
6587}
6588
6589TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006590 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08006591
6592 // InputReader unexpectedly disables and enables pointer capture.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006593 notifyPointerCaptureChanged({});
6594 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08006595
6596 // Ensure that Pointer Capture is disabled.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006597 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08006598 mWindow->consumeCaptureEvent(false);
6599 mWindow->assertNoEvents();
6600}
6601
Prabir Pradhan167e6d92021-02-04 16:18:17 -08006602TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) {
6603 requestAndVerifyPointerCapture(mWindow, true);
6604
6605 // The first window loses focus.
6606 setFocusedWindow(mSecondWindow);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006607 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08006608 mWindow->consumeCaptureEvent(false);
6609
6610 // Request Pointer Capture from the second window before the notification from InputReader
6611 // arrives.
6612 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006613 auto request = mFakePolicy->assertSetPointerCaptureCalled(true);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08006614
6615 // InputReader notifies Pointer Capture was disabled (because of the focus change).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006616 notifyPointerCaptureChanged({});
Prabir Pradhan167e6d92021-02-04 16:18:17 -08006617
6618 // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006619 notifyPointerCaptureChanged(request);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08006620
6621 mSecondWindow->consumeFocusEvent(true);
6622 mSecondWindow->consumeCaptureEvent(true);
6623}
6624
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006625TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) {
6626 // App repeatedly enables and disables capture.
6627 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
6628 auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
6629 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
6630 mFakePolicy->assertSetPointerCaptureCalled(false);
6631 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
6632 auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
6633
6634 // InputReader notifies that PointerCapture has been enabled for the first request. Since the
6635 // first request is now stale, this should do nothing.
6636 notifyPointerCaptureChanged(firstRequest);
6637 mWindow->assertNoEvents();
6638
6639 // InputReader notifies that the second request was enabled.
6640 notifyPointerCaptureChanged(secondRequest);
6641 mWindow->consumeCaptureEvent(true);
6642}
6643
Prabir Pradhan7092e262022-05-03 16:51:09 +00006644TEST_F(InputDispatcherPointerCaptureTests, RapidToggleRequests) {
6645 requestAndVerifyPointerCapture(mWindow, true);
6646
6647 // App toggles pointer capture off and on.
6648 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
6649 mFakePolicy->assertSetPointerCaptureCalled(false);
6650
6651 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
6652 auto enableRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
6653
6654 // InputReader notifies that the latest "enable" request was processed, while skipping over the
6655 // preceding "disable" request.
6656 notifyPointerCaptureChanged(enableRequest);
6657
6658 // Since pointer capture was never disabled during the rapid toggle, the window does not receive
6659 // any notifications.
6660 mWindow->assertNoEvents();
6661}
6662
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006663class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest {
6664protected:
6665 constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8;
Bernardo Rufino7393d172021-02-26 13:56:11 +00006666
6667 constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9;
6668 static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY);
6669
6670 constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7;
6671 static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
6672
6673 // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold
6674 constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5;
6675 static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
6676 static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) <
6677 MAXIMUM_OBSCURING_OPACITY);
6678
Bernardo Rufino6d52e542021-02-15 18:38:10 +00006679 static const int32_t TOUCHED_APP_UID = 10001;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006680 static const int32_t APP_B_UID = 10002;
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006681 static const int32_t APP_C_UID = 10003;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006682
6683 sp<FakeWindowHandle> mTouchWindow;
6684
6685 virtual void SetUp() override {
6686 InputDispatcherTest::SetUp();
Bernardo Rufino6d52e542021-02-15 18:38:10 +00006687 mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched");
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006688 mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY);
6689 }
6690
6691 virtual void TearDown() override {
6692 InputDispatcherTest::TearDown();
6693 mTouchWindow.clear();
6694 }
6695
chaviw3277faf2021-05-19 16:45:23 -05006696 sp<FakeWindowHandle> getOccludingWindow(int32_t uid, std::string name, TouchOcclusionMode mode,
6697 float alpha = 1.0f) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006698 sp<FakeWindowHandle> window = getWindow(uid, name);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08006699 window->setTouchable(false);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006700 window->setTouchOcclusionMode(mode);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006701 window->setAlpha(alpha);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006702 return window;
6703 }
6704
6705 sp<FakeWindowHandle> getWindow(int32_t uid, std::string name) {
6706 std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>();
6707 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006708 sp<FakeWindowHandle>::make(app, mDispatcher, name, ADISPLAY_ID_DEFAULT);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006709 // Generate an arbitrary PID based on the UID
6710 window->setOwnerInfo(1777 + (uid % 10000), uid);
6711 return window;
6712 }
6713
6714 void touch(const std::vector<PointF>& points = {PointF{100, 200}}) {
6715 NotifyMotionArgs args =
6716 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6717 ADISPLAY_ID_DEFAULT, points);
6718 mDispatcher->notifyMotion(&args);
6719 }
6720};
6721
6722TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006723 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006724 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006725 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006726
6727 touch();
6728
6729 mTouchWindow->assertNoEvents();
6730}
6731
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00006732TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufino7393d172021-02-26 13:56:11 +00006733 WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) {
6734 const sp<FakeWindowHandle>& w =
6735 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f);
6736 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6737
6738 touch();
6739
6740 mTouchWindow->assertNoEvents();
6741}
6742
6743TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00006744 WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) {
6745 const sp<FakeWindowHandle>& w =
6746 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
6747 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6748
6749 touch();
6750
6751 w->assertNoEvents();
6752}
6753
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006754TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006755 const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW);
6756 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006757
6758 touch();
6759
6760 mTouchWindow->consumeAnyMotionDown();
6761}
6762
6763TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006764 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006765 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006766 w->setFrame(Rect(0, 0, 50, 50));
6767 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006768
6769 touch({PointF{100, 100}});
6770
6771 mTouchWindow->consumeAnyMotionDown();
6772}
6773
6774TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006775 const sp<FakeWindowHandle>& w =
Bernardo Rufino6d52e542021-02-15 18:38:10 +00006776 getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006777 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6778
6779 touch();
6780
6781 mTouchWindow->consumeAnyMotionDown();
6782}
6783
6784TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) {
6785 const sp<FakeWindowHandle>& w =
6786 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
6787 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006788
6789 touch();
6790
6791 mTouchWindow->consumeAnyMotionDown();
6792}
6793
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00006794TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) {
6795 const sp<FakeWindowHandle>& w =
6796 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
6797 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6798
6799 touch();
6800
6801 w->assertNoEvents();
6802}
6803
6804/**
6805 * This is important to make sure apps can't indirectly learn the position of touches (outside vs
6806 * inside) while letting them pass-through. Note that even though touch passes through the occluding
6807 * window, the occluding window will still receive ACTION_OUTSIDE event.
6808 */
6809TEST_F(InputDispatcherUntrustedTouchesTest,
6810 WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) {
6811 const sp<FakeWindowHandle>& w =
6812 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08006813 w->setWatchOutsideTouch(true);
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00006814 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6815
6816 touch();
6817
6818 w->consumeMotionOutside();
6819}
6820
6821TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) {
6822 const sp<FakeWindowHandle>& w =
6823 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08006824 w->setWatchOutsideTouch(true);
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00006825 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6826
6827 touch();
6828
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08006829 w->consumeMotionOutsideWithZeroedCoords();
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00006830}
6831
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006832TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006833 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006834 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
6835 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006836 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6837
6838 touch();
6839
6840 mTouchWindow->consumeAnyMotionDown();
6841}
6842
6843TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) {
6844 const sp<FakeWindowHandle>& w =
6845 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
6846 MAXIMUM_OBSCURING_OPACITY);
6847 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006848
6849 touch();
6850
6851 mTouchWindow->consumeAnyMotionDown();
6852}
6853
6854TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006855 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006856 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
6857 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006858 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6859
6860 touch();
6861
6862 mTouchWindow->assertNoEvents();
6863}
6864
6865TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) {
6866 // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91
6867 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006868 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
6869 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006870 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006871 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
6872 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006873 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
6874
6875 touch();
6876
6877 mTouchWindow->assertNoEvents();
6878}
6879
6880TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) {
6881 // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75
6882 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006883 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
6884 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006885 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006886 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
6887 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006888 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
6889
6890 touch();
6891
6892 mTouchWindow->consumeAnyMotionDown();
6893}
6894
6895TEST_F(InputDispatcherUntrustedTouchesTest,
6896 WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) {
6897 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006898 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
6899 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006900 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006901 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
6902 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006903 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
6904
6905 touch();
6906
6907 mTouchWindow->consumeAnyMotionDown();
6908}
6909
6910TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) {
6911 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006912 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
6913 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006914 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006915 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
6916 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006917 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006918
6919 touch();
6920
6921 mTouchWindow->assertNoEvents();
6922}
6923
Bernardo Rufino6d52e542021-02-15 18:38:10 +00006924TEST_F(InputDispatcherUntrustedTouchesTest,
6925 WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) {
6926 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006927 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
6928 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00006929 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006930 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
6931 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00006932 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
6933
6934 touch();
6935
6936 mTouchWindow->assertNoEvents();
6937}
6938
6939TEST_F(InputDispatcherUntrustedTouchesTest,
6940 WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) {
6941 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006942 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
6943 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00006944 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006945 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
6946 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00006947 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
6948
6949 touch();
6950
6951 mTouchWindow->consumeAnyMotionDown();
6952}
6953
6954TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) {
6955 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006956 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
6957 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00006958 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6959
6960 touch();
6961
6962 mTouchWindow->consumeAnyMotionDown();
6963}
6964
6965TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) {
6966 const sp<FakeWindowHandle>& w =
6967 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED);
6968 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6969
6970 touch();
6971
6972 mTouchWindow->consumeAnyMotionDown();
6973}
6974
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00006975TEST_F(InputDispatcherUntrustedTouchesTest,
6976 OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) {
6977 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
6978 const sp<FakeWindowHandle>& w =
6979 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f);
6980 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6981
6982 touch();
6983
6984 mTouchWindow->assertNoEvents();
6985}
6986
6987TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) {
6988 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
6989 const sp<FakeWindowHandle>& w =
6990 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f);
6991 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6992
6993 touch();
6994
6995 mTouchWindow->consumeAnyMotionDown();
6996}
6997
6998TEST_F(InputDispatcherUntrustedTouchesTest,
6999 OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) {
7000 mDispatcher->setMaximumObscuringOpacityForTouch(1.0f);
7001 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007002 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7003 OPACITY_ABOVE_THRESHOLD);
7004 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7005
7006 touch();
7007
7008 mTouchWindow->consumeAnyMotionDown();
7009}
7010
7011TEST_F(InputDispatcherUntrustedTouchesTest,
7012 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) {
7013 const sp<FakeWindowHandle>& w1 =
7014 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
7015 OPACITY_BELOW_THRESHOLD);
7016 const sp<FakeWindowHandle>& w2 =
7017 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7018 OPACITY_BELOW_THRESHOLD);
7019 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
7020
7021 touch();
7022
7023 mTouchWindow->assertNoEvents();
7024}
7025
7026/**
7027 * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the
7028 * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold
7029 * (which alone would result in allowing touches) does not affect the blocking behavior.
7030 */
7031TEST_F(InputDispatcherUntrustedTouchesTest,
7032 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) {
7033 const sp<FakeWindowHandle>& wB =
7034 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
7035 OPACITY_BELOW_THRESHOLD);
7036 const sp<FakeWindowHandle>& wC =
7037 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
7038 OPACITY_BELOW_THRESHOLD);
7039 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
7040
7041 touch();
7042
7043 mTouchWindow->assertNoEvents();
7044}
7045
7046/**
7047 * This test is testing that a window from a different UID but with same application token doesn't
7048 * block the touch. Apps can share the application token for close UI collaboration for example.
7049 */
7050TEST_F(InputDispatcherUntrustedTouchesTest,
7051 WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) {
7052 const sp<FakeWindowHandle>& w =
7053 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
7054 w->setApplicationToken(mTouchWindow->getApplicationToken());
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00007055 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7056
7057 touch();
7058
7059 mTouchWindow->consumeAnyMotionDown();
7060}
7061
arthurhungb89ccb02020-12-30 16:19:01 +08007062class InputDispatcherDragTests : public InputDispatcherTest {
7063protected:
7064 std::shared_ptr<FakeApplicationHandle> mApp;
7065 sp<FakeWindowHandle> mWindow;
7066 sp<FakeWindowHandle> mSecondWindow;
7067 sp<FakeWindowHandle> mDragWindow;
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00007068 sp<FakeWindowHandle> mSpyWindow;
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007069 // Mouse would force no-split, set the id as non-zero to verify if drag state could track it.
7070 static constexpr int32_t MOUSE_POINTER_ID = 1;
arthurhungb89ccb02020-12-30 16:19:01 +08007071
7072 void SetUp() override {
7073 InputDispatcherTest::SetUp();
7074 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007075 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +08007076 mWindow->setFrame(Rect(0, 0, 100, 100));
arthurhungb89ccb02020-12-30 16:19:01 +08007077
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007078 mSecondWindow =
7079 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +08007080 mSecondWindow->setFrame(Rect(100, 0, 200, 100));
arthurhungb89ccb02020-12-30 16:19:01 +08007081
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007082 mSpyWindow =
7083 sp<FakeWindowHandle>::make(mApp, mDispatcher, "SpyWindow", ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00007084 mSpyWindow->setSpy(true);
7085 mSpyWindow->setTrustedOverlay(true);
7086 mSpyWindow->setFrame(Rect(0, 0, 200, 100));
7087
arthurhungb89ccb02020-12-30 16:19:01 +08007088 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00007089 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mSpyWindow, mWindow, mSecondWindow}}});
arthurhungb89ccb02020-12-30 16:19:01 +08007090 }
7091
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007092 void injectDown(int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
7093 switch (fromSource) {
7094 case AINPUT_SOURCE_TOUCHSCREEN:
7095 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7096 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
7097 ADISPLAY_ID_DEFAULT, {50, 50}))
7098 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7099 break;
7100 case AINPUT_SOURCE_STYLUS:
7101 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7102 injectMotionEvent(
7103 mDispatcher,
7104 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
7105 AINPUT_SOURCE_STYLUS)
7106 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
7107 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS)
7108 .x(50)
7109 .y(50))
7110 .build()));
7111 break;
7112 case AINPUT_SOURCE_MOUSE:
7113 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7114 injectMotionEvent(
7115 mDispatcher,
7116 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
7117 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
7118 .pointer(PointerBuilder(MOUSE_POINTER_ID,
7119 AMOTION_EVENT_TOOL_TYPE_MOUSE)
7120 .x(50)
7121 .y(50))
7122 .build()));
7123 break;
7124 default:
7125 FAIL() << "Source " << fromSource << " doesn't support drag and drop";
7126 }
arthurhungb89ccb02020-12-30 16:19:01 +08007127
7128 // Window should receive motion event.
7129 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00007130 // Spy window should also receive motion event
7131 mSpyWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung54745652022-04-20 07:17:41 +00007132 }
7133
7134 // Start performing drag, we will create a drag window and transfer touch to it.
7135 // @param sendDown : if true, send a motion down on first window before perform drag and drop.
7136 // Returns true on success.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007137 bool startDrag(bool sendDown = true, int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
Arthur Hung54745652022-04-20 07:17:41 +00007138 if (sendDown) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007139 injectDown(fromSource);
Arthur Hung54745652022-04-20 07:17:41 +00007140 }
arthurhungb89ccb02020-12-30 16:19:01 +08007141
7142 // The drag window covers the entire display
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007143 mDragWindow =
7144 sp<FakeWindowHandle>::make(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00007145 mDragWindow->setTouchableRegion(Region{{0, 0, 0, 0}});
arthurhungb89ccb02020-12-30 16:19:01 +08007146 mDispatcher->setInputWindows(
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00007147 {{ADISPLAY_ID_DEFAULT, {mDragWindow, mSpyWindow, mWindow, mSecondWindow}}});
arthurhungb89ccb02020-12-30 16:19:01 +08007148
7149 // Transfer touch focus to the drag window
Arthur Hung54745652022-04-20 07:17:41 +00007150 bool transferred =
7151 mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(),
Harry Cutts33476232023-01-30 19:57:29 +00007152 /*isDragDrop=*/true);
Arthur Hung54745652022-04-20 07:17:41 +00007153 if (transferred) {
7154 mWindow->consumeMotionCancel();
7155 mDragWindow->consumeMotionDown();
7156 }
7157 return transferred;
arthurhungb89ccb02020-12-30 16:19:01 +08007158 }
7159};
7160
7161TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007162 startDrag();
arthurhungb89ccb02020-12-30 16:19:01 +08007163
7164 // Move on window.
7165 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7166 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
7167 ADISPLAY_ID_DEFAULT, {50, 50}))
7168 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7169 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7170 mWindow->consumeDragEvent(false, 50, 50);
7171 mSecondWindow->assertNoEvents();
7172
7173 // Move to another window.
7174 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7175 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
7176 ADISPLAY_ID_DEFAULT, {150, 50}))
7177 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7178 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7179 mWindow->consumeDragEvent(true, 150, 50);
7180 mSecondWindow->consumeDragEvent(false, 50, 50);
7181
7182 // Move back to original window.
7183 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7184 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
7185 ADISPLAY_ID_DEFAULT, {50, 50}))
7186 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7187 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7188 mWindow->consumeDragEvent(false, 50, 50);
7189 mSecondWindow->consumeDragEvent(true, -50, 50);
7190
7191 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7192 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {50, 50}))
7193 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7194 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
7195 mWindow->assertNoEvents();
7196 mSecondWindow->assertNoEvents();
7197}
7198
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00007199TEST_F(InputDispatcherDragTests, DragEnterAndPointerDownPilfersPointers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007200 startDrag();
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00007201
7202 // No cancel event after drag start
7203 mSpyWindow->assertNoEvents();
7204
7205 const MotionEvent secondFingerDownEvent =
7206 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7207 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Harry Cutts33476232023-01-30 19:57:29 +00007208 .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
7209 .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(60).y(60))
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00007210 .build();
7211 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7212 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
7213 InputEventInjectionSync::WAIT_FOR_RESULT))
7214 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7215
7216 // Receives cancel for first pointer after next pointer down
7217 mSpyWindow->consumeMotionCancel();
7218 mSpyWindow->consumeMotionDown();
7219
7220 mSpyWindow->assertNoEvents();
7221}
7222
arthurhungf452d0b2021-01-06 00:19:52 +08007223TEST_F(InputDispatcherDragTests, DragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007224 startDrag();
arthurhungf452d0b2021-01-06 00:19:52 +08007225
7226 // Move on window.
7227 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7228 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
7229 ADISPLAY_ID_DEFAULT, {50, 50}))
7230 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7231 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7232 mWindow->consumeDragEvent(false, 50, 50);
7233 mSecondWindow->assertNoEvents();
7234
7235 // Move to another window.
7236 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7237 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
7238 ADISPLAY_ID_DEFAULT, {150, 50}))
7239 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7240 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7241 mWindow->consumeDragEvent(true, 150, 50);
7242 mSecondWindow->consumeDragEvent(false, 50, 50);
7243
7244 // drop to another window.
7245 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7246 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7247 {150, 50}))
7248 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7249 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
7250 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
7251 mWindow->assertNoEvents();
7252 mSecondWindow->assertNoEvents();
7253}
7254
arthurhung6d4bed92021-03-17 11:59:33 +08007255TEST_F(InputDispatcherDragTests, StylusDragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007256 startDrag(true, AINPUT_SOURCE_STYLUS);
arthurhung6d4bed92021-03-17 11:59:33 +08007257
7258 // Move on window and keep button pressed.
7259 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7260 injectMotionEvent(mDispatcher,
7261 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
7262 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
7263 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS)
7264 .x(50)
7265 .y(50))
7266 .build()))
7267 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7268 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7269 mWindow->consumeDragEvent(false, 50, 50);
7270 mSecondWindow->assertNoEvents();
7271
7272 // Move to another window and release button, expect to drop item.
7273 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7274 injectMotionEvent(mDispatcher,
7275 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
7276 .buttonState(0)
7277 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS)
7278 .x(150)
7279 .y(50))
7280 .build()))
7281 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7282 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7283 mWindow->assertNoEvents();
7284 mSecondWindow->assertNoEvents();
7285 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
7286
7287 // nothing to the window.
7288 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7289 injectMotionEvent(mDispatcher,
7290 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS)
7291 .buttonState(0)
7292 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS)
7293 .x(150)
7294 .y(50))
7295 .build()))
7296 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7297 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
7298 mWindow->assertNoEvents();
7299 mSecondWindow->assertNoEvents();
7300}
7301
Arthur Hung54745652022-04-20 07:17:41 +00007302TEST_F(InputDispatcherDragTests, DragAndDropOnInvalidWindow) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007303 startDrag();
Arthur Hung6d0571e2021-04-09 20:18:16 +08007304
7305 // Set second window invisible.
7306 mSecondWindow->setVisible(false);
7307 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}});
7308
7309 // Move on window.
7310 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7311 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
7312 ADISPLAY_ID_DEFAULT, {50, 50}))
7313 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7314 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7315 mWindow->consumeDragEvent(false, 50, 50);
7316 mSecondWindow->assertNoEvents();
7317
7318 // Move to another window.
7319 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7320 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
7321 ADISPLAY_ID_DEFAULT, {150, 50}))
7322 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7323 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7324 mWindow->consumeDragEvent(true, 150, 50);
7325 mSecondWindow->assertNoEvents();
7326
7327 // drop to another window.
7328 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7329 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7330 {150, 50}))
7331 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7332 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
7333 mFakePolicy->assertDropTargetEquals(nullptr);
7334 mWindow->assertNoEvents();
7335 mSecondWindow->assertNoEvents();
7336}
7337
Arthur Hung54745652022-04-20 07:17:41 +00007338TEST_F(InputDispatcherDragTests, NoDragAndDropWhenMultiFingers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007339 // Ensure window could track pointerIds if it didn't support split touch.
7340 mWindow->setPreventSplitting(true);
7341
Arthur Hung54745652022-04-20 07:17:41 +00007342 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7343 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7344 {50, 50}))
7345 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7346 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7347
7348 const MotionEvent secondFingerDownEvent =
7349 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7350 .displayId(ADISPLAY_ID_DEFAULT)
7351 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Harry Cutts33476232023-01-30 19:57:29 +00007352 .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
7353 .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(75).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00007354 .build();
7355 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7356 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
7357 InputEventInjectionSync::WAIT_FOR_RESULT))
7358 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Harry Cutts33476232023-01-30 19:57:29 +00007359 mWindow->consumeMotionPointerDown(/*pointerIndex=*/1);
Arthur Hung54745652022-04-20 07:17:41 +00007360
7361 // Should not perform drag and drop when window has multi fingers.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007362 ASSERT_FALSE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +00007363}
7364
7365TEST_F(InputDispatcherDragTests, DragAndDropWhenSplitTouch) {
7366 // First down on second window.
7367 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7368 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7369 {150, 50}))
7370 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7371
7372 mSecondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7373
7374 // Second down on first window.
7375 const MotionEvent secondFingerDownEvent =
7376 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7377 .displayId(ADISPLAY_ID_DEFAULT)
7378 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Harry Cutts33476232023-01-30 19:57:29 +00007379 .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50))
7380 .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00007381 .build();
7382 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7383 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
7384 InputEventInjectionSync::WAIT_FOR_RESULT))
7385 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7386 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7387
7388 // Perform drag and drop from first window.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007389 ASSERT_TRUE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +00007390
7391 // Move on window.
7392 const MotionEvent secondFingerMoveEvent =
7393 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
7394 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Harry Cutts33476232023-01-30 19:57:29 +00007395 .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50))
7396 .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00007397 .build();
7398 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7399 injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT,
7400 InputEventInjectionSync::WAIT_FOR_RESULT));
7401 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7402 mWindow->consumeDragEvent(false, 50, 50);
7403 mSecondWindow->consumeMotionMove();
7404
7405 // Release the drag pointer should perform drop.
7406 const MotionEvent secondFingerUpEvent =
7407 MotionEventBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
7408 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Harry Cutts33476232023-01-30 19:57:29 +00007409 .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50))
7410 .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00007411 .build();
7412 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7413 injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT,
7414 InputEventInjectionSync::WAIT_FOR_RESULT));
7415 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
7416 mFakePolicy->assertDropTargetEquals(mWindow->getToken());
7417 mWindow->assertNoEvents();
7418 mSecondWindow->consumeMotionMove();
7419}
7420
Arthur Hung3915c1f2022-05-31 07:17:17 +00007421TEST_F(InputDispatcherDragTests, DragAndDropWhenMultiDisplays) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007422 startDrag();
Arthur Hung3915c1f2022-05-31 07:17:17 +00007423
7424 // Update window of second display.
7425 sp<FakeWindowHandle> windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007426 sp<FakeWindowHandle>::make(mApp, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung3915c1f2022-05-31 07:17:17 +00007427 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
7428
7429 // Let second display has a touch state.
7430 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7431 injectMotionEvent(mDispatcher,
7432 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
7433 AINPUT_SOURCE_TOUCHSCREEN)
7434 .displayId(SECOND_DISPLAY_ID)
7435 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER)
7436 .x(100)
7437 .y(100))
7438 .build()));
7439 windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN,
Harry Cutts33476232023-01-30 19:57:29 +00007440 SECOND_DISPLAY_ID, /*expectedFlag=*/0);
Arthur Hung3915c1f2022-05-31 07:17:17 +00007441 // Update window again.
7442 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
7443
7444 // Move on window.
7445 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7446 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
7447 ADISPLAY_ID_DEFAULT, {50, 50}))
7448 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7449 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7450 mWindow->consumeDragEvent(false, 50, 50);
7451 mSecondWindow->assertNoEvents();
7452
7453 // Move to another window.
7454 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7455 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
7456 ADISPLAY_ID_DEFAULT, {150, 50}))
7457 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7458 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7459 mWindow->consumeDragEvent(true, 150, 50);
7460 mSecondWindow->consumeDragEvent(false, 50, 50);
7461
7462 // drop to another window.
7463 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7464 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7465 {150, 50}))
7466 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7467 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
7468 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
7469 mWindow->assertNoEvents();
7470 mSecondWindow->assertNoEvents();
7471}
7472
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007473TEST_F(InputDispatcherDragTests, MouseDragAndDrop) {
7474 startDrag(true, AINPUT_SOURCE_MOUSE);
7475 // Move on window.
7476 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7477 injectMotionEvent(mDispatcher,
7478 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
7479 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
7480 .pointer(PointerBuilder(MOUSE_POINTER_ID,
7481 AMOTION_EVENT_TOOL_TYPE_MOUSE)
7482 .x(50)
7483 .y(50))
7484 .build()))
7485 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7486 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7487 mWindow->consumeDragEvent(false, 50, 50);
7488 mSecondWindow->assertNoEvents();
7489
7490 // Move to another window.
7491 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7492 injectMotionEvent(mDispatcher,
7493 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
7494 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
7495 .pointer(PointerBuilder(MOUSE_POINTER_ID,
7496 AMOTION_EVENT_TOOL_TYPE_MOUSE)
7497 .x(150)
7498 .y(50))
7499 .build()))
7500 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7501 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7502 mWindow->consumeDragEvent(true, 150, 50);
7503 mSecondWindow->consumeDragEvent(false, 50, 50);
7504
7505 // drop to another window.
7506 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7507 injectMotionEvent(mDispatcher,
7508 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
7509 .buttonState(0)
7510 .pointer(PointerBuilder(MOUSE_POINTER_ID,
7511 AMOTION_EVENT_TOOL_TYPE_MOUSE)
7512 .x(150)
7513 .y(50))
7514 .build()))
7515 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7516 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
7517 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
7518 mWindow->assertNoEvents();
7519 mSecondWindow->assertNoEvents();
7520}
7521
Vishnu Nair062a8672021-09-03 16:07:44 -07007522class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {};
7523
7524TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) {
7525 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007526 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
7527 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007528 window->setDropInput(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07007529 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
7530 window->setFocusable(true);
7531 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
7532 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00007533 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07007534
7535 // With the flag set, window should not get any input
7536 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
7537 mDispatcher->notifyKey(&keyArgs);
7538 window->assertNoEvents();
7539
7540 NotifyMotionArgs motionArgs =
7541 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
7542 ADISPLAY_ID_DEFAULT);
7543 mDispatcher->notifyMotion(&motionArgs);
7544 window->assertNoEvents();
7545
7546 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007547 window->setDropInput(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07007548 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
7549
7550 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
7551 mDispatcher->notifyKey(&keyArgs);
7552 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
7553
7554 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
7555 ADISPLAY_ID_DEFAULT);
7556 mDispatcher->notifyMotion(&motionArgs);
7557 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7558 window->assertNoEvents();
7559}
7560
7561TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) {
7562 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
7563 std::make_shared<FakeApplicationHandle>();
7564 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007565 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
7566 ADISPLAY_ID_DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -07007567 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
7568 obscuringWindow->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007569 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07007570 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007571 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
7572 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007573 window->setDropInputIfObscured(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07007574 window->setOwnerInfo(222, 222);
7575 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
7576 window->setFocusable(true);
7577 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
7578 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00007579 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07007580
7581 // With the flag set, window should not get any input
7582 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
7583 mDispatcher->notifyKey(&keyArgs);
7584 window->assertNoEvents();
7585
7586 NotifyMotionArgs motionArgs =
7587 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
7588 ADISPLAY_ID_DEFAULT);
7589 mDispatcher->notifyMotion(&motionArgs);
7590 window->assertNoEvents();
7591
7592 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007593 window->setDropInputIfObscured(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07007594 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
7595
7596 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
7597 mDispatcher->notifyKey(&keyArgs);
7598 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
7599
7600 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
7601 ADISPLAY_ID_DEFAULT);
7602 mDispatcher->notifyMotion(&motionArgs);
7603 window->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
7604 window->assertNoEvents();
7605}
7606
7607TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) {
7608 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
7609 std::make_shared<FakeApplicationHandle>();
7610 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007611 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
7612 ADISPLAY_ID_DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -07007613 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
7614 obscuringWindow->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007615 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07007616 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007617 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
7618 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007619 window->setDropInputIfObscured(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07007620 window->setOwnerInfo(222, 222);
7621 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
7622 window->setFocusable(true);
7623 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
7624 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00007625 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07007626
7627 // With the flag set, window should not get any input
7628 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
7629 mDispatcher->notifyKey(&keyArgs);
7630 window->assertNoEvents();
7631
7632 NotifyMotionArgs motionArgs =
7633 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
7634 ADISPLAY_ID_DEFAULT);
7635 mDispatcher->notifyMotion(&motionArgs);
7636 window->assertNoEvents();
7637
7638 // When the window is no longer obscured because it went on top, it should get input
7639 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, obscuringWindow}}});
7640
7641 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
7642 mDispatcher->notifyKey(&keyArgs);
7643 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
7644
7645 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
7646 ADISPLAY_ID_DEFAULT);
7647 mDispatcher->notifyMotion(&motionArgs);
7648 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7649 window->assertNoEvents();
7650}
7651
Antonio Kantekf16f2832021-09-28 04:39:20 +00007652class InputDispatcherTouchModeChangedTests : public InputDispatcherTest {
7653protected:
7654 std::shared_ptr<FakeApplicationHandle> mApp;
Antonio Kantek15beb512022-06-13 22:35:41 +00007655 std::shared_ptr<FakeApplicationHandle> mSecondaryApp;
Antonio Kantekf16f2832021-09-28 04:39:20 +00007656 sp<FakeWindowHandle> mWindow;
7657 sp<FakeWindowHandle> mSecondWindow;
Antonio Kantek15beb512022-06-13 22:35:41 +00007658 sp<FakeWindowHandle> mThirdWindow;
Antonio Kantekf16f2832021-09-28 04:39:20 +00007659
7660 void SetUp() override {
7661 InputDispatcherTest::SetUp();
7662
7663 mApp = std::make_shared<FakeApplicationHandle>();
Antonio Kantek15beb512022-06-13 22:35:41 +00007664 mSecondaryApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007665 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00007666 mWindow->setFocusable(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00007667 setFocusedWindow(mWindow);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007668 mSecondWindow =
7669 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00007670 mSecondWindow->setFocusable(true);
Antonio Kantek15beb512022-06-13 22:35:41 +00007671 mThirdWindow =
7672 sp<FakeWindowHandle>::make(mSecondaryApp, mDispatcher,
7673 "TestWindow3_SecondaryDisplay", SECOND_DISPLAY_ID);
7674 mThirdWindow->setFocusable(true);
Antonio Kantekf16f2832021-09-28 04:39:20 +00007675
7676 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Antonio Kantek15beb512022-06-13 22:35:41 +00007677 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}},
7678 {SECOND_DISPLAY_ID, {mThirdWindow}}});
7679 mThirdWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
Antonio Kantekf16f2832021-09-28 04:39:20 +00007680 mWindow->consumeFocusEvent(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00007681
Antonio Kantek15beb512022-06-13 22:35:41 +00007682 // Set main display initial touch mode to InputDispatcher::kDefaultInTouchMode.
Prabir Pradhan5735a322022-04-11 17:23:34 +00007683 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, WINDOW_PID,
Harry Cutts33476232023-01-30 19:57:29 +00007684 WINDOW_UID, /*hasPermission=*/true, ADISPLAY_ID_DEFAULT)) {
Antonio Kantek48710e42022-03-24 14:19:30 -07007685 mWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
7686 mSecondWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +00007687 mThirdWindow->assertNoEvents();
7688 }
7689
7690 // Set secondary display initial touch mode to InputDispatcher::kDefaultInTouchMode.
7691 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, SECONDARY_WINDOW_PID,
Harry Cutts33476232023-01-30 19:57:29 +00007692 SECONDARY_WINDOW_UID, /*hasPermission=*/true,
Antonio Kantek15beb512022-06-13 22:35:41 +00007693 SECOND_DISPLAY_ID)) {
7694 mWindow->assertNoEvents();
7695 mSecondWindow->assertNoEvents();
7696 mThirdWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek48710e42022-03-24 14:19:30 -07007697 }
Antonio Kantekf16f2832021-09-28 04:39:20 +00007698 }
7699
Antonio Kantek15beb512022-06-13 22:35:41 +00007700 void changeAndVerifyTouchModeInMainDisplayOnly(bool inTouchMode, int32_t pid, int32_t uid,
7701 bool hasPermission) {
Antonio Kanteka042c022022-07-06 16:51:07 -07007702 ASSERT_TRUE(mDispatcher->setInTouchMode(inTouchMode, pid, uid, hasPermission,
7703 ADISPLAY_ID_DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +00007704 mWindow->consumeTouchModeEvent(inTouchMode);
7705 mSecondWindow->consumeTouchModeEvent(inTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +00007706 mThirdWindow->assertNoEvents();
Antonio Kantekf16f2832021-09-28 04:39:20 +00007707 }
7708};
7709
Antonio Kantek26defcf2022-02-08 01:12:27 +00007710TEST_F(InputDispatcherTouchModeChangedTests, FocusedWindowCanChangeTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -08007711 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek15beb512022-06-13 22:35:41 +00007712 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode,
7713 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00007714 /* hasPermission=*/false);
Antonio Kantekf16f2832021-09-28 04:39:20 +00007715}
7716
Antonio Kantek26defcf2022-02-08 01:12:27 +00007717TEST_F(InputDispatcherTouchModeChangedTests, NonFocusedWindowOwnerCannotChangeTouchMode) {
7718 const WindowInfo& windowInfo = *mWindow->getInfo();
7719 int32_t ownerPid = windowInfo.ownerPid;
7720 int32_t ownerUid = windowInfo.ownerUid;
7721 mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1);
7722 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, ownerPid,
Harry Cutts33476232023-01-30 19:57:29 +00007723 ownerUid, /*hasPermission=*/false,
Antonio Kanteka042c022022-07-06 16:51:07 -07007724 ADISPLAY_ID_DEFAULT));
Antonio Kantek26defcf2022-02-08 01:12:27 +00007725 mWindow->assertNoEvents();
7726 mSecondWindow->assertNoEvents();
7727}
7728
7729TEST_F(InputDispatcherTouchModeChangedTests, NonWindowOwnerMayChangeTouchModeOnPermissionGranted) {
7730 const WindowInfo& windowInfo = *mWindow->getInfo();
7731 int32_t ownerPid = windowInfo.ownerPid;
7732 int32_t ownerUid = windowInfo.ownerUid;
7733 mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1);
Antonio Kantek15beb512022-06-13 22:35:41 +00007734 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, ownerPid,
Harry Cutts33476232023-01-30 19:57:29 +00007735 ownerUid, /*hasPermission=*/true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00007736}
7737
Antonio Kantekf16f2832021-09-28 04:39:20 +00007738TEST_F(InputDispatcherTouchModeChangedTests, EventIsNotGeneratedIfNotChangingTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -08007739 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek26defcf2022-02-08 01:12:27 +00007740 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode,
7741 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00007742 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +00007743 mWindow->assertNoEvents();
7744 mSecondWindow->assertNoEvents();
7745}
7746
Antonio Kantek15beb512022-06-13 22:35:41 +00007747TEST_F(InputDispatcherTouchModeChangedTests, ChangeTouchOnSecondaryDisplayOnly) {
7748 const WindowInfo& windowInfo = *mThirdWindow->getInfo();
7749 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
7750 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00007751 /*hasPermission=*/true, SECOND_DISPLAY_ID));
Antonio Kantek15beb512022-06-13 22:35:41 +00007752 mWindow->assertNoEvents();
7753 mSecondWindow->assertNoEvents();
7754 mThirdWindow->consumeTouchModeEvent(!InputDispatcher::kDefaultInTouchMode);
7755}
7756
Antonio Kantek48710e42022-03-24 14:19:30 -07007757TEST_F(InputDispatcherTouchModeChangedTests, CanChangeTouchModeWhenOwningLastInteractedWindow) {
7758 // Interact with the window first.
7759 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
7760 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
7761 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
7762
7763 // Then remove focus.
7764 mWindow->setFocusable(false);
7765 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
7766
7767 // Assert that caller can switch touch mode by owning one of the last interacted window.
7768 const WindowInfo& windowInfo = *mWindow->getInfo();
7769 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
7770 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00007771 /*hasPermission=*/false, ADISPLAY_ID_DEFAULT));
Antonio Kantek48710e42022-03-24 14:19:30 -07007772}
7773
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007774class InputDispatcherSpyWindowTest : public InputDispatcherTest {
7775public:
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007776 sp<FakeWindowHandle> createSpy() {
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007777 std::shared_ptr<FakeApplicationHandle> application =
7778 std::make_shared<FakeApplicationHandle>();
7779 std::string name = "Fake Spy ";
7780 name += std::to_string(mSpyCount++);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007781 sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher,
7782 name.c_str(), ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007783 spy->setSpy(true);
Prabir Pradhan5c85e052021-12-22 02:27:12 -08007784 spy->setTrustedOverlay(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007785 return spy;
7786 }
7787
7788 sp<FakeWindowHandle> createForeground() {
7789 std::shared_ptr<FakeApplicationHandle> application =
7790 std::make_shared<FakeApplicationHandle>();
7791 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007792 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
7793 ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007794 window->setFocusable(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007795 return window;
7796 }
7797
7798private:
7799 int mSpyCount{0};
7800};
7801
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08007802using InputDispatcherSpyWindowDeathTest = InputDispatcherSpyWindowTest;
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007803/**
Prabir Pradhan5c85e052021-12-22 02:27:12 -08007804 * Adding a spy window that is not a trusted overlay causes Dispatcher to abort.
7805 */
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08007806TEST_F(InputDispatcherSpyWindowDeathTest, UntrustedSpy_AbortsDispatcher) {
7807 ScopedSilentDeath _silentDeath;
7808
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007809 auto spy = createSpy();
Prabir Pradhan5c85e052021-12-22 02:27:12 -08007810 spy->setTrustedOverlay(false);
7811 ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}}),
7812 ".* not a trusted overlay");
7813}
7814
7815/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007816 * Input injection into a display with a spy window but no foreground windows should succeed.
7817 */
7818TEST_F(InputDispatcherSpyWindowTest, NoForegroundWindow) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007819 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007820 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}});
7821
7822 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7823 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
7824 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7825 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7826}
7827
7828/**
7829 * Verify the order in which different input windows receive events. The touched foreground window
7830 * (if there is one) should always receive the event first. When there are multiple spy windows, the
7831 * spy windows will receive the event according to their Z-order, where the top-most spy window will
7832 * receive events before ones belows it.
7833 *
7834 * Here, we set up a scenario with four windows in the following Z order from the top:
7835 * spy1, spy2, window, spy3.
7836 * We then inject an event and verify that the foreground "window" receives it first, followed by
7837 * "spy1" and "spy2". The "spy3" does not receive the event because it is underneath the foreground
7838 * window.
7839 */
7840TEST_F(InputDispatcherSpyWindowTest, ReceivesInputInOrder) {
7841 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007842 auto spy1 = createSpy();
7843 auto spy2 = createSpy();
7844 auto spy3 = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007845 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window, spy3}}});
7846 const std::vector<sp<FakeWindowHandle>> channels{spy1, spy2, window, spy3};
7847 const size_t numChannels = channels.size();
7848
Michael Wright8e9a8562022-02-09 13:44:29 +00007849 base::unique_fd epollFd(epoll_create1(EPOLL_CLOEXEC));
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007850 if (!epollFd.ok()) {
7851 FAIL() << "Failed to create epoll fd";
7852 }
7853
7854 for (size_t i = 0; i < numChannels; i++) {
7855 struct epoll_event event = {.events = EPOLLIN, .data.u64 = i};
7856 if (epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, channels[i]->getChannelFd(), &event) < 0) {
7857 FAIL() << "Failed to add fd to epoll";
7858 }
7859 }
7860
7861 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7862 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
7863 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7864
7865 std::vector<size_t> eventOrder;
7866 std::vector<struct epoll_event> events(numChannels);
7867 for (;;) {
7868 const int nFds = epoll_wait(epollFd.get(), events.data(), static_cast<int>(numChannels),
7869 (100ms).count());
7870 if (nFds < 0) {
7871 FAIL() << "Failed to call epoll_wait";
7872 }
7873 if (nFds == 0) {
7874 break; // epoll_wait timed out
7875 }
7876 for (int i = 0; i < nFds; i++) {
Colin Cross5b799302022-10-18 21:52:41 -07007877 ASSERT_EQ(static_cast<uint32_t>(EPOLLIN), events[i].events);
Siarhei Vishniakou31977182022-09-30 08:51:23 -07007878 eventOrder.push_back(static_cast<size_t>(events[i].data.u64));
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007879 channels[i]->consumeMotionDown();
7880 }
7881 }
7882
7883 // Verify the order in which the events were received.
7884 EXPECT_EQ(3u, eventOrder.size());
7885 EXPECT_EQ(2u, eventOrder[0]); // index 2: window
7886 EXPECT_EQ(0u, eventOrder[1]); // index 0: spy1
7887 EXPECT_EQ(1u, eventOrder[2]); // index 1: spy2
7888}
7889
7890/**
7891 * A spy window using the NOT_TOUCHABLE flag does not receive events.
7892 */
7893TEST_F(InputDispatcherSpyWindowTest, NotTouchable) {
7894 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007895 auto spy = createSpy();
7896 spy->setTouchable(false);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007897 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
7898
7899 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7900 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
7901 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7902 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7903 spy->assertNoEvents();
7904}
7905
7906/**
7907 * A spy window will only receive gestures that originate within its touchable region. Gestures that
7908 * have their ACTION_DOWN outside of the touchable region of the spy window will not be dispatched
7909 * to the window.
7910 */
7911TEST_F(InputDispatcherSpyWindowTest, TouchableRegion) {
7912 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007913 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007914 spy->setTouchableRegion(Region{{0, 0, 20, 20}});
7915 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
7916
7917 // Inject an event outside the spy window's touchable region.
7918 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7919 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
7920 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7921 window->consumeMotionDown();
7922 spy->assertNoEvents();
7923 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7924 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
7925 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7926 window->consumeMotionUp();
7927 spy->assertNoEvents();
7928
7929 // Inject an event inside the spy window's touchable region.
7930 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7931 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7932 {5, 10}))
7933 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7934 window->consumeMotionDown();
7935 spy->consumeMotionDown();
7936}
7937
7938/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007939 * A spy window can listen for touches outside its touchable region using the WATCH_OUTSIDE_TOUCHES
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08007940 * flag, but it will get zero-ed out coordinates if the foreground has a different owner.
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007941 */
7942TEST_F(InputDispatcherSpyWindowTest, WatchOutsideTouches) {
7943 auto window = createForeground();
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08007944 window->setOwnerInfo(12, 34);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007945 auto spy = createSpy();
7946 spy->setWatchOutsideTouch(true);
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08007947 spy->setOwnerInfo(56, 78);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007948 spy->setFrame(Rect{0, 0, 20, 20});
7949 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
7950
7951 // Inject an event outside the spy window's frame and touchable region.
7952 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08007953 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7954 {100, 200}))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007955 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7956 window->consumeMotionDown();
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08007957 spy->consumeMotionOutsideWithZeroedCoords();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007958}
7959
7960/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007961 * Even when a spy window spans over multiple foreground windows, the spy should receive all
7962 * pointers that are down within its bounds.
7963 */
7964TEST_F(InputDispatcherSpyWindowTest, ReceivesMultiplePointers) {
7965 auto windowLeft = createForeground();
7966 windowLeft->setFrame({0, 0, 100, 200});
7967 auto windowRight = createForeground();
7968 windowRight->setFrame({100, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007969 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007970 spy->setFrame({0, 0, 200, 200});
7971 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, windowLeft, windowRight}}});
7972
7973 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7974 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7975 {50, 50}))
7976 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7977 windowLeft->consumeMotionDown();
7978 spy->consumeMotionDown();
7979
7980 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08007981 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007982 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Harry Cutts33476232023-01-30 19:57:29 +00007983 .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
7984 .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007985 .build();
7986 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7987 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
7988 InputEventInjectionSync::WAIT_FOR_RESULT))
7989 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7990 windowRight->consumeMotionDown();
Harry Cutts33476232023-01-30 19:57:29 +00007991 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007992}
7993
7994/**
7995 * When the first pointer lands outside the spy window and the second pointer lands inside it, the
7996 * the spy should receive the second pointer with ACTION_DOWN.
7997 */
7998TEST_F(InputDispatcherSpyWindowTest, ReceivesSecondPointerAsDown) {
7999 auto window = createForeground();
8000 window->setFrame({0, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008001 auto spyRight = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008002 spyRight->setFrame({100, 0, 200, 200});
8003 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyRight, window}}});
8004
8005 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8006 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8007 {50, 50}))
8008 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8009 window->consumeMotionDown();
8010 spyRight->assertNoEvents();
8011
8012 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08008013 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008014 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Harry Cutts33476232023-01-30 19:57:29 +00008015 .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
8016 .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008017 .build();
8018 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8019 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8020 InputEventInjectionSync::WAIT_FOR_RESULT))
8021 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Harry Cutts33476232023-01-30 19:57:29 +00008022 window->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008023 spyRight->consumeMotionDown();
8024}
8025
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008026/**
8027 * The spy window should not be able to affect whether or not touches are split. Only the foreground
8028 * windows should be allowed to control split touch.
8029 */
8030TEST_F(InputDispatcherSpyWindowTest, SplitIfNoForegroundWindowTouched) {
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08008031 // This spy window prevents touch splitting. However, we still expect to split touches
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008032 // because a foreground window has not disabled splitting.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008033 auto spy = createSpy();
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08008034 spy->setPreventSplitting(true);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008035
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008036 auto window = createForeground();
8037 window->setFrame(Rect(0, 0, 100, 100));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008038
8039 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8040
8041 // First finger down, no window touched.
8042 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8043 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8044 {100, 200}))
8045 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8046 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8047 window->assertNoEvents();
8048
8049 // Second finger down on window, the window should receive touch down.
8050 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08008051 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008052 .displayId(ADISPLAY_ID_DEFAULT)
8053 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Harry Cutts33476232023-01-30 19:57:29 +00008054 .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(100).y(200))
8055 .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008056 .build();
8057 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8058 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8059 InputEventInjectionSync::WAIT_FOR_RESULT))
8060 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8061
8062 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Harry Cutts33476232023-01-30 19:57:29 +00008063 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008064}
8065
8066/**
8067 * A spy window will usually be implemented as an un-focusable window. Verify that these windows
8068 * do not receive key events.
8069 */
8070TEST_F(InputDispatcherSpyWindowTest, UnfocusableSpyDoesNotReceiveKeyEvents) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008071 auto spy = createSpy();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008072 spy->setFocusable(false);
8073
8074 auto window = createForeground();
8075 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8076 setFocusedWindow(window);
8077 window->consumeFocusEvent(true);
8078
8079 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
8080 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
8081 window->consumeKeyDown(ADISPLAY_ID_NONE);
8082
8083 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
8084 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
8085 window->consumeKeyUp(ADISPLAY_ID_NONE);
8086
8087 spy->assertNoEvents();
8088}
8089
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00008090using InputDispatcherPilferPointersTest = InputDispatcherSpyWindowTest;
8091
8092/**
8093 * A spy window can pilfer pointers. When this happens, touch gestures used by the spy window that
8094 * are currently sent to any other windows - including other spy windows - will also be cancelled.
8095 */
8096TEST_F(InputDispatcherPilferPointersTest, PilferPointers) {
8097 auto window = createForeground();
8098 auto spy1 = createSpy();
8099 auto spy2 = createSpy();
8100 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window}}});
8101
8102 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8103 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8104 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8105 window->consumeMotionDown();
8106 spy1->consumeMotionDown();
8107 spy2->consumeMotionDown();
8108
8109 // Pilfer pointers from the second spy window.
8110 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy2->getToken()));
8111 spy2->assertNoEvents();
8112 spy1->consumeMotionCancel();
8113 window->consumeMotionCancel();
8114
8115 // The rest of the gesture should only be sent to the second spy window.
8116 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8117 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8118 ADISPLAY_ID_DEFAULT))
8119 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8120 spy2->consumeMotionMove();
8121 spy1->assertNoEvents();
8122 window->assertNoEvents();
8123}
8124
8125/**
8126 * A spy window can pilfer pointers for a gesture even after the foreground window has been removed
8127 * in the middle of the gesture.
8128 */
8129TEST_F(InputDispatcherPilferPointersTest, CanPilferAfterWindowIsRemovedMidStream) {
8130 auto window = createForeground();
8131 auto spy = createSpy();
8132 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8133
8134 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8135 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8136 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8137 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8138 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8139
8140 window->releaseChannel();
8141
8142 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
8143
8144 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8145 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8146 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8147 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8148}
8149
8150/**
8151 * After a spy window pilfers pointers, new pointers that go down in its bounds should be sent to
8152 * the spy, but not to any other windows.
8153 */
8154TEST_F(InputDispatcherPilferPointersTest, ContinuesToReceiveGestureAfterPilfer) {
8155 auto spy = createSpy();
8156 auto window = createForeground();
8157
8158 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8159
8160 // First finger down on the window and the spy.
8161 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8162 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8163 {100, 200}))
8164 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8165 spy->consumeMotionDown();
8166 window->consumeMotionDown();
8167
8168 // Spy window pilfers the pointers.
8169 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
8170 window->consumeMotionCancel();
8171
8172 // Second finger down on the window and spy, but the window should not receive the pointer down.
8173 const MotionEvent secondFingerDownEvent =
8174 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8175 .displayId(ADISPLAY_ID_DEFAULT)
8176 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Harry Cutts33476232023-01-30 19:57:29 +00008177 .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(100).y(200))
8178 .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00008179 .build();
8180 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8181 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8182 InputEventInjectionSync::WAIT_FOR_RESULT))
8183 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8184
Harry Cutts33476232023-01-30 19:57:29 +00008185 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00008186
8187 // Third finger goes down outside all windows, so injection should fail.
8188 const MotionEvent thirdFingerDownEvent =
8189 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8190 .displayId(ADISPLAY_ID_DEFAULT)
8191 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Harry Cutts33476232023-01-30 19:57:29 +00008192 .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(100).y(200))
8193 .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
8194 .pointer(PointerBuilder(/*id=*/2, AMOTION_EVENT_TOOL_TYPE_FINGER).x(-5).y(-5))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00008195 .build();
8196 ASSERT_EQ(InputEventInjectionResult::FAILED,
8197 injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
8198 InputEventInjectionSync::WAIT_FOR_RESULT))
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08008199 << "Inject motion event should return InputEventInjectionResult::FAILED";
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00008200
8201 spy->assertNoEvents();
8202 window->assertNoEvents();
8203}
8204
8205/**
8206 * After a spy window pilfers pointers, only the pointers used by the spy should be canceled
8207 */
8208TEST_F(InputDispatcherPilferPointersTest, PartiallyPilferRequiredPointers) {
8209 auto spy = createSpy();
8210 spy->setFrame(Rect(0, 0, 100, 100));
8211 auto window = createForeground();
8212 window->setFrame(Rect(0, 0, 200, 200));
8213
8214 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8215
8216 // First finger down on the window only
8217 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8218 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8219 {150, 150}))
8220 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8221 window->consumeMotionDown();
8222
8223 // Second finger down on the spy and window
8224 const MotionEvent secondFingerDownEvent =
8225 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8226 .displayId(ADISPLAY_ID_DEFAULT)
8227 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Harry Cutts33476232023-01-30 19:57:29 +00008228 .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(150))
8229 .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(10).y(10))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00008230 .build();
8231 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8232 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8233 InputEventInjectionSync::WAIT_FOR_RESULT))
8234 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8235 spy->consumeMotionDown();
8236 window->consumeMotionPointerDown(1);
8237
8238 // Third finger down on the spy and window
8239 const MotionEvent thirdFingerDownEvent =
8240 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8241 .displayId(ADISPLAY_ID_DEFAULT)
8242 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Harry Cutts33476232023-01-30 19:57:29 +00008243 .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(150))
8244 .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(10).y(10))
8245 .pointer(PointerBuilder(/*id=*/2, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00008246 .build();
8247 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8248 injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
8249 InputEventInjectionSync::WAIT_FOR_RESULT))
8250 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8251 spy->consumeMotionPointerDown(1);
8252 window->consumeMotionPointerDown(2);
8253
8254 // Spy window pilfers the pointers.
8255 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
8256 window->consumeMotionPointerUp(/* idx */ 2, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
8257 window->consumeMotionPointerUp(/* idx */ 1, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
8258
8259 spy->assertNoEvents();
8260 window->assertNoEvents();
8261}
8262
8263/**
8264 * After a spy window pilfers pointers, all pilfered pointers that have already been dispatched to
8265 * other windows should be canceled. If this results in the cancellation of all pointers for some
8266 * window, then that window should receive ACTION_CANCEL.
8267 */
8268TEST_F(InputDispatcherPilferPointersTest, PilferAllRequiredPointers) {
8269 auto spy = createSpy();
8270 spy->setFrame(Rect(0, 0, 100, 100));
8271 auto window = createForeground();
8272 window->setFrame(Rect(0, 0, 200, 200));
8273
8274 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8275
8276 // First finger down on both spy and window
8277 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8278 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8279 {10, 10}))
8280 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8281 window->consumeMotionDown();
8282 spy->consumeMotionDown();
8283
8284 // Second finger down on the spy and window
8285 const MotionEvent secondFingerDownEvent =
8286 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8287 .displayId(ADISPLAY_ID_DEFAULT)
8288 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Harry Cutts33476232023-01-30 19:57:29 +00008289 .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(10).y(10))
8290 .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00008291 .build();
8292 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8293 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8294 InputEventInjectionSync::WAIT_FOR_RESULT))
8295 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8296 spy->consumeMotionPointerDown(1);
8297 window->consumeMotionPointerDown(1);
8298
8299 // Spy window pilfers the pointers.
8300 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
8301 window->consumeMotionCancel();
8302
8303 spy->assertNoEvents();
8304 window->assertNoEvents();
8305}
8306
8307/**
8308 * After a spy window pilfers pointers, new pointers that are not touching the spy window can still
8309 * be sent to other windows
8310 */
8311TEST_F(InputDispatcherPilferPointersTest, CanReceivePointersAfterPilfer) {
8312 auto spy = createSpy();
8313 spy->setFrame(Rect(0, 0, 100, 100));
8314 auto window = createForeground();
8315 window->setFrame(Rect(0, 0, 200, 200));
8316
8317 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8318
8319 // First finger down on both window and spy
8320 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8321 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8322 {10, 10}))
8323 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8324 window->consumeMotionDown();
8325 spy->consumeMotionDown();
8326
8327 // Spy window pilfers the pointers.
8328 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
8329 window->consumeMotionCancel();
8330
8331 // Second finger down on the window only
8332 const MotionEvent secondFingerDownEvent =
8333 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8334 .displayId(ADISPLAY_ID_DEFAULT)
8335 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Harry Cutts33476232023-01-30 19:57:29 +00008336 .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(10).y(10))
8337 .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(150))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00008338 .build();
8339 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8340 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8341 InputEventInjectionSync::WAIT_FOR_RESULT))
8342 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8343 window->consumeMotionDown();
8344 window->assertNoEvents();
8345
8346 // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line
8347 spy->consumeMotionMove();
8348 spy->assertNoEvents();
8349}
8350
Prabir Pradhand65552b2021-10-07 11:23:50 -07008351class InputDispatcherStylusInterceptorTest : public InputDispatcherTest {
8352public:
8353 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupStylusOverlayScenario() {
8354 std::shared_ptr<FakeApplicationHandle> overlayApplication =
8355 std::make_shared<FakeApplicationHandle>();
8356 sp<FakeWindowHandle> overlay =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008357 sp<FakeWindowHandle>::make(overlayApplication, mDispatcher,
8358 "Stylus interceptor window", ADISPLAY_ID_DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -07008359 overlay->setFocusable(false);
8360 overlay->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008361 overlay->setTouchable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008362 overlay->setInterceptsStylus(true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07008363 overlay->setTrustedOverlay(true);
8364
8365 std::shared_ptr<FakeApplicationHandle> application =
8366 std::make_shared<FakeApplicationHandle>();
8367 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008368 sp<FakeWindowHandle>::make(application, mDispatcher, "Application window",
8369 ADISPLAY_ID_DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -07008370 window->setFocusable(true);
8371 window->setOwnerInfo(222, 222);
Prabir Pradhand65552b2021-10-07 11:23:50 -07008372
8373 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
8374 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
8375 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00008376 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07008377 return {std::move(overlay), std::move(window)};
8378 }
8379
8380 void sendFingerEvent(int32_t action) {
8381 NotifyMotionArgs motionArgs =
8382 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
8383 ADISPLAY_ID_DEFAULT, {PointF{20, 20}});
8384 mDispatcher->notifyMotion(&motionArgs);
8385 }
8386
8387 void sendStylusEvent(int32_t action) {
8388 NotifyMotionArgs motionArgs =
8389 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
8390 ADISPLAY_ID_DEFAULT, {PointF{30, 40}});
8391 motionArgs.pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS;
8392 mDispatcher->notifyMotion(&motionArgs);
8393 }
8394};
8395
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08008396using InputDispatcherStylusInterceptorDeathTest = InputDispatcherStylusInterceptorTest;
8397
8398TEST_F(InputDispatcherStylusInterceptorDeathTest, UntrustedOverlay_AbortsDispatcher) {
8399 ScopedSilentDeath _silentDeath;
8400
Prabir Pradhand65552b2021-10-07 11:23:50 -07008401 auto [overlay, window] = setupStylusOverlayScenario();
8402 overlay->setTrustedOverlay(false);
8403 // Configuring an untrusted overlay as a stylus interceptor should cause Dispatcher to abort.
8404 ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}),
8405 ".* not a trusted overlay");
8406}
8407
8408TEST_F(InputDispatcherStylusInterceptorTest, ConsmesOnlyStylusEvents) {
8409 auto [overlay, window] = setupStylusOverlayScenario();
8410 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
8411
8412 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
8413 overlay->consumeMotionDown();
8414 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
8415 overlay->consumeMotionUp();
8416
8417 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
8418 window->consumeMotionDown();
8419 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
8420 window->consumeMotionUp();
8421
8422 overlay->assertNoEvents();
8423 window->assertNoEvents();
8424}
8425
8426TEST_F(InputDispatcherStylusInterceptorTest, SpyWindowStylusInterceptor) {
8427 auto [overlay, window] = setupStylusOverlayScenario();
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008428 overlay->setSpy(true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07008429 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
8430
8431 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
8432 overlay->consumeMotionDown();
8433 window->consumeMotionDown();
8434 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
8435 overlay->consumeMotionUp();
8436 window->consumeMotionUp();
8437
8438 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
8439 window->consumeMotionDown();
8440 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
8441 window->consumeMotionUp();
8442
8443 overlay->assertNoEvents();
8444 window->assertNoEvents();
8445}
8446
Prabir Pradhan6dfbf262022-03-14 15:24:30 +00008447/**
8448 * Set up a scenario to test the behavior used by the stylus handwriting detection feature.
8449 * The scenario is as follows:
8450 * - The stylus interceptor overlay is configured as a spy window.
8451 * - The stylus interceptor spy receives the start of a new stylus gesture.
8452 * - It pilfers pointers and then configures itself to no longer be a spy.
8453 * - The stylus interceptor continues to receive the rest of the gesture.
8454 */
8455TEST_F(InputDispatcherStylusInterceptorTest, StylusHandwritingScenario) {
8456 auto [overlay, window] = setupStylusOverlayScenario();
8457 overlay->setSpy(true);
8458 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
8459
8460 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
8461 overlay->consumeMotionDown();
8462 window->consumeMotionDown();
8463
8464 // The interceptor pilfers the pointers.
8465 EXPECT_EQ(OK, mDispatcher->pilferPointers(overlay->getToken()));
8466 window->consumeMotionCancel();
8467
8468 // The interceptor configures itself so that it is no longer a spy.
8469 overlay->setSpy(false);
8470 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
8471
8472 // It continues to receive the rest of the stylus gesture.
8473 sendStylusEvent(AMOTION_EVENT_ACTION_MOVE);
8474 overlay->consumeMotionMove();
8475 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
8476 overlay->consumeMotionUp();
8477
8478 window->assertNoEvents();
8479}
8480
Prabir Pradhan5735a322022-04-11 17:23:34 +00008481struct User {
8482 int32_t mPid;
8483 int32_t mUid;
8484 uint32_t mPolicyFlags{DEFAULT_POLICY_FLAGS};
8485 std::unique_ptr<InputDispatcher>& mDispatcher;
8486
8487 User(std::unique_ptr<InputDispatcher>& dispatcher, int32_t pid, int32_t uid)
8488 : mPid(pid), mUid(uid), mDispatcher(dispatcher) {}
8489
8490 InputEventInjectionResult injectTargetedMotion(int32_t action) const {
8491 return injectMotionEvent(mDispatcher, action, AINPUT_SOURCE_TOUCHSCREEN,
8492 ADISPLAY_ID_DEFAULT, {100, 200},
8493 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
8494 AMOTION_EVENT_INVALID_CURSOR_POSITION},
8495 INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT,
8496 systemTime(SYSTEM_TIME_MONOTONIC), {mUid}, mPolicyFlags);
8497 }
8498
8499 InputEventInjectionResult injectTargetedKey(int32_t action) const {
Harry Cutts33476232023-01-30 19:57:29 +00008500 return inputdispatcher::injectKey(mDispatcher, action, /*repeatCount=*/0, ADISPLAY_ID_NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +00008501 InputEventInjectionSync::WAIT_FOR_RESULT,
Harry Cutts33476232023-01-30 19:57:29 +00008502 INJECT_EVENT_TIMEOUT, /*allowKeyRepeat=*/false, {mUid},
Prabir Pradhan5735a322022-04-11 17:23:34 +00008503 mPolicyFlags);
8504 }
8505
8506 sp<FakeWindowHandle> createWindow() const {
8507 std::shared_ptr<FakeApplicationHandle> overlayApplication =
8508 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008509 sp<FakeWindowHandle> window =
8510 sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, "Owned Window",
8511 ADISPLAY_ID_DEFAULT);
Prabir Pradhan5735a322022-04-11 17:23:34 +00008512 window->setOwnerInfo(mPid, mUid);
8513 return window;
8514 }
8515};
8516
8517using InputDispatcherTargetedInjectionTest = InputDispatcherTest;
8518
8519TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedWindow) {
8520 auto owner = User(mDispatcher, 10, 11);
8521 auto window = owner.createWindow();
8522 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
8523
8524 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
8525 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
8526 window->consumeMotionDown();
8527
8528 setFocusedWindow(window);
8529 window->consumeFocusEvent(true);
8530
8531 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
8532 owner.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
8533 window->consumeKeyDown(ADISPLAY_ID_NONE);
8534}
8535
8536TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedWindow) {
8537 auto owner = User(mDispatcher, 10, 11);
8538 auto window = owner.createWindow();
8539 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
8540
8541 auto rando = User(mDispatcher, 20, 21);
8542 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
8543 rando.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
8544
8545 setFocusedWindow(window);
8546 window->consumeFocusEvent(true);
8547
8548 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
8549 rando.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
8550 window->assertNoEvents();
8551}
8552
8553TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedSpyWindow) {
8554 auto owner = User(mDispatcher, 10, 11);
8555 auto window = owner.createWindow();
8556 auto spy = owner.createWindow();
8557 spy->setSpy(true);
8558 spy->setTrustedOverlay(true);
8559 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8560
8561 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
8562 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
8563 spy->consumeMotionDown();
8564 window->consumeMotionDown();
8565}
8566
8567TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedSpyWindow) {
8568 auto owner = User(mDispatcher, 10, 11);
8569 auto window = owner.createWindow();
8570
8571 auto rando = User(mDispatcher, 20, 21);
8572 auto randosSpy = rando.createWindow();
8573 randosSpy->setSpy(true);
8574 randosSpy->setTrustedOverlay(true);
8575 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}});
8576
8577 // The event is targeted at owner's window, so injection should succeed, but the spy should
8578 // not receive the event.
8579 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
8580 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
8581 randosSpy->assertNoEvents();
8582 window->consumeMotionDown();
8583}
8584
8585TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoAnyWindowWhenNotTargeting) {
8586 auto owner = User(mDispatcher, 10, 11);
8587 auto window = owner.createWindow();
8588
8589 auto rando = User(mDispatcher, 20, 21);
8590 auto randosSpy = rando.createWindow();
8591 randosSpy->setSpy(true);
8592 randosSpy->setTrustedOverlay(true);
8593 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}});
8594
8595 // A user that has injection permission can inject into any window.
8596 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
8597 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
8598 ADISPLAY_ID_DEFAULT));
8599 randosSpy->consumeMotionDown();
8600 window->consumeMotionDown();
8601
8602 setFocusedWindow(randosSpy);
8603 randosSpy->consumeFocusEvent(true);
8604
8605 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher));
8606 randosSpy->consumeKeyDown(ADISPLAY_ID_NONE);
8607 window->assertNoEvents();
8608}
8609
8610TEST_F(InputDispatcherTargetedInjectionTest, CanGenerateActionOutsideToOtherUids) {
8611 auto owner = User(mDispatcher, 10, 11);
8612 auto window = owner.createWindow();
8613
8614 auto rando = User(mDispatcher, 20, 21);
8615 auto randosWindow = rando.createWindow();
8616 randosWindow->setFrame(Rect{-10, -10, -5, -5});
8617 randosWindow->setWatchOutsideTouch(true);
8618 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosWindow, window}}});
8619
8620 // We allow generation of ACTION_OUTSIDE events into windows owned by different uids.
8621 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
8622 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
8623 window->consumeMotionDown();
8624 randosWindow->consumeMotionOutside();
8625}
8626
Garfield Tane84e6f92019-08-29 17:28:41 -07008627} // namespace android::inputdispatcher