blob: 20a1977813b1bcf8b3d01a7e29d988ae7cdbe32b [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 }
163 return arg.getX(0 /*pointerIndex*/) == x && arg.getY(0 /*pointerIndex*/) == y;
164}
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 */
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800549 mLastNotifySwitch = NotifySwitchArgs(1 /*id*/, 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,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000638 mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE,
639 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,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000647 mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE,
648 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,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000677 mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE,
678 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,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000689 mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE,
690 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,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000701 mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE,
702 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,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000713 mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE,
714 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,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000725 mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE,
726 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,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000737 mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE,
738 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,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000748 mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE,
749 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,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000761 mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE,
762 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,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000773 mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE,
774 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,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000787 mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE,
788 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;
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800796 NotifyConfigurationChangedArgs args(10 /*id*/, 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) {
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800804 NotifySwitchArgs args(10 /*id*/, 20 /*eventTime*/, 0 /*policyFlags*/, 1 /*switchValues*/,
805 2 /*switchMask*/);
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) {
chaviw81e2bb92019-12-18 15:03:51 -0800866 status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -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,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001410 INVALID_HMAC, action, /* flags */ 0, AKEYCODE_A, KEY_A, AMETA_NONE,
1411 repeatCount, 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) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001422 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /* repeatCount */ 0, displayId);
1423}
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) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001430 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /* repeatCount */ 0, displayId,
1431 InputEventInjectionSync::WAIT_FOR_RESULT, INJECT_EVENT_TIMEOUT,
1432 /* allowKeyRepeat */ false);
1433}
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) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001437 return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /* repeatCount */ 0, displayId);
1438}
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)
1593 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
1594 .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.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001618 NotifyKeyArgs args(/* id */ 0, currentTime, 0 /*readTime*/, DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
1619 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.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001647 NotifyMotionArgs args(/* id */ 0, currentTime, 0 /*readTime*/, 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) {
1668 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))
1898 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
1899 .x(100)
1900 .y(100))
1901 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER)
1902 .x(150)
1903 .y(150))
1904 .build();
1905 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1906 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
1907 InputEventInjectionSync::WAIT_FOR_RESULT))
1908 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1909
Arthur Hungc539dbb2022-12-08 07:45:36 +00001910 foregroundWindow->consumeMotionPointerDown(1 /* pointerIndex */);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001911 wallpaperWindow->consumeMotionPointerDown(1 /* pointerIndex */, ADISPLAY_ID_DEFAULT,
1912 expectedWallpaperFlags);
Arthur Hungc539dbb2022-12-08 07:45:36 +00001913
1914 const MotionEvent secondFingerUpEvent =
1915 MotionEventBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
1916 .displayId(ADISPLAY_ID_DEFAULT)
1917 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
1918 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
1919 .x(100)
1920 .y(100))
1921 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER)
1922 .x(150)
1923 .y(150))
1924 .build();
1925 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1926 injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT,
1927 InputEventInjectionSync::WAIT_FOR_RESULT))
1928 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1929 foregroundWindow->consumeMotionPointerUp(0);
1930 wallpaperWindow->consumeMotionPointerUp(0, ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1931
1932 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1933 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1934 {100, 100}))
1935 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1936 foregroundWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
1937 wallpaperWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001938}
1939
1940/**
1941 * Two windows: a window on the left and window on the right.
1942 * A third window, wallpaper, is behind both windows, and spans both top windows.
1943 * The first touch down goes to the left window. A second pointer touches down on the right window.
1944 * The touch is split, so both left and right windows should receive ACTION_DOWN.
1945 * The wallpaper will get the full event, so it should receive ACTION_DOWN followed by
1946 * ACTION_POINTER_DOWN(1).
1947 */
1948TEST_F(InputDispatcherTest, TwoWindows_SplitWallpaperTouch) {
1949 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1950 sp<FakeWindowHandle> leftWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001951 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001952 leftWindow->setFrame(Rect(0, 0, 200, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001953 leftWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001954
1955 sp<FakeWindowHandle> rightWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001956 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001957 rightWindow->setFrame(Rect(200, 0, 400, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001958 rightWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001959
1960 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001961 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001962 wallpaperWindow->setFrame(Rect(0, 0, 400, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001963 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001964
1965 mDispatcher->setInputWindows(
1966 {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}});
1967
1968 // Touch down on left window
1969 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1970 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1971 {100, 100}))
1972 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1973
1974 // Both foreground window and its wallpaper should receive the touch down
1975 leftWindow->consumeMotionDown();
1976 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1977
1978 // Second finger down on the right window
1979 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08001980 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001981 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
1982 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
1983 .x(100)
1984 .y(100))
1985 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER)
1986 .x(300)
1987 .y(100))
1988 .build();
1989 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1990 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
1991 InputEventInjectionSync::WAIT_FOR_RESULT))
1992 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1993
1994 leftWindow->consumeMotionMove();
1995 // Since the touch is split, right window gets ACTION_DOWN
1996 rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1997 wallpaperWindow->consumeMotionPointerDown(1 /* pointerIndex */, ADISPLAY_ID_DEFAULT,
1998 expectedWallpaperFlags);
1999
2000 // Now, leftWindow, which received the first finger, disappears.
2001 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {rightWindow, wallpaperWindow}}});
2002 leftWindow->consumeMotionCancel();
2003 // Since a "parent" window of the wallpaper is gone, wallpaper should receive cancel, too.
2004 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2005
2006 // The pointer that's still down on the right window moves, and goes to the right window only.
2007 // As far as the dispatcher's concerned though, both pointers are still present.
2008 const MotionEvent secondFingerMoveEvent =
2009 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2010 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
2011 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
2012 .x(100)
2013 .y(100))
2014 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER)
2015 .x(310)
2016 .y(110))
2017 .build();
2018 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2019 injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT,
2020 InputEventInjectionSync::WAIT_FOR_RESULT));
2021 rightWindow->consumeMotionMove();
2022
2023 leftWindow->assertNoEvents();
2024 rightWindow->assertNoEvents();
2025 wallpaperWindow->assertNoEvents();
2026}
2027
Arthur Hungc539dbb2022-12-08 07:45:36 +00002028/**
2029 * Two windows: a window on the left with dup touch to wallpaper and window on the right without it.
2030 * The touch slips to the right window. so left window and wallpaper should receive ACTION_CANCEL
2031 * The right window should receive ACTION_DOWN.
2032 */
2033TEST_F(InputDispatcherTest, WallpaperWindowWhenSlippery) {
Arthur Hung74c248d2022-11-23 07:09:59 +00002034 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Arthur Hungc539dbb2022-12-08 07:45:36 +00002035 sp<FakeWindowHandle> leftWindow =
2036 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2037 leftWindow->setFrame(Rect(0, 0, 200, 200));
2038 leftWindow->setDupTouchToWallpaper(true);
2039 leftWindow->setSlippery(true);
2040
2041 sp<FakeWindowHandle> rightWindow =
2042 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2043 rightWindow->setFrame(Rect(200, 0, 400, 200));
Arthur Hung74c248d2022-11-23 07:09:59 +00002044
2045 sp<FakeWindowHandle> wallpaperWindow =
2046 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
2047 wallpaperWindow->setIsWallpaper(true);
Arthur Hung74c248d2022-11-23 07:09:59 +00002048
Arthur Hungc539dbb2022-12-08 07:45:36 +00002049 mDispatcher->setInputWindows(
2050 {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}});
Arthur Hung74c248d2022-11-23 07:09:59 +00002051
Arthur Hungc539dbb2022-12-08 07:45:36 +00002052 // Touch down on left window
Arthur Hung74c248d2022-11-23 07:09:59 +00002053 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2054 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Arthur Hungc539dbb2022-12-08 07:45:36 +00002055 {100, 100}))
Arthur Hung74c248d2022-11-23 07:09:59 +00002056 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungc539dbb2022-12-08 07:45:36 +00002057
2058 // Both foreground window and its wallpaper should receive the touch down
2059 leftWindow->consumeMotionDown();
Arthur Hung74c248d2022-11-23 07:09:59 +00002060 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2061
Arthur Hungc539dbb2022-12-08 07:45:36 +00002062 // Move to right window, the left window should receive cancel.
Arthur Hung74c248d2022-11-23 07:09:59 +00002063 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Arthur Hungc539dbb2022-12-08 07:45:36 +00002064 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
2065 ADISPLAY_ID_DEFAULT, {201, 100}))
Arthur Hung74c248d2022-11-23 07:09:59 +00002066 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2067
Arthur Hungc539dbb2022-12-08 07:45:36 +00002068 leftWindow->consumeMotionCancel();
2069 rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2070 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Arthur Hung74c248d2022-11-23 07:09:59 +00002071}
2072
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002073/**
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002074 * Two windows: a window on the left and a window on the right.
2075 * Mouse is hovered from the right window into the left window.
2076 * Next, we tap on the left window, where the cursor was last seen.
2077 * The second tap is done onto the right window.
2078 * The mouse and tap are from two different devices.
2079 * We technically don't need to set the downtime / eventtime for these events, but setting these
2080 * explicitly helps during debugging.
2081 * This test reproduces a crash where there is a mismatch between the downTime and eventTime.
2082 * In the buggy implementation, a tap on the right window would cause a crash.
2083 */
2084TEST_F(InputDispatcherTest, HoverFromLeftToRightAndTap) {
2085 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2086 sp<FakeWindowHandle> leftWindow =
2087 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2088 leftWindow->setFrame(Rect(0, 0, 200, 200));
2089
2090 sp<FakeWindowHandle> rightWindow =
2091 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2092 rightWindow->setFrame(Rect(200, 0, 400, 200));
2093
2094 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}});
2095 // All times need to start at the current time, otherwise the dispatcher will drop the events as
2096 // stale.
2097 const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC);
2098 const int32_t mouseDeviceId = 6;
2099 const int32_t touchDeviceId = 4;
2100 // Move the cursor from right
2101 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2102 injectMotionEvent(mDispatcher,
2103 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2104 AINPUT_SOURCE_MOUSE)
2105 .deviceId(mouseDeviceId)
2106 .downTime(baseTime + 10)
2107 .eventTime(baseTime + 20)
2108 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2109 .x(300)
2110 .y(100))
2111 .build()));
2112 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2113
2114 // .. to the left window
2115 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2116 injectMotionEvent(mDispatcher,
2117 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2118 AINPUT_SOURCE_MOUSE)
2119 .deviceId(mouseDeviceId)
2120 .downTime(baseTime + 10)
2121 .eventTime(baseTime + 30)
2122 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2123 .x(110)
2124 .y(100))
2125 .build()));
2126 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2127 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2128 // Now tap the left window
2129 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2130 injectMotionEvent(mDispatcher,
2131 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2132 AINPUT_SOURCE_TOUCHSCREEN)
2133 .deviceId(touchDeviceId)
2134 .downTime(baseTime + 40)
2135 .eventTime(baseTime + 40)
2136 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER)
2137 .x(100)
2138 .y(100))
2139 .build()));
2140 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2141 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2142
2143 // release tap
2144 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2145 injectMotionEvent(mDispatcher,
2146 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2147 AINPUT_SOURCE_TOUCHSCREEN)
2148 .deviceId(touchDeviceId)
2149 .downTime(baseTime + 40)
2150 .eventTime(baseTime + 50)
2151 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER)
2152 .x(100)
2153 .y(100))
2154 .build()));
2155 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2156
2157 // Tap the window on the right
2158 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2159 injectMotionEvent(mDispatcher,
2160 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2161 AINPUT_SOURCE_TOUCHSCREEN)
2162 .deviceId(touchDeviceId)
2163 .downTime(baseTime + 60)
2164 .eventTime(baseTime + 60)
2165 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER)
2166 .x(300)
2167 .y(100))
2168 .build()));
2169 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2170
2171 // release tap
2172 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2173 injectMotionEvent(mDispatcher,
2174 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2175 AINPUT_SOURCE_TOUCHSCREEN)
2176 .deviceId(touchDeviceId)
2177 .downTime(baseTime + 60)
2178 .eventTime(baseTime + 70)
2179 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER)
2180 .x(300)
2181 .y(100))
2182 .build()));
2183 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2184
2185 // No more events
2186 leftWindow->assertNoEvents();
2187 rightWindow->assertNoEvents();
2188}
2189
2190/**
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002191 * On the display, have a single window, and also an area where there's no window.
2192 * First pointer touches the "no window" area of the screen. Second pointer touches the window.
2193 * Make sure that the window receives the second pointer, and first pointer is simply ignored.
2194 */
2195TEST_F(InputDispatcherTest, SplitWorksWhenEmptyAreaIsTouched) {
2196 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2197 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002198 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002199
2200 mDispatcher->setInputWindows({{DISPLAY_ID, {window}}});
2201 NotifyMotionArgs args;
2202
2203 // Touch down on the empty space
2204 mDispatcher->notifyMotion(&(args = generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{-1, -1}})));
2205
2206 mDispatcher->waitForIdle();
2207 window->assertNoEvents();
2208
2209 // Now touch down on the window with another pointer
2210 mDispatcher->notifyMotion(&(args = generateTouchArgs(POINTER_1_DOWN, {{-1, -1}, {10, 10}})));
2211 mDispatcher->waitForIdle();
2212 window->consumeMotionDown();
2213}
2214
2215/**
2216 * Same test as above, but instead of touching the empty space, the first touch goes to
2217 * non-touchable window.
2218 */
2219TEST_F(InputDispatcherTest, SplitWorksWhenNonTouchableWindowIsTouched) {
2220 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2221 sp<FakeWindowHandle> window1 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002222 sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002223 window1->setTouchableRegion(Region{{0, 0, 100, 100}});
2224 window1->setTouchable(false);
2225 sp<FakeWindowHandle> window2 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002226 sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002227 window2->setTouchableRegion(Region{{100, 0, 200, 100}});
2228
2229 mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}});
2230
2231 NotifyMotionArgs args;
2232 // Touch down on the non-touchable window
2233 mDispatcher->notifyMotion(&(args = generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}})));
2234
2235 mDispatcher->waitForIdle();
2236 window1->assertNoEvents();
2237 window2->assertNoEvents();
2238
2239 // Now touch down on the window with another pointer
2240 mDispatcher->notifyMotion(&(args = generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}})));
2241 mDispatcher->waitForIdle();
2242 window2->consumeMotionDown();
2243}
2244
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002245/**
2246 * When splitting touch events the downTime should be adjusted such that the downTime corresponds
2247 * to the event time of the first ACTION_DOWN sent to the particular window.
2248 */
2249TEST_F(InputDispatcherTest, SplitTouchesSendCorrectActionDownTime) {
2250 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2251 sp<FakeWindowHandle> window1 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002252 sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002253 window1->setTouchableRegion(Region{{0, 0, 100, 100}});
2254 sp<FakeWindowHandle> window2 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002255 sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002256 window2->setTouchableRegion(Region{{100, 0, 200, 100}});
2257
2258 mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}});
2259
2260 NotifyMotionArgs args;
2261 // Touch down on the first window
2262 mDispatcher->notifyMotion(&(args = generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}})));
2263
2264 mDispatcher->waitForIdle();
2265 InputEvent* inputEvent1 = window1->consume();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002266 ASSERT_NE(inputEvent1, nullptr);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002267 window2->assertNoEvents();
2268 MotionEvent& motionEvent1 = static_cast<MotionEvent&>(*inputEvent1);
2269 nsecs_t downTimeForWindow1 = motionEvent1.getDownTime();
2270 ASSERT_EQ(motionEvent1.getDownTime(), motionEvent1.getEventTime());
2271
2272 // Now touch down on the window with another pointer
2273 mDispatcher->notifyMotion(&(args = generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}})));
2274 mDispatcher->waitForIdle();
2275 InputEvent* inputEvent2 = window2->consume();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002276 ASSERT_NE(inputEvent2, nullptr);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002277 MotionEvent& motionEvent2 = static_cast<MotionEvent&>(*inputEvent2);
2278 nsecs_t downTimeForWindow2 = motionEvent2.getDownTime();
2279 ASSERT_NE(downTimeForWindow1, downTimeForWindow2);
2280 ASSERT_EQ(motionEvent2.getDownTime(), motionEvent2.getEventTime());
2281
2282 // Now move the pointer on the second window
2283 mDispatcher->notifyMotion(
2284 &(args = generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{50, 50}, {151, 51}})));
2285 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002286 window2->consumeMotionEvent(WithDownTime(downTimeForWindow2));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002287
2288 // Now add new touch down on the second window
2289 mDispatcher->notifyMotion(
2290 &(args = generateTouchArgs(POINTER_2_DOWN, {{50, 50}, {151, 51}, {150, 50}})));
2291 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002292 window2->consumeMotionEvent(WithDownTime(downTimeForWindow2));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002293
2294 // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line
2295 window1->consumeMotionMove();
2296 window1->assertNoEvents();
2297
2298 // Now move the pointer on the first window
2299 mDispatcher->notifyMotion(
2300 &(args = generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{51, 51}, {151, 51}})));
2301 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002302 window1->consumeMotionEvent(WithDownTime(downTimeForWindow1));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002303
2304 mDispatcher->notifyMotion(&(
2305 args = generateTouchArgs(POINTER_3_DOWN, {{51, 51}, {151, 51}, {150, 50}, {50, 50}})));
2306 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002307 window1->consumeMotionEvent(WithDownTime(downTimeForWindow1));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002308}
2309
Garfield Tandf26e862020-07-01 20:18:19 -07002310TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) {
Chris Yea209fde2020-07-22 13:54:51 -07002311 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07002312 sp<FakeWindowHandle> windowLeft =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002313 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07002314 windowLeft->setFrame(Rect(0, 0, 600, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07002315 sp<FakeWindowHandle> windowRight =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002316 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07002317 windowRight->setFrame(Rect(600, 0, 1200, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07002318
2319 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2320
2321 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
2322
2323 // Start cursor position in right window so that we can move the cursor to left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002324 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002325 injectMotionEvent(mDispatcher,
2326 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2327 AINPUT_SOURCE_MOUSE)
2328 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2329 .x(900)
2330 .y(400))
2331 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002332 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07002333
2334 // Move cursor into left window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002335 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002336 injectMotionEvent(mDispatcher,
2337 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2338 AINPUT_SOURCE_MOUSE)
2339 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2340 .x(300)
2341 .y(400))
2342 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002343 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2344 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07002345
2346 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002347 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002348 injectMotionEvent(mDispatcher,
2349 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2350 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2351 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2352 .x(300)
2353 .y(400))
2354 .build()));
2355 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2356
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002357 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002358 injectMotionEvent(mDispatcher,
2359 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
2360 AINPUT_SOURCE_MOUSE)
2361 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2362 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2363 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2364 .x(300)
2365 .y(400))
2366 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002367 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
Garfield Tandf26e862020-07-01 20:18:19 -07002368
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002369 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002370 injectMotionEvent(mDispatcher,
2371 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
2372 AINPUT_SOURCE_MOUSE)
2373 .buttonState(0)
2374 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2375 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2376 .x(300)
2377 .y(400))
2378 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002379 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE));
Garfield Tandf26e862020-07-01 20:18:19 -07002380
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002381 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002382 injectMotionEvent(mDispatcher,
2383 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
2384 .buttonState(0)
2385 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2386 .x(300)
2387 .y(400))
2388 .build()));
2389 windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT);
2390
2391 // Move mouse cursor back to right window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002392 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002393 injectMotionEvent(mDispatcher,
2394 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2395 AINPUT_SOURCE_MOUSE)
2396 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2397 .x(900)
2398 .y(400))
2399 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002400 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2401 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002402
2403 // No more events
2404 windowLeft->assertNoEvents();
2405 windowRight->assertNoEvents();
2406}
2407
2408TEST_F(InputDispatcherTest, HoverWithSpyWindows) {
2409 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2410
2411 sp<FakeWindowHandle> spyWindow =
2412 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
2413 spyWindow->setFrame(Rect(0, 0, 600, 800));
2414 spyWindow->setTrustedOverlay(true);
2415 spyWindow->setSpy(true);
2416 sp<FakeWindowHandle> window =
2417 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2418 window->setFrame(Rect(0, 0, 600, 800));
2419
2420 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2421 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
2422
2423 // Send mouse cursor to the window
2424 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2425 injectMotionEvent(mDispatcher,
2426 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
2427 AINPUT_SOURCE_MOUSE)
2428 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2429 .x(100)
2430 .y(100))
2431 .build()));
2432
2433 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2434 WithSource(AINPUT_SOURCE_MOUSE)));
2435 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2436 WithSource(AINPUT_SOURCE_MOUSE)));
2437
2438 window->assertNoEvents();
2439 spyWindow->assertNoEvents();
Garfield Tandf26e862020-07-01 20:18:19 -07002440}
2441
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08002442TEST_F(InputDispatcherTest, MouseAndTouchWithSpyWindows) {
2443 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2444
2445 sp<FakeWindowHandle> spyWindow =
2446 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
2447 spyWindow->setFrame(Rect(0, 0, 600, 800));
2448 spyWindow->setTrustedOverlay(true);
2449 spyWindow->setSpy(true);
2450 sp<FakeWindowHandle> window =
2451 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2452 window->setFrame(Rect(0, 0, 600, 800));
2453
2454 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2455 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
2456
2457 // Send mouse cursor to the window
2458 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2459 injectMotionEvent(mDispatcher,
2460 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
2461 AINPUT_SOURCE_MOUSE)
2462 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2463 .x(100)
2464 .y(100))
2465 .build()));
2466
2467 // Move mouse cursor
2468 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2469 injectMotionEvent(mDispatcher,
2470 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2471 AINPUT_SOURCE_MOUSE)
2472 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2473 .x(110)
2474 .y(110))
2475 .build()));
2476
2477 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2478 WithSource(AINPUT_SOURCE_MOUSE)));
2479 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2480 WithSource(AINPUT_SOURCE_MOUSE)));
2481 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2482 WithSource(AINPUT_SOURCE_MOUSE)));
2483 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2484 WithSource(AINPUT_SOURCE_MOUSE)));
2485 // Touch down on the window
2486 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2487 injectMotionEvent(mDispatcher,
2488 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2489 AINPUT_SOURCE_TOUCHSCREEN)
2490 .deviceId(SECOND_DEVICE_ID)
2491 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER)
2492 .x(200)
2493 .y(200))
2494 .build()));
2495 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2496 WithSource(AINPUT_SOURCE_MOUSE)));
2497 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2498 WithSource(AINPUT_SOURCE_MOUSE)));
2499 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2500 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
2501 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2502 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
2503
2504 // pilfer the motion, retaining the gesture on the spy window.
2505 EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken()));
2506 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
2507 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
2508
2509 // Touch UP on the window
2510 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2511 injectMotionEvent(mDispatcher,
2512 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2513 AINPUT_SOURCE_TOUCHSCREEN)
2514 .deviceId(SECOND_DEVICE_ID)
2515 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER)
2516 .x(200)
2517 .y(200))
2518 .build()));
2519 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
2520 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
2521
2522 // Previously, a touch was pilfered. However, that gesture was just finished. Now, we are going
2523 // to send a new gesture. It should again go to both windows (spy and the window below), just
2524 // like the first gesture did, before pilfering. The window configuration has not changed.
2525
2526 // One more tap - DOWN
2527 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2528 injectMotionEvent(mDispatcher,
2529 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2530 AINPUT_SOURCE_TOUCHSCREEN)
2531 .deviceId(SECOND_DEVICE_ID)
2532 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER)
2533 .x(250)
2534 .y(250))
2535 .build()));
2536 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2537 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
2538 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2539 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
2540
2541 // Touch UP on the window
2542 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2543 injectMotionEvent(mDispatcher,
2544 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2545 AINPUT_SOURCE_TOUCHSCREEN)
2546 .deviceId(SECOND_DEVICE_ID)
2547 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER)
2548 .x(250)
2549 .y(250))
2550 .build()));
2551 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
2552 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
2553 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
2554 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
2555
2556 window->assertNoEvents();
2557 spyWindow->assertNoEvents();
2558}
2559
Garfield Tandf26e862020-07-01 20:18:19 -07002560// This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected
2561// directly in this test.
2562TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) {
Chris Yea209fde2020-07-22 13:54:51 -07002563 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07002564 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002565 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07002566 window->setFrame(Rect(0, 0, 1200, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07002567
2568 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2569
2570 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2571
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002572 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002573 injectMotionEvent(mDispatcher,
2574 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
2575 AINPUT_SOURCE_MOUSE)
2576 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2577 .x(300)
2578 .y(400))
2579 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002580 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07002581 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002582 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002583 injectMotionEvent(mDispatcher,
2584 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2585 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2586 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2587 .x(300)
2588 .y(400))
2589 .build()));
2590 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2591
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002592 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002593 injectMotionEvent(mDispatcher,
2594 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
2595 AINPUT_SOURCE_MOUSE)
2596 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2597 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2598 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2599 .x(300)
2600 .y(400))
2601 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002602 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
Garfield Tandf26e862020-07-01 20:18:19 -07002603
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002604 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002605 injectMotionEvent(mDispatcher,
2606 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
2607 AINPUT_SOURCE_MOUSE)
2608 .buttonState(0)
2609 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2610 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2611 .x(300)
2612 .y(400))
2613 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002614 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE));
Garfield Tandf26e862020-07-01 20:18:19 -07002615
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002616 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002617 injectMotionEvent(mDispatcher,
2618 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
2619 .buttonState(0)
2620 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2621 .x(300)
2622 .y(400))
2623 .build()));
2624 window->consumeMotionUp(ADISPLAY_ID_DEFAULT);
2625
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002626 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002627 injectMotionEvent(mDispatcher,
2628 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT,
2629 AINPUT_SOURCE_MOUSE)
2630 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2631 .x(300)
2632 .y(400))
2633 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002634 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
Garfield Tandf26e862020-07-01 20:18:19 -07002635}
2636
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08002637/**
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00002638 * Hover over a window, and then remove that window. Make sure that HOVER_EXIT for that event
2639 * is generated.
2640 */
2641TEST_F(InputDispatcherTest, HoverExitIsSentToRemovedWindow) {
2642 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2643 sp<FakeWindowHandle> window =
2644 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2645 window->setFrame(Rect(0, 0, 1200, 800));
2646
2647 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2648
2649 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2650
2651 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2652 injectMotionEvent(mDispatcher,
2653 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
2654 AINPUT_SOURCE_MOUSE)
2655 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2656 .x(300)
2657 .y(400))
2658 .build()));
2659 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2660
2661 // Remove the window, but keep the channel.
2662 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
2663 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2664}
2665
2666/**
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08002667 * Inject a mouse hover event followed by a tap from touchscreen.
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00002668 * The tap causes a HOVER_EXIT event to be generated because the current event
2669 * stream's source has been switched.
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08002670 */
2671TEST_F(InputDispatcherTest, MouseHoverAndTouchTap) {
2672 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2673 sp<FakeWindowHandle> window =
2674 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2675 window->setFrame(Rect(0, 0, 100, 100));
2676
2677 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2678
2679 // Inject a hover_move from mouse.
2680 NotifyMotionArgs motionArgs =
2681 generateMotionArgs(AMOTION_EVENT_ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE,
2682 ADISPLAY_ID_DEFAULT, {{50, 50}});
2683 motionArgs.xCursorPosition = 50;
2684 motionArgs.yCursorPosition = 50;
2685 mDispatcher->notifyMotion(&motionArgs);
2686 ASSERT_NO_FATAL_FAILURE(
2687 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2688 WithSource(AINPUT_SOURCE_MOUSE))));
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08002689
2690 // Tap on the window
2691 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2692 ADISPLAY_ID_DEFAULT, {{10, 10}});
2693 mDispatcher->notifyMotion(&motionArgs);
2694 ASSERT_NO_FATAL_FAILURE(
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00002695 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2696 WithSource(AINPUT_SOURCE_MOUSE))));
2697
2698 ASSERT_NO_FATAL_FAILURE(
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08002699 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2700 WithSource(AINPUT_SOURCE_TOUCHSCREEN))));
2701
2702 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
2703 ADISPLAY_ID_DEFAULT, {{10, 10}});
2704 mDispatcher->notifyMotion(&motionArgs);
2705 ASSERT_NO_FATAL_FAILURE(
2706 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
2707 WithSource(AINPUT_SOURCE_TOUCHSCREEN))));
2708}
2709
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02002710TEST_F(InputDispatcherTest, HoverEnterMoveRemoveWindowsInSecondDisplay) {
2711 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2712 sp<FakeWindowHandle> windowDefaultDisplay =
2713 sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay",
2714 ADISPLAY_ID_DEFAULT);
2715 windowDefaultDisplay->setFrame(Rect(0, 0, 600, 800));
2716 sp<FakeWindowHandle> windowSecondDisplay =
2717 sp<FakeWindowHandle>::make(application, mDispatcher, "SecondDisplay",
2718 SECOND_DISPLAY_ID);
2719 windowSecondDisplay->setFrame(Rect(0, 0, 600, 800));
2720
2721 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}},
2722 {SECOND_DISPLAY_ID, {windowSecondDisplay}}});
2723
2724 // Set cursor position in window in default display and check that hover enter and move
2725 // events are generated.
2726 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2727 injectMotionEvent(mDispatcher,
2728 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2729 AINPUT_SOURCE_MOUSE)
2730 .displayId(ADISPLAY_ID_DEFAULT)
2731 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2732 .x(300)
2733 .y(600))
2734 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002735 windowDefaultDisplay->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02002736
2737 // Remove all windows in secondary display and check that no event happens on window in
2738 // primary display.
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002739 mDispatcher->setInputWindows(
2740 {{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}}, {SECOND_DISPLAY_ID, {}}});
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02002741 windowDefaultDisplay->assertNoEvents();
2742
2743 // Move cursor position in window in default display and check that only hover move
2744 // event is generated and not hover enter event.
2745 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}},
2746 {SECOND_DISPLAY_ID, {windowSecondDisplay}}});
2747 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2748 injectMotionEvent(mDispatcher,
2749 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2750 AINPUT_SOURCE_MOUSE)
2751 .displayId(ADISPLAY_ID_DEFAULT)
2752 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2753 .x(400)
2754 .y(700))
2755 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002756 windowDefaultDisplay->consumeMotionEvent(
2757 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2758 WithSource(AINPUT_SOURCE_MOUSE)));
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02002759 windowDefaultDisplay->assertNoEvents();
2760}
2761
Garfield Tan00f511d2019-06-12 16:55:40 -07002762TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) {
Chris Yea209fde2020-07-22 13:54:51 -07002763 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tan00f511d2019-06-12 16:55:40 -07002764
2765 sp<FakeWindowHandle> windowLeft =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002766 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07002767 windowLeft->setFrame(Rect(0, 0, 600, 800));
Garfield Tan00f511d2019-06-12 16:55:40 -07002768 sp<FakeWindowHandle> windowRight =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002769 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07002770 windowRight->setFrame(Rect(600, 0, 1200, 800));
Garfield Tan00f511d2019-06-12 16:55:40 -07002771
2772 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2773
Arthur Hung72d8dc32020-03-28 00:48:39 +00002774 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
Garfield Tan00f511d2019-06-12 16:55:40 -07002775
2776 // Inject an event with coordinate in the area of right window, with mouse cursor in the area of
2777 // left window. This event should be dispatched to the left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002778 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tan00f511d2019-06-12 16:55:40 -07002779 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07002780 ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400}));
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002781 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07002782 windowRight->assertNoEvents();
2783}
2784
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002785TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) {
Chris Yea209fde2020-07-22 13:54:51 -07002786 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002787 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
2788 "Fake Window", ADISPLAY_ID_DEFAULT);
Vishnu Nair47074b82020-08-14 11:54:47 -07002789 window->setFocusable(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002790
Arthur Hung72d8dc32020-03-28 00:48:39 +00002791 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002792 setFocusedWindow(window);
2793
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002794 window->consumeFocusEvent(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002795
2796 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
2797 mDispatcher->notifyKey(&keyArgs);
2798
2799 // Window should receive key down event.
2800 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2801
2802 // When device reset happens, that key stream should be terminated with FLAG_CANCELED
2803 // on the app side.
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002804 NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002805 mDispatcher->notifyDeviceReset(&args);
2806 window->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
2807 AKEY_EVENT_FLAG_CANCELED);
2808}
2809
2810TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) {
Chris Yea209fde2020-07-22 13:54:51 -07002811 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002812 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
2813 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002814
Arthur Hung72d8dc32020-03-28 00:48:39 +00002815 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002816
2817 NotifyMotionArgs motionArgs =
2818 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2819 ADISPLAY_ID_DEFAULT);
2820 mDispatcher->notifyMotion(&motionArgs);
2821
2822 // Window should receive motion down event.
2823 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2824
2825 // When device reset happens, that motion stream should be terminated with ACTION_CANCEL
2826 // on the app side.
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002827 NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002828 mDispatcher->notifyDeviceReset(&args);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08002829 window->consumeMotionEvent(
2830 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002831}
2832
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08002833TEST_F(InputDispatcherTest, InterceptKeyByPolicy) {
2834 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002835 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
2836 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08002837 window->setFocusable(true);
2838
2839 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2840 setFocusedWindow(window);
2841
2842 window->consumeFocusEvent(true);
2843
2844 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
2845 const std::chrono::milliseconds interceptKeyTimeout = 50ms;
2846 const nsecs_t injectTime = keyArgs.eventTime;
2847 mFakePolicy->setInterceptKeyTimeout(interceptKeyTimeout);
2848 mDispatcher->notifyKey(&keyArgs);
2849 // The dispatching time should be always greater than or equal to intercept key timeout.
2850 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2851 ASSERT_TRUE((systemTime(SYSTEM_TIME_MONOTONIC) - injectTime) >=
2852 std::chrono::nanoseconds(interceptKeyTimeout).count());
2853}
2854
2855TEST_F(InputDispatcherTest, InterceptKeyIfKeyUp) {
2856 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002857 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
2858 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08002859 window->setFocusable(true);
2860
2861 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2862 setFocusedWindow(window);
2863
2864 window->consumeFocusEvent(true);
2865
2866 NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
2867 NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
2868 mFakePolicy->setInterceptKeyTimeout(150ms);
2869 mDispatcher->notifyKey(&keyDown);
2870 mDispatcher->notifyKey(&keyUp);
2871
2872 // Window should receive key event immediately when same key up.
2873 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2874 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
2875}
2876
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07002877/**
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08002878 * Two windows. First is a regular window. Second does not overlap with the first, and has
2879 * WATCH_OUTSIDE_TOUCH.
2880 * Both windows are owned by the same UID.
2881 * Tap first window. Make sure that the second window receives ACTION_OUTSIDE with correct, non-zero
2882 * coordinates. The coordinates are not zeroed out because both windows are owned by the same UID.
2883 */
2884TEST_F(InputDispatcherTest, ActionOutsideForOwnedWindowHasValidCoordinates) {
2885 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2886 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
2887 "First Window", ADISPLAY_ID_DEFAULT);
2888 window->setFrame(Rect{0, 0, 100, 100});
2889
2890 sp<FakeWindowHandle> outsideWindow =
2891 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
2892 ADISPLAY_ID_DEFAULT);
2893 outsideWindow->setFrame(Rect{100, 100, 200, 200});
2894 outsideWindow->setWatchOutsideTouch(true);
2895 // outsideWindow must be above 'window' to receive ACTION_OUTSIDE events when 'window' is tapped
2896 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {outsideWindow, window}}});
2897
2898 // Tap on first window.
2899 NotifyMotionArgs motionArgs =
2900 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2901 ADISPLAY_ID_DEFAULT, {PointF{50, 50}});
2902 mDispatcher->notifyMotion(&motionArgs);
2903 window->consumeMotionDown();
2904 // The coordinates of the tap in 'outsideWindow' are relative to its top left corner.
2905 // Therefore, we should offset them by (100, 100) relative to the screen's top left corner.
2906 outsideWindow->consumeMotionEvent(
2907 AllOf(WithMotionAction(ACTION_OUTSIDE), WithCoords(-50, -50)));
2908}
2909
2910/**
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00002911 * This test documents the behavior of WATCH_OUTSIDE_TOUCH. The window will get ACTION_OUTSIDE when
2912 * a another pointer causes ACTION_DOWN to be sent to another window for the first time. Only one
2913 * ACTION_OUTSIDE event is sent per gesture.
2914 */
2915TEST_F(InputDispatcherTest, ActionOutsideSentOnlyWhenAWindowIsTouched) {
2916 // There are three windows that do not overlap. `window` wants to WATCH_OUTSIDE_TOUCH.
2917 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002918 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
2919 "First Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00002920 window->setWatchOutsideTouch(true);
2921 window->setFrame(Rect{0, 0, 100, 100});
2922 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002923 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
2924 ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00002925 secondWindow->setFrame(Rect{100, 100, 200, 200});
2926 sp<FakeWindowHandle> thirdWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002927 sp<FakeWindowHandle>::make(application, mDispatcher, "Third Window",
2928 ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00002929 thirdWindow->setFrame(Rect{200, 200, 300, 300});
2930 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, secondWindow, thirdWindow}}});
2931
2932 // First pointer lands outside all windows. `window` does not get ACTION_OUTSIDE.
2933 NotifyMotionArgs motionArgs =
2934 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2935 ADISPLAY_ID_DEFAULT, {PointF{-10, -10}});
2936 mDispatcher->notifyMotion(&motionArgs);
2937 window->assertNoEvents();
2938 secondWindow->assertNoEvents();
2939
2940 // The second pointer lands inside `secondWindow`, which should receive a DOWN event.
2941 // Now, `window` should get ACTION_OUTSIDE.
2942 motionArgs = generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2943 {PointF{-10, -10}, PointF{105, 105}});
2944 mDispatcher->notifyMotion(&motionArgs);
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08002945 const std::map<int32_t, PointF> expectedPointers{{0, PointF{-10, -10}}, {1, PointF{105, 105}}};
2946 window->consumeMotionEvent(
2947 AllOf(WithMotionAction(ACTION_OUTSIDE), WithPointers(expectedPointers)));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00002948 secondWindow->consumeMotionDown();
2949 thirdWindow->assertNoEvents();
2950
2951 // The third pointer lands inside `thirdWindow`, which should receive a DOWN event. There is
2952 // no ACTION_OUTSIDE sent to `window` because one has already been sent for this gesture.
2953 motionArgs = generateMotionArgs(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2954 {PointF{-10, -10}, PointF{105, 105}, PointF{205, 205}});
2955 mDispatcher->notifyMotion(&motionArgs);
2956 window->assertNoEvents();
2957 secondWindow->consumeMotionMove();
2958 thirdWindow->consumeMotionDown();
2959}
2960
Prabir Pradhan814fe082022-07-22 20:22:18 +00002961TEST_F(InputDispatcherTest, OnWindowInfosChanged_RemoveAllWindowsOnDisplay) {
2962 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002963 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
2964 "Fake Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan814fe082022-07-22 20:22:18 +00002965 window->setFocusable(true);
2966
2967 mDispatcher->onWindowInfosChanged({*window->getInfo()}, {});
2968 setFocusedWindow(window);
2969
2970 window->consumeFocusEvent(true);
2971
2972 NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
2973 NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
2974 mDispatcher->notifyKey(&keyDown);
2975 mDispatcher->notifyKey(&keyUp);
2976
2977 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2978 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
2979
2980 // All windows are removed from the display. Ensure that we can no longer dispatch to it.
2981 mDispatcher->onWindowInfosChanged({}, {});
2982
2983 window->consumeFocusEvent(false);
2984
2985 mDispatcher->notifyKey(&keyDown);
2986 mDispatcher->notifyKey(&keyUp);
2987 window->assertNoEvents();
2988}
2989
Arthur Hung96483742022-11-15 03:30:48 +00002990TEST_F(InputDispatcherTest, NonSplitTouchableWindowReceivesMultiTouch) {
2991 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2992 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
2993 "Fake Window", ADISPLAY_ID_DEFAULT);
2994 // Ensure window is non-split and have some transform.
2995 window->setPreventSplitting(true);
2996 window->setWindowOffset(20, 40);
2997 mDispatcher->onWindowInfosChanged({*window->getInfo()}, {});
2998
2999 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3000 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3001 {50, 50}))
3002 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3003 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3004
3005 const MotionEvent secondFingerDownEvent =
3006 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3007 .displayId(ADISPLAY_ID_DEFAULT)
3008 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
3009 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
3010 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER)
3011 .x(-30)
3012 .y(-50))
3013 .build();
3014 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3015 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
3016 InputEventInjectionSync::WAIT_FOR_RESULT))
3017 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3018
3019 const MotionEvent* event = window->consumeMotion();
3020 EXPECT_EQ(POINTER_1_DOWN, event->getAction());
3021 EXPECT_EQ(70, event->getX(0)); // 50 + 20
3022 EXPECT_EQ(90, event->getY(0)); // 50 + 40
3023 EXPECT_EQ(-10, event->getX(1)); // -30 + 20
3024 EXPECT_EQ(-10, event->getY(1)); // -50 + 40
3025}
3026
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003027/**
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003028 * Ensure the correct coordinate spaces are used by InputDispatcher.
3029 *
3030 * InputDispatcher works in the display space, so its coordinate system is relative to the display
3031 * panel. Windows get events in the window space, and get raw coordinates in the logical display
3032 * space.
3033 */
3034class InputDispatcherDisplayProjectionTest : public InputDispatcherTest {
3035public:
3036 void SetUp() override {
3037 InputDispatcherTest::SetUp();
3038 mDisplayInfos.clear();
3039 mWindowInfos.clear();
3040 }
3041
3042 void addDisplayInfo(int displayId, const ui::Transform& transform) {
3043 gui::DisplayInfo info;
3044 info.displayId = displayId;
3045 info.transform = transform;
3046 mDisplayInfos.push_back(std::move(info));
3047 mDispatcher->onWindowInfosChanged(mWindowInfos, mDisplayInfos);
3048 }
3049
3050 void addWindow(const sp<WindowInfoHandle>& windowHandle) {
3051 mWindowInfos.push_back(*windowHandle->getInfo());
3052 mDispatcher->onWindowInfosChanged(mWindowInfos, mDisplayInfos);
3053 }
3054
3055 // Set up a test scenario where the display has a scaled projection and there are two windows
3056 // on the display.
3057 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupScaledDisplayScenario() {
3058 // The display has a projection that has a scale factor of 2 and 4 in the x and y directions
3059 // respectively.
3060 ui::Transform displayTransform;
3061 displayTransform.set(2, 0, 0, 4);
3062 addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform);
3063
3064 std::shared_ptr<FakeApplicationHandle> application =
3065 std::make_shared<FakeApplicationHandle>();
3066
3067 // Add two windows to the display. Their frames are represented in the display space.
3068 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003069 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
3070 ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003071 firstWindow->setFrame(Rect(0, 0, 100, 200), displayTransform);
3072 addWindow(firstWindow);
3073
3074 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003075 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3076 ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003077 secondWindow->setFrame(Rect(100, 200, 200, 400), displayTransform);
3078 addWindow(secondWindow);
3079 return {std::move(firstWindow), std::move(secondWindow)};
3080 }
3081
3082private:
3083 std::vector<gui::DisplayInfo> mDisplayInfos;
3084 std::vector<gui::WindowInfo> mWindowInfos;
3085};
3086
3087TEST_F(InputDispatcherDisplayProjectionTest, HitTestsInDisplaySpace) {
3088 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
3089 // Send down to the first window. The point is represented in the display space. The point is
3090 // selected so that if the hit test was done with the transform applied to it, then it would
3091 // end up in the incorrect window.
3092 NotifyMotionArgs downMotionArgs =
3093 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3094 ADISPLAY_ID_DEFAULT, {PointF{75, 55}});
3095 mDispatcher->notifyMotion(&downMotionArgs);
3096
3097 firstWindow->consumeMotionDown();
3098 secondWindow->assertNoEvents();
3099}
3100
3101// Ensure that when a MotionEvent is injected through the InputDispatcher::injectInputEvent() API,
3102// the event should be treated as being in the logical display space.
3103TEST_F(InputDispatcherDisplayProjectionTest, InjectionInLogicalDisplaySpace) {
3104 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
3105 // Send down to the first window. The point is represented in the logical display space. The
3106 // point is selected so that if the hit test was done in logical display space, then it would
3107 // end up in the incorrect window.
3108 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3109 PointF{75 * 2, 55 * 4});
3110
3111 firstWindow->consumeMotionDown();
3112 secondWindow->assertNoEvents();
3113}
3114
Prabir Pradhandaa2f142021-12-10 09:30:08 +00003115// Ensure that when a MotionEvent that has a custom transform is injected, the post-transformed
3116// event should be treated as being in the logical display space.
3117TEST_F(InputDispatcherDisplayProjectionTest, InjectionWithTransformInLogicalDisplaySpace) {
3118 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
3119
3120 const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0};
3121 ui::Transform injectedEventTransform;
3122 injectedEventTransform.set(matrix);
3123 const vec2 expectedPoint{75, 55}; // The injected point in the logical display space.
3124 const vec2 untransformedPoint = injectedEventTransform.inverse().transform(expectedPoint);
3125
3126 MotionEvent event = MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3127 .displayId(ADISPLAY_ID_DEFAULT)
3128 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
3129 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
3130 .x(untransformedPoint.x)
3131 .y(untransformedPoint.y))
3132 .build();
3133 event.transform(matrix);
3134
3135 injectMotionEvent(mDispatcher, event, INJECT_EVENT_TIMEOUT,
3136 InputEventInjectionSync::WAIT_FOR_RESULT);
3137
3138 firstWindow->consumeMotionDown();
3139 secondWindow->assertNoEvents();
3140}
3141
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003142TEST_F(InputDispatcherDisplayProjectionTest, WindowGetsEventsInCorrectCoordinateSpace) {
3143 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
3144
3145 // Send down to the second window.
3146 NotifyMotionArgs downMotionArgs =
3147 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3148 ADISPLAY_ID_DEFAULT, {PointF{150, 220}});
3149 mDispatcher->notifyMotion(&downMotionArgs);
3150
3151 firstWindow->assertNoEvents();
3152 const MotionEvent* event = secondWindow->consumeMotion();
3153 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, event->getAction());
3154
3155 // Ensure that the events from the "getRaw" API are in logical display coordinates.
3156 EXPECT_EQ(300, event->getRawX(0));
3157 EXPECT_EQ(880, event->getRawY(0));
3158
3159 // Ensure that the x and y values are in the window's coordinate space.
3160 // The left-top of the second window is at (100, 200) in display space, which is (200, 800) in
3161 // the logical display space. This will be the origin of the window space.
3162 EXPECT_EQ(100, event->getX(0));
3163 EXPECT_EQ(80, event->getY(0));
3164}
3165
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003166using TransferFunction = std::function<bool(const std::unique_ptr<InputDispatcher>& dispatcher,
3167 sp<IBinder>, sp<IBinder>)>;
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003168
3169class TransferTouchFixture : public InputDispatcherTest,
3170 public ::testing::WithParamInterface<TransferFunction> {};
3171
3172TEST_P(TransferTouchFixture, TransferTouch_OnePointer) {
Chris Yea209fde2020-07-22 13:54:51 -07003173 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08003174
3175 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003176 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003177 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
3178 ADISPLAY_ID_DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00003179 firstWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003180 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003181 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3182 ADISPLAY_ID_DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00003183 sp<FakeWindowHandle> wallpaper =
3184 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
3185 wallpaper->setIsWallpaper(true);
Svet Ganov5d3bc372020-01-26 23:11:07 -08003186 // Add the windows to the dispatcher
Arthur Hungc539dbb2022-12-08 07:45:36 +00003187 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow, wallpaper}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08003188
3189 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003190 NotifyMotionArgs downMotionArgs =
3191 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3192 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08003193 mDispatcher->notifyMotion(&downMotionArgs);
Arthur Hungc539dbb2022-12-08 07:45:36 +00003194
Svet Ganov5d3bc372020-01-26 23:11:07 -08003195 // Only the first window should get the down event
3196 firstWindow->consumeMotionDown();
3197 secondWindow->assertNoEvents();
Arthur Hungc539dbb2022-12-08 07:45:36 +00003198 wallpaper->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08003199
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003200 // Transfer touch to the second window
3201 TransferFunction f = GetParam();
3202 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
3203 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08003204 // The first window gets cancel and the second gets down
3205 firstWindow->consumeMotionCancel();
3206 secondWindow->consumeMotionDown();
Arthur Hungc539dbb2022-12-08 07:45:36 +00003207 wallpaper->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08003208
3209 // Send up event to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003210 NotifyMotionArgs upMotionArgs =
3211 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3212 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08003213 mDispatcher->notifyMotion(&upMotionArgs);
3214 // The first window gets no events and the second gets up
3215 firstWindow->assertNoEvents();
3216 secondWindow->consumeMotionUp();
Arthur Hungc539dbb2022-12-08 07:45:36 +00003217 wallpaper->assertNoEvents();
Svet Ganov5d3bc372020-01-26 23:11:07 -08003218}
3219
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07003220/**
3221 * When 'transferTouch' API is invoked, dispatcher needs to find the "best" window to take touch
3222 * from. When we have spy windows, there are several windows to choose from: either spy, or the
3223 * 'real' (non-spy) window. Always prefer the 'real' window because that's what would be most
3224 * natural to the user.
3225 * In this test, we are sending a pointer to both spy window and first window. We then try to
3226 * transfer touch to the second window. The dispatcher should identify the first window as the
3227 * one that should lose the gesture, and therefore the action should be to move the gesture from
3228 * the first window to the second.
3229 * The main goal here is to test the behaviour of 'transferTouch' API, but it's still valid to test
3230 * the other API, as well.
3231 */
3232TEST_P(TransferTouchFixture, TransferTouch_MultipleWindowsWithSpy) {
3233 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3234
3235 // Create a couple of windows + a spy window
3236 sp<FakeWindowHandle> spyWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003237 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07003238 spyWindow->setTrustedOverlay(true);
3239 spyWindow->setSpy(true);
3240 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003241 sp<FakeWindowHandle>::make(application, mDispatcher, "First", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07003242 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003243 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07003244
3245 // Add the windows to the dispatcher
3246 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, firstWindow, secondWindow}}});
3247
3248 // Send down to the first window
3249 NotifyMotionArgs downMotionArgs =
3250 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3251 ADISPLAY_ID_DEFAULT);
3252 mDispatcher->notifyMotion(&downMotionArgs);
3253 // Only the first window and spy should get the down event
3254 spyWindow->consumeMotionDown();
3255 firstWindow->consumeMotionDown();
3256
3257 // Transfer touch to the second window. Non-spy window should be preferred over the spy window
3258 // if f === 'transferTouch'.
3259 TransferFunction f = GetParam();
3260 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
3261 ASSERT_TRUE(success);
3262 // The first window gets cancel and the second gets down
3263 firstWindow->consumeMotionCancel();
3264 secondWindow->consumeMotionDown();
3265
3266 // Send up event to the second window
3267 NotifyMotionArgs upMotionArgs =
3268 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3269 ADISPLAY_ID_DEFAULT);
3270 mDispatcher->notifyMotion(&upMotionArgs);
3271 // The first window gets no events and the second+spy get up
3272 firstWindow->assertNoEvents();
3273 spyWindow->consumeMotionUp();
3274 secondWindow->consumeMotionUp();
3275}
3276
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003277TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07003278 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08003279
3280 PointF touchPoint = {10, 10};
3281
3282 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003283 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003284 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
3285 ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08003286 firstWindow->setPreventSplitting(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003287 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003288 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3289 ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08003290 secondWindow->setPreventSplitting(true);
Svet Ganov5d3bc372020-01-26 23:11:07 -08003291
3292 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00003293 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08003294
3295 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003296 NotifyMotionArgs downMotionArgs =
3297 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3298 ADISPLAY_ID_DEFAULT, {touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08003299 mDispatcher->notifyMotion(&downMotionArgs);
3300 // Only the first window should get the down event
3301 firstWindow->consumeMotionDown();
3302 secondWindow->assertNoEvents();
3303
3304 // Send pointer down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003305 NotifyMotionArgs pointerDownMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003306 generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003307 {touchPoint, touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08003308 mDispatcher->notifyMotion(&pointerDownMotionArgs);
3309 // Only the first window should get the pointer down event
3310 firstWindow->consumeMotionPointerDown(1);
3311 secondWindow->assertNoEvents();
3312
3313 // Transfer touch focus to the second window
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003314 TransferFunction f = GetParam();
3315 bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
3316 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08003317 // The first window gets cancel and the second gets down and pointer down
3318 firstWindow->consumeMotionCancel();
3319 secondWindow->consumeMotionDown();
3320 secondWindow->consumeMotionPointerDown(1);
3321
3322 // Send pointer up to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003323 NotifyMotionArgs pointerUpMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003324 generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003325 {touchPoint, touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08003326 mDispatcher->notifyMotion(&pointerUpMotionArgs);
3327 // The first window gets nothing and the second gets pointer up
3328 firstWindow->assertNoEvents();
3329 secondWindow->consumeMotionPointerUp(1);
3330
3331 // Send up event to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003332 NotifyMotionArgs upMotionArgs =
3333 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3334 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08003335 mDispatcher->notifyMotion(&upMotionArgs);
3336 // The first window gets nothing and the second gets up
3337 firstWindow->assertNoEvents();
3338 secondWindow->consumeMotionUp();
3339}
3340
Arthur Hungc539dbb2022-12-08 07:45:36 +00003341TEST_P(TransferTouchFixture, TransferTouch_MultipleWallpapers) {
3342 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3343
3344 // Create a couple of windows
3345 sp<FakeWindowHandle> firstWindow =
3346 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
3347 ADISPLAY_ID_DEFAULT);
3348 firstWindow->setDupTouchToWallpaper(true);
3349 sp<FakeWindowHandle> secondWindow =
3350 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3351 ADISPLAY_ID_DEFAULT);
3352 secondWindow->setDupTouchToWallpaper(true);
3353
3354 sp<FakeWindowHandle> wallpaper1 =
3355 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper1", ADISPLAY_ID_DEFAULT);
3356 wallpaper1->setIsWallpaper(true);
3357
3358 sp<FakeWindowHandle> wallpaper2 =
3359 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper2", ADISPLAY_ID_DEFAULT);
3360 wallpaper2->setIsWallpaper(true);
3361 // Add the windows to the dispatcher
3362 mDispatcher->setInputWindows(
3363 {{ADISPLAY_ID_DEFAULT, {firstWindow, wallpaper1, secondWindow, wallpaper2}}});
3364
3365 // Send down to the first window
3366 NotifyMotionArgs downMotionArgs =
3367 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3368 ADISPLAY_ID_DEFAULT);
3369 mDispatcher->notifyMotion(&downMotionArgs);
3370
3371 // Only the first window should get the down event
3372 firstWindow->consumeMotionDown();
3373 secondWindow->assertNoEvents();
3374 wallpaper1->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
3375 wallpaper2->assertNoEvents();
3376
3377 // Transfer touch focus to the second window
3378 TransferFunction f = GetParam();
3379 bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
3380 ASSERT_TRUE(success);
3381
3382 // The first window gets cancel and the second gets down
3383 firstWindow->consumeMotionCancel();
3384 secondWindow->consumeMotionDown();
3385 wallpaper1->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
3386 wallpaper2->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
3387
3388 // Send up event to the second window
3389 NotifyMotionArgs upMotionArgs =
3390 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3391 ADISPLAY_ID_DEFAULT);
3392 mDispatcher->notifyMotion(&upMotionArgs);
3393 // The first window gets no events and the second gets up
3394 firstWindow->assertNoEvents();
3395 secondWindow->consumeMotionUp();
3396 wallpaper1->assertNoEvents();
3397 wallpaper2->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
3398}
3399
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003400// For the cases of single pointer touch and two pointers non-split touch, the api's
3401// 'transferTouch' and 'transferTouchFocus' are equivalent in behaviour. They only differ
3402// for the case where there are multiple pointers split across several windows.
3403INSTANTIATE_TEST_SUITE_P(TransferFunctionTests, TransferTouchFixture,
3404 ::testing::Values(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003405 [&](const std::unique_ptr<InputDispatcher>& dispatcher,
3406 sp<IBinder> /*ignored*/, sp<IBinder> destChannelToken) {
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07003407 return dispatcher->transferTouch(destChannelToken,
3408 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003409 },
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003410 [&](const std::unique_ptr<InputDispatcher>& dispatcher,
3411 sp<IBinder> from, sp<IBinder> to) {
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003412 return dispatcher->transferTouchFocus(from, to,
3413 false /*isDragAndDrop*/);
3414 }));
3415
Svet Ganov5d3bc372020-01-26 23:11:07 -08003416TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07003417 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08003418
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003419 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003420 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
3421 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08003422 firstWindow->setFrame(Rect(0, 0, 600, 400));
Svet Ganov5d3bc372020-01-26 23:11:07 -08003423
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003424 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003425 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3426 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08003427 secondWindow->setFrame(Rect(0, 400, 600, 800));
Svet Ganov5d3bc372020-01-26 23:11:07 -08003428
3429 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00003430 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08003431
3432 PointF pointInFirst = {300, 200};
3433 PointF pointInSecond = {300, 600};
3434
3435 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003436 NotifyMotionArgs firstDownMotionArgs =
3437 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3438 ADISPLAY_ID_DEFAULT, {pointInFirst});
Svet Ganov5d3bc372020-01-26 23:11:07 -08003439 mDispatcher->notifyMotion(&firstDownMotionArgs);
3440 // Only the first window should get the down event
3441 firstWindow->consumeMotionDown();
3442 secondWindow->assertNoEvents();
3443
3444 // Send down to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003445 NotifyMotionArgs secondDownMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003446 generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003447 {pointInFirst, pointInSecond});
Svet Ganov5d3bc372020-01-26 23:11:07 -08003448 mDispatcher->notifyMotion(&secondDownMotionArgs);
3449 // The first window gets a move and the second a down
3450 firstWindow->consumeMotionMove();
3451 secondWindow->consumeMotionDown();
3452
3453 // Transfer touch focus to the second window
3454 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
3455 // The first window gets cancel and the new gets pointer down (it already saw down)
3456 firstWindow->consumeMotionCancel();
3457 secondWindow->consumeMotionPointerDown(1);
3458
3459 // Send pointer up to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003460 NotifyMotionArgs pointerUpMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003461 generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003462 {pointInFirst, pointInSecond});
Svet Ganov5d3bc372020-01-26 23:11:07 -08003463 mDispatcher->notifyMotion(&pointerUpMotionArgs);
3464 // The first window gets nothing and the second gets pointer up
3465 firstWindow->assertNoEvents();
3466 secondWindow->consumeMotionPointerUp(1);
3467
3468 // Send up event to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003469 NotifyMotionArgs upMotionArgs =
3470 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3471 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08003472 mDispatcher->notifyMotion(&upMotionArgs);
3473 // The first window gets nothing and the second gets up
3474 firstWindow->assertNoEvents();
3475 secondWindow->consumeMotionUp();
3476}
3477
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003478// Same as TransferTouchFocus_TwoPointersSplitTouch, but using 'transferTouch' api.
3479// Unlike 'transferTouchFocus', calling 'transferTouch' when there are two windows receiving
3480// touch is not supported, so the touch should continue on those windows and the transferred-to
3481// window should get nothing.
3482TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) {
3483 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3484
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003485 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003486 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
3487 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003488 firstWindow->setFrame(Rect(0, 0, 600, 400));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003489
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003490 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003491 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3492 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003493 secondWindow->setFrame(Rect(0, 400, 600, 800));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003494
3495 // Add the windows to the dispatcher
3496 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
3497
3498 PointF pointInFirst = {300, 200};
3499 PointF pointInSecond = {300, 600};
3500
3501 // Send down to the first window
3502 NotifyMotionArgs firstDownMotionArgs =
3503 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3504 ADISPLAY_ID_DEFAULT, {pointInFirst});
3505 mDispatcher->notifyMotion(&firstDownMotionArgs);
3506 // Only the first window should get the down event
3507 firstWindow->consumeMotionDown();
3508 secondWindow->assertNoEvents();
3509
3510 // Send down to the second window
3511 NotifyMotionArgs secondDownMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003512 generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003513 {pointInFirst, pointInSecond});
3514 mDispatcher->notifyMotion(&secondDownMotionArgs);
3515 // The first window gets a move and the second a down
3516 firstWindow->consumeMotionMove();
3517 secondWindow->consumeMotionDown();
3518
3519 // Transfer touch focus to the second window
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07003520 const bool transferred =
3521 mDispatcher->transferTouch(secondWindow->getToken(), ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003522 // The 'transferTouch' call should not succeed, because there are 2 touched windows
3523 ASSERT_FALSE(transferred);
3524 firstWindow->assertNoEvents();
3525 secondWindow->assertNoEvents();
3526
3527 // The rest of the dispatch should proceed as normal
3528 // Send pointer up to the second window
3529 NotifyMotionArgs pointerUpMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003530 generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003531 {pointInFirst, pointInSecond});
3532 mDispatcher->notifyMotion(&pointerUpMotionArgs);
3533 // The first window gets MOVE and the second gets pointer up
3534 firstWindow->consumeMotionMove();
3535 secondWindow->consumeMotionUp();
3536
3537 // Send up event to the first window
3538 NotifyMotionArgs upMotionArgs =
3539 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3540 ADISPLAY_ID_DEFAULT);
3541 mDispatcher->notifyMotion(&upMotionArgs);
3542 // The first window gets nothing and the second gets up
3543 firstWindow->consumeMotionUp();
3544 secondWindow->assertNoEvents();
3545}
3546
Arthur Hungabbb9d82021-09-01 14:52:30 +00003547// This case will create two windows and one mirrored window on the default display and mirror
3548// two windows on the second display. It will test if 'transferTouchFocus' works fine if we put
3549// the windows info of second display before default display.
3550TEST_F(InputDispatcherTest, TransferTouchFocus_CloneSurface) {
3551 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3552 sp<FakeWindowHandle> firstWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003553 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00003554 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003555 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003556 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00003557 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003558
3559 sp<FakeWindowHandle> mirrorWindowInPrimary =
3560 firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT);
3561 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003562
3563 sp<FakeWindowHandle> firstWindowInSecondary =
3564 firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
3565 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003566
3567 sp<FakeWindowHandle> secondWindowInSecondary =
3568 secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
3569 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003570
3571 // Update window info, let it find window handle of second display first.
3572 mDispatcher->setInputWindows(
3573 {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}},
3574 {ADISPLAY_ID_DEFAULT,
3575 {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}});
3576
3577 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3578 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3579 {50, 50}))
3580 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3581
3582 // Window should receive motion event.
3583 firstWindowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3584
3585 // Transfer touch focus
3586 ASSERT_TRUE(mDispatcher->transferTouchFocus(firstWindowInPrimary->getToken(),
3587 secondWindowInPrimary->getToken()));
3588 // The first window gets cancel.
3589 firstWindowInPrimary->consumeMotionCancel();
3590 secondWindowInPrimary->consumeMotionDown();
3591
3592 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3593 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
3594 ADISPLAY_ID_DEFAULT, {150, 50}))
3595 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3596 firstWindowInPrimary->assertNoEvents();
3597 secondWindowInPrimary->consumeMotionMove();
3598
3599 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3600 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3601 {150, 50}))
3602 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3603 firstWindowInPrimary->assertNoEvents();
3604 secondWindowInPrimary->consumeMotionUp();
3605}
3606
3607// Same as TransferTouchFocus_CloneSurface, but this touch on the secondary display and use
3608// 'transferTouch' api.
3609TEST_F(InputDispatcherTest, TransferTouch_CloneSurface) {
3610 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3611 sp<FakeWindowHandle> firstWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003612 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00003613 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003614 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003615 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00003616 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003617
3618 sp<FakeWindowHandle> mirrorWindowInPrimary =
3619 firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT);
3620 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003621
3622 sp<FakeWindowHandle> firstWindowInSecondary =
3623 firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
3624 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003625
3626 sp<FakeWindowHandle> secondWindowInSecondary =
3627 secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
3628 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003629
3630 // Update window info, let it find window handle of second display first.
3631 mDispatcher->setInputWindows(
3632 {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}},
3633 {ADISPLAY_ID_DEFAULT,
3634 {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}});
3635
3636 // Touch on second display.
3637 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3638 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {50, 50}))
3639 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3640
3641 // Window should receive motion event.
3642 firstWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID);
3643
3644 // Transfer touch focus
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07003645 ASSERT_TRUE(mDispatcher->transferTouch(secondWindowInSecondary->getToken(), SECOND_DISPLAY_ID));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003646
3647 // The first window gets cancel.
3648 firstWindowInPrimary->consumeMotionCancel(SECOND_DISPLAY_ID);
3649 secondWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID);
3650
3651 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3652 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
3653 SECOND_DISPLAY_ID, {150, 50}))
3654 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3655 firstWindowInPrimary->assertNoEvents();
3656 secondWindowInPrimary->consumeMotionMove(SECOND_DISPLAY_ID);
3657
3658 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3659 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {150, 50}))
3660 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3661 firstWindowInPrimary->assertNoEvents();
3662 secondWindowInPrimary->consumeMotionUp(SECOND_DISPLAY_ID);
3663}
3664
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003665TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07003666 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003667 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3668 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003669
Vishnu Nair47074b82020-08-14 11:54:47 -07003670 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00003671 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003672 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003673
3674 window->consumeFocusEvent(true);
3675
3676 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
3677 mDispatcher->notifyKey(&keyArgs);
3678
3679 // Window should receive key down event.
3680 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3681}
3682
3683TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07003684 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003685 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3686 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003687
Arthur Hung72d8dc32020-03-28 00:48:39 +00003688 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003689
3690 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
3691 mDispatcher->notifyKey(&keyArgs);
3692 mDispatcher->waitForIdle();
3693
3694 window->assertNoEvents();
3695}
3696
3697// If a window is touchable, but does not have focus, it should receive motion events, but not keys
3698TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) {
Chris Yea209fde2020-07-22 13:54:51 -07003699 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003700 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3701 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003702
Arthur Hung72d8dc32020-03-28 00:48:39 +00003703 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003704
3705 // Send key
3706 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
3707 mDispatcher->notifyKey(&keyArgs);
3708 // Send motion
3709 NotifyMotionArgs motionArgs =
3710 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3711 ADISPLAY_ID_DEFAULT);
3712 mDispatcher->notifyMotion(&motionArgs);
3713
3714 // Window should receive only the motion event
3715 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3716 window->assertNoEvents(); // Key event or focus event will not be received
3717}
3718
arthurhungea3f4fc2020-12-21 23:18:53 +08003719TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) {
3720 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3721
arthurhungea3f4fc2020-12-21 23:18:53 +08003722 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003723 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
3724 ADISPLAY_ID_DEFAULT);
arthurhungea3f4fc2020-12-21 23:18:53 +08003725 firstWindow->setFrame(Rect(0, 0, 600, 400));
arthurhungea3f4fc2020-12-21 23:18:53 +08003726
arthurhungea3f4fc2020-12-21 23:18:53 +08003727 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003728 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3729 ADISPLAY_ID_DEFAULT);
arthurhungea3f4fc2020-12-21 23:18:53 +08003730 secondWindow->setFrame(Rect(0, 400, 600, 800));
arthurhungea3f4fc2020-12-21 23:18:53 +08003731
3732 // Add the windows to the dispatcher
3733 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
3734
3735 PointF pointInFirst = {300, 200};
3736 PointF pointInSecond = {300, 600};
3737
3738 // Send down to the first window
3739 NotifyMotionArgs firstDownMotionArgs =
3740 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3741 ADISPLAY_ID_DEFAULT, {pointInFirst});
3742 mDispatcher->notifyMotion(&firstDownMotionArgs);
3743 // Only the first window should get the down event
3744 firstWindow->consumeMotionDown();
3745 secondWindow->assertNoEvents();
3746
3747 // Send down to the second window
3748 NotifyMotionArgs secondDownMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003749 generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
arthurhungea3f4fc2020-12-21 23:18:53 +08003750 {pointInFirst, pointInSecond});
3751 mDispatcher->notifyMotion(&secondDownMotionArgs);
3752 // The first window gets a move and the second a down
3753 firstWindow->consumeMotionMove();
3754 secondWindow->consumeMotionDown();
3755
3756 // Send pointer cancel to the second window
3757 NotifyMotionArgs pointerUpMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003758 generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
arthurhungea3f4fc2020-12-21 23:18:53 +08003759 {pointInFirst, pointInSecond});
3760 pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED;
3761 mDispatcher->notifyMotion(&pointerUpMotionArgs);
3762 // The first window gets move and the second gets cancel.
3763 firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
3764 secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
3765
3766 // Send up event.
3767 NotifyMotionArgs upMotionArgs =
3768 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3769 ADISPLAY_ID_DEFAULT);
3770 mDispatcher->notifyMotion(&upMotionArgs);
3771 // The first window gets up and the second gets nothing.
3772 firstWindow->consumeMotionUp();
3773 secondWindow->assertNoEvents();
3774}
3775
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00003776TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) {
3777 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3778
3779 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003780 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00003781 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3782 std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline;
3783 graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2;
3784 graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3;
3785
3786 window->sendTimeline(1 /*inputEventId*/, graphicsTimeline);
3787 window->assertNoEvents();
3788 mDispatcher->waitForIdle();
3789}
3790
chaviwd1c23182019-12-20 18:44:56 -08003791class FakeMonitorReceiver {
Michael Wright3a240c42019-12-10 20:53:41 +00003792public:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003793 FakeMonitorReceiver(const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003794 int32_t displayId) {
Garfield Tan15601662020-09-22 15:32:38 -07003795 base::Result<std::unique_ptr<InputChannel>> channel =
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08003796 dispatcher->createInputMonitor(displayId, name, MONITOR_PID);
Garfield Tan15601662020-09-22 15:32:38 -07003797 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
Michael Wright3a240c42019-12-10 20:53:41 +00003798 }
3799
chaviwd1c23182019-12-20 18:44:56 -08003800 sp<IBinder> getToken() { return mInputReceiver->getToken(); }
3801
3802 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
3803 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN,
3804 expectedDisplayId, expectedFlags);
3805 }
3806
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003807 std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); }
3808
3809 void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); }
3810
chaviwd1c23182019-12-20 18:44:56 -08003811 void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
3812 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN,
3813 expectedDisplayId, expectedFlags);
3814 }
3815
Siarhei Vishniakouca205502021-07-16 21:31:58 +00003816 void consumeMotionMove(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
3817 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE,
3818 expectedDisplayId, expectedFlags);
3819 }
3820
chaviwd1c23182019-12-20 18:44:56 -08003821 void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
3822 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP,
3823 expectedDisplayId, expectedFlags);
3824 }
3825
Siarhei Vishniakouca205502021-07-16 21:31:58 +00003826 void consumeMotionCancel(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08003827 mInputReceiver->consumeMotionEvent(
3828 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
3829 WithDisplayId(expectedDisplayId),
3830 WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED)));
Siarhei Vishniakouca205502021-07-16 21:31:58 +00003831 }
3832
Arthur Hungfbfa5722021-11-16 02:45:54 +00003833 void consumeMotionPointerDown(int32_t pointerIdx) {
3834 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
3835 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
3836 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, ADISPLAY_ID_DEFAULT,
3837 0 /*expectedFlags*/);
3838 }
3839
Evan Rosky84f07f02021-04-16 10:42:42 -07003840 MotionEvent* consumeMotion() {
3841 InputEvent* event = mInputReceiver->consume();
3842 if (!event) {
3843 ADD_FAILURE() << "No event was produced";
3844 return nullptr;
3845 }
3846 if (event->getType() != AINPUT_EVENT_TYPE_MOTION) {
3847 ADD_FAILURE() << "Received event of type " << event->getType() << " instead of motion";
3848 return nullptr;
3849 }
3850 return static_cast<MotionEvent*>(event);
3851 }
3852
chaviwd1c23182019-12-20 18:44:56 -08003853 void assertNoEvents() { mInputReceiver->assertNoEvents(); }
3854
3855private:
3856 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Michael Wright3a240c42019-12-10 20:53:41 +00003857};
3858
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003859using InputDispatcherMonitorTest = InputDispatcherTest;
3860
Siarhei Vishniakouca205502021-07-16 21:31:58 +00003861/**
3862 * Two entities that receive touch: A window, and a global monitor.
3863 * The touch goes to the window, and then the window disappears.
3864 * The monitor does not get cancel right away. But if more events come in, the touch gets canceled
3865 * for the monitor, as well.
3866 * 1. foregroundWindow
3867 * 2. monitor <-- global monitor (doesn't observe z order, receives all events)
3868 */
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003869TEST_F(InputDispatcherMonitorTest, MonitorTouchIsCanceledWhenForegroundWindowDisappears) {
Siarhei Vishniakouca205502021-07-16 21:31:58 +00003870 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3871 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003872 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00003873
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003874 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00003875
3876 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3877 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3878 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3879 {100, 200}))
3880 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3881
3882 // Both the foreground window and the global monitor should receive the touch down
3883 window->consumeMotionDown();
3884 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
3885
3886 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3887 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
3888 ADISPLAY_ID_DEFAULT, {110, 200}))
3889 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3890
3891 window->consumeMotionMove();
3892 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
3893
3894 // Now the foreground window goes away
3895 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
3896 window->consumeMotionCancel();
3897 monitor.assertNoEvents(); // Global monitor does not get a cancel yet
3898
3899 // If more events come in, there will be no more foreground window to send them to. This will
3900 // cause a cancel for the monitor, as well.
3901 ASSERT_EQ(InputEventInjectionResult::FAILED,
3902 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
3903 ADISPLAY_ID_DEFAULT, {120, 200}))
3904 << "Injection should fail because the window was removed";
3905 window->assertNoEvents();
3906 // Global monitor now gets the cancel
3907 monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
3908}
3909
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003910TEST_F(InputDispatcherMonitorTest, ReceivesMotionEvents) {
Chris Yea209fde2020-07-22 13:54:51 -07003911 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003912 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3913 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00003914 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00003915
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003916 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00003917
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003918 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00003919 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003920 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00003921 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08003922 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00003923}
3924
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003925TEST_F(InputDispatcherMonitorTest, MonitorCannotPilferPointers) {
3926 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00003927
Chris Yea209fde2020-07-22 13:54:51 -07003928 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003929 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3930 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00003931 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00003932
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003933 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00003934 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003935 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
chaviwd1c23182019-12-20 18:44:56 -08003936 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003937 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00003938
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003939 // Pilfer pointers from the monitor.
3940 // This should not do anything and the window should continue to receive events.
3941 EXPECT_NE(OK, mDispatcher->pilferPointers(monitor.getToken()));
Michael Wright3a240c42019-12-10 20:53:41 +00003942
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003943 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003944 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
3945 ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003946 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003947
3948 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
3949 window->consumeMotionMove(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00003950}
3951
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003952TEST_F(InputDispatcherMonitorTest, NoWindowTransform) {
Evan Rosky84f07f02021-04-16 10:42:42 -07003953 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003954 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3955 "Fake Window", ADISPLAY_ID_DEFAULT);
Evan Rosky84f07f02021-04-16 10:42:42 -07003956 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3957 window->setWindowOffset(20, 40);
3958 window->setWindowTransform(0, 1, -1, 0);
3959
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003960 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Evan Rosky84f07f02021-04-16 10:42:42 -07003961
3962 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3963 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
3964 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3965 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3966 MotionEvent* event = monitor.consumeMotion();
3967 // Even though window has transform, gesture monitor must not.
3968 ASSERT_EQ(ui::Transform(), event->getTransform());
3969}
3970
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003971TEST_F(InputDispatcherMonitorTest, InjectionFailsWithNoWindow) {
Arthur Hungb3307ee2021-10-14 10:57:37 +00003972 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003973 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Arthur Hungb3307ee2021-10-14 10:57:37 +00003974
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003975 ASSERT_EQ(InputEventInjectionResult::FAILED,
Arthur Hungb3307ee2021-10-14 10:57:37 +00003976 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003977 << "Injection should fail if there is a monitor, but no touchable window";
3978 monitor.assertNoEvents();
Arthur Hungb3307ee2021-10-14 10:57:37 +00003979}
3980
chaviw81e2bb92019-12-18 15:03:51 -08003981TEST_F(InputDispatcherTest, TestMoveEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07003982 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003983 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3984 "Fake Window", ADISPLAY_ID_DEFAULT);
chaviw81e2bb92019-12-18 15:03:51 -08003985
Arthur Hung72d8dc32020-03-28 00:48:39 +00003986 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
chaviw81e2bb92019-12-18 15:03:51 -08003987
3988 NotifyMotionArgs motionArgs =
3989 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3990 ADISPLAY_ID_DEFAULT);
3991
3992 mDispatcher->notifyMotion(&motionArgs);
3993 // Window should receive motion down event.
3994 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3995
3996 motionArgs.action = AMOTION_EVENT_ACTION_MOVE;
Garfield Tanc51d1ba2020-01-28 13:24:04 -08003997 motionArgs.id += 1;
chaviw81e2bb92019-12-18 15:03:51 -08003998 motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
3999 motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
4000 motionArgs.pointerCoords[0].getX() - 10);
4001
4002 mDispatcher->notifyMotion(&motionArgs);
4003 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT,
4004 0 /*expectedFlags*/);
4005}
4006
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004007/**
4008 * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to
4009 * the device default right away. In the test scenario, we check both the default value,
4010 * and the action of enabling / disabling.
4011 */
4012TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) {
Chris Yea209fde2020-07-22 13:54:51 -07004013 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004014 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4015 "Test window", ADISPLAY_ID_DEFAULT);
Antonio Kantekea47acb2021-12-23 12:41:25 -08004016 const WindowInfo& windowInfo = *window->getInfo();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004017
4018 // Set focused application.
4019 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07004020 window->setFocusable(true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004021
4022 SCOPED_TRACE("Check default value of touch mode");
Arthur Hung72d8dc32020-03-28 00:48:39 +00004023 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004024 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004025 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
4026
4027 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07004028 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004029 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004030 window->consumeFocusEvent(false /*hasFocus*/, true /*inTouchMode*/);
4031
4032 SCOPED_TRACE("Disable touch mode");
Antonio Kantekea47acb2021-12-23 12:41:25 -08004033 mDispatcher->setInTouchMode(false, windowInfo.ownerPid, windowInfo.ownerUid,
Antonio Kanteka042c022022-07-06 16:51:07 -07004034 true /*hasPermission*/, ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00004035 window->consumeTouchModeEvent(false);
Vishnu Nair47074b82020-08-14 11:54:47 -07004036 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004037 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004038 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004039 window->consumeFocusEvent(true /*hasFocus*/, false /*inTouchMode*/);
4040
4041 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07004042 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004043 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004044 window->consumeFocusEvent(false /*hasFocus*/, false /*inTouchMode*/);
4045
4046 SCOPED_TRACE("Enable touch mode again");
Antonio Kantekea47acb2021-12-23 12:41:25 -08004047 mDispatcher->setInTouchMode(true, windowInfo.ownerPid, windowInfo.ownerUid,
Antonio Kanteka042c022022-07-06 16:51:07 -07004048 true /*hasPermission*/, ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00004049 window->consumeTouchModeEvent(true);
Vishnu Nair47074b82020-08-14 11:54:47 -07004050 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004051 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004052 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004053 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
4054
4055 window->assertNoEvents();
4056}
4057
Gang Wange9087892020-01-07 12:17:14 -05004058TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07004059 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004060 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4061 "Test window", ADISPLAY_ID_DEFAULT);
Gang Wange9087892020-01-07 12:17:14 -05004062
4063 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07004064 window->setFocusable(true);
Gang Wange9087892020-01-07 12:17:14 -05004065
Arthur Hung72d8dc32020-03-28 00:48:39 +00004066 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004067 setFocusedWindow(window);
4068
Gang Wange9087892020-01-07 12:17:14 -05004069 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
4070
4071 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
4072 mDispatcher->notifyKey(&keyArgs);
4073
4074 InputEvent* event = window->consume();
4075 ASSERT_NE(event, nullptr);
4076
4077 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
4078 ASSERT_NE(verified, nullptr);
4079 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY);
4080
4081 ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos);
4082 ASSERT_EQ(keyArgs.deviceId, verified->deviceId);
4083 ASSERT_EQ(keyArgs.source, verified->source);
4084 ASSERT_EQ(keyArgs.displayId, verified->displayId);
4085
4086 const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified);
4087
4088 ASSERT_EQ(keyArgs.action, verifiedKey.action);
Gang Wange9087892020-01-07 12:17:14 -05004089 ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08004090 ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos);
Gang Wange9087892020-01-07 12:17:14 -05004091 ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode);
4092 ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode);
4093 ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState);
4094 ASSERT_EQ(0, verifiedKey.repeatCount);
4095}
4096
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08004097TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07004098 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004099 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4100 "Test window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08004101
4102 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
4103
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07004104 ui::Transform transform;
4105 transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
4106
4107 gui::DisplayInfo displayInfo;
4108 displayInfo.displayId = ADISPLAY_ID_DEFAULT;
4109 displayInfo.transform = transform;
4110
4111 mDispatcher->onWindowInfosChanged({*window->getInfo()}, {displayInfo});
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08004112
4113 NotifyMotionArgs motionArgs =
4114 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4115 ADISPLAY_ID_DEFAULT);
4116 mDispatcher->notifyMotion(&motionArgs);
4117
4118 InputEvent* event = window->consume();
4119 ASSERT_NE(event, nullptr);
4120
4121 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
4122 ASSERT_NE(verified, nullptr);
4123 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION);
4124
4125 EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos);
4126 EXPECT_EQ(motionArgs.deviceId, verified->deviceId);
4127 EXPECT_EQ(motionArgs.source, verified->source);
4128 EXPECT_EQ(motionArgs.displayId, verified->displayId);
4129
4130 const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified);
4131
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07004132 const vec2 rawXY =
4133 MotionEvent::calculateTransformedXY(motionArgs.source, transform,
4134 motionArgs.pointerCoords[0].getXYValue());
4135 EXPECT_EQ(rawXY.x, verifiedMotion.rawX);
4136 EXPECT_EQ(rawXY.y, verifiedMotion.rawY);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08004137 EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08004138 EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08004139 EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08004140 EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState);
4141 EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState);
4142}
4143
chaviw09c8d2d2020-08-24 15:48:26 -07004144/**
4145 * Ensure that separate calls to sign the same data are generating the same key.
4146 * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance
4147 * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky
4148 * tests.
4149 */
4150TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) {
4151 KeyEvent event = getTestKeyEvent();
4152 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
4153
4154 std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent);
4155 std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent);
4156 ASSERT_EQ(hmac1, hmac2);
4157}
4158
4159/**
4160 * Ensure that changes in VerifiedKeyEvent produce a different hmac.
4161 */
4162TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) {
4163 KeyEvent event = getTestKeyEvent();
4164 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
4165 std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent);
4166
4167 verifiedEvent.deviceId += 1;
4168 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
4169
4170 verifiedEvent.source += 1;
4171 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
4172
4173 verifiedEvent.eventTimeNanos += 1;
4174 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
4175
4176 verifiedEvent.displayId += 1;
4177 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
4178
4179 verifiedEvent.action += 1;
4180 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
4181
4182 verifiedEvent.downTimeNanos += 1;
4183 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
4184
4185 verifiedEvent.flags += 1;
4186 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
4187
4188 verifiedEvent.keyCode += 1;
4189 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
4190
4191 verifiedEvent.scanCode += 1;
4192 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
4193
4194 verifiedEvent.metaState += 1;
4195 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
4196
4197 verifiedEvent.repeatCount += 1;
4198 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
4199}
4200
Vishnu Nair958da932020-08-21 17:12:37 -07004201TEST_F(InputDispatcherTest, SetFocusedWindow) {
4202 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4203 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004204 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07004205 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004206 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07004207 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
4208
4209 // Top window is also focusable but is not granted focus.
4210 windowTop->setFocusable(true);
4211 windowSecond->setFocusable(true);
4212 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
4213 setFocusedWindow(windowSecond);
4214
4215 windowSecond->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004216 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4217 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004218
4219 // Focused window should receive event.
4220 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
4221 windowTop->assertNoEvents();
4222}
4223
4224TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) {
4225 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4226 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004227 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07004228 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
4229
4230 window->setFocusable(true);
4231 // Release channel for window is no longer valid.
4232 window->releaseChannel();
4233 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4234 setFocusedWindow(window);
4235
4236 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004237 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
4238 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07004239
4240 // window channel is invalid, so it should not receive any input event.
4241 window->assertNoEvents();
4242}
4243
4244TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) {
4245 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4246 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004247 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08004248 window->setFocusable(false);
Vishnu Nair958da932020-08-21 17:12:37 -07004249 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
4250
Vishnu Nair958da932020-08-21 17:12:37 -07004251 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4252 setFocusedWindow(window);
4253
4254 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004255 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
4256 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07004257
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08004258 // window is not focusable, so it should not receive any input event.
Vishnu Nair958da932020-08-21 17:12:37 -07004259 window->assertNoEvents();
4260}
4261
4262TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) {
4263 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4264 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004265 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07004266 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004267 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07004268 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
4269
4270 windowTop->setFocusable(true);
4271 windowSecond->setFocusable(true);
4272 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
4273 setFocusedWindow(windowTop);
4274 windowTop->consumeFocusEvent(true);
4275
4276 setFocusedWindow(windowSecond, windowTop);
4277 windowSecond->consumeFocusEvent(true);
4278 windowTop->consumeFocusEvent(false);
4279
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004280 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4281 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004282
4283 // Focused window should receive event.
4284 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
4285}
4286
4287TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestFocusTokenNotFocused) {
4288 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4289 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004290 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07004291 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004292 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07004293 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
4294
4295 windowTop->setFocusable(true);
4296 windowSecond->setFocusable(true);
4297 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
4298 setFocusedWindow(windowSecond, windowTop);
4299
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004300 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
4301 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07004302
4303 // Event should be dropped.
4304 windowTop->assertNoEvents();
4305 windowSecond->assertNoEvents();
4306}
4307
4308TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) {
4309 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4310 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004311 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07004312 sp<FakeWindowHandle> previousFocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004313 sp<FakeWindowHandle>::make(application, mDispatcher, "previousFocusedWindow",
4314 ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07004315 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
4316
4317 window->setFocusable(true);
4318 previousFocusedWindow->setFocusable(true);
4319 window->setVisible(false);
4320 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}});
4321 setFocusedWindow(previousFocusedWindow);
4322 previousFocusedWindow->consumeFocusEvent(true);
4323
4324 // Requesting focus on invisible window takes focus from currently focused window.
4325 setFocusedWindow(window);
4326 previousFocusedWindow->consumeFocusEvent(false);
4327
4328 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004329 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Vishnu Nair958da932020-08-21 17:12:37 -07004330 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004331 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07004332
4333 // Window does not get focus event or key down.
4334 window->assertNoEvents();
4335
4336 // Window becomes visible.
4337 window->setVisible(true);
4338 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4339
4340 // Window receives focus event.
4341 window->consumeFocusEvent(true);
4342 // Focused window receives key down.
4343 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4344}
4345
Vishnu Nair599f1412021-06-21 10:39:58 -07004346TEST_F(InputDispatcherTest, DisplayRemoved) {
4347 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4348 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004349 sp<FakeWindowHandle>::make(application, mDispatcher, "window", ADISPLAY_ID_DEFAULT);
Vishnu Nair599f1412021-06-21 10:39:58 -07004350 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
4351
4352 // window is granted focus.
4353 window->setFocusable(true);
4354 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4355 setFocusedWindow(window);
4356 window->consumeFocusEvent(true);
4357
4358 // When a display is removed window loses focus.
4359 mDispatcher->displayRemoved(ADISPLAY_ID_DEFAULT);
4360 window->consumeFocusEvent(false);
4361}
4362
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004363/**
4364 * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY,
4365 * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top
4366 * of the 'slipperyEnterWindow'.
4367 *
4368 * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such
4369 * a way so that the touched location is no longer covered by the top window.
4370 *
4371 * Next, inject a MOVE event. Because the top window already moved earlier, this event is now
4372 * positioned over the bottom (slipperyEnterWindow) only. And because the top window had
4373 * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive
4374 * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting
4375 * with ACTION_DOWN).
4376 * Thus, the touch has been transferred from the top window into the bottom window, because the top
4377 * window moved itself away from the touched location and had Flag::SLIPPERY.
4378 *
4379 * Even though the top window moved away from the touched location, it is still obscuring the bottom
4380 * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_
4381 * OBSCURED should be set for the MotionEvent that reaches the bottom window.
4382 *
4383 * In this test, we ensure that the event received by the bottom window has
4384 * FLAG_WINDOW_IS_PARTIALLY_OBSCURED.
4385 */
4386TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) {
Prabir Pradhan5735a322022-04-11 17:23:34 +00004387 constexpr int32_t SLIPPERY_PID = WINDOW_PID + 1;
4388 constexpr int32_t SLIPPERY_UID = WINDOW_UID + 1;
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004389
4390 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4391 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
4392
4393 sp<FakeWindowHandle> slipperyExitWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004394 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08004395 slipperyExitWindow->setSlippery(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004396 // Make sure this one overlaps the bottom window
4397 slipperyExitWindow->setFrame(Rect(25, 25, 75, 75));
4398 // Change the owner uid/pid of the window so that it is considered to be occluding the bottom
4399 // one. Windows with the same owner are not considered to be occluding each other.
4400 slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID);
4401
4402 sp<FakeWindowHandle> slipperyEnterWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004403 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004404 slipperyExitWindow->setFrame(Rect(0, 0, 100, 100));
4405
4406 mDispatcher->setInputWindows(
4407 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
4408
4409 // Use notifyMotion instead of injecting to avoid dealing with injection permissions
4410 NotifyMotionArgs args = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4411 ADISPLAY_ID_DEFAULT, {{50, 50}});
4412 mDispatcher->notifyMotion(&args);
4413 slipperyExitWindow->consumeMotionDown();
4414 slipperyExitWindow->setFrame(Rect(70, 70, 100, 100));
4415 mDispatcher->setInputWindows(
4416 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
4417
4418 args = generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4419 ADISPLAY_ID_DEFAULT, {{51, 51}});
4420 mDispatcher->notifyMotion(&args);
4421
4422 slipperyExitWindow->consumeMotionCancel();
4423
4424 slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT,
4425 AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
4426}
4427
Garfield Tan1c7bc862020-01-28 13:24:04 -08004428class InputDispatcherKeyRepeatTest : public InputDispatcherTest {
4429protected:
4430 static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms
4431 static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms
4432
Chris Yea209fde2020-07-22 13:54:51 -07004433 std::shared_ptr<FakeApplicationHandle> mApp;
Garfield Tan1c7bc862020-01-28 13:24:04 -08004434 sp<FakeWindowHandle> mWindow;
4435
4436 virtual void SetUp() override {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004437 mFakePolicy = sp<FakeInputDispatcherPolicy>::make();
Garfield Tan1c7bc862020-01-28 13:24:04 -08004438 mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004439 mDispatcher = std::make_unique<InputDispatcher>(mFakePolicy);
Garfield Tan1c7bc862020-01-28 13:24:04 -08004440 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
4441 ASSERT_EQ(OK, mDispatcher->start());
4442
4443 setUpWindow();
4444 }
4445
4446 void setUpWindow() {
Chris Yea209fde2020-07-22 13:54:51 -07004447 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004448 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Garfield Tan1c7bc862020-01-28 13:24:04 -08004449
Vishnu Nair47074b82020-08-14 11:54:47 -07004450 mWindow->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004451 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004452 setFocusedWindow(mWindow);
Garfield Tan1c7bc862020-01-28 13:24:04 -08004453 mWindow->consumeFocusEvent(true);
4454 }
4455
Chris Ye2ad95392020-09-01 13:44:44 -07004456 void sendAndConsumeKeyDown(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08004457 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07004458 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08004459 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event
4460 mDispatcher->notifyKey(&keyArgs);
4461
4462 // Window should receive key down event.
4463 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4464 }
4465
4466 void expectKeyRepeatOnce(int32_t repeatCount) {
4467 SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount));
4468 InputEvent* repeatEvent = mWindow->consume();
4469 ASSERT_NE(nullptr, repeatEvent);
4470
4471 uint32_t eventType = repeatEvent->getType();
4472 ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, eventType);
4473
4474 KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent);
4475 uint32_t eventAction = repeatKeyEvent->getAction();
4476 EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction);
4477 EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount());
4478 }
4479
Chris Ye2ad95392020-09-01 13:44:44 -07004480 void sendAndConsumeKeyUp(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08004481 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07004482 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08004483 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event
4484 mDispatcher->notifyKey(&keyArgs);
4485
4486 // Window should receive key down event.
4487 mWindow->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
4488 0 /*expectedFlags*/);
4489 }
4490};
4491
4492TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) {
Chris Ye2ad95392020-09-01 13:44:44 -07004493 sendAndConsumeKeyDown(1 /* deviceId */);
4494 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
4495 expectKeyRepeatOnce(repeatCount);
4496 }
4497}
4498
4499TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) {
4500 sendAndConsumeKeyDown(1 /* deviceId */);
4501 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
4502 expectKeyRepeatOnce(repeatCount);
4503 }
4504 sendAndConsumeKeyDown(2 /* deviceId */);
4505 /* repeatCount will start from 1 for deviceId 2 */
Garfield Tan1c7bc862020-01-28 13:24:04 -08004506 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
4507 expectKeyRepeatOnce(repeatCount);
4508 }
4509}
4510
4511TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) {
Chris Ye2ad95392020-09-01 13:44:44 -07004512 sendAndConsumeKeyDown(1 /* deviceId */);
Garfield Tan1c7bc862020-01-28 13:24:04 -08004513 expectKeyRepeatOnce(1 /*repeatCount*/);
Chris Ye2ad95392020-09-01 13:44:44 -07004514 sendAndConsumeKeyUp(1 /* deviceId */);
4515 mWindow->assertNoEvents();
4516}
4517
4518TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) {
4519 sendAndConsumeKeyDown(1 /* deviceId */);
4520 expectKeyRepeatOnce(1 /*repeatCount*/);
4521 sendAndConsumeKeyDown(2 /* deviceId */);
4522 expectKeyRepeatOnce(1 /*repeatCount*/);
4523 // Stale key up from device 1.
4524 sendAndConsumeKeyUp(1 /* deviceId */);
4525 // Device 2 is still down, keep repeating
4526 expectKeyRepeatOnce(2 /*repeatCount*/);
4527 expectKeyRepeatOnce(3 /*repeatCount*/);
4528 // Device 2 key up
4529 sendAndConsumeKeyUp(2 /* deviceId */);
4530 mWindow->assertNoEvents();
4531}
4532
4533TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) {
4534 sendAndConsumeKeyDown(1 /* deviceId */);
4535 expectKeyRepeatOnce(1 /*repeatCount*/);
4536 sendAndConsumeKeyDown(2 /* deviceId */);
4537 expectKeyRepeatOnce(1 /*repeatCount*/);
4538 // Device 2 which holds the key repeating goes up, expect the repeating to stop.
4539 sendAndConsumeKeyUp(2 /* deviceId */);
4540 // Device 1 still holds key down, but the repeating was already stopped
Garfield Tan1c7bc862020-01-28 13:24:04 -08004541 mWindow->assertNoEvents();
4542}
4543
liushenxiang42232912021-05-21 20:24:09 +08004544TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) {
4545 sendAndConsumeKeyDown(DEVICE_ID);
4546 expectKeyRepeatOnce(1 /*repeatCount*/);
4547 NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID);
4548 mDispatcher->notifyDeviceReset(&args);
4549 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT,
4550 AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS);
4551 mWindow->assertNoEvents();
4552}
4553
Garfield Tan1c7bc862020-01-28 13:24:04 -08004554TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) {
Chris Ye2ad95392020-09-01 13:44:44 -07004555 sendAndConsumeKeyDown(1 /* deviceId */);
Garfield Tan1c7bc862020-01-28 13:24:04 -08004556 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
4557 InputEvent* repeatEvent = mWindow->consume();
4558 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
4559 EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER,
4560 IdGenerator::getSource(repeatEvent->getId()));
4561 }
4562}
4563
4564TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) {
Chris Ye2ad95392020-09-01 13:44:44 -07004565 sendAndConsumeKeyDown(1 /* deviceId */);
Garfield Tan1c7bc862020-01-28 13:24:04 -08004566
4567 std::unordered_set<int32_t> idSet;
4568 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
4569 InputEvent* repeatEvent = mWindow->consume();
4570 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
4571 int32_t id = repeatEvent->getId();
4572 EXPECT_EQ(idSet.end(), idSet.find(id));
4573 idSet.insert(id);
4574 }
4575}
4576
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004577/* Test InputDispatcher for MultiDisplay */
4578class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest {
4579public:
Prabir Pradhan3608aad2019-10-02 17:08:26 -07004580 virtual void SetUp() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004581 InputDispatcherTest::SetUp();
Arthur Hungb92218b2018-08-14 12:00:21 +08004582
Chris Yea209fde2020-07-22 13:54:51 -07004583 application1 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004584 windowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004585 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08004586
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004587 // Set focus window for primary display, but focused display would be second one.
4588 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1);
Vishnu Nair47074b82020-08-14 11:54:47 -07004589 windowInPrimary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004590 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004591 setFocusedWindow(windowInPrimary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004592 windowInPrimary->consumeFocusEvent(true);
Arthur Hungb92218b2018-08-14 12:00:21 +08004593
Chris Yea209fde2020-07-22 13:54:51 -07004594 application2 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004595 windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004596 sp<FakeWindowHandle>::make(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004597 // Set focus to second display window.
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004598 // Set focus display to second one.
4599 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
4600 // Set focus window for second display.
4601 mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2);
Vishnu Nair47074b82020-08-14 11:54:47 -07004602 windowInSecondary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004603 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004604 setFocusedWindow(windowInSecondary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004605 windowInSecondary->consumeFocusEvent(true);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004606 }
4607
Prabir Pradhan3608aad2019-10-02 17:08:26 -07004608 virtual void TearDown() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004609 InputDispatcherTest::TearDown();
4610
Chris Yea209fde2020-07-22 13:54:51 -07004611 application1.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004612 windowInPrimary.clear();
Chris Yea209fde2020-07-22 13:54:51 -07004613 application2.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004614 windowInSecondary.clear();
4615 }
4616
4617protected:
Chris Yea209fde2020-07-22 13:54:51 -07004618 std::shared_ptr<FakeApplicationHandle> application1;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004619 sp<FakeWindowHandle> windowInPrimary;
Chris Yea209fde2020-07-22 13:54:51 -07004620 std::shared_ptr<FakeApplicationHandle> application2;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004621 sp<FakeWindowHandle> windowInSecondary;
4622};
4623
4624TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) {
4625 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004626 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4627 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
4628 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004629 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08004630 windowInSecondary->assertNoEvents();
4631
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004632 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004633 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4634 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
4635 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08004636 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004637 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +08004638}
4639
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004640TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) {
Tiger Huang721e26f2018-07-24 22:26:19 +08004641 // Test inject a key down with display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08004642 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4643 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004644 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004645 windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Tiger Huang721e26f2018-07-24 22:26:19 +08004646 windowInSecondary->assertNoEvents();
4647
4648 // Test inject a key down without display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08004649 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004650 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08004651 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004652 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hungb92218b2018-08-14 12:00:21 +08004653
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08004654 // Remove all windows in secondary display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00004655 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}});
Arthur Hungb92218b2018-08-14 12:00:21 +08004656
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004657 // Old focus should receive a cancel event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004658 windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE,
4659 AKEY_EVENT_FLAG_CANCELED);
Arthur Hungb92218b2018-08-14 12:00:21 +08004660
4661 // Test inject a key down, should timeout because of no target window.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08004662 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004663 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Arthur Hungb92218b2018-08-14 12:00:21 +08004664 windowInPrimary->assertNoEvents();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004665 windowInSecondary->consumeFocusEvent(false);
Arthur Hungb92218b2018-08-14 12:00:21 +08004666 windowInSecondary->assertNoEvents();
4667}
4668
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004669// Test per-display input monitors for motion event.
4670TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) {
chaviwd1c23182019-12-20 18:44:56 -08004671 FakeMonitorReceiver monitorInPrimary =
4672 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
4673 FakeMonitorReceiver monitorInSecondary =
4674 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004675
4676 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004677 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4678 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
4679 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004680 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08004681 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004682 windowInSecondary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08004683 monitorInSecondary.assertNoEvents();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004684
4685 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004686 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4687 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
4688 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004689 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08004690 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004691 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
chaviwd1c23182019-12-20 18:44:56 -08004692 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004693
4694 // Test inject a non-pointer motion event.
4695 // If specific a display, it will dispatch to the focused window of particular display,
4696 // or it will dispatch to the focused window of focused display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004697 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4698 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE))
4699 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004700 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08004701 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004702 windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08004703 monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004704}
4705
4706// Test per-display input monitors for key event.
4707TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) {
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004708 // Input monitor per display.
chaviwd1c23182019-12-20 18:44:56 -08004709 FakeMonitorReceiver monitorInPrimary =
4710 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
4711 FakeMonitorReceiver monitorInSecondary =
4712 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004713
4714 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004715 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4716 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004717 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08004718 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004719 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08004720 monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004721}
4722
Vishnu Nair958da932020-08-21 17:12:37 -07004723TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) {
4724 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004725 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07004726 secondWindowInPrimary->setFocusable(true);
4727 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}});
4728 setFocusedWindow(secondWindowInPrimary);
4729 windowInPrimary->consumeFocusEvent(false);
4730 secondWindowInPrimary->consumeFocusEvent(true);
4731
4732 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004733 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
4734 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004735 windowInPrimary->assertNoEvents();
4736 windowInSecondary->assertNoEvents();
4737 secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4738}
4739
Arthur Hungdfd528e2021-12-08 13:23:04 +00004740TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CancelTouch_MultiDisplay) {
4741 FakeMonitorReceiver monitorInPrimary =
4742 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
4743 FakeMonitorReceiver monitorInSecondary =
4744 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
4745
4746 // Test touch down on primary display.
4747 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4748 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
4749 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4750 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4751 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
4752
4753 // Test touch down on second display.
4754 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4755 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
4756 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4757 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
4758 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
4759
4760 // Trigger cancel touch.
4761 mDispatcher->cancelCurrentTouch();
4762 windowInPrimary->consumeMotionCancel(ADISPLAY_ID_DEFAULT);
4763 monitorInPrimary.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
4764 windowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID);
4765 monitorInSecondary.consumeMotionCancel(SECOND_DISPLAY_ID);
4766
4767 // Test inject a move motion event, no window/monitor should receive the event.
4768 ASSERT_EQ(InputEventInjectionResult::FAILED,
4769 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4770 ADISPLAY_ID_DEFAULT, {110, 200}))
4771 << "Inject motion event should return InputEventInjectionResult::FAILED";
4772 windowInPrimary->assertNoEvents();
4773 monitorInPrimary.assertNoEvents();
4774
4775 ASSERT_EQ(InputEventInjectionResult::FAILED,
4776 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4777 SECOND_DISPLAY_ID, {110, 200}))
4778 << "Inject motion event should return InputEventInjectionResult::FAILED";
4779 windowInSecondary->assertNoEvents();
4780 monitorInSecondary.assertNoEvents();
4781}
4782
Jackal Guof9696682018-10-05 12:23:23 +08004783class InputFilterTest : public InputDispatcherTest {
4784protected:
Prabir Pradhan81420cc2021-09-06 10:28:50 -07004785 void testNotifyMotion(int32_t displayId, bool expectToBeFiltered,
4786 const ui::Transform& transform = ui::Transform()) {
Jackal Guof9696682018-10-05 12:23:23 +08004787 NotifyMotionArgs motionArgs;
4788
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004789 motionArgs =
4790 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Jackal Guof9696682018-10-05 12:23:23 +08004791 mDispatcher->notifyMotion(&motionArgs);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004792 motionArgs =
4793 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Jackal Guof9696682018-10-05 12:23:23 +08004794 mDispatcher->notifyMotion(&motionArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004795 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08004796 if (expectToBeFiltered) {
Prabir Pradhan81420cc2021-09-06 10:28:50 -07004797 const auto xy = transform.transform(motionArgs.pointerCoords->getXYValue());
4798 mFakePolicy->assertFilterInputEventWasCalled(motionArgs, xy);
Jackal Guof9696682018-10-05 12:23:23 +08004799 } else {
4800 mFakePolicy->assertFilterInputEventWasNotCalled();
4801 }
4802 }
4803
4804 void testNotifyKey(bool expectToBeFiltered) {
4805 NotifyKeyArgs keyArgs;
4806
4807 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
4808 mDispatcher->notifyKey(&keyArgs);
4809 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP);
4810 mDispatcher->notifyKey(&keyArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004811 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08004812
4813 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08004814 mFakePolicy->assertFilterInputEventWasCalled(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08004815 } else {
4816 mFakePolicy->assertFilterInputEventWasNotCalled();
4817 }
4818 }
4819};
4820
4821// Test InputFilter for MotionEvent
4822TEST_F(InputFilterTest, MotionEvent_InputFilter) {
4823 // Since the InputFilter is disabled by default, check if touch events aren't filtered.
4824 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
4825 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
4826
4827 // Enable InputFilter
4828 mDispatcher->setInputFilterEnabled(true);
4829 // Test touch on both primary and second display, and check if both events are filtered.
4830 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true);
4831 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true);
4832
4833 // Disable InputFilter
4834 mDispatcher->setInputFilterEnabled(false);
4835 // Test touch on both primary and second display, and check if both events aren't filtered.
4836 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
4837 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
4838}
4839
4840// Test InputFilter for KeyEvent
4841TEST_F(InputFilterTest, KeyEvent_InputFilter) {
4842 // Since the InputFilter is disabled by default, check if key event aren't filtered.
4843 testNotifyKey(/*expectToBeFiltered*/ false);
4844
4845 // Enable InputFilter
4846 mDispatcher->setInputFilterEnabled(true);
4847 // Send a key event, and check if it is filtered.
4848 testNotifyKey(/*expectToBeFiltered*/ true);
4849
4850 // Disable InputFilter
4851 mDispatcher->setInputFilterEnabled(false);
4852 // Send a key event, and check if it isn't filtered.
4853 testNotifyKey(/*expectToBeFiltered*/ false);
4854}
4855
Prabir Pradhan81420cc2021-09-06 10:28:50 -07004856// Ensure that MotionEvents sent to the InputFilter through InputListener are converted to the
4857// logical display coordinate space.
4858TEST_F(InputFilterTest, MotionEvent_UsesLogicalDisplayCoordinates_notifyMotion) {
4859 ui::Transform firstDisplayTransform;
4860 firstDisplayTransform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
4861 ui::Transform secondDisplayTransform;
4862 secondDisplayTransform.set({-6.6, -5.5, -4.4, -3.3, -2.2, -1.1, 0, 0, 1});
4863
4864 std::vector<gui::DisplayInfo> displayInfos(2);
4865 displayInfos[0].displayId = ADISPLAY_ID_DEFAULT;
4866 displayInfos[0].transform = firstDisplayTransform;
4867 displayInfos[1].displayId = SECOND_DISPLAY_ID;
4868 displayInfos[1].transform = secondDisplayTransform;
4869
4870 mDispatcher->onWindowInfosChanged({}, displayInfos);
4871
4872 // Enable InputFilter
4873 mDispatcher->setInputFilterEnabled(true);
4874
4875 // Ensure the correct transforms are used for the displays.
4876 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true, firstDisplayTransform);
4877 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true, secondDisplayTransform);
4878}
4879
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004880class InputFilterInjectionPolicyTest : public InputDispatcherTest {
4881protected:
4882 virtual void SetUp() override {
4883 InputDispatcherTest::SetUp();
4884
4885 /**
4886 * We don't need to enable input filter to test the injected event policy, but we enabled it
4887 * here to make the tests more realistic, since this policy only matters when inputfilter is
4888 * on.
4889 */
4890 mDispatcher->setInputFilterEnabled(true);
4891
4892 std::shared_ptr<InputApplicationHandle> application =
4893 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004894 mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Test Window",
4895 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004896
4897 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
4898 mWindow->setFocusable(true);
4899 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
4900 setFocusedWindow(mWindow);
4901 mWindow->consumeFocusEvent(true);
4902 }
4903
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00004904 void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
4905 int32_t flags) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004906 KeyEvent event;
4907
4908 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
4909 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD,
4910 ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A,
4911 KEY_A, AMETA_NONE, 0 /*repeatCount*/, eventTime, eventTime);
4912 const int32_t additionalPolicyFlags =
4913 POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT;
4914 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Prabir Pradhan5735a322022-04-11 17:23:34 +00004915 mDispatcher->injectInputEvent(&event, {} /*targetUid*/,
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004916 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
4917 policyFlags | additionalPolicyFlags));
4918
4919 InputEvent* received = mWindow->consume();
4920 ASSERT_NE(nullptr, received);
4921 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00004922 ASSERT_EQ(received->getType(), AINPUT_EVENT_TYPE_KEY);
4923 KeyEvent& keyEvent = static_cast<KeyEvent&>(*received);
4924 ASSERT_EQ(flags, keyEvent.getFlags());
4925 }
4926
4927 void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
4928 int32_t flags) {
4929 MotionEvent event;
4930 PointerProperties pointerProperties[1];
4931 PointerCoords pointerCoords[1];
4932 pointerProperties[0].clear();
4933 pointerProperties[0].id = 0;
4934 pointerCoords[0].clear();
4935 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300);
4936 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400);
4937
4938 ui::Transform identityTransform;
4939 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
4940 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN,
4941 DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0,
4942 AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE,
4943 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -07004944 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, eventTime,
Evan Rosky09576692021-07-01 12:22:09 -07004945 eventTime,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00004946 /*pointerCount*/ 1, pointerProperties, pointerCoords);
4947
4948 const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER;
4949 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Prabir Pradhan5735a322022-04-11 17:23:34 +00004950 mDispatcher->injectInputEvent(&event, {} /*targetUid*/,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00004951 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
4952 policyFlags | additionalPolicyFlags));
4953
4954 InputEvent* received = mWindow->consume();
4955 ASSERT_NE(nullptr, received);
4956 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
4957 ASSERT_EQ(received->getType(), AINPUT_EVENT_TYPE_MOTION);
4958 MotionEvent& motionEvent = static_cast<MotionEvent&>(*received);
4959 ASSERT_EQ(flags, motionEvent.getFlags());
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004960 }
4961
4962private:
4963 sp<FakeWindowHandle> mWindow;
4964};
4965
4966TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) {
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00004967 // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input
4968 // filter. Without it, the event will no different from a regularly injected event, and the
4969 // injected device id will be overwritten.
4970 testInjectedKey(POLICY_FLAG_FILTERED, 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/,
4971 0 /*flags*/);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004972}
4973
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00004974TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004975 testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00004976 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/,
4977 AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
4978}
4979
4980TEST_F(InputFilterInjectionPolicyTest,
4981 MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
4982 testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
4983 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/,
4984 AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004985}
4986
4987TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) {
4988 testInjectedKey(0 /*policyFlags*/, 3 /*injectedDeviceId*/,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00004989 VIRTUAL_KEYBOARD_ID /*resolvedDeviceId*/, 0 /*flags*/);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004990}
4991
chaviwfd6d3512019-03-25 13:23:49 -07004992class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest {
Prabir Pradhan3608aad2019-10-02 17:08:26 -07004993 virtual void SetUp() override {
chaviwfd6d3512019-03-25 13:23:49 -07004994 InputDispatcherTest::SetUp();
4995
Chris Yea209fde2020-07-22 13:54:51 -07004996 std::shared_ptr<FakeApplicationHandle> application =
4997 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004998 mUnfocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004999 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07005000 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
chaviwfd6d3512019-03-25 13:23:49 -07005001
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005002 mFocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005003 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005004 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
chaviwfd6d3512019-03-25 13:23:49 -07005005
5006 // Set focused application.
5007 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07005008 mFocusedWindow->setFocusable(true);
chaviwfd6d3512019-03-25 13:23:49 -07005009
5010 // Expect one focus window exist in display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00005011 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005012 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005013 mFocusedWindow->consumeFocusEvent(true);
chaviwfd6d3512019-03-25 13:23:49 -07005014 }
5015
Prabir Pradhan3608aad2019-10-02 17:08:26 -07005016 virtual void TearDown() override {
chaviwfd6d3512019-03-25 13:23:49 -07005017 InputDispatcherTest::TearDown();
5018
5019 mUnfocusedWindow.clear();
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005020 mFocusedWindow.clear();
chaviwfd6d3512019-03-25 13:23:49 -07005021 }
5022
5023protected:
5024 sp<FakeWindowHandle> mUnfocusedWindow;
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005025 sp<FakeWindowHandle> mFocusedWindow;
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07005026 static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60};
chaviwfd6d3512019-03-25 13:23:49 -07005027};
5028
5029// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
5030// DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received
5031// the onPointerDownOutsideFocus callback.
5032TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005033 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07005034 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5035 {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005036 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07005037 mUnfocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07005038
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08005039 ASSERT_TRUE(mDispatcher->waitForIdle());
chaviwfd6d3512019-03-25 13:23:49 -07005040 mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken());
5041}
5042
5043// Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action
5044// DOWN on the window that doesn't have focus. Ensure no window received the
5045// onPointerDownOutsideFocus callback.
5046TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005047 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07005048 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005049 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07005050 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07005051
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08005052 ASSERT_TRUE(mDispatcher->waitForIdle());
5053 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07005054}
5055
5056// Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't
5057// have focus. Ensure no window received the onPointerDownOutsideFocus callback.
5058TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005059 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5060 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005061 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07005062 mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07005063
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08005064 ASSERT_TRUE(mDispatcher->waitForIdle());
5065 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07005066}
5067
5068// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
5069// DOWN on the window that already has focus. Ensure no window received the
5070// onPointerDownOutsideFocus callback.
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005071TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005072 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005073 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07005074 FOCUSED_WINDOW_TOUCH_POINT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005075 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07005076 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07005077
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08005078 ASSERT_TRUE(mDispatcher->waitForIdle());
5079 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07005080}
5081
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08005082// Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag
5083// NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback.
5084TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) {
5085 const MotionEvent event =
5086 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
5087 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
5088 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(20).y(20))
5089 .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE)
5090 .build();
5091 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(mDispatcher, event))
5092 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5093 mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
5094
5095 ASSERT_TRUE(mDispatcher->waitForIdle());
5096 mFakePolicy->assertOnPointerDownWasNotCalled();
5097 // Ensure that the unfocused window did not receive any FOCUS events.
5098 mUnfocusedWindow->assertNoEvents();
5099}
5100
chaviwaf87b3e2019-10-01 16:59:28 -07005101// These tests ensures we can send touch events to a single client when there are multiple input
5102// windows that point to the same client token.
5103class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest {
5104 virtual void SetUp() override {
5105 InputDispatcherTest::SetUp();
5106
Chris Yea209fde2020-07-22 13:54:51 -07005107 std::shared_ptr<FakeApplicationHandle> application =
5108 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005109 mWindow1 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 1",
5110 ADISPLAY_ID_DEFAULT);
chaviwaf87b3e2019-10-01 16:59:28 -07005111 mWindow1->setFrame(Rect(0, 0, 100, 100));
5112
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005113 mWindow2 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 2",
5114 ADISPLAY_ID_DEFAULT, mWindow1->getToken());
chaviwaf87b3e2019-10-01 16:59:28 -07005115 mWindow2->setFrame(Rect(100, 100, 200, 200));
5116
Arthur Hung72d8dc32020-03-28 00:48:39 +00005117 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
chaviwaf87b3e2019-10-01 16:59:28 -07005118 }
5119
5120protected:
5121 sp<FakeWindowHandle> mWindow1;
5122 sp<FakeWindowHandle> mWindow2;
5123
5124 // Helper function to convert the point from screen coordinates into the window's space
chaviw3277faf2021-05-19 16:45:23 -05005125 static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) {
chaviw1ff3d1e2020-07-01 15:53:47 -07005126 vec2 vals = windowInfo->transform.transform(point.x, point.y);
5127 return {vals.x, vals.y};
chaviwaf87b3e2019-10-01 16:59:28 -07005128 }
5129
5130 void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction,
5131 const std::vector<PointF>& points) {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005132 const std::string name = window->getName();
chaviwaf87b3e2019-10-01 16:59:28 -07005133 InputEvent* event = window->consume();
5134
5135 ASSERT_NE(nullptr, event) << name.c_str()
5136 << ": consumer should have returned non-NULL event.";
5137
5138 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType())
5139 << name.c_str() << "expected " << inputEventTypeToString(AINPUT_EVENT_TYPE_MOTION)
5140 << " event, got " << inputEventTypeToString(event->getType()) << " event";
5141
5142 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005143 assertMotionAction(expectedAction, motionEvent.getAction());
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08005144 ASSERT_EQ(points.size(), motionEvent.getPointerCount());
chaviwaf87b3e2019-10-01 16:59:28 -07005145
5146 for (size_t i = 0; i < points.size(); i++) {
5147 float expectedX = points[i].x;
5148 float expectedY = points[i].y;
5149
5150 EXPECT_EQ(expectedX, motionEvent.getX(i))
5151 << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str()
5152 << ", got " << motionEvent.getX(i);
5153 EXPECT_EQ(expectedY, motionEvent.getY(i))
5154 << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str()
5155 << ", got " << motionEvent.getY(i);
5156 }
5157 }
chaviw9eaa22c2020-07-01 16:21:27 -07005158
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08005159 void touchAndAssertPositions(int32_t action, const std::vector<PointF>& touchedPoints,
chaviw9eaa22c2020-07-01 16:21:27 -07005160 std::vector<PointF> expectedPoints) {
5161 NotifyMotionArgs motionArgs = generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN,
5162 ADISPLAY_ID_DEFAULT, touchedPoints);
5163 mDispatcher->notifyMotion(&motionArgs);
5164
5165 // Always consume from window1 since it's the window that has the InputReceiver
5166 consumeMotionEvent(mWindow1, action, expectedPoints);
5167 }
chaviwaf87b3e2019-10-01 16:59:28 -07005168};
5169
5170TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) {
5171 // Touch Window 1
5172 PointF touchedPoint = {10, 10};
5173 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07005174 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07005175
5176 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07005177 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07005178
5179 // Touch Window 2
5180 touchedPoint = {150, 150};
5181 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07005182 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07005183}
5184
chaviw9eaa22c2020-07-01 16:21:27 -07005185TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) {
5186 // Set scale value for window2
chaviwaf87b3e2019-10-01 16:59:28 -07005187 mWindow2->setWindowScale(0.5f, 0.5f);
5188
5189 // Touch Window 1
5190 PointF touchedPoint = {10, 10};
5191 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07005192 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07005193 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07005194 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07005195
5196 // Touch Window 2
5197 touchedPoint = {150, 150};
5198 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07005199 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
5200 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07005201
chaviw9eaa22c2020-07-01 16:21:27 -07005202 // Update the transform so rotation is set
5203 mWindow2->setWindowTransform(0, -1, 1, 0);
5204 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
5205 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07005206}
5207
chaviw9eaa22c2020-07-01 16:21:27 -07005208TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00005209 mWindow2->setWindowScale(0.5f, 0.5f);
5210
5211 // Touch Window 1
5212 std::vector<PointF> touchedPoints = {PointF{10, 10}};
5213 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07005214 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00005215
5216 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07005217 touchedPoints.push_back(PointF{150, 150});
5218 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08005219 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00005220
chaviw9eaa22c2020-07-01 16:21:27 -07005221 // Release Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08005222 touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07005223 expectedPoints.pop_back();
Chavi Weingarten65f98b82020-01-16 18:56:50 +00005224
chaviw9eaa22c2020-07-01 16:21:27 -07005225 // Update the transform so rotation is set for Window 2
5226 mWindow2->setWindowTransform(0, -1, 1, 0);
5227 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08005228 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00005229}
5230
chaviw9eaa22c2020-07-01 16:21:27 -07005231TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00005232 mWindow2->setWindowScale(0.5f, 0.5f);
5233
5234 // Touch Window 1
5235 std::vector<PointF> touchedPoints = {PointF{10, 10}};
5236 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07005237 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00005238
5239 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07005240 touchedPoints.push_back(PointF{150, 150});
5241 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00005242
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08005243 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00005244
5245 // Move both windows
5246 touchedPoints = {{20, 20}, {175, 175}};
5247 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
5248 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
5249
chaviw9eaa22c2020-07-01 16:21:27 -07005250 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00005251
chaviw9eaa22c2020-07-01 16:21:27 -07005252 // Release Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08005253 touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07005254 expectedPoints.pop_back();
5255
5256 // Touch Window 2
5257 mWindow2->setWindowTransform(0, -1, 1, 0);
5258 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08005259 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07005260
5261 // Move both windows
5262 touchedPoints = {{20, 20}, {175, 175}};
5263 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
5264 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
5265
5266 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00005267}
5268
5269TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) {
5270 mWindow1->setWindowScale(0.5f, 0.5f);
5271
5272 // Touch Window 1
5273 std::vector<PointF> touchedPoints = {PointF{10, 10}};
5274 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07005275 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00005276
5277 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07005278 touchedPoints.push_back(PointF{150, 150});
5279 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00005280
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08005281 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00005282
5283 // Move both windows
5284 touchedPoints = {{20, 20}, {175, 175}};
5285 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
5286 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
5287
chaviw9eaa22c2020-07-01 16:21:27 -07005288 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00005289}
5290
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005291class InputDispatcherSingleWindowAnr : public InputDispatcherTest {
5292 virtual void SetUp() override {
5293 InputDispatcherTest::SetUp();
5294
Chris Yea209fde2020-07-22 13:54:51 -07005295 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005296 mApplication->setDispatchingTimeout(20ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005297 mWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "TestWindow",
5298 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005299 mWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05005300 mWindow->setDispatchingTimeout(30ms);
Vishnu Nair47074b82020-08-14 11:54:47 -07005301 mWindow->setFocusable(true);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005302
5303 // Set focused application.
5304 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
5305
5306 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005307 setFocusedWindow(mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005308 mWindow->consumeFocusEvent(true);
5309 }
5310
5311 virtual void TearDown() override {
5312 InputDispatcherTest::TearDown();
5313 mWindow.clear();
5314 }
5315
5316protected:
Chris Yea209fde2020-07-22 13:54:51 -07005317 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005318 sp<FakeWindowHandle> mWindow;
5319 static constexpr PointF WINDOW_LOCATION = {20, 20};
5320
5321 void tapOnWindow() {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005322 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005323 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5324 WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005325 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005326 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5327 WINDOW_LOCATION));
5328 }
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005329
5330 sp<FakeWindowHandle> addSpyWindow() {
5331 sp<FakeWindowHandle> spy =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005332 sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005333 spy->setTrustedOverlay(true);
5334 spy->setFocusable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08005335 spy->setSpy(true);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005336 spy->setDispatchingTimeout(30ms);
5337 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, mWindow}}});
5338 return spy;
5339 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005340};
5341
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005342// Send a tap and respond, which should not cause an ANR.
5343TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) {
5344 tapOnWindow();
5345 mWindow->consumeMotionDown();
5346 mWindow->consumeMotionUp();
5347 ASSERT_TRUE(mDispatcher->waitForIdle());
5348 mFakePolicy->assertNotifyAnrWasNotCalled();
5349}
5350
5351// Send a regular key and respond, which should not cause an ANR.
5352TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005353 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005354 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
5355 ASSERT_TRUE(mDispatcher->waitForIdle());
5356 mFakePolicy->assertNotifyAnrWasNotCalled();
5357}
5358
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05005359TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) {
5360 mWindow->setFocusable(false);
5361 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
5362 mWindow->consumeFocusEvent(false);
5363
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005364 InputEventInjectionResult result =
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05005365 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005366 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/,
5367 false /* allowKeyRepeat */);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005368 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05005369 // Key will not go to window because we have no focused window.
5370 // The 'no focused window' ANR timer should start instead.
5371
5372 // Now, the focused application goes away.
5373 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr);
5374 // The key should get dropped and there should be no ANR.
5375
5376 ASSERT_TRUE(mDispatcher->waitForIdle());
5377 mFakePolicy->assertNotifyAnrWasNotCalled();
5378}
5379
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005380// Send an event to the app and have the app not respond right away.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005381// When ANR is raised, policy will tell the dispatcher to cancel the events for that window.
5382// So InputDispatcher will enqueue ACTION_CANCEL event as well.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005383TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005384 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005385 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5386 WINDOW_LOCATION));
5387
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005388 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN
5389 ASSERT_TRUE(sequenceNum);
5390 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005391 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005392
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005393 mWindow->finishEvent(*sequenceNum);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08005394 mWindow->consumeMotionEvent(
5395 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005396 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08005397 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005398}
5399
5400// Send a key to the app and have the app not respond right away.
5401TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) {
5402 // Inject a key, and don't respond - expect that ANR is called.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005403 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005404 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent();
5405 ASSERT_TRUE(sequenceNum);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005406 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005407 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005408 ASSERT_TRUE(mDispatcher->waitForIdle());
5409}
5410
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005411// We have a focused application, but no focused window
5412TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) {
Vishnu Nair47074b82020-08-14 11:54:47 -07005413 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005414 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
5415 mWindow->consumeFocusEvent(false);
5416
5417 // taps on the window work as normal
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005418 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005419 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5420 WINDOW_LOCATION));
5421 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
5422 mDispatcher->waitForIdle();
5423 mFakePolicy->assertNotifyAnrWasNotCalled();
5424
5425 // Once a focused event arrives, we get an ANR for this application
5426 // We specify the injection timeout to be smaller than the application timeout, to ensure that
5427 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005428 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005429 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005430 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, false /* allowKeyRepeat */);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005431 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005432 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Vishnu Naire4df8752022-09-08 09:17:55 -07005433 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005434 ASSERT_TRUE(mDispatcher->waitForIdle());
5435}
5436
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08005437/**
5438 * Make sure the stale key is dropped before causing an ANR. So even if there's no focused window,
5439 * there will not be an ANR.
5440 */
5441TEST_F(InputDispatcherSingleWindowAnr, StaleKeyEventDoesNotAnr) {
5442 mWindow->setFocusable(false);
5443 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
5444 mWindow->consumeFocusEvent(false);
5445
5446 KeyEvent event;
5447 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC) -
5448 std::chrono::nanoseconds(STALE_EVENT_TIMEOUT).count();
5449
5450 // Define a valid key down event that is stale (too old).
5451 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
5452 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, /* flags */ 0, AKEYCODE_A, KEY_A,
5453 AMETA_NONE, 1 /*repeatCount*/, eventTime, eventTime);
5454
5455 const int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER;
5456
5457 InputEventInjectionResult result =
Prabir Pradhan5735a322022-04-11 17:23:34 +00005458 mDispatcher->injectInputEvent(&event, {} /* targetUid */,
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08005459 InputEventInjectionSync::WAIT_FOR_RESULT,
5460 INJECT_EVENT_TIMEOUT, policyFlags);
5461 ASSERT_EQ(InputEventInjectionResult::FAILED, result)
5462 << "Injection should fail because the event is stale";
5463
5464 ASSERT_TRUE(mDispatcher->waitForIdle());
5465 mFakePolicy->assertNotifyAnrWasNotCalled();
5466 mWindow->assertNoEvents();
5467}
5468
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005469// We have a focused application, but no focused window
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005470// Make sure that we don't notify policy twice about the same ANR.
5471TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) {
Vishnu Nair47074b82020-08-14 11:54:47 -07005472 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005473 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
5474 mWindow->consumeFocusEvent(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005475
5476 // Once a focused event arrives, we get an ANR for this application
5477 // We specify the injection timeout to be smaller than the application timeout, to ensure that
5478 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005479 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005480 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005481 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, false /* allowKeyRepeat */);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005482 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Vishnu Naire4df8752022-09-08 09:17:55 -07005483 const std::chrono::duration appTimeout =
5484 mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
5485 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005486
Vishnu Naire4df8752022-09-08 09:17:55 -07005487 std::this_thread::sleep_for(appTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005488 // ANR should not be raised again. It is up to policy to do that if it desires.
5489 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005490
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005491 // If we now get a focused window, the ANR should stop, but the policy handles that via
5492 // 'notifyFocusChanged' callback. This is implemented in the policy so we can't test it here.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005493 ASSERT_TRUE(mDispatcher->waitForIdle());
5494}
5495
5496// We have a focused application, but no focused window
5497TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) {
Vishnu Nair47074b82020-08-14 11:54:47 -07005498 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005499 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
5500 mWindow->consumeFocusEvent(false);
5501
5502 // Once a focused event arrives, we get an ANR for this application
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005503 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005504 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005505 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
5506 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005507
Vishnu Naire4df8752022-09-08 09:17:55 -07005508 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
5509 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005510
5511 // Future focused events get dropped right away
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005512 ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005513 ASSERT_TRUE(mDispatcher->waitForIdle());
5514 mWindow->assertNoEvents();
5515}
5516
5517/**
5518 * Ensure that the implementation is valid. Since we are using multiset to keep track of the
5519 * ANR timeouts, we are allowing entries with identical timestamps in the same connection.
5520 * If we process 1 of the events, but ANR on the second event with the same timestamp,
5521 * the ANR mechanism should still work.
5522 *
5523 * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the
5524 * DOWN event, while not responding on the second one.
5525 */
5526TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) {
5527 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
5528 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5529 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
5530 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
5531 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005532 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005533
5534 // Now send ACTION_UP, with identical timestamp
5535 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
5536 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
5537 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
5538 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005539 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005540
5541 // We have now sent down and up. Let's consume first event and then ANR on the second.
5542 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5543 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005544 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005545}
5546
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005547// A spy window can receive an ANR
5548TEST_F(InputDispatcherSingleWindowAnr, SpyWindowAnr) {
5549 sp<FakeWindowHandle> spy = addSpyWindow();
5550
5551 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5552 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5553 WINDOW_LOCATION));
5554 mWindow->consumeMotionDown();
5555
5556 std::optional<uint32_t> sequenceNum = spy->receiveEvent(); // ACTION_DOWN
5557 ASSERT_TRUE(sequenceNum);
5558 const std::chrono::duration timeout = spy->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005559 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, spy);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005560
5561 spy->finishEvent(*sequenceNum);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08005562 spy->consumeMotionEvent(
5563 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005564 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08005565 mFakePolicy->assertNotifyWindowResponsiveWasCalled(spy->getToken(), mWindow->getPid());
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005566}
5567
5568// If an app is not responding to a key event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005569// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005570TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnKey) {
5571 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005572
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005573 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5574 injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005575 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005576 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005577
5578 // Stuck on the ACTION_UP
5579 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005580 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005581
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005582 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005583 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005584 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5585 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005586
5587 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion
5588 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08005589 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005590 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005591 spy->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005592}
5593
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005594// If an app is not responding to a motion event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005595// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005596TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnMotion) {
5597 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005598
5599 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005600 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5601 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005602
5603 mWindow->consumeMotionDown();
5604 // Stuck on the ACTION_UP
5605 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005606 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005607
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005608 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005609 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005610 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5611 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005612
5613 mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion
5614 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08005615 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005616 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005617 spy->assertNoEvents();
5618}
5619
5620TEST_F(InputDispatcherSingleWindowAnr, UnresponsiveMonitorAnr) {
5621 mDispatcher->setMonitorDispatchingTimeoutForTest(30ms);
5622
5623 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
5624
5625 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5626 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5627 WINDOW_LOCATION));
5628
5629 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5630 const std::optional<uint32_t> consumeSeq = monitor.receiveEvent();
5631 ASSERT_TRUE(consumeSeq);
5632
Prabir Pradhanedd96402022-02-15 01:46:16 -08005633 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(30ms, monitor.getToken(), MONITOR_PID);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005634
5635 monitor.finishEvent(*consumeSeq);
5636 monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
5637
5638 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08005639 mFakePolicy->assertNotifyWindowResponsiveWasCalled(monitor.getToken(), MONITOR_PID);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005640}
5641
5642// If a window is unresponsive, then you get anr. if the window later catches up and starts to
5643// process events, you don't get an anr. When the window later becomes unresponsive again, you
5644// get an ANR again.
5645// 1. tap -> block on ACTION_UP -> receive ANR
5646// 2. consume all pending events (= queue becomes healthy again)
5647// 3. tap again -> block on ACTION_UP again -> receive ANR second time
5648TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) {
5649 tapOnWindow();
5650
5651 mWindow->consumeMotionDown();
5652 // Block on ACTION_UP
5653 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005654 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005655 mWindow->consumeMotionUp(); // Now the connection should be healthy again
5656 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08005657 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005658 mWindow->assertNoEvents();
5659
5660 tapOnWindow();
5661 mWindow->consumeMotionDown();
Prabir Pradhanedd96402022-02-15 01:46:16 -08005662 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005663 mWindow->consumeMotionUp();
5664
5665 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08005666 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005667 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005668 mWindow->assertNoEvents();
5669}
5670
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005671// If a connection remains unresponsive for a while, make sure policy is only notified once about
5672// it.
5673TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005674 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005675 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5676 WINDOW_LOCATION));
5677
5678 const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005679 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005680 std::this_thread::sleep_for(windowTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005681 // 'notifyConnectionUnresponsive' should only be called once per connection
5682 mFakePolicy->assertNotifyAnrWasNotCalled();
5683 // When the ANR happened, dispatcher should abort the current event stream via ACTION_CANCEL
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005684 mWindow->consumeMotionDown();
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08005685 mWindow->consumeMotionEvent(
5686 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005687 mWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005688 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08005689 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005690 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005691}
5692
5693/**
5694 * If a window is processing a motion event, and then a key event comes in, the key event should
5695 * not to to the focused window until the motion is processed.
5696 *
5697 * Warning!!!
5698 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
5699 * and the injection timeout that we specify when injecting the key.
5700 * We must have the injection timeout (10ms) be smaller than
5701 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
5702 *
5703 * If that value changes, this test should also change.
5704 */
5705TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) {
5706 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
5707 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
5708
5709 tapOnWindow();
5710 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
5711 ASSERT_TRUE(downSequenceNum);
5712 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
5713 ASSERT_TRUE(upSequenceNum);
5714 // Don't finish the events yet, and send a key
5715 // Injection will "succeed" because we will eventually give up and send the key to the focused
5716 // window even if motions are still being processed. But because the injection timeout is short,
5717 // we will receive INJECTION_TIMED_OUT as the result.
5718
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005719 InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005720 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005721 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
5722 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005723 // Key will not be sent to the window, yet, because the window is still processing events
5724 // and the key remains pending, waiting for the touch events to be processed
5725 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
5726 ASSERT_FALSE(keySequenceNum);
5727
5728 std::this_thread::sleep_for(500ms);
5729 // if we wait long enough though, dispatcher will give up, and still send the key
5730 // to the focused window, even though we have not yet finished the motion event
5731 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5732 mWindow->finishEvent(*downSequenceNum);
5733 mWindow->finishEvent(*upSequenceNum);
5734}
5735
5736/**
5737 * If a window is processing a motion event, and then a key event comes in, the key event should
5738 * not go to the focused window until the motion is processed.
5739 * If then a new motion comes in, then the pending key event should be going to the currently
5740 * focused window right away.
5741 */
5742TEST_F(InputDispatcherSingleWindowAnr,
5743 PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) {
5744 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
5745 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
5746
5747 tapOnWindow();
5748 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
5749 ASSERT_TRUE(downSequenceNum);
5750 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
5751 ASSERT_TRUE(upSequenceNum);
5752 // Don't finish the events yet, and send a key
5753 // Injection is async, so it will succeed
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005754 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005755 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005756 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005757 // At this point, key is still pending, and should not be sent to the application yet.
5758 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
5759 ASSERT_FALSE(keySequenceNum);
5760
5761 // Now tap down again. It should cause the pending key to go to the focused window right away.
5762 tapOnWindow();
5763 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd
5764 // the other events yet. We can finish events in any order.
5765 mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN
5766 mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP
5767 mWindow->consumeMotionDown();
5768 mWindow->consumeMotionUp();
5769 mWindow->assertNoEvents();
5770}
5771
5772class InputDispatcherMultiWindowAnr : public InputDispatcherTest {
5773 virtual void SetUp() override {
5774 InputDispatcherTest::SetUp();
5775
Chris Yea209fde2020-07-22 13:54:51 -07005776 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005777 mApplication->setDispatchingTimeout(10ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005778 mUnfocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Unfocused",
5779 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005780 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005781 // Adding FLAG_WATCH_OUTSIDE_TOUCH to receive ACTION_OUTSIDE when another window is tapped
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08005782 mUnfocusedWindow->setWatchOutsideTouch(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005783
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005784 mFocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Focused",
5785 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05005786 mFocusedWindow->setDispatchingTimeout(30ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005787 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005788
5789 // Set focused application.
5790 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
Vishnu Nair47074b82020-08-14 11:54:47 -07005791 mFocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005792
5793 // Expect one focus window exist in display.
5794 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005795 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005796 mFocusedWindow->consumeFocusEvent(true);
5797 }
5798
5799 virtual void TearDown() override {
5800 InputDispatcherTest::TearDown();
5801
5802 mUnfocusedWindow.clear();
5803 mFocusedWindow.clear();
5804 }
5805
5806protected:
Chris Yea209fde2020-07-22 13:54:51 -07005807 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005808 sp<FakeWindowHandle> mUnfocusedWindow;
5809 sp<FakeWindowHandle> mFocusedWindow;
5810 static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20};
5811 static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75};
5812 static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40};
5813
5814 void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); }
5815
5816 void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); }
5817
5818private:
5819 void tap(const PointF& location) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005820 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005821 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5822 location));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005823 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005824 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5825 location));
5826 }
5827};
5828
5829// If we have 2 windows that are both unresponsive, the one with the shortest timeout
5830// should be ANR'd first.
5831TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) {
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 FOCUSED_WINDOW_LOCATION))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005835 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005836 mFocusedWindow->consumeMotionDown();
5837 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
5838 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
5839 // We consumed all events, so no ANR
5840 ASSERT_TRUE(mDispatcher->waitForIdle());
5841 mFakePolicy->assertNotifyAnrWasNotCalled();
5842
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005843 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005844 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5845 FOCUSED_WINDOW_LOCATION));
5846 std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent();
5847 ASSERT_TRUE(unfocusedSequenceNum);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005848
5849 const std::chrono::duration timeout =
5850 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005851 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005852 // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event
5853 // sequence to make it consistent
5854 mFocusedWindow->consumeMotionCancel();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005855 mUnfocusedWindow->finishEvent(*unfocusedSequenceNum);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005856 mFocusedWindow->consumeMotionDown();
5857 // This cancel is generated because the connection was unresponsive
5858 mFocusedWindow->consumeMotionCancel();
5859 mFocusedWindow->assertNoEvents();
5860 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005861 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08005862 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
5863 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005864 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005865}
5866
5867// If we have 2 windows with identical timeouts that are both unresponsive,
5868// it doesn't matter which order they should have ANR.
5869// But we should receive ANR for both.
5870TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) {
5871 // Set the timeout for unfocused window to match the focused window
5872 mUnfocusedWindow->setDispatchingTimeout(10ms);
5873 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
5874
5875 tapOnFocusedWindow();
5876 // we should have ACTION_DOWN/ACTION_UP on focused window and ACTION_OUTSIDE on unfocused window
Prabir Pradhanedd96402022-02-15 01:46:16 -08005877 sp<IBinder> anrConnectionToken1, anrConnectionToken2;
5878 ASSERT_NO_FATAL_FAILURE(anrConnectionToken1 = mFakePolicy->getUnresponsiveWindowToken(10ms));
5879 ASSERT_NO_FATAL_FAILURE(anrConnectionToken2 = mFakePolicy->getUnresponsiveWindowToken(0ms));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005880
5881 // We don't know which window will ANR first. But both of them should happen eventually.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005882 ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 ||
5883 mFocusedWindow->getToken() == anrConnectionToken2);
5884 ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 ||
5885 mUnfocusedWindow->getToken() == anrConnectionToken2);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005886
5887 ASSERT_TRUE(mDispatcher->waitForIdle());
5888 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005889
5890 mFocusedWindow->consumeMotionDown();
5891 mFocusedWindow->consumeMotionUp();
5892 mUnfocusedWindow->consumeMotionOutside();
5893
Prabir Pradhanedd96402022-02-15 01:46:16 -08005894 sp<IBinder> responsiveToken1, responsiveToken2;
5895 ASSERT_NO_FATAL_FAILURE(responsiveToken1 = mFakePolicy->getResponsiveWindowToken());
5896 ASSERT_NO_FATAL_FAILURE(responsiveToken2 = mFakePolicy->getResponsiveWindowToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005897
5898 // Both applications should be marked as responsive, in any order
5899 ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 ||
5900 mFocusedWindow->getToken() == responsiveToken2);
5901 ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 ||
5902 mUnfocusedWindow->getToken() == responsiveToken2);
5903 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005904}
5905
5906// If a window is already not responding, the second tap on the same window should be ignored.
5907// We should also log an error to account for the dropped event (not tested here).
5908// At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events.
5909TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) {
5910 tapOnFocusedWindow();
5911 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
5912 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
5913 // Receive the events, but don't respond
5914 std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN
5915 ASSERT_TRUE(downEventSequenceNum);
5916 std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP
5917 ASSERT_TRUE(upEventSequenceNum);
5918 const std::chrono::duration timeout =
5919 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005920 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005921
5922 // Tap once again
5923 // We cannot use "tapOnFocusedWindow" because it asserts the injection result to be success
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005924 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005925 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5926 FOCUSED_WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005927 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005928 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5929 FOCUSED_WINDOW_LOCATION));
5930 // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a
5931 // valid touch target
5932 mUnfocusedWindow->assertNoEvents();
5933
5934 // Consume the first tap
5935 mFocusedWindow->finishEvent(*downEventSequenceNum);
5936 mFocusedWindow->finishEvent(*upEventSequenceNum);
5937 ASSERT_TRUE(mDispatcher->waitForIdle());
5938 // The second tap did not go to the focused window
5939 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005940 // Since all events are finished, connection should be deemed healthy again
Prabir Pradhanedd96402022-02-15 01:46:16 -08005941 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
5942 mFocusedWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005943 mFakePolicy->assertNotifyAnrWasNotCalled();
5944}
5945
5946// If you tap outside of all windows, there will not be ANR
5947TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005948 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005949 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5950 LOCATION_OUTSIDE_ALL_WINDOWS));
5951 ASSERT_TRUE(mDispatcher->waitForIdle());
5952 mFakePolicy->assertNotifyAnrWasNotCalled();
5953}
5954
5955// Since the focused window is paused, tapping on it should not produce any events
5956TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) {
5957 mFocusedWindow->setPaused(true);
5958 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
5959
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005960 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005961 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5962 FOCUSED_WINDOW_LOCATION));
5963
5964 std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT));
5965 ASSERT_TRUE(mDispatcher->waitForIdle());
5966 // Should not ANR because the window is paused, and touches shouldn't go to it
5967 mFakePolicy->assertNotifyAnrWasNotCalled();
5968
5969 mFocusedWindow->assertNoEvents();
5970 mUnfocusedWindow->assertNoEvents();
5971}
5972
5973/**
5974 * If a window is processing a motion event, and then a key event comes in, the key event should
5975 * not to to the focused window until the motion is processed.
5976 * If a different window becomes focused at this time, the key should go to that window instead.
5977 *
5978 * Warning!!!
5979 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
5980 * and the injection timeout that we specify when injecting the key.
5981 * We must have the injection timeout (10ms) be smaller than
5982 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
5983 *
5984 * If that value changes, this test should also change.
5985 */
5986TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) {
5987 // Set a long ANR timeout to prevent it from triggering
5988 mFocusedWindow->setDispatchingTimeout(2s);
5989 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
5990
5991 tapOnUnfocusedWindow();
5992 std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent();
5993 ASSERT_TRUE(downSequenceNum);
5994 std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent();
5995 ASSERT_TRUE(upSequenceNum);
5996 // Don't finish the events yet, and send a key
5997 // Injection will succeed because we will eventually give up and send the key to the focused
5998 // window even if motions are still being processed.
5999
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006000 InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006001 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006002 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/);
6003 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006004 // Key will not be sent to the window, yet, because the window is still processing events
6005 // and the key remains pending, waiting for the touch events to be processed
6006 std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent();
6007 ASSERT_FALSE(keySequenceNum);
6008
6009 // Switch the focus to the "unfocused" window that we tapped. Expect the key to go there
Vishnu Nair47074b82020-08-14 11:54:47 -07006010 mFocusedWindow->setFocusable(false);
6011 mUnfocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006012 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07006013 setFocusedWindow(mUnfocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006014
6015 // Focus events should precede the key events
6016 mUnfocusedWindow->consumeFocusEvent(true);
6017 mFocusedWindow->consumeFocusEvent(false);
6018
6019 // Finish the tap events, which should unblock dispatcher
6020 mUnfocusedWindow->finishEvent(*downSequenceNum);
6021 mUnfocusedWindow->finishEvent(*upSequenceNum);
6022
6023 // Now that all queues are cleared and no backlog in the connections, the key event
6024 // can finally go to the newly focused "mUnfocusedWindow".
6025 mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
6026 mFocusedWindow->assertNoEvents();
6027 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006028 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006029}
6030
6031// When the touch stream is split across 2 windows, and one of them does not respond,
6032// then ANR should be raised and the touch should be canceled for the unresponsive window.
6033// The other window should not be affected by that.
6034TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) {
6035 // Touch Window 1
6036 NotifyMotionArgs motionArgs =
6037 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6038 ADISPLAY_ID_DEFAULT, {FOCUSED_WINDOW_LOCATION});
6039 mDispatcher->notifyMotion(&motionArgs);
6040 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
6041 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
6042
6043 // Touch Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006044 motionArgs = generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6045 {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006046 mDispatcher->notifyMotion(&motionArgs);
6047
6048 const std::chrono::duration timeout =
6049 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006050 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006051
6052 mUnfocusedWindow->consumeMotionDown();
6053 mFocusedWindow->consumeMotionDown();
6054 // Focused window may or may not receive ACTION_MOVE
6055 // But it should definitely receive ACTION_CANCEL due to the ANR
6056 InputEvent* event;
6057 std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event);
6058 ASSERT_TRUE(moveOrCancelSequenceNum);
6059 mFocusedWindow->finishEvent(*moveOrCancelSequenceNum);
6060 ASSERT_NE(nullptr, event);
6061 ASSERT_EQ(event->getType(), AINPUT_EVENT_TYPE_MOTION);
6062 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
6063 if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) {
6064 mFocusedWindow->consumeMotionCancel();
6065 } else {
6066 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction());
6067 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006068 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006069 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
6070 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006071
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006072 mUnfocusedWindow->assertNoEvents();
6073 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006074 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006075}
6076
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05006077/**
6078 * If we have no focused window, and a key comes in, we start the ANR timer.
6079 * The focused application should add a focused window before the timer runs out to prevent ANR.
6080 *
6081 * If the user touches another application during this time, the key should be dropped.
6082 * Next, if a new focused window comes in, without toggling the focused application,
6083 * then no ANR should occur.
6084 *
6085 * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication',
6086 * but in some cases the policy may not update the focused application.
6087 */
6088TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) {
6089 std::shared_ptr<FakeApplicationHandle> focusedApplication =
6090 std::make_shared<FakeApplicationHandle>();
6091 focusedApplication->setDispatchingTimeout(60ms);
6092 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication);
6093 // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused.
6094 mFocusedWindow->setFocusable(false);
6095
6096 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
6097 mFocusedWindow->consumeFocusEvent(false);
6098
6099 // Send a key. The ANR timer should start because there is no focused window.
6100 // 'focusedApplication' will get blamed if this timer completes.
6101 // Key will not be sent anywhere because we have no focused window. It will remain pending.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006102 InputEventInjectionResult result =
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05006103 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08006104 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/,
6105 false /* allowKeyRepeat */);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006106 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05006107
6108 // Wait until dispatcher starts the "no focused window" timer. If we don't wait here,
6109 // then the injected touches won't cause the focused event to get dropped.
6110 // The dispatcher only checks for whether the queue should be pruned upon queueing.
6111 // If we inject the touch right away and the ANR timer hasn't started, the touch event would
6112 // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'.
6113 // For this test, it means that the key would get delivered to the window once it becomes
6114 // focused.
6115 std::this_thread::sleep_for(10ms);
6116
6117 // Touch unfocused window. This should force the pending key to get dropped.
6118 NotifyMotionArgs motionArgs =
6119 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6120 ADISPLAY_ID_DEFAULT, {UNFOCUSED_WINDOW_LOCATION});
6121 mDispatcher->notifyMotion(&motionArgs);
6122
6123 // We do not consume the motion right away, because that would require dispatcher to first
6124 // process (== drop) the key event, and by that time, ANR will be raised.
6125 // Set the focused window first.
6126 mFocusedWindow->setFocusable(true);
6127 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
6128 setFocusedWindow(mFocusedWindow);
6129 mFocusedWindow->consumeFocusEvent(true);
6130 // We do not call "setFocusedApplication" here, even though the newly focused window belongs
6131 // to another application. This could be a bug / behaviour in the policy.
6132
6133 mUnfocusedWindow->consumeMotionDown();
6134
6135 ASSERT_TRUE(mDispatcher->waitForIdle());
6136 // Should not ANR because we actually have a focused window. It was just added too slowly.
6137 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled());
6138}
6139
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05006140// These tests ensure we cannot send touch events to a window that's positioned behind a window
6141// that has feature NO_INPUT_CHANNEL.
6142// Layout:
6143// Top (closest to user)
6144// mNoInputWindow (above all windows)
6145// mBottomWindow
6146// Bottom (furthest from user)
6147class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest {
6148 virtual void SetUp() override {
6149 InputDispatcherTest::SetUp();
6150
6151 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006152 mNoInputWindow =
6153 sp<FakeWindowHandle>::make(mApplication, mDispatcher,
6154 "Window without input channel", ADISPLAY_ID_DEFAULT,
6155 std::make_optional<sp<IBinder>>(nullptr) /*token*/);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08006156 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05006157 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
6158 // It's perfectly valid for this window to not have an associated input channel
6159
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006160 mBottomWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Bottom window",
6161 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05006162 mBottomWindow->setFrame(Rect(0, 0, 100, 100));
6163
6164 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
6165 }
6166
6167protected:
6168 std::shared_ptr<FakeApplicationHandle> mApplication;
6169 sp<FakeWindowHandle> mNoInputWindow;
6170 sp<FakeWindowHandle> mBottomWindow;
6171};
6172
6173TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) {
6174 PointF touchedPoint = {10, 10};
6175
6176 NotifyMotionArgs motionArgs =
6177 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6178 ADISPLAY_ID_DEFAULT, {touchedPoint});
6179 mDispatcher->notifyMotion(&motionArgs);
6180
6181 mNoInputWindow->assertNoEvents();
6182 // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have
6183 // an input channel, it is not marked as FLAG_NOT_TOUCHABLE,
6184 // and therefore should prevent mBottomWindow from receiving touches
6185 mBottomWindow->assertNoEvents();
6186}
6187
6188/**
6189 * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel,
6190 * ensure that this window does not receive any touches, and blocks touches to windows underneath.
6191 */
6192TEST_F(InputDispatcherMultiWindowOcclusionTests,
6193 NoInputChannelFeature_DropsTouchesWithValidChannel) {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006194 mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher,
6195 "Window with input channel and NO_INPUT_CHANNEL",
6196 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05006197
Prabir Pradhan51e7db02022-02-07 06:02:57 -08006198 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05006199 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
6200 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
6201
6202 PointF touchedPoint = {10, 10};
6203
6204 NotifyMotionArgs motionArgs =
6205 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6206 ADISPLAY_ID_DEFAULT, {touchedPoint});
6207 mDispatcher->notifyMotion(&motionArgs);
6208
6209 mNoInputWindow->assertNoEvents();
6210 mBottomWindow->assertNoEvents();
6211}
6212
Vishnu Nair958da932020-08-21 17:12:37 -07006213class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest {
6214protected:
6215 std::shared_ptr<FakeApplicationHandle> mApp;
6216 sp<FakeWindowHandle> mWindow;
6217 sp<FakeWindowHandle> mMirror;
6218
6219 virtual void SetUp() override {
6220 InputDispatcherTest::SetUp();
6221 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006222 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
6223 mMirror = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindowMirror",
6224 ADISPLAY_ID_DEFAULT, mWindow->getToken());
Vishnu Nair958da932020-08-21 17:12:37 -07006225 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
6226 mWindow->setFocusable(true);
6227 mMirror->setFocusable(true);
6228 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
6229 }
6230};
6231
6232TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) {
6233 // Request focus on a mirrored window
6234 setFocusedWindow(mMirror);
6235
6236 // window gets focused
6237 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006238 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
6239 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07006240 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
6241}
6242
6243// A focused & mirrored window remains focused only if the window and its mirror are both
6244// focusable.
6245TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) {
6246 setFocusedWindow(mMirror);
6247
6248 // window gets focused
6249 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006250 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
6251 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07006252 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006253 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
6254 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07006255 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
6256
6257 mMirror->setFocusable(false);
6258 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
6259
6260 // window loses focus since one of the windows associated with the token in not focusable
6261 mWindow->consumeFocusEvent(false);
6262
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006263 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
6264 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07006265 mWindow->assertNoEvents();
6266}
6267
6268// A focused & mirrored window remains focused until the window and its mirror both become
6269// invisible.
6270TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) {
6271 setFocusedWindow(mMirror);
6272
6273 // window gets focused
6274 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006275 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
6276 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07006277 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006278 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
6279 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07006280 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
6281
6282 mMirror->setVisible(false);
6283 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
6284
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006285 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
6286 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07006287 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006288 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
6289 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07006290 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
6291
6292 mWindow->setVisible(false);
6293 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
6294
6295 // window loses focus only after all windows associated with the token become invisible.
6296 mWindow->consumeFocusEvent(false);
6297
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006298 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
6299 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07006300 mWindow->assertNoEvents();
6301}
6302
6303// A focused & mirrored window remains focused until both windows are removed.
6304TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) {
6305 setFocusedWindow(mMirror);
6306
6307 // window gets focused
6308 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006309 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
6310 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07006311 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006312 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
6313 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07006314 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
6315
6316 // single window is removed but the window token remains focused
6317 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}});
6318
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006319 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
6320 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07006321 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006322 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
6323 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07006324 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
6325
6326 // Both windows are removed
6327 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
6328 mWindow->consumeFocusEvent(false);
6329
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006330 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
6331 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07006332 mWindow->assertNoEvents();
6333}
6334
6335// Focus request can be pending until one window becomes visible.
6336TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) {
6337 // Request focus on an invisible mirror.
6338 mWindow->setVisible(false);
6339 mMirror->setVisible(false);
6340 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
6341 setFocusedWindow(mMirror);
6342
6343 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006344 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Vishnu Nair958da932020-08-21 17:12:37 -07006345 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006346 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07006347
6348 mMirror->setVisible(true);
6349 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
6350
6351 // window gets focused
6352 mWindow->consumeFocusEvent(true);
6353 // window gets the pending key event
6354 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
6355}
Prabir Pradhan99987712020-11-10 18:43:05 -08006356
6357class InputDispatcherPointerCaptureTests : public InputDispatcherTest {
6358protected:
6359 std::shared_ptr<FakeApplicationHandle> mApp;
6360 sp<FakeWindowHandle> mWindow;
6361 sp<FakeWindowHandle> mSecondWindow;
6362
6363 void SetUp() override {
6364 InputDispatcherTest::SetUp();
6365 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006366 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -08006367 mWindow->setFocusable(true);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006368 mSecondWindow =
6369 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -08006370 mSecondWindow->setFocusable(true);
6371
6372 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
6373 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}});
6374
6375 setFocusedWindow(mWindow);
6376 mWindow->consumeFocusEvent(true);
6377 }
6378
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006379 void notifyPointerCaptureChanged(const PointerCaptureRequest& request) {
6380 const NotifyPointerCaptureChangedArgs args = generatePointerCaptureChangedArgs(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08006381 mDispatcher->notifyPointerCaptureChanged(&args);
6382 }
6383
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006384 PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window,
6385 bool enabled) {
Prabir Pradhan99987712020-11-10 18:43:05 -08006386 mDispatcher->requestPointerCapture(window->getToken(), enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006387 auto request = mFakePolicy->assertSetPointerCaptureCalled(enabled);
6388 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08006389 window->consumeCaptureEvent(enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006390 return request;
Prabir Pradhan99987712020-11-10 18:43:05 -08006391 }
6392};
6393
6394TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) {
6395 // Ensure that capture cannot be obtained for unfocused windows.
6396 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
6397 mFakePolicy->assertSetPointerCaptureNotCalled();
6398 mSecondWindow->assertNoEvents();
6399
6400 // Ensure that capture can be enabled from the focus window.
6401 requestAndVerifyPointerCapture(mWindow, true);
6402
6403 // Ensure that capture cannot be disabled from a window that does not have capture.
6404 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false);
6405 mFakePolicy->assertSetPointerCaptureNotCalled();
6406
6407 // Ensure that capture can be disabled from the window with capture.
6408 requestAndVerifyPointerCapture(mWindow, false);
6409}
6410
6411TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006412 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08006413
6414 setFocusedWindow(mSecondWindow);
6415
6416 // Ensure that the capture disabled event was sent first.
6417 mWindow->consumeCaptureEvent(false);
6418 mWindow->consumeFocusEvent(false);
6419 mSecondWindow->consumeFocusEvent(true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006420 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08006421
6422 // Ensure that additional state changes from InputReader are not sent to the window.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006423 notifyPointerCaptureChanged({});
6424 notifyPointerCaptureChanged(request);
6425 notifyPointerCaptureChanged({});
Prabir Pradhan99987712020-11-10 18:43:05 -08006426 mWindow->assertNoEvents();
6427 mSecondWindow->assertNoEvents();
6428 mFakePolicy->assertSetPointerCaptureNotCalled();
6429}
6430
6431TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006432 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08006433
6434 // InputReader unexpectedly disables and enables pointer capture.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006435 notifyPointerCaptureChanged({});
6436 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08006437
6438 // Ensure that Pointer Capture is disabled.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006439 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08006440 mWindow->consumeCaptureEvent(false);
6441 mWindow->assertNoEvents();
6442}
6443
Prabir Pradhan167e6d92021-02-04 16:18:17 -08006444TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) {
6445 requestAndVerifyPointerCapture(mWindow, true);
6446
6447 // The first window loses focus.
6448 setFocusedWindow(mSecondWindow);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006449 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08006450 mWindow->consumeCaptureEvent(false);
6451
6452 // Request Pointer Capture from the second window before the notification from InputReader
6453 // arrives.
6454 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006455 auto request = mFakePolicy->assertSetPointerCaptureCalled(true);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08006456
6457 // InputReader notifies Pointer Capture was disabled (because of the focus change).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006458 notifyPointerCaptureChanged({});
Prabir Pradhan167e6d92021-02-04 16:18:17 -08006459
6460 // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006461 notifyPointerCaptureChanged(request);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08006462
6463 mSecondWindow->consumeFocusEvent(true);
6464 mSecondWindow->consumeCaptureEvent(true);
6465}
6466
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006467TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) {
6468 // App repeatedly enables and disables capture.
6469 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
6470 auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
6471 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
6472 mFakePolicy->assertSetPointerCaptureCalled(false);
6473 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
6474 auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
6475
6476 // InputReader notifies that PointerCapture has been enabled for the first request. Since the
6477 // first request is now stale, this should do nothing.
6478 notifyPointerCaptureChanged(firstRequest);
6479 mWindow->assertNoEvents();
6480
6481 // InputReader notifies that the second request was enabled.
6482 notifyPointerCaptureChanged(secondRequest);
6483 mWindow->consumeCaptureEvent(true);
6484}
6485
Prabir Pradhan7092e262022-05-03 16:51:09 +00006486TEST_F(InputDispatcherPointerCaptureTests, RapidToggleRequests) {
6487 requestAndVerifyPointerCapture(mWindow, true);
6488
6489 // App toggles pointer capture off and on.
6490 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
6491 mFakePolicy->assertSetPointerCaptureCalled(false);
6492
6493 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
6494 auto enableRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
6495
6496 // InputReader notifies that the latest "enable" request was processed, while skipping over the
6497 // preceding "disable" request.
6498 notifyPointerCaptureChanged(enableRequest);
6499
6500 // Since pointer capture was never disabled during the rapid toggle, the window does not receive
6501 // any notifications.
6502 mWindow->assertNoEvents();
6503}
6504
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006505class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest {
6506protected:
6507 constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8;
Bernardo Rufino7393d172021-02-26 13:56:11 +00006508
6509 constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9;
6510 static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY);
6511
6512 constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7;
6513 static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
6514
6515 // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold
6516 constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5;
6517 static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
6518 static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) <
6519 MAXIMUM_OBSCURING_OPACITY);
6520
Bernardo Rufino6d52e542021-02-15 18:38:10 +00006521 static const int32_t TOUCHED_APP_UID = 10001;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006522 static const int32_t APP_B_UID = 10002;
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006523 static const int32_t APP_C_UID = 10003;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006524
6525 sp<FakeWindowHandle> mTouchWindow;
6526
6527 virtual void SetUp() override {
6528 InputDispatcherTest::SetUp();
Bernardo Rufino6d52e542021-02-15 18:38:10 +00006529 mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched");
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006530 mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY);
6531 }
6532
6533 virtual void TearDown() override {
6534 InputDispatcherTest::TearDown();
6535 mTouchWindow.clear();
6536 }
6537
chaviw3277faf2021-05-19 16:45:23 -05006538 sp<FakeWindowHandle> getOccludingWindow(int32_t uid, std::string name, TouchOcclusionMode mode,
6539 float alpha = 1.0f) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006540 sp<FakeWindowHandle> window = getWindow(uid, name);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08006541 window->setTouchable(false);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006542 window->setTouchOcclusionMode(mode);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006543 window->setAlpha(alpha);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006544 return window;
6545 }
6546
6547 sp<FakeWindowHandle> getWindow(int32_t uid, std::string name) {
6548 std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>();
6549 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006550 sp<FakeWindowHandle>::make(app, mDispatcher, name, ADISPLAY_ID_DEFAULT);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006551 // Generate an arbitrary PID based on the UID
6552 window->setOwnerInfo(1777 + (uid % 10000), uid);
6553 return window;
6554 }
6555
6556 void touch(const std::vector<PointF>& points = {PointF{100, 200}}) {
6557 NotifyMotionArgs args =
6558 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6559 ADISPLAY_ID_DEFAULT, points);
6560 mDispatcher->notifyMotion(&args);
6561 }
6562};
6563
6564TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006565 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006566 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006567 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006568
6569 touch();
6570
6571 mTouchWindow->assertNoEvents();
6572}
6573
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00006574TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufino7393d172021-02-26 13:56:11 +00006575 WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) {
6576 const sp<FakeWindowHandle>& w =
6577 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f);
6578 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6579
6580 touch();
6581
6582 mTouchWindow->assertNoEvents();
6583}
6584
6585TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00006586 WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) {
6587 const sp<FakeWindowHandle>& w =
6588 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
6589 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6590
6591 touch();
6592
6593 w->assertNoEvents();
6594}
6595
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006596TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006597 const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW);
6598 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006599
6600 touch();
6601
6602 mTouchWindow->consumeAnyMotionDown();
6603}
6604
6605TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006606 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006607 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006608 w->setFrame(Rect(0, 0, 50, 50));
6609 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006610
6611 touch({PointF{100, 100}});
6612
6613 mTouchWindow->consumeAnyMotionDown();
6614}
6615
6616TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006617 const sp<FakeWindowHandle>& w =
Bernardo Rufino6d52e542021-02-15 18:38:10 +00006618 getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006619 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6620
6621 touch();
6622
6623 mTouchWindow->consumeAnyMotionDown();
6624}
6625
6626TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) {
6627 const sp<FakeWindowHandle>& w =
6628 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
6629 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006630
6631 touch();
6632
6633 mTouchWindow->consumeAnyMotionDown();
6634}
6635
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00006636TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) {
6637 const sp<FakeWindowHandle>& w =
6638 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
6639 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6640
6641 touch();
6642
6643 w->assertNoEvents();
6644}
6645
6646/**
6647 * This is important to make sure apps can't indirectly learn the position of touches (outside vs
6648 * inside) while letting them pass-through. Note that even though touch passes through the occluding
6649 * window, the occluding window will still receive ACTION_OUTSIDE event.
6650 */
6651TEST_F(InputDispatcherUntrustedTouchesTest,
6652 WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) {
6653 const sp<FakeWindowHandle>& w =
6654 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08006655 w->setWatchOutsideTouch(true);
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00006656 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6657
6658 touch();
6659
6660 w->consumeMotionOutside();
6661}
6662
6663TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) {
6664 const sp<FakeWindowHandle>& w =
6665 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08006666 w->setWatchOutsideTouch(true);
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00006667 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6668
6669 touch();
6670
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08006671 w->consumeMotionOutsideWithZeroedCoords();
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00006672}
6673
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006674TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006675 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006676 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
6677 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006678 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6679
6680 touch();
6681
6682 mTouchWindow->consumeAnyMotionDown();
6683}
6684
6685TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) {
6686 const sp<FakeWindowHandle>& w =
6687 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
6688 MAXIMUM_OBSCURING_OPACITY);
6689 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006690
6691 touch();
6692
6693 mTouchWindow->consumeAnyMotionDown();
6694}
6695
6696TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006697 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006698 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
6699 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006700 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6701
6702 touch();
6703
6704 mTouchWindow->assertNoEvents();
6705}
6706
6707TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) {
6708 // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91
6709 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006710 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
6711 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006712 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006713 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
6714 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006715 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
6716
6717 touch();
6718
6719 mTouchWindow->assertNoEvents();
6720}
6721
6722TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) {
6723 // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75
6724 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006725 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
6726 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006727 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006728 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
6729 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006730 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
6731
6732 touch();
6733
6734 mTouchWindow->consumeAnyMotionDown();
6735}
6736
6737TEST_F(InputDispatcherUntrustedTouchesTest,
6738 WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) {
6739 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006740 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
6741 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006742 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006743 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
6744 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006745 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
6746
6747 touch();
6748
6749 mTouchWindow->consumeAnyMotionDown();
6750}
6751
6752TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) {
6753 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006754 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
6755 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006756 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006757 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
6758 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006759 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006760
6761 touch();
6762
6763 mTouchWindow->assertNoEvents();
6764}
6765
Bernardo Rufino6d52e542021-02-15 18:38:10 +00006766TEST_F(InputDispatcherUntrustedTouchesTest,
6767 WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) {
6768 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006769 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
6770 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00006771 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006772 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
6773 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00006774 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
6775
6776 touch();
6777
6778 mTouchWindow->assertNoEvents();
6779}
6780
6781TEST_F(InputDispatcherUntrustedTouchesTest,
6782 WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) {
6783 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006784 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
6785 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00006786 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006787 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
6788 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00006789 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
6790
6791 touch();
6792
6793 mTouchWindow->consumeAnyMotionDown();
6794}
6795
6796TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) {
6797 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006798 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
6799 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00006800 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6801
6802 touch();
6803
6804 mTouchWindow->consumeAnyMotionDown();
6805}
6806
6807TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) {
6808 const sp<FakeWindowHandle>& w =
6809 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED);
6810 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6811
6812 touch();
6813
6814 mTouchWindow->consumeAnyMotionDown();
6815}
6816
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00006817TEST_F(InputDispatcherUntrustedTouchesTest,
6818 OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) {
6819 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
6820 const sp<FakeWindowHandle>& w =
6821 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f);
6822 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6823
6824 touch();
6825
6826 mTouchWindow->assertNoEvents();
6827}
6828
6829TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) {
6830 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
6831 const sp<FakeWindowHandle>& w =
6832 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f);
6833 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6834
6835 touch();
6836
6837 mTouchWindow->consumeAnyMotionDown();
6838}
6839
6840TEST_F(InputDispatcherUntrustedTouchesTest,
6841 OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) {
6842 mDispatcher->setMaximumObscuringOpacityForTouch(1.0f);
6843 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006844 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
6845 OPACITY_ABOVE_THRESHOLD);
6846 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6847
6848 touch();
6849
6850 mTouchWindow->consumeAnyMotionDown();
6851}
6852
6853TEST_F(InputDispatcherUntrustedTouchesTest,
6854 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) {
6855 const sp<FakeWindowHandle>& w1 =
6856 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
6857 OPACITY_BELOW_THRESHOLD);
6858 const sp<FakeWindowHandle>& w2 =
6859 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
6860 OPACITY_BELOW_THRESHOLD);
6861 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
6862
6863 touch();
6864
6865 mTouchWindow->assertNoEvents();
6866}
6867
6868/**
6869 * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the
6870 * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold
6871 * (which alone would result in allowing touches) does not affect the blocking behavior.
6872 */
6873TEST_F(InputDispatcherUntrustedTouchesTest,
6874 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) {
6875 const sp<FakeWindowHandle>& wB =
6876 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
6877 OPACITY_BELOW_THRESHOLD);
6878 const sp<FakeWindowHandle>& wC =
6879 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
6880 OPACITY_BELOW_THRESHOLD);
6881 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
6882
6883 touch();
6884
6885 mTouchWindow->assertNoEvents();
6886}
6887
6888/**
6889 * This test is testing that a window from a different UID but with same application token doesn't
6890 * block the touch. Apps can share the application token for close UI collaboration for example.
6891 */
6892TEST_F(InputDispatcherUntrustedTouchesTest,
6893 WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) {
6894 const sp<FakeWindowHandle>& w =
6895 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
6896 w->setApplicationToken(mTouchWindow->getApplicationToken());
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00006897 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6898
6899 touch();
6900
6901 mTouchWindow->consumeAnyMotionDown();
6902}
6903
arthurhungb89ccb02020-12-30 16:19:01 +08006904class InputDispatcherDragTests : public InputDispatcherTest {
6905protected:
6906 std::shared_ptr<FakeApplicationHandle> mApp;
6907 sp<FakeWindowHandle> mWindow;
6908 sp<FakeWindowHandle> mSecondWindow;
6909 sp<FakeWindowHandle> mDragWindow;
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00006910 sp<FakeWindowHandle> mSpyWindow;
Arthur Hungb75c2aa2022-07-15 09:35:36 +00006911 // Mouse would force no-split, set the id as non-zero to verify if drag state could track it.
6912 static constexpr int32_t MOUSE_POINTER_ID = 1;
arthurhungb89ccb02020-12-30 16:19:01 +08006913
6914 void SetUp() override {
6915 InputDispatcherTest::SetUp();
6916 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006917 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +08006918 mWindow->setFrame(Rect(0, 0, 100, 100));
arthurhungb89ccb02020-12-30 16:19:01 +08006919
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006920 mSecondWindow =
6921 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +08006922 mSecondWindow->setFrame(Rect(100, 0, 200, 100));
arthurhungb89ccb02020-12-30 16:19:01 +08006923
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006924 mSpyWindow =
6925 sp<FakeWindowHandle>::make(mApp, mDispatcher, "SpyWindow", ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00006926 mSpyWindow->setSpy(true);
6927 mSpyWindow->setTrustedOverlay(true);
6928 mSpyWindow->setFrame(Rect(0, 0, 200, 100));
6929
arthurhungb89ccb02020-12-30 16:19:01 +08006930 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00006931 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mSpyWindow, mWindow, mSecondWindow}}});
arthurhungb89ccb02020-12-30 16:19:01 +08006932 }
6933
Arthur Hungb75c2aa2022-07-15 09:35:36 +00006934 void injectDown(int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
6935 switch (fromSource) {
6936 case AINPUT_SOURCE_TOUCHSCREEN:
6937 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6938 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
6939 ADISPLAY_ID_DEFAULT, {50, 50}))
6940 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6941 break;
6942 case AINPUT_SOURCE_STYLUS:
6943 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6944 injectMotionEvent(
6945 mDispatcher,
6946 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
6947 AINPUT_SOURCE_STYLUS)
6948 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
6949 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS)
6950 .x(50)
6951 .y(50))
6952 .build()));
6953 break;
6954 case AINPUT_SOURCE_MOUSE:
6955 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6956 injectMotionEvent(
6957 mDispatcher,
6958 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
6959 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
6960 .pointer(PointerBuilder(MOUSE_POINTER_ID,
6961 AMOTION_EVENT_TOOL_TYPE_MOUSE)
6962 .x(50)
6963 .y(50))
6964 .build()));
6965 break;
6966 default:
6967 FAIL() << "Source " << fromSource << " doesn't support drag and drop";
6968 }
arthurhungb89ccb02020-12-30 16:19:01 +08006969
6970 // Window should receive motion event.
6971 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00006972 // Spy window should also receive motion event
6973 mSpyWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung54745652022-04-20 07:17:41 +00006974 }
6975
6976 // Start performing drag, we will create a drag window and transfer touch to it.
6977 // @param sendDown : if true, send a motion down on first window before perform drag and drop.
6978 // Returns true on success.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00006979 bool startDrag(bool sendDown = true, int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
Arthur Hung54745652022-04-20 07:17:41 +00006980 if (sendDown) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00006981 injectDown(fromSource);
Arthur Hung54745652022-04-20 07:17:41 +00006982 }
arthurhungb89ccb02020-12-30 16:19:01 +08006983
6984 // The drag window covers the entire display
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006985 mDragWindow =
6986 sp<FakeWindowHandle>::make(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00006987 mDragWindow->setTouchableRegion(Region{{0, 0, 0, 0}});
arthurhungb89ccb02020-12-30 16:19:01 +08006988 mDispatcher->setInputWindows(
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00006989 {{ADISPLAY_ID_DEFAULT, {mDragWindow, mSpyWindow, mWindow, mSecondWindow}}});
arthurhungb89ccb02020-12-30 16:19:01 +08006990
6991 // Transfer touch focus to the drag window
Arthur Hung54745652022-04-20 07:17:41 +00006992 bool transferred =
6993 mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(),
6994 true /* isDragDrop */);
6995 if (transferred) {
6996 mWindow->consumeMotionCancel();
6997 mDragWindow->consumeMotionDown();
6998 }
6999 return transferred;
arthurhungb89ccb02020-12-30 16:19:01 +08007000 }
7001};
7002
7003TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007004 startDrag();
arthurhungb89ccb02020-12-30 16:19:01 +08007005
7006 // Move on window.
7007 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7008 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
7009 ADISPLAY_ID_DEFAULT, {50, 50}))
7010 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7011 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7012 mWindow->consumeDragEvent(false, 50, 50);
7013 mSecondWindow->assertNoEvents();
7014
7015 // Move to another window.
7016 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7017 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
7018 ADISPLAY_ID_DEFAULT, {150, 50}))
7019 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7020 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7021 mWindow->consumeDragEvent(true, 150, 50);
7022 mSecondWindow->consumeDragEvent(false, 50, 50);
7023
7024 // Move back to original window.
7025 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7026 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
7027 ADISPLAY_ID_DEFAULT, {50, 50}))
7028 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7029 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7030 mWindow->consumeDragEvent(false, 50, 50);
7031 mSecondWindow->consumeDragEvent(true, -50, 50);
7032
7033 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7034 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {50, 50}))
7035 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7036 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
7037 mWindow->assertNoEvents();
7038 mSecondWindow->assertNoEvents();
7039}
7040
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00007041TEST_F(InputDispatcherDragTests, DragEnterAndPointerDownPilfersPointers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007042 startDrag();
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00007043
7044 // No cancel event after drag start
7045 mSpyWindow->assertNoEvents();
7046
7047 const MotionEvent secondFingerDownEvent =
7048 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7049 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
7050 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
7051 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(60).y(60))
7052 .build();
7053 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7054 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
7055 InputEventInjectionSync::WAIT_FOR_RESULT))
7056 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7057
7058 // Receives cancel for first pointer after next pointer down
7059 mSpyWindow->consumeMotionCancel();
7060 mSpyWindow->consumeMotionDown();
7061
7062 mSpyWindow->assertNoEvents();
7063}
7064
arthurhungf452d0b2021-01-06 00:19:52 +08007065TEST_F(InputDispatcherDragTests, DragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007066 startDrag();
arthurhungf452d0b2021-01-06 00:19:52 +08007067
7068 // Move on window.
7069 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7070 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
7071 ADISPLAY_ID_DEFAULT, {50, 50}))
7072 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7073 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7074 mWindow->consumeDragEvent(false, 50, 50);
7075 mSecondWindow->assertNoEvents();
7076
7077 // Move to another window.
7078 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7079 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
7080 ADISPLAY_ID_DEFAULT, {150, 50}))
7081 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7082 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7083 mWindow->consumeDragEvent(true, 150, 50);
7084 mSecondWindow->consumeDragEvent(false, 50, 50);
7085
7086 // drop to another window.
7087 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7088 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7089 {150, 50}))
7090 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7091 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
7092 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
7093 mWindow->assertNoEvents();
7094 mSecondWindow->assertNoEvents();
7095}
7096
arthurhung6d4bed92021-03-17 11:59:33 +08007097TEST_F(InputDispatcherDragTests, StylusDragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007098 startDrag(true, AINPUT_SOURCE_STYLUS);
arthurhung6d4bed92021-03-17 11:59:33 +08007099
7100 // Move on window and keep button pressed.
7101 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7102 injectMotionEvent(mDispatcher,
7103 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
7104 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
7105 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS)
7106 .x(50)
7107 .y(50))
7108 .build()))
7109 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7110 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7111 mWindow->consumeDragEvent(false, 50, 50);
7112 mSecondWindow->assertNoEvents();
7113
7114 // Move to another window and release button, expect to drop item.
7115 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7116 injectMotionEvent(mDispatcher,
7117 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
7118 .buttonState(0)
7119 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS)
7120 .x(150)
7121 .y(50))
7122 .build()))
7123 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7124 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7125 mWindow->assertNoEvents();
7126 mSecondWindow->assertNoEvents();
7127 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
7128
7129 // nothing to the window.
7130 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7131 injectMotionEvent(mDispatcher,
7132 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS)
7133 .buttonState(0)
7134 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS)
7135 .x(150)
7136 .y(50))
7137 .build()))
7138 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7139 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
7140 mWindow->assertNoEvents();
7141 mSecondWindow->assertNoEvents();
7142}
7143
Arthur Hung54745652022-04-20 07:17:41 +00007144TEST_F(InputDispatcherDragTests, DragAndDropOnInvalidWindow) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007145 startDrag();
Arthur Hung6d0571e2021-04-09 20:18:16 +08007146
7147 // Set second window invisible.
7148 mSecondWindow->setVisible(false);
7149 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}});
7150
7151 // Move on window.
7152 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7153 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
7154 ADISPLAY_ID_DEFAULT, {50, 50}))
7155 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7156 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7157 mWindow->consumeDragEvent(false, 50, 50);
7158 mSecondWindow->assertNoEvents();
7159
7160 // Move to another window.
7161 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7162 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
7163 ADISPLAY_ID_DEFAULT, {150, 50}))
7164 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7165 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7166 mWindow->consumeDragEvent(true, 150, 50);
7167 mSecondWindow->assertNoEvents();
7168
7169 // drop to another window.
7170 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7171 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7172 {150, 50}))
7173 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7174 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
7175 mFakePolicy->assertDropTargetEquals(nullptr);
7176 mWindow->assertNoEvents();
7177 mSecondWindow->assertNoEvents();
7178}
7179
Arthur Hung54745652022-04-20 07:17:41 +00007180TEST_F(InputDispatcherDragTests, NoDragAndDropWhenMultiFingers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007181 // Ensure window could track pointerIds if it didn't support split touch.
7182 mWindow->setPreventSplitting(true);
7183
Arthur Hung54745652022-04-20 07:17:41 +00007184 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7185 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7186 {50, 50}))
7187 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7188 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7189
7190 const MotionEvent secondFingerDownEvent =
7191 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7192 .displayId(ADISPLAY_ID_DEFAULT)
7193 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
7194 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
7195 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(75).y(50))
7196 .build();
7197 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7198 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
7199 InputEventInjectionSync::WAIT_FOR_RESULT))
7200 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7201 mWindow->consumeMotionPointerDown(1 /* pointerIndex */);
7202
7203 // Should not perform drag and drop when window has multi fingers.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007204 ASSERT_FALSE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +00007205}
7206
7207TEST_F(InputDispatcherDragTests, DragAndDropWhenSplitTouch) {
7208 // First down on second window.
7209 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7210 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7211 {150, 50}))
7212 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7213
7214 mSecondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7215
7216 // Second down on first window.
7217 const MotionEvent secondFingerDownEvent =
7218 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7219 .displayId(ADISPLAY_ID_DEFAULT)
7220 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
7221 .pointer(
7222 PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50))
7223 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
7224 .build();
7225 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7226 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
7227 InputEventInjectionSync::WAIT_FOR_RESULT))
7228 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7229 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7230
7231 // Perform drag and drop from first window.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007232 ASSERT_TRUE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +00007233
7234 // Move on window.
7235 const MotionEvent secondFingerMoveEvent =
7236 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
7237 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
7238 .pointer(
7239 PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50))
7240 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
7241 .build();
7242 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7243 injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT,
7244 InputEventInjectionSync::WAIT_FOR_RESULT));
7245 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7246 mWindow->consumeDragEvent(false, 50, 50);
7247 mSecondWindow->consumeMotionMove();
7248
7249 // Release the drag pointer should perform drop.
7250 const MotionEvent secondFingerUpEvent =
7251 MotionEventBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
7252 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
7253 .pointer(
7254 PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50))
7255 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
7256 .build();
7257 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7258 injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT,
7259 InputEventInjectionSync::WAIT_FOR_RESULT));
7260 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
7261 mFakePolicy->assertDropTargetEquals(mWindow->getToken());
7262 mWindow->assertNoEvents();
7263 mSecondWindow->consumeMotionMove();
7264}
7265
Arthur Hung3915c1f2022-05-31 07:17:17 +00007266TEST_F(InputDispatcherDragTests, DragAndDropWhenMultiDisplays) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007267 startDrag();
Arthur Hung3915c1f2022-05-31 07:17:17 +00007268
7269 // Update window of second display.
7270 sp<FakeWindowHandle> windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007271 sp<FakeWindowHandle>::make(mApp, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung3915c1f2022-05-31 07:17:17 +00007272 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
7273
7274 // Let second display has a touch state.
7275 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7276 injectMotionEvent(mDispatcher,
7277 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
7278 AINPUT_SOURCE_TOUCHSCREEN)
7279 .displayId(SECOND_DISPLAY_ID)
7280 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER)
7281 .x(100)
7282 .y(100))
7283 .build()));
7284 windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN,
7285 SECOND_DISPLAY_ID, 0 /* expectedFlag */);
7286 // Update window again.
7287 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
7288
7289 // Move on window.
7290 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7291 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
7292 ADISPLAY_ID_DEFAULT, {50, 50}))
7293 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7294 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7295 mWindow->consumeDragEvent(false, 50, 50);
7296 mSecondWindow->assertNoEvents();
7297
7298 // Move to another window.
7299 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7300 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
7301 ADISPLAY_ID_DEFAULT, {150, 50}))
7302 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7303 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7304 mWindow->consumeDragEvent(true, 150, 50);
7305 mSecondWindow->consumeDragEvent(false, 50, 50);
7306
7307 // drop to another window.
7308 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7309 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7310 {150, 50}))
7311 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7312 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
7313 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
7314 mWindow->assertNoEvents();
7315 mSecondWindow->assertNoEvents();
7316}
7317
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007318TEST_F(InputDispatcherDragTests, MouseDragAndDrop) {
7319 startDrag(true, AINPUT_SOURCE_MOUSE);
7320 // Move on window.
7321 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7322 injectMotionEvent(mDispatcher,
7323 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
7324 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
7325 .pointer(PointerBuilder(MOUSE_POINTER_ID,
7326 AMOTION_EVENT_TOOL_TYPE_MOUSE)
7327 .x(50)
7328 .y(50))
7329 .build()))
7330 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7331 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7332 mWindow->consumeDragEvent(false, 50, 50);
7333 mSecondWindow->assertNoEvents();
7334
7335 // Move to another window.
7336 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7337 injectMotionEvent(mDispatcher,
7338 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
7339 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
7340 .pointer(PointerBuilder(MOUSE_POINTER_ID,
7341 AMOTION_EVENT_TOOL_TYPE_MOUSE)
7342 .x(150)
7343 .y(50))
7344 .build()))
7345 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7346 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7347 mWindow->consumeDragEvent(true, 150, 50);
7348 mSecondWindow->consumeDragEvent(false, 50, 50);
7349
7350 // drop to another window.
7351 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7352 injectMotionEvent(mDispatcher,
7353 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
7354 .buttonState(0)
7355 .pointer(PointerBuilder(MOUSE_POINTER_ID,
7356 AMOTION_EVENT_TOOL_TYPE_MOUSE)
7357 .x(150)
7358 .y(50))
7359 .build()))
7360 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7361 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
7362 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
7363 mWindow->assertNoEvents();
7364 mSecondWindow->assertNoEvents();
7365}
7366
Vishnu Nair062a8672021-09-03 16:07:44 -07007367class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {};
7368
7369TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) {
7370 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007371 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
7372 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007373 window->setDropInput(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07007374 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
7375 window->setFocusable(true);
7376 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
7377 setFocusedWindow(window);
7378 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
7379
7380 // With the flag set, window should not get any input
7381 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
7382 mDispatcher->notifyKey(&keyArgs);
7383 window->assertNoEvents();
7384
7385 NotifyMotionArgs motionArgs =
7386 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
7387 ADISPLAY_ID_DEFAULT);
7388 mDispatcher->notifyMotion(&motionArgs);
7389 window->assertNoEvents();
7390
7391 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007392 window->setDropInput(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07007393 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
7394
7395 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
7396 mDispatcher->notifyKey(&keyArgs);
7397 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
7398
7399 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
7400 ADISPLAY_ID_DEFAULT);
7401 mDispatcher->notifyMotion(&motionArgs);
7402 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7403 window->assertNoEvents();
7404}
7405
7406TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) {
7407 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
7408 std::make_shared<FakeApplicationHandle>();
7409 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007410 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
7411 ADISPLAY_ID_DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -07007412 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
7413 obscuringWindow->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007414 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07007415 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007416 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
7417 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007418 window->setDropInputIfObscured(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07007419 window->setOwnerInfo(222, 222);
7420 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
7421 window->setFocusable(true);
7422 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
7423 setFocusedWindow(window);
7424 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
7425
7426 // With the flag set, window should not get any input
7427 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
7428 mDispatcher->notifyKey(&keyArgs);
7429 window->assertNoEvents();
7430
7431 NotifyMotionArgs motionArgs =
7432 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
7433 ADISPLAY_ID_DEFAULT);
7434 mDispatcher->notifyMotion(&motionArgs);
7435 window->assertNoEvents();
7436
7437 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007438 window->setDropInputIfObscured(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07007439 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
7440
7441 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
7442 mDispatcher->notifyKey(&keyArgs);
7443 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
7444
7445 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
7446 ADISPLAY_ID_DEFAULT);
7447 mDispatcher->notifyMotion(&motionArgs);
7448 window->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
7449 window->assertNoEvents();
7450}
7451
7452TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) {
7453 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
7454 std::make_shared<FakeApplicationHandle>();
7455 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007456 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
7457 ADISPLAY_ID_DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -07007458 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
7459 obscuringWindow->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007460 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07007461 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007462 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
7463 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007464 window->setDropInputIfObscured(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07007465 window->setOwnerInfo(222, 222);
7466 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
7467 window->setFocusable(true);
7468 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
7469 setFocusedWindow(window);
7470 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
7471
7472 // With the flag set, window should not get any input
7473 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
7474 mDispatcher->notifyKey(&keyArgs);
7475 window->assertNoEvents();
7476
7477 NotifyMotionArgs motionArgs =
7478 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
7479 ADISPLAY_ID_DEFAULT);
7480 mDispatcher->notifyMotion(&motionArgs);
7481 window->assertNoEvents();
7482
7483 // When the window is no longer obscured because it went on top, it should get input
7484 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, obscuringWindow}}});
7485
7486 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
7487 mDispatcher->notifyKey(&keyArgs);
7488 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
7489
7490 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
7491 ADISPLAY_ID_DEFAULT);
7492 mDispatcher->notifyMotion(&motionArgs);
7493 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7494 window->assertNoEvents();
7495}
7496
Antonio Kantekf16f2832021-09-28 04:39:20 +00007497class InputDispatcherTouchModeChangedTests : public InputDispatcherTest {
7498protected:
7499 std::shared_ptr<FakeApplicationHandle> mApp;
Antonio Kantek15beb512022-06-13 22:35:41 +00007500 std::shared_ptr<FakeApplicationHandle> mSecondaryApp;
Antonio Kantekf16f2832021-09-28 04:39:20 +00007501 sp<FakeWindowHandle> mWindow;
7502 sp<FakeWindowHandle> mSecondWindow;
Antonio Kantek15beb512022-06-13 22:35:41 +00007503 sp<FakeWindowHandle> mThirdWindow;
Antonio Kantekf16f2832021-09-28 04:39:20 +00007504
7505 void SetUp() override {
7506 InputDispatcherTest::SetUp();
7507
7508 mApp = std::make_shared<FakeApplicationHandle>();
Antonio Kantek15beb512022-06-13 22:35:41 +00007509 mSecondaryApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007510 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00007511 mWindow->setFocusable(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00007512 setFocusedWindow(mWindow);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007513 mSecondWindow =
7514 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00007515 mSecondWindow->setFocusable(true);
Antonio Kantek15beb512022-06-13 22:35:41 +00007516 mThirdWindow =
7517 sp<FakeWindowHandle>::make(mSecondaryApp, mDispatcher,
7518 "TestWindow3_SecondaryDisplay", SECOND_DISPLAY_ID);
7519 mThirdWindow->setFocusable(true);
Antonio Kantekf16f2832021-09-28 04:39:20 +00007520
7521 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Antonio Kantek15beb512022-06-13 22:35:41 +00007522 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}},
7523 {SECOND_DISPLAY_ID, {mThirdWindow}}});
7524 mThirdWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
Antonio Kantekf16f2832021-09-28 04:39:20 +00007525 mWindow->consumeFocusEvent(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00007526
Antonio Kantek15beb512022-06-13 22:35:41 +00007527 // Set main display initial touch mode to InputDispatcher::kDefaultInTouchMode.
Prabir Pradhan5735a322022-04-11 17:23:34 +00007528 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, WINDOW_PID,
Antonio Kantek15beb512022-06-13 22:35:41 +00007529 WINDOW_UID, true /* hasPermission */,
7530 ADISPLAY_ID_DEFAULT)) {
Antonio Kantek48710e42022-03-24 14:19:30 -07007531 mWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
7532 mSecondWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +00007533 mThirdWindow->assertNoEvents();
7534 }
7535
7536 // Set secondary display initial touch mode to InputDispatcher::kDefaultInTouchMode.
7537 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, SECONDARY_WINDOW_PID,
7538 SECONDARY_WINDOW_UID, true /* hasPermission */,
7539 SECOND_DISPLAY_ID)) {
7540 mWindow->assertNoEvents();
7541 mSecondWindow->assertNoEvents();
7542 mThirdWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek48710e42022-03-24 14:19:30 -07007543 }
Antonio Kantekf16f2832021-09-28 04:39:20 +00007544 }
7545
Antonio Kantek15beb512022-06-13 22:35:41 +00007546 void changeAndVerifyTouchModeInMainDisplayOnly(bool inTouchMode, int32_t pid, int32_t uid,
7547 bool hasPermission) {
Antonio Kanteka042c022022-07-06 16:51:07 -07007548 ASSERT_TRUE(mDispatcher->setInTouchMode(inTouchMode, pid, uid, hasPermission,
7549 ADISPLAY_ID_DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +00007550 mWindow->consumeTouchModeEvent(inTouchMode);
7551 mSecondWindow->consumeTouchModeEvent(inTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +00007552 mThirdWindow->assertNoEvents();
Antonio Kantekf16f2832021-09-28 04:39:20 +00007553 }
7554};
7555
Antonio Kantek26defcf2022-02-08 01:12:27 +00007556TEST_F(InputDispatcherTouchModeChangedTests, FocusedWindowCanChangeTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -08007557 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek15beb512022-06-13 22:35:41 +00007558 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode,
7559 windowInfo.ownerPid, windowInfo.ownerUid,
7560 false /* hasPermission */);
Antonio Kantekf16f2832021-09-28 04:39:20 +00007561}
7562
Antonio Kantek26defcf2022-02-08 01:12:27 +00007563TEST_F(InputDispatcherTouchModeChangedTests, NonFocusedWindowOwnerCannotChangeTouchMode) {
7564 const WindowInfo& windowInfo = *mWindow->getInfo();
7565 int32_t ownerPid = windowInfo.ownerPid;
7566 int32_t ownerUid = windowInfo.ownerUid;
7567 mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1);
7568 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, ownerPid,
Antonio Kanteka042c022022-07-06 16:51:07 -07007569 ownerUid, false /*hasPermission*/,
7570 ADISPLAY_ID_DEFAULT));
Antonio Kantek26defcf2022-02-08 01:12:27 +00007571 mWindow->assertNoEvents();
7572 mSecondWindow->assertNoEvents();
7573}
7574
7575TEST_F(InputDispatcherTouchModeChangedTests, NonWindowOwnerMayChangeTouchModeOnPermissionGranted) {
7576 const WindowInfo& windowInfo = *mWindow->getInfo();
7577 int32_t ownerPid = windowInfo.ownerPid;
7578 int32_t ownerUid = windowInfo.ownerUid;
7579 mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1);
Antonio Kantek15beb512022-06-13 22:35:41 +00007580 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, ownerPid,
7581 ownerUid, true /*hasPermission*/);
Antonio Kantek26defcf2022-02-08 01:12:27 +00007582}
7583
Antonio Kantekf16f2832021-09-28 04:39:20 +00007584TEST_F(InputDispatcherTouchModeChangedTests, EventIsNotGeneratedIfNotChangingTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -08007585 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek26defcf2022-02-08 01:12:27 +00007586 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode,
7587 windowInfo.ownerPid, windowInfo.ownerUid,
Antonio Kanteka042c022022-07-06 16:51:07 -07007588 true /*hasPermission*/, ADISPLAY_ID_DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +00007589 mWindow->assertNoEvents();
7590 mSecondWindow->assertNoEvents();
7591}
7592
Antonio Kantek15beb512022-06-13 22:35:41 +00007593TEST_F(InputDispatcherTouchModeChangedTests, ChangeTouchOnSecondaryDisplayOnly) {
7594 const WindowInfo& windowInfo = *mThirdWindow->getInfo();
7595 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
7596 windowInfo.ownerPid, windowInfo.ownerUid,
7597 true /*hasPermission*/, SECOND_DISPLAY_ID));
7598 mWindow->assertNoEvents();
7599 mSecondWindow->assertNoEvents();
7600 mThirdWindow->consumeTouchModeEvent(!InputDispatcher::kDefaultInTouchMode);
7601}
7602
Antonio Kantek48710e42022-03-24 14:19:30 -07007603TEST_F(InputDispatcherTouchModeChangedTests, CanChangeTouchModeWhenOwningLastInteractedWindow) {
7604 // Interact with the window first.
7605 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
7606 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
7607 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
7608
7609 // Then remove focus.
7610 mWindow->setFocusable(false);
7611 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
7612
7613 // Assert that caller can switch touch mode by owning one of the last interacted window.
7614 const WindowInfo& windowInfo = *mWindow->getInfo();
7615 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
7616 windowInfo.ownerPid, windowInfo.ownerUid,
Antonio Kanteka042c022022-07-06 16:51:07 -07007617 false /*hasPermission*/, ADISPLAY_ID_DEFAULT));
Antonio Kantek48710e42022-03-24 14:19:30 -07007618}
7619
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007620class InputDispatcherSpyWindowTest : public InputDispatcherTest {
7621public:
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007622 sp<FakeWindowHandle> createSpy() {
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007623 std::shared_ptr<FakeApplicationHandle> application =
7624 std::make_shared<FakeApplicationHandle>();
7625 std::string name = "Fake Spy ";
7626 name += std::to_string(mSpyCount++);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007627 sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher,
7628 name.c_str(), ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007629 spy->setSpy(true);
Prabir Pradhan5c85e052021-12-22 02:27:12 -08007630 spy->setTrustedOverlay(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007631 return spy;
7632 }
7633
7634 sp<FakeWindowHandle> createForeground() {
7635 std::shared_ptr<FakeApplicationHandle> application =
7636 std::make_shared<FakeApplicationHandle>();
7637 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007638 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
7639 ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007640 window->setFocusable(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007641 return window;
7642 }
7643
7644private:
7645 int mSpyCount{0};
7646};
7647
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08007648using InputDispatcherSpyWindowDeathTest = InputDispatcherSpyWindowTest;
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007649/**
Prabir Pradhan5c85e052021-12-22 02:27:12 -08007650 * Adding a spy window that is not a trusted overlay causes Dispatcher to abort.
7651 */
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08007652TEST_F(InputDispatcherSpyWindowDeathTest, UntrustedSpy_AbortsDispatcher) {
7653 ScopedSilentDeath _silentDeath;
7654
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007655 auto spy = createSpy();
Prabir Pradhan5c85e052021-12-22 02:27:12 -08007656 spy->setTrustedOverlay(false);
7657 ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}}),
7658 ".* not a trusted overlay");
7659}
7660
7661/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007662 * Input injection into a display with a spy window but no foreground windows should succeed.
7663 */
7664TEST_F(InputDispatcherSpyWindowTest, NoForegroundWindow) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007665 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007666 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}});
7667
7668 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7669 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
7670 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7671 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7672}
7673
7674/**
7675 * Verify the order in which different input windows receive events. The touched foreground window
7676 * (if there is one) should always receive the event first. When there are multiple spy windows, the
7677 * spy windows will receive the event according to their Z-order, where the top-most spy window will
7678 * receive events before ones belows it.
7679 *
7680 * Here, we set up a scenario with four windows in the following Z order from the top:
7681 * spy1, spy2, window, spy3.
7682 * We then inject an event and verify that the foreground "window" receives it first, followed by
7683 * "spy1" and "spy2". The "spy3" does not receive the event because it is underneath the foreground
7684 * window.
7685 */
7686TEST_F(InputDispatcherSpyWindowTest, ReceivesInputInOrder) {
7687 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007688 auto spy1 = createSpy();
7689 auto spy2 = createSpy();
7690 auto spy3 = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007691 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window, spy3}}});
7692 const std::vector<sp<FakeWindowHandle>> channels{spy1, spy2, window, spy3};
7693 const size_t numChannels = channels.size();
7694
Michael Wright8e9a8562022-02-09 13:44:29 +00007695 base::unique_fd epollFd(epoll_create1(EPOLL_CLOEXEC));
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007696 if (!epollFd.ok()) {
7697 FAIL() << "Failed to create epoll fd";
7698 }
7699
7700 for (size_t i = 0; i < numChannels; i++) {
7701 struct epoll_event event = {.events = EPOLLIN, .data.u64 = i};
7702 if (epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, channels[i]->getChannelFd(), &event) < 0) {
7703 FAIL() << "Failed to add fd to epoll";
7704 }
7705 }
7706
7707 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7708 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
7709 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7710
7711 std::vector<size_t> eventOrder;
7712 std::vector<struct epoll_event> events(numChannels);
7713 for (;;) {
7714 const int nFds = epoll_wait(epollFd.get(), events.data(), static_cast<int>(numChannels),
7715 (100ms).count());
7716 if (nFds < 0) {
7717 FAIL() << "Failed to call epoll_wait";
7718 }
7719 if (nFds == 0) {
7720 break; // epoll_wait timed out
7721 }
7722 for (int i = 0; i < nFds; i++) {
Colin Cross5b799302022-10-18 21:52:41 -07007723 ASSERT_EQ(static_cast<uint32_t>(EPOLLIN), events[i].events);
Siarhei Vishniakou31977182022-09-30 08:51:23 -07007724 eventOrder.push_back(static_cast<size_t>(events[i].data.u64));
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007725 channels[i]->consumeMotionDown();
7726 }
7727 }
7728
7729 // Verify the order in which the events were received.
7730 EXPECT_EQ(3u, eventOrder.size());
7731 EXPECT_EQ(2u, eventOrder[0]); // index 2: window
7732 EXPECT_EQ(0u, eventOrder[1]); // index 0: spy1
7733 EXPECT_EQ(1u, eventOrder[2]); // index 1: spy2
7734}
7735
7736/**
7737 * A spy window using the NOT_TOUCHABLE flag does not receive events.
7738 */
7739TEST_F(InputDispatcherSpyWindowTest, NotTouchable) {
7740 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007741 auto spy = createSpy();
7742 spy->setTouchable(false);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007743 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
7744
7745 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7746 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
7747 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7748 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7749 spy->assertNoEvents();
7750}
7751
7752/**
7753 * A spy window will only receive gestures that originate within its touchable region. Gestures that
7754 * have their ACTION_DOWN outside of the touchable region of the spy window will not be dispatched
7755 * to the window.
7756 */
7757TEST_F(InputDispatcherSpyWindowTest, TouchableRegion) {
7758 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007759 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007760 spy->setTouchableRegion(Region{{0, 0, 20, 20}});
7761 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
7762
7763 // Inject an event outside the spy window's touchable region.
7764 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7765 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
7766 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7767 window->consumeMotionDown();
7768 spy->assertNoEvents();
7769 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7770 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
7771 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7772 window->consumeMotionUp();
7773 spy->assertNoEvents();
7774
7775 // Inject an event inside the spy window's touchable region.
7776 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7777 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7778 {5, 10}))
7779 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7780 window->consumeMotionDown();
7781 spy->consumeMotionDown();
7782}
7783
7784/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007785 * A spy window can listen for touches outside its touchable region using the WATCH_OUTSIDE_TOUCHES
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08007786 * flag, but it will get zero-ed out coordinates if the foreground has a different owner.
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007787 */
7788TEST_F(InputDispatcherSpyWindowTest, WatchOutsideTouches) {
7789 auto window = createForeground();
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08007790 window->setOwnerInfo(12, 34);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007791 auto spy = createSpy();
7792 spy->setWatchOutsideTouch(true);
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08007793 spy->setOwnerInfo(56, 78);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007794 spy->setFrame(Rect{0, 0, 20, 20});
7795 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
7796
7797 // Inject an event outside the spy window's frame and touchable region.
7798 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08007799 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7800 {100, 200}))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007801 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7802 window->consumeMotionDown();
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08007803 spy->consumeMotionOutsideWithZeroedCoords();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007804}
7805
7806/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007807 * Even when a spy window spans over multiple foreground windows, the spy should receive all
7808 * pointers that are down within its bounds.
7809 */
7810TEST_F(InputDispatcherSpyWindowTest, ReceivesMultiplePointers) {
7811 auto windowLeft = createForeground();
7812 windowLeft->setFrame({0, 0, 100, 200});
7813 auto windowRight = createForeground();
7814 windowRight->setFrame({100, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007815 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007816 spy->setFrame({0, 0, 200, 200});
7817 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, windowLeft, windowRight}}});
7818
7819 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7820 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7821 {50, 50}))
7822 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7823 windowLeft->consumeMotionDown();
7824 spy->consumeMotionDown();
7825
7826 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08007827 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007828 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
7829 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
7830 .pointer(
7831 PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50))
7832 .build();
7833 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7834 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
7835 InputEventInjectionSync::WAIT_FOR_RESULT))
7836 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7837 windowRight->consumeMotionDown();
7838 spy->consumeMotionPointerDown(1 /*pointerIndex*/);
7839}
7840
7841/**
7842 * When the first pointer lands outside the spy window and the second pointer lands inside it, the
7843 * the spy should receive the second pointer with ACTION_DOWN.
7844 */
7845TEST_F(InputDispatcherSpyWindowTest, ReceivesSecondPointerAsDown) {
7846 auto window = createForeground();
7847 window->setFrame({0, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007848 auto spyRight = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007849 spyRight->setFrame({100, 0, 200, 200});
7850 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyRight, window}}});
7851
7852 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7853 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7854 {50, 50}))
7855 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7856 window->consumeMotionDown();
7857 spyRight->assertNoEvents();
7858
7859 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08007860 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007861 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
7862 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
7863 .pointer(
7864 PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50))
7865 .build();
7866 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7867 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
7868 InputEventInjectionSync::WAIT_FOR_RESULT))
7869 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7870 window->consumeMotionPointerDown(1 /*pointerIndex*/);
7871 spyRight->consumeMotionDown();
7872}
7873
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007874/**
7875 * The spy window should not be able to affect whether or not touches are split. Only the foreground
7876 * windows should be allowed to control split touch.
7877 */
7878TEST_F(InputDispatcherSpyWindowTest, SplitIfNoForegroundWindowTouched) {
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08007879 // This spy window prevents touch splitting. However, we still expect to split touches
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007880 // because a foreground window has not disabled splitting.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007881 auto spy = createSpy();
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08007882 spy->setPreventSplitting(true);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007883
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007884 auto window = createForeground();
7885 window->setFrame(Rect(0, 0, 100, 100));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007886
7887 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
7888
7889 // First finger down, no window touched.
7890 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7891 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7892 {100, 200}))
7893 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7894 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7895 window->assertNoEvents();
7896
7897 // Second finger down on window, the window should receive touch down.
7898 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08007899 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007900 .displayId(ADISPLAY_ID_DEFAULT)
7901 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
7902 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
7903 .x(100)
7904 .y(200))
7905 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
7906 .build();
7907 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7908 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
7909 InputEventInjectionSync::WAIT_FOR_RESULT))
7910 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7911
7912 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7913 spy->consumeMotionPointerDown(1 /* pointerIndex */);
7914}
7915
7916/**
7917 * A spy window will usually be implemented as an un-focusable window. Verify that these windows
7918 * do not receive key events.
7919 */
7920TEST_F(InputDispatcherSpyWindowTest, UnfocusableSpyDoesNotReceiveKeyEvents) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007921 auto spy = createSpy();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007922 spy->setFocusable(false);
7923
7924 auto window = createForeground();
7925 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
7926 setFocusedWindow(window);
7927 window->consumeFocusEvent(true);
7928
7929 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7930 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
7931 window->consumeKeyDown(ADISPLAY_ID_NONE);
7932
7933 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7934 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
7935 window->consumeKeyUp(ADISPLAY_ID_NONE);
7936
7937 spy->assertNoEvents();
7938}
7939
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00007940using InputDispatcherPilferPointersTest = InputDispatcherSpyWindowTest;
7941
7942/**
7943 * A spy window can pilfer pointers. When this happens, touch gestures used by the spy window that
7944 * are currently sent to any other windows - including other spy windows - will also be cancelled.
7945 */
7946TEST_F(InputDispatcherPilferPointersTest, PilferPointers) {
7947 auto window = createForeground();
7948 auto spy1 = createSpy();
7949 auto spy2 = createSpy();
7950 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window}}});
7951
7952 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7953 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
7954 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7955 window->consumeMotionDown();
7956 spy1->consumeMotionDown();
7957 spy2->consumeMotionDown();
7958
7959 // Pilfer pointers from the second spy window.
7960 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy2->getToken()));
7961 spy2->assertNoEvents();
7962 spy1->consumeMotionCancel();
7963 window->consumeMotionCancel();
7964
7965 // The rest of the gesture should only be sent to the second spy window.
7966 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7967 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
7968 ADISPLAY_ID_DEFAULT))
7969 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7970 spy2->consumeMotionMove();
7971 spy1->assertNoEvents();
7972 window->assertNoEvents();
7973}
7974
7975/**
7976 * A spy window can pilfer pointers for a gesture even after the foreground window has been removed
7977 * in the middle of the gesture.
7978 */
7979TEST_F(InputDispatcherPilferPointersTest, CanPilferAfterWindowIsRemovedMidStream) {
7980 auto window = createForeground();
7981 auto spy = createSpy();
7982 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
7983
7984 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7985 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
7986 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7987 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7988 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7989
7990 window->releaseChannel();
7991
7992 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
7993
7994 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7995 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
7996 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7997 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
7998}
7999
8000/**
8001 * After a spy window pilfers pointers, new pointers that go down in its bounds should be sent to
8002 * the spy, but not to any other windows.
8003 */
8004TEST_F(InputDispatcherPilferPointersTest, ContinuesToReceiveGestureAfterPilfer) {
8005 auto spy = createSpy();
8006 auto window = createForeground();
8007
8008 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8009
8010 // First finger down on the window and the spy.
8011 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8012 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8013 {100, 200}))
8014 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8015 spy->consumeMotionDown();
8016 window->consumeMotionDown();
8017
8018 // Spy window pilfers the pointers.
8019 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
8020 window->consumeMotionCancel();
8021
8022 // Second finger down on the window and spy, but the window should not receive the pointer down.
8023 const MotionEvent secondFingerDownEvent =
8024 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8025 .displayId(ADISPLAY_ID_DEFAULT)
8026 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
8027 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
8028 .x(100)
8029 .y(200))
8030 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
8031 .build();
8032 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8033 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8034 InputEventInjectionSync::WAIT_FOR_RESULT))
8035 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8036
8037 spy->consumeMotionPointerDown(1 /*pointerIndex*/);
8038
8039 // Third finger goes down outside all windows, so injection should fail.
8040 const MotionEvent thirdFingerDownEvent =
8041 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8042 .displayId(ADISPLAY_ID_DEFAULT)
8043 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
8044 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
8045 .x(100)
8046 .y(200))
8047 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
8048 .pointer(PointerBuilder(/* id */ 2, AMOTION_EVENT_TOOL_TYPE_FINGER).x(-5).y(-5))
8049 .build();
8050 ASSERT_EQ(InputEventInjectionResult::FAILED,
8051 injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
8052 InputEventInjectionSync::WAIT_FOR_RESULT))
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08008053 << "Inject motion event should return InputEventInjectionResult::FAILED";
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00008054
8055 spy->assertNoEvents();
8056 window->assertNoEvents();
8057}
8058
8059/**
8060 * After a spy window pilfers pointers, only the pointers used by the spy should be canceled
8061 */
8062TEST_F(InputDispatcherPilferPointersTest, PartiallyPilferRequiredPointers) {
8063 auto spy = createSpy();
8064 spy->setFrame(Rect(0, 0, 100, 100));
8065 auto window = createForeground();
8066 window->setFrame(Rect(0, 0, 200, 200));
8067
8068 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8069
8070 // First finger down on the window only
8071 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8072 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8073 {150, 150}))
8074 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8075 window->consumeMotionDown();
8076
8077 // Second finger down on the spy and window
8078 const MotionEvent secondFingerDownEvent =
8079 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8080 .displayId(ADISPLAY_ID_DEFAULT)
8081 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
8082 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
8083 .x(150)
8084 .y(150))
8085 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(10).y(10))
8086 .build();
8087 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8088 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8089 InputEventInjectionSync::WAIT_FOR_RESULT))
8090 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8091 spy->consumeMotionDown();
8092 window->consumeMotionPointerDown(1);
8093
8094 // Third finger down on the spy and window
8095 const MotionEvent thirdFingerDownEvent =
8096 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8097 .displayId(ADISPLAY_ID_DEFAULT)
8098 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
8099 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
8100 .x(150)
8101 .y(150))
8102 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(10).y(10))
8103 .pointer(PointerBuilder(/* id */ 2, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
8104 .build();
8105 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8106 injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
8107 InputEventInjectionSync::WAIT_FOR_RESULT))
8108 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8109 spy->consumeMotionPointerDown(1);
8110 window->consumeMotionPointerDown(2);
8111
8112 // Spy window pilfers the pointers.
8113 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
8114 window->consumeMotionPointerUp(/* idx */ 2, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
8115 window->consumeMotionPointerUp(/* idx */ 1, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
8116
8117 spy->assertNoEvents();
8118 window->assertNoEvents();
8119}
8120
8121/**
8122 * After a spy window pilfers pointers, all pilfered pointers that have already been dispatched to
8123 * other windows should be canceled. If this results in the cancellation of all pointers for some
8124 * window, then that window should receive ACTION_CANCEL.
8125 */
8126TEST_F(InputDispatcherPilferPointersTest, PilferAllRequiredPointers) {
8127 auto spy = createSpy();
8128 spy->setFrame(Rect(0, 0, 100, 100));
8129 auto window = createForeground();
8130 window->setFrame(Rect(0, 0, 200, 200));
8131
8132 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8133
8134 // First finger down on both spy and window
8135 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8136 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8137 {10, 10}))
8138 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8139 window->consumeMotionDown();
8140 spy->consumeMotionDown();
8141
8142 // Second finger down on the spy and window
8143 const MotionEvent secondFingerDownEvent =
8144 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8145 .displayId(ADISPLAY_ID_DEFAULT)
8146 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
8147 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(10).y(10))
8148 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
8149 .build();
8150 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8151 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8152 InputEventInjectionSync::WAIT_FOR_RESULT))
8153 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8154 spy->consumeMotionPointerDown(1);
8155 window->consumeMotionPointerDown(1);
8156
8157 // Spy window pilfers the pointers.
8158 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
8159 window->consumeMotionCancel();
8160
8161 spy->assertNoEvents();
8162 window->assertNoEvents();
8163}
8164
8165/**
8166 * After a spy window pilfers pointers, new pointers that are not touching the spy window can still
8167 * be sent to other windows
8168 */
8169TEST_F(InputDispatcherPilferPointersTest, CanReceivePointersAfterPilfer) {
8170 auto spy = createSpy();
8171 spy->setFrame(Rect(0, 0, 100, 100));
8172 auto window = createForeground();
8173 window->setFrame(Rect(0, 0, 200, 200));
8174
8175 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8176
8177 // First finger down on both window and spy
8178 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8179 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8180 {10, 10}))
8181 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8182 window->consumeMotionDown();
8183 spy->consumeMotionDown();
8184
8185 // Spy window pilfers the pointers.
8186 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
8187 window->consumeMotionCancel();
8188
8189 // Second finger down on the window only
8190 const MotionEvent secondFingerDownEvent =
8191 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8192 .displayId(ADISPLAY_ID_DEFAULT)
8193 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
8194 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(10).y(10))
8195 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER)
8196 .x(150)
8197 .y(150))
8198 .build();
8199 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8200 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8201 InputEventInjectionSync::WAIT_FOR_RESULT))
8202 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8203 window->consumeMotionDown();
8204 window->assertNoEvents();
8205
8206 // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line
8207 spy->consumeMotionMove();
8208 spy->assertNoEvents();
8209}
8210
Prabir Pradhand65552b2021-10-07 11:23:50 -07008211class InputDispatcherStylusInterceptorTest : public InputDispatcherTest {
8212public:
8213 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupStylusOverlayScenario() {
8214 std::shared_ptr<FakeApplicationHandle> overlayApplication =
8215 std::make_shared<FakeApplicationHandle>();
8216 sp<FakeWindowHandle> overlay =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008217 sp<FakeWindowHandle>::make(overlayApplication, mDispatcher,
8218 "Stylus interceptor window", ADISPLAY_ID_DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -07008219 overlay->setFocusable(false);
8220 overlay->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008221 overlay->setTouchable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008222 overlay->setInterceptsStylus(true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07008223 overlay->setTrustedOverlay(true);
8224
8225 std::shared_ptr<FakeApplicationHandle> application =
8226 std::make_shared<FakeApplicationHandle>();
8227 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008228 sp<FakeWindowHandle>::make(application, mDispatcher, "Application window",
8229 ADISPLAY_ID_DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -07008230 window->setFocusable(true);
8231 window->setOwnerInfo(222, 222);
Prabir Pradhand65552b2021-10-07 11:23:50 -07008232
8233 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
8234 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
8235 setFocusedWindow(window);
8236 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
8237 return {std::move(overlay), std::move(window)};
8238 }
8239
8240 void sendFingerEvent(int32_t action) {
8241 NotifyMotionArgs motionArgs =
8242 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
8243 ADISPLAY_ID_DEFAULT, {PointF{20, 20}});
8244 mDispatcher->notifyMotion(&motionArgs);
8245 }
8246
8247 void sendStylusEvent(int32_t action) {
8248 NotifyMotionArgs motionArgs =
8249 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
8250 ADISPLAY_ID_DEFAULT, {PointF{30, 40}});
8251 motionArgs.pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS;
8252 mDispatcher->notifyMotion(&motionArgs);
8253 }
8254};
8255
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08008256using InputDispatcherStylusInterceptorDeathTest = InputDispatcherStylusInterceptorTest;
8257
8258TEST_F(InputDispatcherStylusInterceptorDeathTest, UntrustedOverlay_AbortsDispatcher) {
8259 ScopedSilentDeath _silentDeath;
8260
Prabir Pradhand65552b2021-10-07 11:23:50 -07008261 auto [overlay, window] = setupStylusOverlayScenario();
8262 overlay->setTrustedOverlay(false);
8263 // Configuring an untrusted overlay as a stylus interceptor should cause Dispatcher to abort.
8264 ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}),
8265 ".* not a trusted overlay");
8266}
8267
8268TEST_F(InputDispatcherStylusInterceptorTest, ConsmesOnlyStylusEvents) {
8269 auto [overlay, window] = setupStylusOverlayScenario();
8270 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
8271
8272 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
8273 overlay->consumeMotionDown();
8274 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
8275 overlay->consumeMotionUp();
8276
8277 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
8278 window->consumeMotionDown();
8279 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
8280 window->consumeMotionUp();
8281
8282 overlay->assertNoEvents();
8283 window->assertNoEvents();
8284}
8285
8286TEST_F(InputDispatcherStylusInterceptorTest, SpyWindowStylusInterceptor) {
8287 auto [overlay, window] = setupStylusOverlayScenario();
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008288 overlay->setSpy(true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07008289 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
8290
8291 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
8292 overlay->consumeMotionDown();
8293 window->consumeMotionDown();
8294 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
8295 overlay->consumeMotionUp();
8296 window->consumeMotionUp();
8297
8298 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
8299 window->consumeMotionDown();
8300 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
8301 window->consumeMotionUp();
8302
8303 overlay->assertNoEvents();
8304 window->assertNoEvents();
8305}
8306
Prabir Pradhan6dfbf262022-03-14 15:24:30 +00008307/**
8308 * Set up a scenario to test the behavior used by the stylus handwriting detection feature.
8309 * The scenario is as follows:
8310 * - The stylus interceptor overlay is configured as a spy window.
8311 * - The stylus interceptor spy receives the start of a new stylus gesture.
8312 * - It pilfers pointers and then configures itself to no longer be a spy.
8313 * - The stylus interceptor continues to receive the rest of the gesture.
8314 */
8315TEST_F(InputDispatcherStylusInterceptorTest, StylusHandwritingScenario) {
8316 auto [overlay, window] = setupStylusOverlayScenario();
8317 overlay->setSpy(true);
8318 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
8319
8320 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
8321 overlay->consumeMotionDown();
8322 window->consumeMotionDown();
8323
8324 // The interceptor pilfers the pointers.
8325 EXPECT_EQ(OK, mDispatcher->pilferPointers(overlay->getToken()));
8326 window->consumeMotionCancel();
8327
8328 // The interceptor configures itself so that it is no longer a spy.
8329 overlay->setSpy(false);
8330 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
8331
8332 // It continues to receive the rest of the stylus gesture.
8333 sendStylusEvent(AMOTION_EVENT_ACTION_MOVE);
8334 overlay->consumeMotionMove();
8335 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
8336 overlay->consumeMotionUp();
8337
8338 window->assertNoEvents();
8339}
8340
Prabir Pradhan5735a322022-04-11 17:23:34 +00008341struct User {
8342 int32_t mPid;
8343 int32_t mUid;
8344 uint32_t mPolicyFlags{DEFAULT_POLICY_FLAGS};
8345 std::unique_ptr<InputDispatcher>& mDispatcher;
8346
8347 User(std::unique_ptr<InputDispatcher>& dispatcher, int32_t pid, int32_t uid)
8348 : mPid(pid), mUid(uid), mDispatcher(dispatcher) {}
8349
8350 InputEventInjectionResult injectTargetedMotion(int32_t action) const {
8351 return injectMotionEvent(mDispatcher, action, AINPUT_SOURCE_TOUCHSCREEN,
8352 ADISPLAY_ID_DEFAULT, {100, 200},
8353 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
8354 AMOTION_EVENT_INVALID_CURSOR_POSITION},
8355 INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT,
8356 systemTime(SYSTEM_TIME_MONOTONIC), {mUid}, mPolicyFlags);
8357 }
8358
8359 InputEventInjectionResult injectTargetedKey(int32_t action) const {
8360 return inputdispatcher::injectKey(mDispatcher, action, 0 /* repeatCount*/, ADISPLAY_ID_NONE,
8361 InputEventInjectionSync::WAIT_FOR_RESULT,
8362 INJECT_EVENT_TIMEOUT, false /*allowKeyRepeat*/, {mUid},
8363 mPolicyFlags);
8364 }
8365
8366 sp<FakeWindowHandle> createWindow() const {
8367 std::shared_ptr<FakeApplicationHandle> overlayApplication =
8368 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008369 sp<FakeWindowHandle> window =
8370 sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, "Owned Window",
8371 ADISPLAY_ID_DEFAULT);
Prabir Pradhan5735a322022-04-11 17:23:34 +00008372 window->setOwnerInfo(mPid, mUid);
8373 return window;
8374 }
8375};
8376
8377using InputDispatcherTargetedInjectionTest = InputDispatcherTest;
8378
8379TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedWindow) {
8380 auto owner = User(mDispatcher, 10, 11);
8381 auto window = owner.createWindow();
8382 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
8383
8384 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
8385 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
8386 window->consumeMotionDown();
8387
8388 setFocusedWindow(window);
8389 window->consumeFocusEvent(true);
8390
8391 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
8392 owner.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
8393 window->consumeKeyDown(ADISPLAY_ID_NONE);
8394}
8395
8396TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedWindow) {
8397 auto owner = User(mDispatcher, 10, 11);
8398 auto window = owner.createWindow();
8399 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
8400
8401 auto rando = User(mDispatcher, 20, 21);
8402 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
8403 rando.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
8404
8405 setFocusedWindow(window);
8406 window->consumeFocusEvent(true);
8407
8408 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
8409 rando.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
8410 window->assertNoEvents();
8411}
8412
8413TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedSpyWindow) {
8414 auto owner = User(mDispatcher, 10, 11);
8415 auto window = owner.createWindow();
8416 auto spy = owner.createWindow();
8417 spy->setSpy(true);
8418 spy->setTrustedOverlay(true);
8419 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8420
8421 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
8422 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
8423 spy->consumeMotionDown();
8424 window->consumeMotionDown();
8425}
8426
8427TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedSpyWindow) {
8428 auto owner = User(mDispatcher, 10, 11);
8429 auto window = owner.createWindow();
8430
8431 auto rando = User(mDispatcher, 20, 21);
8432 auto randosSpy = rando.createWindow();
8433 randosSpy->setSpy(true);
8434 randosSpy->setTrustedOverlay(true);
8435 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}});
8436
8437 // The event is targeted at owner's window, so injection should succeed, but the spy should
8438 // not receive the event.
8439 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
8440 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
8441 randosSpy->assertNoEvents();
8442 window->consumeMotionDown();
8443}
8444
8445TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoAnyWindowWhenNotTargeting) {
8446 auto owner = User(mDispatcher, 10, 11);
8447 auto window = owner.createWindow();
8448
8449 auto rando = User(mDispatcher, 20, 21);
8450 auto randosSpy = rando.createWindow();
8451 randosSpy->setSpy(true);
8452 randosSpy->setTrustedOverlay(true);
8453 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}});
8454
8455 // A user that has injection permission can inject into any window.
8456 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
8457 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
8458 ADISPLAY_ID_DEFAULT));
8459 randosSpy->consumeMotionDown();
8460 window->consumeMotionDown();
8461
8462 setFocusedWindow(randosSpy);
8463 randosSpy->consumeFocusEvent(true);
8464
8465 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher));
8466 randosSpy->consumeKeyDown(ADISPLAY_ID_NONE);
8467 window->assertNoEvents();
8468}
8469
8470TEST_F(InputDispatcherTargetedInjectionTest, CanGenerateActionOutsideToOtherUids) {
8471 auto owner = User(mDispatcher, 10, 11);
8472 auto window = owner.createWindow();
8473
8474 auto rando = User(mDispatcher, 20, 21);
8475 auto randosWindow = rando.createWindow();
8476 randosWindow->setFrame(Rect{-10, -10, -5, -5});
8477 randosWindow->setWatchOutsideTouch(true);
8478 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosWindow, window}}});
8479
8480 // We allow generation of ACTION_OUTSIDE events into windows owned by different uids.
8481 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
8482 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
8483 window->consumeMotionDown();
8484 randosWindow->consumeMotionOutside();
8485}
8486
Garfield Tane84e6f92019-08-29 17:28:41 -07008487} // namespace android::inputdispatcher