blob: f8d425639b19e942e468e0415d06c1a628a20ff9 [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Garfield Tan0fc2fa72019-08-29 17:22:15 -070017#include "../dispatcher/InputDispatcher.h"
Michael Wrightd02c5b62014-02-10 15:10:22 -080018
Siarhei Vishniakou1c494c52021-08-11 20:25:01 -070019#include <android-base/properties.h>
Prabir Pradhana3ab87a2022-01-27 10:00:21 -080020#include <android-base/silent_death_test.h>
Garfield Tan1c7bc862020-01-28 13:24:04 -080021#include <android-base/stringprintf.h>
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -070022#include <android-base/thread_annotations.h>
Robert Carr803535b2018-08-02 16:38:15 -070023#include <binder/Binder.h>
Michael Wright8e9a8562022-02-09 13:44:29 +000024#include <fcntl.h>
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -080025#include <gmock/gmock.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080026#include <gtest/gtest.h>
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100027#include <input/Input.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080028#include <linux/input.h>
Prabir Pradhan07e05b62021-11-19 03:57:24 -080029#include <sys/epoll.h>
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100030
Garfield Tan1c7bc862020-01-28 13:24:04 -080031#include <cinttypes>
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -080032#include <compare>
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -070033#include <thread>
Garfield Tan1c7bc862020-01-28 13:24:04 -080034#include <unordered_set>
chaviwd1c23182019-12-20 18:44:56 -080035#include <vector>
Michael Wrightd02c5b62014-02-10 15:10:22 -080036
Garfield Tan1c7bc862020-01-28 13:24:04 -080037using android::base::StringPrintf;
chaviw3277faf2021-05-19 16:45:23 -050038using android::gui::FocusRequest;
39using android::gui::TouchOcclusionMode;
40using android::gui::WindowInfo;
41using android::gui::WindowInfoHandle;
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080042using android::os::InputEventInjectionResult;
43using android::os::InputEventInjectionSync;
Garfield Tan1c7bc862020-01-28 13:24:04 -080044
Garfield Tane84e6f92019-08-29 17:28:41 -070045namespace android::inputdispatcher {
Michael Wrightd02c5b62014-02-10 15:10:22 -080046
Dominik Laskowski2f01d772022-03-23 16:01:29 -070047using namespace ftl::flag_operators;
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -080048using testing::AllOf;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070049
Michael Wrightd02c5b62014-02-10 15:10:22 -080050// An arbitrary time value.
Prabir Pradhan5735a322022-04-11 17:23:34 +000051static constexpr nsecs_t ARBITRARY_TIME = 1234;
Michael Wrightd02c5b62014-02-10 15:10:22 -080052
53// An arbitrary device id.
Prabir Pradhan5735a322022-04-11 17:23:34 +000054static constexpr int32_t DEVICE_ID = 1;
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -080055static constexpr int32_t SECOND_DEVICE_ID = 2;
Michael Wrightd02c5b62014-02-10 15:10:22 -080056
Jeff Brownf086ddb2014-02-11 14:28:48 -080057// An arbitrary display id.
Arthur Hungabbb9d82021-09-01 14:52:30 +000058static constexpr int32_t DISPLAY_ID = ADISPLAY_ID_DEFAULT;
59static constexpr int32_t SECOND_DISPLAY_ID = 1;
Jeff Brownf086ddb2014-02-11 14:28:48 -080060
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -080061static constexpr int32_t ACTION_OUTSIDE = AMOTION_EVENT_ACTION_OUTSIDE;
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -080062static constexpr int32_t ACTION_CANCEL = AMOTION_EVENT_ACTION_CANCEL;
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -080063static constexpr int32_t POINTER_1_DOWN =
64 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +000065static constexpr int32_t POINTER_2_DOWN =
66 AMOTION_EVENT_ACTION_POINTER_DOWN | (2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +000067static constexpr int32_t POINTER_3_DOWN =
68 AMOTION_EVENT_ACTION_POINTER_DOWN | (3 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Arthur Hungc539dbb2022-12-08 07:45:36 +000069static constexpr int32_t POINTER_0_UP =
70 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -080071static constexpr int32_t POINTER_1_UP =
72 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
73
Antonio Kantek15beb512022-06-13 22:35:41 +000074// The default pid and uid for windows created on the primary display by the test.
Prabir Pradhan5735a322022-04-11 17:23:34 +000075static constexpr int32_t WINDOW_PID = 999;
76static constexpr int32_t WINDOW_UID = 1001;
77
Antonio Kantek15beb512022-06-13 22:35:41 +000078// The default pid and uid for the windows created on the secondary display by the test.
79static constexpr int32_t SECONDARY_WINDOW_PID = 1010;
80static constexpr int32_t SECONDARY_WINDOW_UID = 1012;
81
Prabir Pradhan5735a322022-04-11 17:23:34 +000082// The default policy flags to use for event injection by tests.
83static constexpr uint32_t DEFAULT_POLICY_FLAGS = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER;
Michael Wrightd02c5b62014-02-10 15:10:22 -080084
Siarhei Vishniakou58cfc602020-12-14 23:21:30 +000085// An arbitrary pid of the gesture monitor window
86static constexpr int32_t MONITOR_PID = 2001;
87
Siarhei Vishniakou289e9242022-02-15 14:50:16 -080088static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 1000ms;
89
Arthur Hungc539dbb2022-12-08 07:45:36 +000090static constexpr int expectedWallpaperFlags =
91 AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED;
92
chaviwd1c23182019-12-20 18:44:56 -080093struct PointF {
94 float x;
95 float y;
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -080096 auto operator<=>(const PointF&) const = default;
chaviwd1c23182019-12-20 18:44:56 -080097};
Michael Wrightd02c5b62014-02-10 15:10:22 -080098
Gang Wang342c9272020-01-13 13:15:04 -050099/**
100 * Return a DOWN key event with KEYCODE_A.
101 */
102static KeyEvent getTestKeyEvent() {
103 KeyEvent event;
104
Garfield Tanfbe732e2020-01-24 11:26:14 -0800105 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
106 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
107 ARBITRARY_TIME, ARBITRARY_TIME);
Gang Wang342c9272020-01-13 13:15:04 -0500108 return event;
109}
110
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000111static void assertMotionAction(int32_t expectedAction, int32_t receivedAction) {
112 ASSERT_EQ(expectedAction, receivedAction)
113 << "expected " << MotionEvent::actionToString(expectedAction) << ", got "
114 << MotionEvent::actionToString(receivedAction);
115}
116
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -0800117MATCHER_P(WithMotionAction, action, "MotionEvent with specified action") {
118 bool matches = action == arg.getAction();
119 if (!matches) {
120 *result_listener << "expected action " << MotionEvent::actionToString(action)
121 << ", but got " << MotionEvent::actionToString(arg.getAction());
122 }
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -0800123 if (action == AMOTION_EVENT_ACTION_DOWN) {
124 if (!matches) {
125 *result_listener << "; ";
126 }
127 *result_listener << "downTime should match eventTime for ACTION_DOWN events";
128 matches &= arg.getDownTime() == arg.getEventTime();
129 }
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -0800130 if (action == AMOTION_EVENT_ACTION_CANCEL) {
131 if (!matches) {
132 *result_listener << "; ";
133 }
134 *result_listener << "expected FLAG_CANCELED to be set with ACTION_CANCEL, but was not set";
135 matches &= (arg.getFlags() & AMOTION_EVENT_FLAG_CANCELED) != 0;
136 }
137 return matches;
138}
139
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -0800140MATCHER_P(WithDownTime, downTime, "InputEvent with specified downTime") {
141 return arg.getDownTime() == downTime;
142}
143
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -0800144MATCHER_P(WithDisplayId, displayId, "InputEvent with specified displayId") {
145 return arg.getDisplayId() == displayId;
146}
147
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -0800148MATCHER_P(WithSource, source, "InputEvent with specified source") {
149 *result_listener << "expected source " << inputEventSourceToString(source) << ", but got "
150 << inputEventSourceToString(arg.getSource());
151 return arg.getSource() == source;
152}
153
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -0800154MATCHER_P(WithFlags, flags, "InputEvent with specified flags") {
155 return arg.getFlags() == flags;
156}
157
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -0800158MATCHER_P2(WithCoords, x, y, "MotionEvent with specified coordinates") {
159 if (arg.getPointerCount() != 1) {
160 *result_listener << "Expected 1 pointer, got " << arg.getPointerCount();
161 return false;
162 }
Harry Cutts33476232023-01-30 19:57:29 +0000163 return arg.getX(/*pointerIndex=*/0) == x && arg.getY(/*pointerIndex=*/0) == y;
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -0800164}
165
166MATCHER_P(WithPointers, pointers, "MotionEvent with specified pointers") {
167 // Build a map for the received pointers, by pointer id
168 std::map<int32_t /*pointerId*/, PointF> actualPointers;
169 for (size_t pointerIndex = 0; pointerIndex < arg.getPointerCount(); pointerIndex++) {
170 const int32_t pointerId = arg.getPointerId(pointerIndex);
171 actualPointers[pointerId] = {arg.getX(pointerIndex), arg.getY(pointerIndex)};
172 }
173 return pointers == actualPointers;
174}
175
Michael Wrightd02c5b62014-02-10 15:10:22 -0800176// --- FakeInputDispatcherPolicy ---
177
178class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface {
179 InputDispatcherConfiguration mConfig;
180
Prabir Pradhanedd96402022-02-15 01:46:16 -0800181 using AnrResult = std::pair<sp<IBinder>, int32_t /*pid*/>;
182
Michael Wrightd02c5b62014-02-10 15:10:22 -0800183protected:
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000184 virtual ~FakeInputDispatcherPolicy() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800185
186public:
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000187 FakeInputDispatcherPolicy() {}
Jackal Guof9696682018-10-05 12:23:23 +0800188
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800189 void assertFilterInputEventWasCalled(const NotifyKeyArgs& args) {
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700190 assertFilterInputEventWasCalledInternal([&args](const InputEvent& event) {
191 ASSERT_EQ(event.getType(), AINPUT_EVENT_TYPE_KEY);
192 EXPECT_EQ(event.getDisplayId(), args.displayId);
193
194 const auto& keyEvent = static_cast<const KeyEvent&>(event);
195 EXPECT_EQ(keyEvent.getEventTime(), args.eventTime);
196 EXPECT_EQ(keyEvent.getAction(), args.action);
197 });
Jackal Guof9696682018-10-05 12:23:23 +0800198 }
199
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700200 void assertFilterInputEventWasCalled(const NotifyMotionArgs& args, vec2 point) {
201 assertFilterInputEventWasCalledInternal([&](const InputEvent& event) {
202 ASSERT_EQ(event.getType(), AINPUT_EVENT_TYPE_MOTION);
203 EXPECT_EQ(event.getDisplayId(), args.displayId);
204
205 const auto& motionEvent = static_cast<const MotionEvent&>(event);
206 EXPECT_EQ(motionEvent.getEventTime(), args.eventTime);
207 EXPECT_EQ(motionEvent.getAction(), args.action);
208 EXPECT_EQ(motionEvent.getX(0), point.x);
209 EXPECT_EQ(motionEvent.getY(0), point.y);
210 EXPECT_EQ(motionEvent.getRawX(0), point.x);
211 EXPECT_EQ(motionEvent.getRawY(0), point.y);
212 });
Jackal Guof9696682018-10-05 12:23:23 +0800213 }
214
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700215 void assertFilterInputEventWasNotCalled() {
216 std::scoped_lock lock(mLock);
217 ASSERT_EQ(nullptr, mFilteredEvent);
218 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800219
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800220 void assertNotifyConfigurationChangedWasCalled(nsecs_t when) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700221 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800222 ASSERT_TRUE(mConfigurationChangedTime)
223 << "Timed out waiting for configuration changed call";
224 ASSERT_EQ(*mConfigurationChangedTime, when);
225 mConfigurationChangedTime = std::nullopt;
226 }
227
228 void assertNotifySwitchWasCalled(const NotifySwitchArgs& args) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700229 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800230 ASSERT_TRUE(mLastNotifySwitch);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800231 // We do not check id because it is not exposed to the policy
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800232 EXPECT_EQ(args.eventTime, mLastNotifySwitch->eventTime);
233 EXPECT_EQ(args.policyFlags, mLastNotifySwitch->policyFlags);
234 EXPECT_EQ(args.switchValues, mLastNotifySwitch->switchValues);
235 EXPECT_EQ(args.switchMask, mLastNotifySwitch->switchMask);
236 mLastNotifySwitch = std::nullopt;
237 }
238
chaviwfd6d3512019-03-25 13:23:49 -0700239 void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700240 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800241 ASSERT_EQ(touchedToken, mOnPointerDownToken);
242 mOnPointerDownToken.clear();
243 }
244
245 void assertOnPointerDownWasNotCalled() {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700246 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800247 ASSERT_TRUE(mOnPointerDownToken == nullptr)
248 << "Expected onPointerDownOutsideFocus to not have been called";
chaviwfd6d3512019-03-25 13:23:49 -0700249 }
250
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700251 // This function must be called soon after the expected ANR timer starts,
252 // because we are also checking how much time has passed.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500253 void assertNotifyNoFocusedWindowAnrWasCalled(
Chris Yea209fde2020-07-22 13:54:51 -0700254 std::chrono::nanoseconds timeout,
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500255 const std::shared_ptr<InputApplicationHandle>& expectedApplication) {
Prabir Pradhanedd96402022-02-15 01:46:16 -0800256 std::unique_lock lock(mLock);
257 android::base::ScopedLockAssertion assumeLocked(mLock);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500258 std::shared_ptr<InputApplicationHandle> application;
Prabir Pradhanedd96402022-02-15 01:46:16 -0800259 ASSERT_NO_FATAL_FAILURE(
260 application = getAnrTokenLockedInterruptible(timeout, mAnrApplications, lock));
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500261 ASSERT_EQ(expectedApplication, application);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700262 }
263
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000264 void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout,
Prabir Pradhanedd96402022-02-15 01:46:16 -0800265 const sp<WindowInfoHandle>& window) {
266 LOG_ALWAYS_FATAL_IF(window == nullptr, "window should not be null");
267 assertNotifyWindowUnresponsiveWasCalled(timeout, window->getToken(),
268 window->getInfo()->ownerPid);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500269 }
270
Prabir Pradhanedd96402022-02-15 01:46:16 -0800271 void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout,
272 const sp<IBinder>& expectedToken,
273 int32_t expectedPid) {
274 std::unique_lock lock(mLock);
275 android::base::ScopedLockAssertion assumeLocked(mLock);
276 AnrResult result;
277 ASSERT_NO_FATAL_FAILURE(result =
278 getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock));
279 const auto& [token, pid] = result;
280 ASSERT_EQ(expectedToken, token);
281 ASSERT_EQ(expectedPid, pid);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500282 }
283
Prabir Pradhanedd96402022-02-15 01:46:16 -0800284 /** Wrap call with ASSERT_NO_FATAL_FAILURE() to ensure the return value is valid. */
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000285 sp<IBinder> getUnresponsiveWindowToken(std::chrono::nanoseconds timeout) {
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500286 std::unique_lock lock(mLock);
287 android::base::ScopedLockAssertion assumeLocked(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800288 AnrResult result = getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock);
289 const auto& [token, _] = result;
290 return token;
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000291 }
292
Prabir Pradhanedd96402022-02-15 01:46:16 -0800293 void assertNotifyWindowResponsiveWasCalled(const sp<IBinder>& expectedToken,
294 int32_t expectedPid) {
295 std::unique_lock lock(mLock);
296 android::base::ScopedLockAssertion assumeLocked(mLock);
297 AnrResult result;
298 ASSERT_NO_FATAL_FAILURE(
299 result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock));
300 const auto& [token, pid] = result;
301 ASSERT_EQ(expectedToken, token);
302 ASSERT_EQ(expectedPid, pid);
303 }
304
305 /** Wrap call with ASSERT_NO_FATAL_FAILURE() to ensure the return value is valid. */
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000306 sp<IBinder> getResponsiveWindowToken() {
307 std::unique_lock lock(mLock);
308 android::base::ScopedLockAssertion assumeLocked(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800309 AnrResult result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock);
310 const auto& [token, _] = result;
311 return token;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700312 }
313
314 void assertNotifyAnrWasNotCalled() {
315 std::scoped_lock lock(mLock);
316 ASSERT_TRUE(mAnrApplications.empty());
Prabir Pradhanedd96402022-02-15 01:46:16 -0800317 ASSERT_TRUE(mAnrWindows.empty());
318 ASSERT_TRUE(mResponsiveWindows.empty())
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500319 << "ANR was not called, but please also consume the 'connection is responsive' "
320 "signal";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700321 }
322
Garfield Tan1c7bc862020-01-28 13:24:04 -0800323 void setKeyRepeatConfiguration(nsecs_t timeout, nsecs_t delay) {
324 mConfig.keyRepeatTimeout = timeout;
325 mConfig.keyRepeatDelay = delay;
326 }
327
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000328 PointerCaptureRequest assertSetPointerCaptureCalled(bool enabled) {
Prabir Pradhan99987712020-11-10 18:43:05 -0800329 std::unique_lock lock(mLock);
330 base::ScopedLockAssertion assumeLocked(mLock);
331
332 if (!mPointerCaptureChangedCondition.wait_for(lock, 100ms,
333 [this, enabled]() REQUIRES(mLock) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000334 return mPointerCaptureRequest->enable ==
Prabir Pradhan99987712020-11-10 18:43:05 -0800335 enabled;
336 })) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000337 ADD_FAILURE() << "Timed out waiting for setPointerCapture(" << enabled
338 << ") to be called.";
339 return {};
Prabir Pradhan99987712020-11-10 18:43:05 -0800340 }
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000341 auto request = *mPointerCaptureRequest;
342 mPointerCaptureRequest.reset();
343 return request;
Prabir Pradhan99987712020-11-10 18:43:05 -0800344 }
345
346 void assertSetPointerCaptureNotCalled() {
347 std::unique_lock lock(mLock);
348 base::ScopedLockAssertion assumeLocked(mLock);
349
350 if (mPointerCaptureChangedCondition.wait_for(lock, 100ms) != std::cv_status::timeout) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000351 FAIL() << "Expected setPointerCapture(request) to not be called, but was called. "
Prabir Pradhan99987712020-11-10 18:43:05 -0800352 "enabled = "
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000353 << std::to_string(mPointerCaptureRequest->enable);
Prabir Pradhan99987712020-11-10 18:43:05 -0800354 }
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000355 mPointerCaptureRequest.reset();
Prabir Pradhan99987712020-11-10 18:43:05 -0800356 }
357
arthurhungf452d0b2021-01-06 00:19:52 +0800358 void assertDropTargetEquals(const sp<IBinder>& targetToken) {
359 std::scoped_lock lock(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800360 ASSERT_TRUE(mNotifyDropWindowWasCalled);
arthurhungf452d0b2021-01-06 00:19:52 +0800361 ASSERT_EQ(targetToken, mDropTargetWindowToken);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800362 mNotifyDropWindowWasCalled = false;
arthurhungf452d0b2021-01-06 00:19:52 +0800363 }
364
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800365 void assertNotifyInputChannelBrokenWasCalled(const sp<IBinder>& token) {
366 std::unique_lock lock(mLock);
367 base::ScopedLockAssertion assumeLocked(mLock);
368 std::optional<sp<IBinder>> receivedToken =
369 getItemFromStorageLockedInterruptible(100ms, mBrokenInputChannels, lock,
370 mNotifyInputChannelBroken);
371 ASSERT_TRUE(receivedToken.has_value());
372 ASSERT_EQ(token, *receivedToken);
373 }
374
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800375 /**
376 * Set policy timeout. A value of zero means next key will not be intercepted.
377 */
378 void setInterceptKeyTimeout(std::chrono::milliseconds timeout) {
379 mInterceptKeyTimeout = timeout;
380 }
381
Michael Wrightd02c5b62014-02-10 15:10:22 -0800382private:
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700383 std::mutex mLock;
384 std::unique_ptr<InputEvent> mFilteredEvent GUARDED_BY(mLock);
385 std::optional<nsecs_t> mConfigurationChangedTime GUARDED_BY(mLock);
386 sp<IBinder> mOnPointerDownToken GUARDED_BY(mLock);
387 std::optional<NotifySwitchArgs> mLastNotifySwitch GUARDED_BY(mLock);
Jackal Guof9696682018-10-05 12:23:23 +0800388
Prabir Pradhan99987712020-11-10 18:43:05 -0800389 std::condition_variable mPointerCaptureChangedCondition;
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000390
391 std::optional<PointerCaptureRequest> mPointerCaptureRequest GUARDED_BY(mLock);
Prabir Pradhan99987712020-11-10 18:43:05 -0800392
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700393 // ANR handling
Chris Yea209fde2020-07-22 13:54:51 -0700394 std::queue<std::shared_ptr<InputApplicationHandle>> mAnrApplications GUARDED_BY(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800395 std::queue<AnrResult> mAnrWindows GUARDED_BY(mLock);
396 std::queue<AnrResult> mResponsiveWindows GUARDED_BY(mLock);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700397 std::condition_variable mNotifyAnr;
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800398 std::queue<sp<IBinder>> mBrokenInputChannels GUARDED_BY(mLock);
399 std::condition_variable mNotifyInputChannelBroken;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700400
arthurhungf452d0b2021-01-06 00:19:52 +0800401 sp<IBinder> mDropTargetWindowToken GUARDED_BY(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800402 bool mNotifyDropWindowWasCalled GUARDED_BY(mLock) = false;
arthurhungf452d0b2021-01-06 00:19:52 +0800403
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800404 std::chrono::milliseconds mInterceptKeyTimeout = 0ms;
405
Prabir Pradhanedd96402022-02-15 01:46:16 -0800406 // All three ANR-related callbacks behave the same way, so we use this generic function to wait
407 // for a specific container to become non-empty. When the container is non-empty, return the
408 // first entry from the container and erase it.
409 template <class T>
410 T getAnrTokenLockedInterruptible(std::chrono::nanoseconds timeout, std::queue<T>& storage,
411 std::unique_lock<std::mutex>& lock) REQUIRES(mLock) {
412 // If there is an ANR, Dispatcher won't be idle because there are still events
413 // in the waitQueue that we need to check on. So we can't wait for dispatcher to be idle
414 // before checking if ANR was called.
415 // Since dispatcher is not guaranteed to call notifyNoFocusedWindowAnr right away, we need
416 // to provide it some time to act. 100ms seems reasonable.
417 std::chrono::duration timeToWait = timeout + 100ms; // provide some slack
418 const std::chrono::time_point start = std::chrono::steady_clock::now();
419 std::optional<T> token =
420 getItemFromStorageLockedInterruptible(timeToWait, storage, lock, mNotifyAnr);
421 if (!token.has_value()) {
422 ADD_FAILURE() << "Did not receive the ANR callback";
423 return {};
424 }
425
426 const std::chrono::duration waited = std::chrono::steady_clock::now() - start;
427 // Ensure that the ANR didn't get raised too early. We can't be too strict here because
428 // the dispatcher started counting before this function was called
429 if (std::chrono::abs(timeout - waited) > 100ms) {
430 ADD_FAILURE() << "ANR was raised too early or too late. Expected "
431 << std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count()
432 << "ms, but waited "
433 << std::chrono::duration_cast<std::chrono::milliseconds>(waited).count()
434 << "ms instead";
435 }
436 return *token;
437 }
438
439 template <class T>
440 std::optional<T> getItemFromStorageLockedInterruptible(std::chrono::nanoseconds timeout,
441 std::queue<T>& storage,
442 std::unique_lock<std::mutex>& lock,
443 std::condition_variable& condition)
444 REQUIRES(mLock) {
445 condition.wait_for(lock, timeout,
446 [&storage]() REQUIRES(mLock) { return !storage.empty(); });
447 if (storage.empty()) {
448 ADD_FAILURE() << "Did not receive the expected callback";
449 return std::nullopt;
450 }
451 T item = storage.front();
452 storage.pop();
453 return std::make_optional(item);
454 }
455
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600456 void notifyConfigurationChanged(nsecs_t when) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700457 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800458 mConfigurationChangedTime = when;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800459 }
460
Prabir Pradhanedd96402022-02-15 01:46:16 -0800461 void notifyWindowUnresponsive(const sp<IBinder>& connectionToken, std::optional<int32_t> pid,
462 const std::string&) override {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700463 std::scoped_lock lock(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800464 ASSERT_TRUE(pid.has_value());
465 mAnrWindows.push({connectionToken, *pid});
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700466 mNotifyAnr.notify_all();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500467 }
468
Prabir Pradhanedd96402022-02-15 01:46:16 -0800469 void notifyWindowResponsive(const sp<IBinder>& connectionToken,
470 std::optional<int32_t> pid) override {
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500471 std::scoped_lock lock(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800472 ASSERT_TRUE(pid.has_value());
473 mResponsiveWindows.push({connectionToken, *pid});
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500474 mNotifyAnr.notify_all();
475 }
476
477 void notifyNoFocusedWindowAnr(
478 const std::shared_ptr<InputApplicationHandle>& applicationHandle) override {
479 std::scoped_lock lock(mLock);
480 mAnrApplications.push(applicationHandle);
481 mNotifyAnr.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800482 }
483
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800484 void notifyInputChannelBroken(const sp<IBinder>& connectionToken) override {
485 std::scoped_lock lock(mLock);
486 mBrokenInputChannels.push(connectionToken);
487 mNotifyInputChannelBroken.notify_all();
488 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800489
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600490 void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {}
Robert Carr740167f2018-10-11 19:03:41 -0700491
Chris Yef59a2f42020-10-16 12:55:26 -0700492 void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType,
493 InputDeviceSensorAccuracy accuracy, nsecs_t timestamp,
494 const std::vector<float>& values) override {}
495
496 void notifySensorAccuracy(int deviceId, InputDeviceSensorType sensorType,
497 InputDeviceSensorAccuracy accuracy) override {}
Bernardo Rufino2e1f6512020-10-08 13:42:07 +0000498
Chris Yefb552902021-02-03 17:18:37 -0800499 void notifyVibratorState(int32_t deviceId, bool isOn) override {}
500
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600501 void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800502 *outConfig = mConfig;
503 }
504
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600505 bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700506 std::scoped_lock lock(mLock);
Jackal Guof9696682018-10-05 12:23:23 +0800507 switch (inputEvent->getType()) {
508 case AINPUT_EVENT_TYPE_KEY: {
509 const KeyEvent* keyEvent = static_cast<const KeyEvent*>(inputEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800510 mFilteredEvent = std::make_unique<KeyEvent>(*keyEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800511 break;
512 }
513
514 case AINPUT_EVENT_TYPE_MOTION: {
515 const MotionEvent* motionEvent = static_cast<const MotionEvent*>(inputEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800516 mFilteredEvent = std::make_unique<MotionEvent>(*motionEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800517 break;
518 }
519 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800520 return true;
521 }
522
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800523 void interceptKeyBeforeQueueing(const KeyEvent* inputEvent, uint32_t&) override {
524 if (inputEvent->getAction() == AKEY_EVENT_ACTION_UP) {
525 // Clear intercept state when we handled the event.
526 mInterceptKeyTimeout = 0ms;
527 }
528 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800529
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600530 void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800531
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600532 nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent*, uint32_t) override {
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800533 nsecs_t delay = std::chrono::nanoseconds(mInterceptKeyTimeout).count();
534 // Clear intercept state so we could dispatch the event in next wake.
535 mInterceptKeyTimeout = 0ms;
536 return delay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800537 }
538
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600539 bool dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent*, uint32_t, KeyEvent*) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800540 return false;
541 }
542
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600543 void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask,
544 uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700545 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800546 /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is
547 * essentially a passthrough for notifySwitch.
548 */
Harry Cutts33476232023-01-30 19:57:29 +0000549 mLastNotifySwitch = NotifySwitchArgs(/*id=*/1, when, policyFlags, switchValues, switchMask);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800550 }
551
Sean Stoutb4e0a592021-02-23 07:34:53 -0800552 void pokeUserActivity(nsecs_t, int32_t, int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800553
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600554 void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700555 std::scoped_lock lock(mLock);
chaviwfd6d3512019-03-25 13:23:49 -0700556 mOnPointerDownToken = newToken;
557 }
558
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000559 void setPointerCapture(const PointerCaptureRequest& request) override {
Prabir Pradhan99987712020-11-10 18:43:05 -0800560 std::scoped_lock lock(mLock);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000561 mPointerCaptureRequest = {request};
Prabir Pradhan99987712020-11-10 18:43:05 -0800562 mPointerCaptureChangedCondition.notify_all();
563 }
564
arthurhungf452d0b2021-01-06 00:19:52 +0800565 void notifyDropWindow(const sp<IBinder>& token, float x, float y) override {
566 std::scoped_lock lock(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800567 mNotifyDropWindowWasCalled = true;
arthurhungf452d0b2021-01-06 00:19:52 +0800568 mDropTargetWindowToken = token;
569 }
570
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700571 void assertFilterInputEventWasCalledInternal(
572 const std::function<void(const InputEvent&)>& verify) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700573 std::scoped_lock lock(mLock);
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -0800574 ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called.";
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700575 verify(*mFilteredEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800576 mFilteredEvent = nullptr;
Jackal Guof9696682018-10-05 12:23:23 +0800577 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800578};
579
Michael Wrightd02c5b62014-02-10 15:10:22 -0800580// --- InputDispatcherTest ---
581
582class InputDispatcherTest : public testing::Test {
583protected:
584 sp<FakeInputDispatcherPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700585 std::unique_ptr<InputDispatcher> mDispatcher;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800586
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000587 void SetUp() override {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700588 mFakePolicy = sp<FakeInputDispatcherPolicy>::make();
Siarhei Vishniakou289e9242022-02-15 14:50:16 -0800589 mDispatcher = std::make_unique<InputDispatcher>(mFakePolicy, STALE_EVENT_TIMEOUT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800590 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000591 // Start InputDispatcher thread
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700592 ASSERT_EQ(OK, mDispatcher->start());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800593 }
594
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000595 void TearDown() override {
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700596 ASSERT_EQ(OK, mDispatcher->stop());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800597 mFakePolicy.clear();
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700598 mDispatcher.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800599 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700600
601 /**
602 * Used for debugging when writing the test
603 */
604 void dumpDispatcherState() {
605 std::string dump;
606 mDispatcher->dump(dump);
607 std::stringstream ss(dump);
608 std::string to;
609
610 while (std::getline(ss, to, '\n')) {
611 ALOGE("%s", to.c_str());
612 }
613 }
Vishnu Nair958da932020-08-21 17:12:37 -0700614
chaviw3277faf2021-05-19 16:45:23 -0500615 void setFocusedWindow(const sp<WindowInfoHandle>& window,
616 const sp<WindowInfoHandle>& focusedWindow = nullptr) {
Vishnu Nair958da932020-08-21 17:12:37 -0700617 FocusRequest request;
618 request.token = window->getToken();
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +0000619 request.windowName = window->getName();
Vishnu Nair958da932020-08-21 17:12:37 -0700620 if (focusedWindow) {
621 request.focusedToken = focusedWindow->getToken();
622 }
623 request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
624 request.displayId = window->getInfo()->displayId;
625 mDispatcher->setFocusedWindow(request);
626 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800627};
628
Michael Wrightd02c5b62014-02-10 15:10:22 -0800629TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) {
630 KeyEvent event;
631
632 // Rejects undefined key actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800633 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
634 INVALID_HMAC,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600635 /*action*/ -1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME,
636 ARBITRARY_TIME);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800637 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000638 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000639 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800640 << "Should reject key events with undefined action.";
641
642 // Rejects ACTION_MULTIPLE since it is not supported despite being defined in the API.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800643 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
644 INVALID_HMAC, AKEY_EVENT_ACTION_MULTIPLE, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600645 ARBITRARY_TIME, ARBITRARY_TIME);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800646 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000647 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000648 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800649 << "Should reject key events with ACTION_MULTIPLE.";
650}
651
652TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) {
653 MotionEvent event;
654 PointerProperties pointerProperties[MAX_POINTERS + 1];
655 PointerCoords pointerCoords[MAX_POINTERS + 1];
Siarhei Vishniakou01747382022-01-20 13:23:27 -0800656 for (size_t i = 0; i <= MAX_POINTERS; i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800657 pointerProperties[i].clear();
658 pointerProperties[i].id = i;
659 pointerCoords[i].clear();
660 }
661
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800662 // Some constants commonly used below
663 constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN;
664 constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE;
665 constexpr int32_t metaState = AMETA_NONE;
666 constexpr MotionClassification classification = MotionClassification::NONE;
667
chaviw9eaa22c2020-07-01 16:21:27 -0700668 ui::Transform identityTransform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800669 // Rejects undefined motion actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800670 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
chaviw9eaa22c2020-07-01 16:21:27 -0700671 /*action*/ -1, 0, 0, edgeFlags, metaState, 0, classification,
672 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700673 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
674 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700675 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800676 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000677 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000678 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800679 << "Should reject motion events with undefined action.";
680
681 // Rejects pointer down with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800682 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -0800683 POINTER_1_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
684 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
685 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
686 ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500687 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800688 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000689 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000690 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800691 << "Should reject motion events with pointer down index too large.";
692
Garfield Tanfbe732e2020-01-24 11:26:14 -0800693 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700694 AMOTION_EVENT_ACTION_POINTER_DOWN |
695 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700696 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
697 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700698 identityTransform, ARBITRARY_TIME, ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500699 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800700 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000701 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000702 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800703 << "Should reject motion events with pointer down index too small.";
704
705 // Rejects pointer up with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800706 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -0800707 POINTER_1_UP, 0, 0, edgeFlags, metaState, 0, classification, identityTransform,
708 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
709 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
710 ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500711 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800712 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000713 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000714 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800715 << "Should reject motion events with pointer up index too large.";
716
Garfield Tanfbe732e2020-01-24 11:26:14 -0800717 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700718 AMOTION_EVENT_ACTION_POINTER_UP |
719 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700720 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
721 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700722 identityTransform, ARBITRARY_TIME, ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500723 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800724 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000725 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000726 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800727 << "Should reject motion events with pointer up index too small.";
728
729 // Rejects motion events with invalid number of pointers.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800730 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
731 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700732 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700733 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
734 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700735 /*pointerCount*/ 0, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800736 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000737 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000738 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800739 << "Should reject motion events with 0 pointers.";
740
Garfield Tanfbe732e2020-01-24 11:26:14 -0800741 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
742 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700743 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700744 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
745 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700746 /*pointerCount*/ MAX_POINTERS + 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800747 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000748 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000749 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800750 << "Should reject motion events with more than MAX_POINTERS pointers.";
751
752 // Rejects motion events with invalid pointer ids.
753 pointerProperties[0].id = -1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800754 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
755 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700756 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700757 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
758 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700759 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800760 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000761 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000762 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800763 << "Should reject motion events with pointer ids less than 0.";
764
765 pointerProperties[0].id = MAX_POINTER_ID + 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800766 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
767 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700768 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700769 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
770 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700771 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800772 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000773 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000774 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800775 << "Should reject motion events with pointer ids greater than MAX_POINTER_ID.";
776
777 // Rejects motion events with duplicate pointer ids.
778 pointerProperties[0].id = 1;
779 pointerProperties[1].id = 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800780 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
781 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700782 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700783 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
784 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700785 /*pointerCount*/ 2, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800786 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000787 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000788 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800789 << "Should reject motion events with duplicate pointer ids.";
790}
791
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800792/* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */
793
794TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) {
795 constexpr nsecs_t eventTime = 20;
Harry Cutts33476232023-01-30 19:57:29 +0000796 NotifyConfigurationChangedArgs args(/*id=*/10, eventTime);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800797 mDispatcher->notifyConfigurationChanged(&args);
798 ASSERT_TRUE(mDispatcher->waitForIdle());
799
800 mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime);
801}
802
803TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) {
Harry Cutts33476232023-01-30 19:57:29 +0000804 NotifySwitchArgs args(/*id=*/10, /*eventTime=*/20, /*policyFlags=*/0, /*switchValues=*/1,
805 /*switchMask=*/2);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800806 mDispatcher->notifySwitch(&args);
807
808 // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener
809 args.policyFlags |= POLICY_FLAG_TRUSTED;
810 mFakePolicy->assertNotifySwitchWasCalled(args);
811}
812
Arthur Hungb92218b2018-08-14 12:00:21 +0800813// --- InputDispatcherTest SetInputWindowTest ---
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700814static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms;
Siarhei Vishniakou1c494c52021-08-11 20:25:01 -0700815// Default input dispatching timeout if there is no focused application or paused window
816// from which to determine an appropriate dispatching timeout.
817static const std::chrono::duration DISPATCHING_TIMEOUT = std::chrono::milliseconds(
818 android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS *
819 android::base::HwTimeoutMultiplier());
Arthur Hungb92218b2018-08-14 12:00:21 +0800820
821class FakeApplicationHandle : public InputApplicationHandle {
822public:
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700823 FakeApplicationHandle() {
824 mInfo.name = "Fake Application";
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700825 mInfo.token = sp<BBinder>::make();
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500826 mInfo.dispatchingTimeoutMillis =
827 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700828 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800829 virtual ~FakeApplicationHandle() {}
830
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000831 virtual bool updateInfo() override { return true; }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700832
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500833 void setDispatchingTimeout(std::chrono::milliseconds timeout) {
834 mInfo.dispatchingTimeoutMillis = timeout.count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700835 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800836};
837
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800838class FakeInputReceiver {
Arthur Hungb92218b2018-08-14 12:00:21 +0800839public:
Garfield Tan15601662020-09-22 15:32:38 -0700840 explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name)
chaviwd1c23182019-12-20 18:44:56 -0800841 : mName(name) {
Garfield Tan15601662020-09-22 15:32:38 -0700842 mConsumer = std::make_unique<InputConsumer>(std::move(clientChannel));
chaviwd1c23182019-12-20 18:44:56 -0800843 }
844
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800845 InputEvent* consume() {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700846 InputEvent* event;
847 std::optional<uint32_t> consumeSeq = receiveEvent(&event);
848 if (!consumeSeq) {
849 return nullptr;
850 }
851 finishEvent(*consumeSeq);
852 return event;
853 }
854
855 /**
856 * Receive an event without acknowledging it.
857 * Return the sequence number that could later be used to send finished signal.
858 */
859 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Arthur Hungb92218b2018-08-14 12:00:21 +0800860 uint32_t consumeSeq;
861 InputEvent* event;
Arthur Hungb92218b2018-08-14 12:00:21 +0800862
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800863 std::chrono::time_point start = std::chrono::steady_clock::now();
864 status_t status = WOULD_BLOCK;
865 while (status == WOULD_BLOCK) {
Harry Cutts33476232023-01-30 19:57:29 +0000866 status = mConsumer->consume(&mEventFactory, /*consumeBatches=*/true, -1, &consumeSeq,
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800867 &event);
868 std::chrono::duration elapsed = std::chrono::steady_clock::now() - start;
869 if (elapsed > 100ms) {
870 break;
871 }
872 }
873
874 if (status == WOULD_BLOCK) {
875 // Just means there's no event available.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700876 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800877 }
878
879 if (status != OK) {
880 ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK.";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700881 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800882 }
883 if (event == nullptr) {
884 ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700885 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800886 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700887 if (outEvent != nullptr) {
888 *outEvent = event;
889 }
890 return consumeSeq;
891 }
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800892
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700893 /**
894 * To be used together with "receiveEvent" to complete the consumption of an event.
895 */
896 void finishEvent(uint32_t consumeSeq) {
897 const status_t status = mConsumer->sendFinishedSignal(consumeSeq, true);
898 ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK.";
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800899 }
900
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000901 void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) {
902 const status_t status = mConsumer->sendTimeline(inputEventId, timeline);
903 ASSERT_EQ(OK, status);
904 }
905
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000906 void consumeEvent(int32_t expectedEventType, int32_t expectedAction,
907 std::optional<int32_t> expectedDisplayId,
908 std::optional<int32_t> expectedFlags) {
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800909 InputEvent* event = consume();
910
911 ASSERT_NE(nullptr, event) << mName.c_str()
912 << ": consumer should have returned non-NULL event.";
Arthur Hungb92218b2018-08-14 12:00:21 +0800913 ASSERT_EQ(expectedEventType, event->getType())
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700914 << mName.c_str() << " expected " << inputEventTypeToString(expectedEventType)
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800915 << " event, got " << inputEventTypeToString(event->getType()) << " event";
Arthur Hungb92218b2018-08-14 12:00:21 +0800916
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000917 if (expectedDisplayId.has_value()) {
918 EXPECT_EQ(expectedDisplayId, event->getDisplayId());
919 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800920
Tiger Huang8664f8c2018-10-11 19:14:35 +0800921 switch (expectedEventType) {
922 case AINPUT_EVENT_TYPE_KEY: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800923 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event);
924 EXPECT_EQ(expectedAction, keyEvent.getAction());
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000925 if (expectedFlags.has_value()) {
926 EXPECT_EQ(expectedFlags.value(), keyEvent.getFlags());
927 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800928 break;
929 }
930 case AINPUT_EVENT_TYPE_MOTION: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800931 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000932 assertMotionAction(expectedAction, motionEvent.getAction());
933
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000934 if (expectedFlags.has_value()) {
935 EXPECT_EQ(expectedFlags.value(), motionEvent.getFlags());
936 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800937 break;
938 }
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100939 case AINPUT_EVENT_TYPE_FOCUS: {
940 FAIL() << "Use 'consumeFocusEvent' for FOCUS events";
941 }
Prabir Pradhan99987712020-11-10 18:43:05 -0800942 case AINPUT_EVENT_TYPE_CAPTURE: {
943 FAIL() << "Use 'consumeCaptureEvent' for CAPTURE events";
944 }
Antonio Kantekf16f2832021-09-28 04:39:20 +0000945 case AINPUT_EVENT_TYPE_TOUCH_MODE: {
946 FAIL() << "Use 'consumeTouchModeEvent' for TOUCH_MODE events";
947 }
arthurhungb89ccb02020-12-30 16:19:01 +0800948 case AINPUT_EVENT_TYPE_DRAG: {
949 FAIL() << "Use 'consumeDragEvent' for DRAG events";
950 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800951 default: {
952 FAIL() << mName.c_str() << ": invalid event type: " << expectedEventType;
953 }
954 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800955 }
956
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -0800957 MotionEvent* consumeMotion() {
958 InputEvent* event = consume();
959
960 if (event == nullptr) {
961 ADD_FAILURE() << mName << ": expected a MotionEvent, but didn't get one.";
962 return nullptr;
963 }
964
965 if (event->getType() != AINPUT_EVENT_TYPE_MOTION) {
966 ADD_FAILURE() << mName << " expected a MotionEvent, got "
967 << inputEventTypeToString(event->getType()) << " event";
968 return nullptr;
969 }
970 return static_cast<MotionEvent*>(event);
971 }
972
973 void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) {
974 MotionEvent* motionEvent = consumeMotion();
975 ASSERT_NE(nullptr, motionEvent) << "Did not get a motion event, but expected " << matcher;
976 ASSERT_THAT(*motionEvent, matcher);
977 }
978
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100979 void consumeFocusEvent(bool hasFocus, bool inTouchMode) {
980 InputEvent* event = consume();
981 ASSERT_NE(nullptr, event) << mName.c_str()
982 << ": consumer should have returned non-NULL event.";
983 ASSERT_EQ(AINPUT_EVENT_TYPE_FOCUS, event->getType())
984 << "Got " << inputEventTypeToString(event->getType())
985 << " event instead of FOCUS event";
986
987 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
988 << mName.c_str() << ": event displayId should always be NONE.";
989
990 FocusEvent* focusEvent = static_cast<FocusEvent*>(event);
991 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100992 }
993
Prabir Pradhan99987712020-11-10 18:43:05 -0800994 void consumeCaptureEvent(bool hasCapture) {
995 const InputEvent* event = consume();
996 ASSERT_NE(nullptr, event) << mName.c_str()
997 << ": consumer should have returned non-NULL event.";
998 ASSERT_EQ(AINPUT_EVENT_TYPE_CAPTURE, event->getType())
999 << "Got " << inputEventTypeToString(event->getType())
1000 << " event instead of CAPTURE event";
1001
1002 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1003 << mName.c_str() << ": event displayId should always be NONE.";
1004
1005 const auto& captureEvent = static_cast<const CaptureEvent&>(*event);
1006 EXPECT_EQ(hasCapture, captureEvent.getPointerCaptureEnabled());
1007 }
1008
arthurhungb89ccb02020-12-30 16:19:01 +08001009 void consumeDragEvent(bool isExiting, float x, float y) {
1010 const InputEvent* event = consume();
1011 ASSERT_NE(nullptr, event) << mName.c_str()
1012 << ": consumer should have returned non-NULL event.";
1013 ASSERT_EQ(AINPUT_EVENT_TYPE_DRAG, event->getType())
1014 << "Got " << inputEventTypeToString(event->getType())
1015 << " event instead of DRAG event";
1016
1017 EXPECT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1018 << mName.c_str() << ": event displayId should always be NONE.";
1019
1020 const auto& dragEvent = static_cast<const DragEvent&>(*event);
1021 EXPECT_EQ(isExiting, dragEvent.isExiting());
1022 EXPECT_EQ(x, dragEvent.getX());
1023 EXPECT_EQ(y, dragEvent.getY());
1024 }
1025
Antonio Kantekf16f2832021-09-28 04:39:20 +00001026 void consumeTouchModeEvent(bool inTouchMode) {
1027 const InputEvent* event = consume();
1028 ASSERT_NE(nullptr, event) << mName.c_str()
1029 << ": consumer should have returned non-NULL event.";
1030 ASSERT_EQ(AINPUT_EVENT_TYPE_TOUCH_MODE, event->getType())
1031 << "Got " << inputEventTypeToString(event->getType())
1032 << " event instead of TOUCH_MODE event";
1033
1034 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1035 << mName.c_str() << ": event displayId should always be NONE.";
1036 const auto& touchModeEvent = static_cast<const TouchModeEvent&>(*event);
1037 EXPECT_EQ(inTouchMode, touchModeEvent.isInTouchMode());
1038 }
1039
chaviwd1c23182019-12-20 18:44:56 -08001040 void assertNoEvents() {
1041 InputEvent* event = consume();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001042 if (event == nullptr) {
1043 return;
1044 }
1045 if (event->getType() == AINPUT_EVENT_TYPE_KEY) {
1046 KeyEvent& keyEvent = static_cast<KeyEvent&>(*event);
1047 ADD_FAILURE() << "Received key event "
1048 << KeyEvent::actionToString(keyEvent.getAction());
1049 } else if (event->getType() == AINPUT_EVENT_TYPE_MOTION) {
1050 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
1051 ADD_FAILURE() << "Received motion event "
1052 << MotionEvent::actionToString(motionEvent.getAction());
1053 } else if (event->getType() == AINPUT_EVENT_TYPE_FOCUS) {
1054 FocusEvent& focusEvent = static_cast<FocusEvent&>(*event);
1055 ADD_FAILURE() << "Received focus event, hasFocus = "
1056 << (focusEvent.getHasFocus() ? "true" : "false");
Prabir Pradhan99987712020-11-10 18:43:05 -08001057 } else if (event->getType() == AINPUT_EVENT_TYPE_CAPTURE) {
1058 const auto& captureEvent = static_cast<CaptureEvent&>(*event);
1059 ADD_FAILURE() << "Received capture event, pointerCaptureEnabled = "
1060 << (captureEvent.getPointerCaptureEnabled() ? "true" : "false");
Antonio Kantekf16f2832021-09-28 04:39:20 +00001061 } else if (event->getType() == AINPUT_EVENT_TYPE_TOUCH_MODE) {
1062 const auto& touchModeEvent = static_cast<TouchModeEvent&>(*event);
1063 ADD_FAILURE() << "Received touch mode event, inTouchMode = "
1064 << (touchModeEvent.isInTouchMode() ? "true" : "false");
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001065 }
1066 FAIL() << mName.c_str()
1067 << ": should not have received any events, so consume() should return NULL";
chaviwd1c23182019-12-20 18:44:56 -08001068 }
1069
1070 sp<IBinder> getToken() { return mConsumer->getChannel()->getConnectionToken(); }
1071
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001072 int getChannelFd() { return mConsumer->getChannel()->getFd().get(); }
1073
chaviwd1c23182019-12-20 18:44:56 -08001074protected:
1075 std::unique_ptr<InputConsumer> mConsumer;
1076 PreallocatedInputEventFactory mEventFactory;
1077
1078 std::string mName;
1079};
1080
chaviw3277faf2021-05-19 16:45:23 -05001081class FakeWindowHandle : public WindowInfoHandle {
chaviwd1c23182019-12-20 18:44:56 -08001082public:
1083 static const int32_t WIDTH = 600;
1084 static const int32_t HEIGHT = 800;
chaviwd1c23182019-12-20 18:44:56 -08001085
Chris Yea209fde2020-07-22 13:54:51 -07001086 FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001087 const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name,
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001088 int32_t displayId, std::optional<sp<IBinder>> token = std::nullopt)
chaviwd1c23182019-12-20 18:44:56 -08001089 : mName(name) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001090 if (token == std::nullopt) {
Garfield Tan15601662020-09-22 15:32:38 -07001091 base::Result<std::unique_ptr<InputChannel>> channel =
1092 dispatcher->createInputChannel(name);
1093 token = (*channel)->getConnectionToken();
1094 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
chaviwd1c23182019-12-20 18:44:56 -08001095 }
1096
1097 inputApplicationHandle->updateInfo();
1098 mInfo.applicationInfo = *inputApplicationHandle->getInfo();
1099
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001100 mInfo.token = *token;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001101 mInfo.id = sId++;
chaviwd1c23182019-12-20 18:44:56 -08001102 mInfo.name = name;
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -05001103 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001104 mInfo.alpha = 1.0;
chaviwd1c23182019-12-20 18:44:56 -08001105 mInfo.frameLeft = 0;
1106 mInfo.frameTop = 0;
1107 mInfo.frameRight = WIDTH;
1108 mInfo.frameBottom = HEIGHT;
chaviw1ff3d1e2020-07-01 15:53:47 -07001109 mInfo.transform.set(0, 0);
chaviwd1c23182019-12-20 18:44:56 -08001110 mInfo.globalScaleFactor = 1.0;
1111 mInfo.touchableRegion.clear();
1112 mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT));
Prabir Pradhan5735a322022-04-11 17:23:34 +00001113 mInfo.ownerPid = WINDOW_PID;
1114 mInfo.ownerUid = WINDOW_UID;
chaviwd1c23182019-12-20 18:44:56 -08001115 mInfo.displayId = displayId;
Prabir Pradhan51e7db02022-02-07 06:02:57 -08001116 mInfo.inputConfig = WindowInfo::InputConfig::DEFAULT;
chaviwd1c23182019-12-20 18:44:56 -08001117 }
1118
Arthur Hungabbb9d82021-09-01 14:52:30 +00001119 sp<FakeWindowHandle> clone(
1120 const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001121 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t displayId) {
Arthur Hungabbb9d82021-09-01 14:52:30 +00001122 sp<FakeWindowHandle> handle =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001123 sp<FakeWindowHandle>::make(inputApplicationHandle, dispatcher,
1124 mInfo.name + "(Mirror)", displayId, mInfo.token);
Arthur Hungabbb9d82021-09-01 14:52:30 +00001125 return handle;
1126 }
1127
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001128 void setTouchable(bool touchable) {
1129 mInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, !touchable);
1130 }
chaviwd1c23182019-12-20 18:44:56 -08001131
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001132 void setFocusable(bool focusable) {
1133 mInfo.setInputConfig(WindowInfo::InputConfig::NOT_FOCUSABLE, !focusable);
1134 }
1135
1136 void setVisible(bool visible) {
1137 mInfo.setInputConfig(WindowInfo::InputConfig::NOT_VISIBLE, !visible);
1138 }
Vishnu Nair958da932020-08-21 17:12:37 -07001139
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001140 void setDispatchingTimeout(std::chrono::nanoseconds timeout) {
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -05001141 mInfo.dispatchingTimeout = timeout;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001142 }
1143
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001144 void setPaused(bool paused) {
1145 mInfo.setInputConfig(WindowInfo::InputConfig::PAUSE_DISPATCHING, paused);
1146 }
1147
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08001148 void setPreventSplitting(bool preventSplitting) {
1149 mInfo.setInputConfig(WindowInfo::InputConfig::PREVENT_SPLITTING, preventSplitting);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001150 }
1151
1152 void setSlippery(bool slippery) {
1153 mInfo.setInputConfig(WindowInfo::InputConfig::SLIPPERY, slippery);
1154 }
1155
1156 void setWatchOutsideTouch(bool watchOutside) {
1157 mInfo.setInputConfig(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH, watchOutside);
1158 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001159
Prabir Pradhan51e7db02022-02-07 06:02:57 -08001160 void setSpy(bool spy) { mInfo.setInputConfig(WindowInfo::InputConfig::SPY, spy); }
1161
1162 void setInterceptsStylus(bool interceptsStylus) {
1163 mInfo.setInputConfig(WindowInfo::InputConfig::INTERCEPTS_STYLUS, interceptsStylus);
1164 }
1165
1166 void setDropInput(bool dropInput) {
1167 mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT, dropInput);
1168 }
1169
1170 void setDropInputIfObscured(bool dropInputIfObscured) {
1171 mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED, dropInputIfObscured);
1172 }
1173
1174 void setNoInputChannel(bool noInputChannel) {
1175 mInfo.setInputConfig(WindowInfo::InputConfig::NO_INPUT_CHANNEL, noInputChannel);
1176 }
1177
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001178 void setAlpha(float alpha) { mInfo.alpha = alpha; }
1179
chaviw3277faf2021-05-19 16:45:23 -05001180 void setTouchOcclusionMode(TouchOcclusionMode mode) { mInfo.touchOcclusionMode = mode; }
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001181
Bernardo Rufino7393d172021-02-26 13:56:11 +00001182 void setApplicationToken(sp<IBinder> token) { mInfo.applicationInfo.token = token; }
1183
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001184 void setFrame(const Rect& frame, const ui::Transform& displayTransform = ui::Transform()) {
chaviwd1c23182019-12-20 18:44:56 -08001185 mInfo.frameLeft = frame.left;
1186 mInfo.frameTop = frame.top;
1187 mInfo.frameRight = frame.right;
1188 mInfo.frameBottom = frame.bottom;
1189 mInfo.touchableRegion.clear();
1190 mInfo.addTouchableRegion(frame);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001191
1192 const Rect logicalDisplayFrame = displayTransform.transform(frame);
1193 ui::Transform translate;
1194 translate.set(-logicalDisplayFrame.left, -logicalDisplayFrame.top);
1195 mInfo.transform = translate * displayTransform;
chaviwd1c23182019-12-20 18:44:56 -08001196 }
1197
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001198 void setTouchableRegion(const Region& region) { mInfo.touchableRegion = region; }
1199
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001200 void setIsWallpaper(bool isWallpaper) {
1201 mInfo.setInputConfig(WindowInfo::InputConfig::IS_WALLPAPER, isWallpaper);
1202 }
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001203
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001204 void setDupTouchToWallpaper(bool hasWallpaper) {
1205 mInfo.setInputConfig(WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER, hasWallpaper);
1206 }
chaviwd1c23182019-12-20 18:44:56 -08001207
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001208 void setTrustedOverlay(bool trustedOverlay) {
1209 mInfo.setInputConfig(WindowInfo::InputConfig::TRUSTED_OVERLAY, trustedOverlay);
1210 }
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001211
chaviw9eaa22c2020-07-01 16:21:27 -07001212 void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) {
1213 mInfo.transform.set(dsdx, dtdx, dtdy, dsdy);
1214 }
1215
1216 void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); }
chaviwaf87b3e2019-10-01 16:59:28 -07001217
yunho.shinf4a80b82020-11-16 21:13:57 +09001218 void setWindowOffset(float offsetX, float offsetY) { mInfo.transform.set(offsetX, offsetY); }
1219
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001220 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
1221 consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId,
1222 expectedFlags);
1223 }
1224
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001225 void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
1226 consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, expectedDisplayId, expectedFlags);
1227 }
1228
Svet Ganov5d3bc372020-01-26 23:11:07 -08001229 void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001230 int32_t expectedFlags = 0) {
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08001231 consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(expectedDisplayId),
1232 WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED)));
Svet Ganov5d3bc372020-01-26 23:11:07 -08001233 }
1234
1235 void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001236 int32_t expectedFlags = 0) {
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08001237 consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
1238 WithDisplayId(expectedDisplayId), WithFlags(expectedFlags)));
Svet Ganov5d3bc372020-01-26 23:11:07 -08001239 }
1240
1241 void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001242 int32_t expectedFlags = 0) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001243 consumeAnyMotionDown(expectedDisplayId, expectedFlags);
1244 }
1245
1246 void consumeAnyMotionDown(std::optional<int32_t> expectedDisplayId = std::nullopt,
1247 std::optional<int32_t> expectedFlags = std::nullopt) {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001248 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId,
1249 expectedFlags);
1250 }
1251
Svet Ganov5d3bc372020-01-26 23:11:07 -08001252 void consumeMotionPointerDown(int32_t pointerIdx,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001253 int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1254 int32_t expectedFlags = 0) {
1255 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
1256 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001257 consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags);
1258 }
1259
1260 void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001261 int32_t expectedFlags = 0) {
1262 int32_t action = AMOTION_EVENT_ACTION_POINTER_UP |
1263 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001264 consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags);
1265 }
1266
1267 void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001268 int32_t expectedFlags = 0) {
Michael Wright3a240c42019-12-10 20:53:41 +00001269 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId,
1270 expectedFlags);
1271 }
1272
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05001273 void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1274 int32_t expectedFlags = 0) {
1275 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, expectedDisplayId,
1276 expectedFlags);
1277 }
1278
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08001279 void consumeMotionOutsideWithZeroedCoords(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1280 int32_t expectedFlags = 0) {
1281 InputEvent* event = consume();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08001282 ASSERT_NE(nullptr, event);
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08001283 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType());
1284 const MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
1285 EXPECT_EQ(AMOTION_EVENT_ACTION_OUTSIDE, motionEvent.getActionMasked());
1286 EXPECT_EQ(0.f, motionEvent.getRawPointerCoords(0)->getX());
1287 EXPECT_EQ(0.f, motionEvent.getRawPointerCoords(0)->getY());
1288 }
1289
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001290 void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) {
1291 ASSERT_NE(mInputReceiver, nullptr)
1292 << "Cannot consume events from a window with no receiver";
1293 mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode);
1294 }
1295
Prabir Pradhan99987712020-11-10 18:43:05 -08001296 void consumeCaptureEvent(bool hasCapture) {
1297 ASSERT_NE(mInputReceiver, nullptr)
1298 << "Cannot consume events from a window with no receiver";
1299 mInputReceiver->consumeCaptureEvent(hasCapture);
1300 }
1301
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08001302 void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) {
1303 MotionEvent* motionEvent = consumeMotion();
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08001304 ASSERT_NE(nullptr, motionEvent) << "Did not get a motion event, but expected " << matcher;
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08001305 ASSERT_THAT(*motionEvent, matcher);
1306 }
1307
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001308 void consumeEvent(int32_t expectedEventType, int32_t expectedAction,
1309 std::optional<int32_t> expectedDisplayId,
1310 std::optional<int32_t> expectedFlags) {
chaviwd1c23182019-12-20 18:44:56 -08001311 ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver";
1312 mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId,
1313 expectedFlags);
1314 }
1315
arthurhungb89ccb02020-12-30 16:19:01 +08001316 void consumeDragEvent(bool isExiting, float x, float y) {
1317 mInputReceiver->consumeDragEvent(isExiting, x, y);
1318 }
1319
Antonio Kantekf16f2832021-09-28 04:39:20 +00001320 void consumeTouchModeEvent(bool inTouchMode) {
1321 ASSERT_NE(mInputReceiver, nullptr)
1322 << "Cannot consume events from a window with no receiver";
1323 mInputReceiver->consumeTouchModeEvent(inTouchMode);
1324 }
1325
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001326 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001327 if (mInputReceiver == nullptr) {
1328 ADD_FAILURE() << "Invalid receive event on window with no receiver";
1329 return std::nullopt;
1330 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001331 return mInputReceiver->receiveEvent(outEvent);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001332 }
1333
1334 void finishEvent(uint32_t sequenceNum) {
1335 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
1336 mInputReceiver->finishEvent(sequenceNum);
1337 }
1338
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00001339 void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) {
1340 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
1341 mInputReceiver->sendTimeline(inputEventId, timeline);
1342 }
1343
chaviwaf87b3e2019-10-01 16:59:28 -07001344 InputEvent* consume() {
1345 if (mInputReceiver == nullptr) {
1346 return nullptr;
1347 }
1348 return mInputReceiver->consume();
1349 }
1350
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00001351 MotionEvent* consumeMotion() {
1352 InputEvent* event = consume();
1353 if (event == nullptr) {
1354 ADD_FAILURE() << "Consume failed : no event";
1355 return nullptr;
1356 }
1357 if (event->getType() != AINPUT_EVENT_TYPE_MOTION) {
1358 ADD_FAILURE() << "Instead of motion event, got "
1359 << inputEventTypeToString(event->getType());
1360 return nullptr;
1361 }
1362 return static_cast<MotionEvent*>(event);
1363 }
1364
Arthur Hungb92218b2018-08-14 12:00:21 +08001365 void assertNoEvents() {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001366 if (mInputReceiver == nullptr &&
Prabir Pradhan51e7db02022-02-07 06:02:57 -08001367 mInfo.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001368 return; // Can't receive events if the window does not have input channel
1369 }
1370 ASSERT_NE(nullptr, mInputReceiver)
1371 << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL";
chaviwd1c23182019-12-20 18:44:56 -08001372 mInputReceiver->assertNoEvents();
Arthur Hungb92218b2018-08-14 12:00:21 +08001373 }
1374
chaviwaf87b3e2019-10-01 16:59:28 -07001375 sp<IBinder> getToken() { return mInfo.token; }
1376
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001377 const std::string& getName() { return mName; }
1378
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001379 void setOwnerInfo(int32_t ownerPid, int32_t ownerUid) {
1380 mInfo.ownerPid = ownerPid;
1381 mInfo.ownerUid = ownerUid;
1382 }
1383
Prabir Pradhanedd96402022-02-15 01:46:16 -08001384 int32_t getPid() const { return mInfo.ownerPid; }
1385
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001386 void destroyReceiver() { mInputReceiver = nullptr; }
1387
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001388 int getChannelFd() { return mInputReceiver->getChannelFd(); }
1389
chaviwd1c23182019-12-20 18:44:56 -08001390private:
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001391 const std::string mName;
chaviwd1c23182019-12-20 18:44:56 -08001392 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001393 static std::atomic<int32_t> sId; // each window gets a unique id, like in surfaceflinger
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001394};
1395
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001396std::atomic<int32_t> FakeWindowHandle::sId{1};
1397
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001398static InputEventInjectionResult injectKey(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001399 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t repeatCount,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001400 int32_t displayId = ADISPLAY_ID_NONE,
1401 InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001402 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Prabir Pradhan5735a322022-04-11 17:23:34 +00001403 bool allowKeyRepeat = true, std::optional<int32_t> targetUid = {},
1404 uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
Arthur Hungb92218b2018-08-14 12:00:21 +08001405 KeyEvent event;
1406 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1407
1408 // Define a valid key down event.
Garfield Tanfbe732e2020-01-24 11:26:14 -08001409 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId,
Harry Cutts33476232023-01-30 19:57:29 +00001410 INVALID_HMAC, action, /*flags=*/0, AKEYCODE_A, KEY_A, AMETA_NONE, repeatCount,
1411 currentTime, currentTime);
Arthur Hungb92218b2018-08-14 12:00:21 +08001412
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001413 if (!allowKeyRepeat) {
1414 policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT;
1415 }
Arthur Hungb92218b2018-08-14 12:00:21 +08001416 // Inject event until dispatch out.
Prabir Pradhan5735a322022-04-11 17:23:34 +00001417 return dispatcher->injectInputEvent(&event, targetUid, syncMode, injectionTimeout, policyFlags);
Arthur Hungb92218b2018-08-14 12:00:21 +08001418}
1419
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001420static InputEventInjectionResult injectKeyDown(const std::unique_ptr<InputDispatcher>& dispatcher,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001421 int32_t displayId = ADISPLAY_ID_NONE) {
Harry Cutts33476232023-01-30 19:57:29 +00001422 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001423}
1424
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001425// Inject a down event that has key repeat disabled. This allows InputDispatcher to idle without
1426// sending a subsequent key up. When key repeat is enabled, the dispatcher cannot idle because it
1427// has to be woken up to process the repeating key.
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001428static InputEventInjectionResult injectKeyDownNoRepeat(
1429 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t displayId = ADISPLAY_ID_NONE) {
Harry Cutts33476232023-01-30 19:57:29 +00001430 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001431 InputEventInjectionSync::WAIT_FOR_RESULT, INJECT_EVENT_TIMEOUT,
Harry Cutts33476232023-01-30 19:57:29 +00001432 /*allowKeyRepeat=*/false);
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001433}
1434
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001435static InputEventInjectionResult injectKeyUp(const std::unique_ptr<InputDispatcher>& dispatcher,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001436 int32_t displayId = ADISPLAY_ID_NONE) {
Harry Cutts33476232023-01-30 19:57:29 +00001437 return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /*repeatCount=*/0, displayId);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001438}
1439
Garfield Tandf26e862020-07-01 20:18:19 -07001440class PointerBuilder {
1441public:
1442 PointerBuilder(int32_t id, int32_t toolType) {
1443 mProperties.clear();
1444 mProperties.id = id;
1445 mProperties.toolType = toolType;
1446 mCoords.clear();
1447 }
1448
1449 PointerBuilder& x(float x) { return axis(AMOTION_EVENT_AXIS_X, x); }
1450
1451 PointerBuilder& y(float y) { return axis(AMOTION_EVENT_AXIS_Y, y); }
1452
1453 PointerBuilder& axis(int32_t axis, float value) {
1454 mCoords.setAxisValue(axis, value);
1455 return *this;
1456 }
1457
1458 PointerProperties buildProperties() const { return mProperties; }
1459
1460 PointerCoords buildCoords() const { return mCoords; }
1461
1462private:
1463 PointerProperties mProperties;
1464 PointerCoords mCoords;
1465};
1466
1467class MotionEventBuilder {
1468public:
1469 MotionEventBuilder(int32_t action, int32_t source) {
1470 mAction = action;
1471 mSource = source;
1472 mEventTime = systemTime(SYSTEM_TIME_MONOTONIC);
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08001473 mDownTime = mEventTime;
Garfield Tandf26e862020-07-01 20:18:19 -07001474 }
1475
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08001476 MotionEventBuilder& deviceId(int32_t deviceId) {
1477 mDeviceId = deviceId;
1478 return *this;
1479 }
1480
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08001481 MotionEventBuilder& downTime(nsecs_t downTime) {
1482 mDownTime = downTime;
1483 return *this;
1484 }
1485
Garfield Tandf26e862020-07-01 20:18:19 -07001486 MotionEventBuilder& eventTime(nsecs_t eventTime) {
1487 mEventTime = eventTime;
1488 return *this;
1489 }
1490
1491 MotionEventBuilder& displayId(int32_t displayId) {
1492 mDisplayId = displayId;
1493 return *this;
1494 }
1495
1496 MotionEventBuilder& actionButton(int32_t actionButton) {
1497 mActionButton = actionButton;
1498 return *this;
1499 }
1500
arthurhung6d4bed92021-03-17 11:59:33 +08001501 MotionEventBuilder& buttonState(int32_t buttonState) {
1502 mButtonState = buttonState;
Garfield Tandf26e862020-07-01 20:18:19 -07001503 return *this;
1504 }
1505
1506 MotionEventBuilder& rawXCursorPosition(float rawXCursorPosition) {
1507 mRawXCursorPosition = rawXCursorPosition;
1508 return *this;
1509 }
1510
1511 MotionEventBuilder& rawYCursorPosition(float rawYCursorPosition) {
1512 mRawYCursorPosition = rawYCursorPosition;
1513 return *this;
1514 }
1515
1516 MotionEventBuilder& pointer(PointerBuilder pointer) {
1517 mPointers.push_back(pointer);
1518 return *this;
1519 }
1520
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08001521 MotionEventBuilder& addFlag(uint32_t flags) {
1522 mFlags |= flags;
1523 return *this;
1524 }
1525
Garfield Tandf26e862020-07-01 20:18:19 -07001526 MotionEvent build() {
1527 std::vector<PointerProperties> pointerProperties;
1528 std::vector<PointerCoords> pointerCoords;
1529 for (const PointerBuilder& pointer : mPointers) {
1530 pointerProperties.push_back(pointer.buildProperties());
1531 pointerCoords.push_back(pointer.buildCoords());
1532 }
1533
1534 // Set mouse cursor position for the most common cases to avoid boilerplate.
1535 if (mSource == AINPUT_SOURCE_MOUSE &&
1536 !MotionEvent::isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition) &&
1537 mPointers.size() == 1) {
1538 mRawXCursorPosition = pointerCoords[0].getX();
1539 mRawYCursorPosition = pointerCoords[0].getY();
1540 }
1541
1542 MotionEvent event;
chaviw9eaa22c2020-07-01 16:21:27 -07001543 ui::Transform identityTransform;
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08001544 event.initialize(InputEvent::nextId(), mDeviceId, mSource, mDisplayId, INVALID_HMAC,
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08001545 mAction, mActionButton, mFlags, /* edgeFlags */ 0, AMETA_NONE,
chaviw9eaa22c2020-07-01 16:21:27 -07001546 mButtonState, MotionClassification::NONE, identityTransform,
1547 /* xPrecision */ 0, /* yPrecision */ 0, mRawXCursorPosition,
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08001548 mRawYCursorPosition, identityTransform, mDownTime, mEventTime,
Prabir Pradhanb9b18502021-08-26 12:30:32 -07001549 mPointers.size(), pointerProperties.data(), pointerCoords.data());
Garfield Tandf26e862020-07-01 20:18:19 -07001550
1551 return event;
1552 }
1553
1554private:
1555 int32_t mAction;
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08001556 int32_t mDeviceId = DEVICE_ID;
Garfield Tandf26e862020-07-01 20:18:19 -07001557 int32_t mSource;
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08001558 nsecs_t mDownTime;
Garfield Tandf26e862020-07-01 20:18:19 -07001559 nsecs_t mEventTime;
1560 int32_t mDisplayId{ADISPLAY_ID_DEFAULT};
1561 int32_t mActionButton{0};
1562 int32_t mButtonState{0};
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08001563 int32_t mFlags{0};
Garfield Tandf26e862020-07-01 20:18:19 -07001564 float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
1565 float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
1566
1567 std::vector<PointerBuilder> mPointers;
1568};
1569
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08001570class MotionArgsBuilder {
1571public:
1572 MotionArgsBuilder(int32_t action, int32_t source) {
1573 mAction = action;
1574 mSource = source;
1575 mEventTime = systemTime(SYSTEM_TIME_MONOTONIC);
1576 mDownTime = mEventTime;
1577 }
1578
1579 MotionArgsBuilder& deviceId(int32_t deviceId) {
1580 mDeviceId = deviceId;
1581 return *this;
1582 }
1583
1584 MotionArgsBuilder& downTime(nsecs_t downTime) {
1585 mDownTime = downTime;
1586 return *this;
1587 }
1588
1589 MotionArgsBuilder& eventTime(nsecs_t eventTime) {
1590 mEventTime = eventTime;
1591 return *this;
1592 }
1593
1594 MotionArgsBuilder& displayId(int32_t displayId) {
1595 mDisplayId = displayId;
1596 return *this;
1597 }
1598
1599 MotionArgsBuilder& policyFlags(int32_t policyFlags) {
1600 mPolicyFlags = policyFlags;
1601 return *this;
1602 }
1603
1604 MotionArgsBuilder& actionButton(int32_t actionButton) {
1605 mActionButton = actionButton;
1606 return *this;
1607 }
1608
1609 MotionArgsBuilder& buttonState(int32_t buttonState) {
1610 mButtonState = buttonState;
1611 return *this;
1612 }
1613
1614 MotionArgsBuilder& rawXCursorPosition(float rawXCursorPosition) {
1615 mRawXCursorPosition = rawXCursorPosition;
1616 return *this;
1617 }
1618
1619 MotionArgsBuilder& rawYCursorPosition(float rawYCursorPosition) {
1620 mRawYCursorPosition = rawYCursorPosition;
1621 return *this;
1622 }
1623
1624 MotionArgsBuilder& pointer(PointerBuilder pointer) {
1625 mPointers.push_back(pointer);
1626 return *this;
1627 }
1628
1629 MotionArgsBuilder& addFlag(uint32_t flags) {
1630 mFlags |= flags;
1631 return *this;
1632 }
1633
1634 NotifyMotionArgs build() {
1635 std::vector<PointerProperties> pointerProperties;
1636 std::vector<PointerCoords> pointerCoords;
1637 for (const PointerBuilder& pointer : mPointers) {
1638 pointerProperties.push_back(pointer.buildProperties());
1639 pointerCoords.push_back(pointer.buildCoords());
1640 }
1641
1642 // Set mouse cursor position for the most common cases to avoid boilerplate.
1643 if (mSource == AINPUT_SOURCE_MOUSE &&
1644 !MotionEvent::isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition) &&
1645 mPointers.size() == 1) {
1646 mRawXCursorPosition = pointerCoords[0].getX();
1647 mRawYCursorPosition = pointerCoords[0].getY();
1648 }
1649
1650 NotifyMotionArgs args(InputEvent::nextId(), mEventTime, /*readTime=*/mEventTime, mDeviceId,
1651 mSource, mDisplayId, mPolicyFlags, mAction, mActionButton, mFlags,
1652 AMETA_NONE, mButtonState, MotionClassification::NONE, /*edgeFlags=*/0,
1653 mPointers.size(), pointerProperties.data(), pointerCoords.data(),
1654 /*xPrecision=*/0, /*yPrecision=*/0, mRawXCursorPosition,
1655 mRawYCursorPosition, mDownTime, /*videoFrames=*/{});
1656
1657 return args;
1658 }
1659
1660private:
1661 int32_t mAction;
1662 int32_t mDeviceId = DEVICE_ID;
1663 int32_t mSource;
1664 nsecs_t mDownTime;
1665 nsecs_t mEventTime;
1666 int32_t mDisplayId{ADISPLAY_ID_DEFAULT};
1667 int32_t mPolicyFlags = DEFAULT_POLICY_FLAGS;
1668 int32_t mActionButton{0};
1669 int32_t mButtonState{0};
1670 int32_t mFlags{0};
1671 float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
1672 float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
1673
1674 std::vector<PointerBuilder> mPointers;
1675};
1676
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001677static InputEventInjectionResult injectMotionEvent(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001678 const std::unique_ptr<InputDispatcher>& dispatcher, const MotionEvent& event,
Garfield Tandf26e862020-07-01 20:18:19 -07001679 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Prabir Pradhan5735a322022-04-11 17:23:34 +00001680 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT,
1681 std::optional<int32_t> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
1682 return dispatcher->injectInputEvent(&event, targetUid, injectionMode, injectionTimeout,
1683 policyFlags);
Garfield Tandf26e862020-07-01 20:18:19 -07001684}
1685
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001686static InputEventInjectionResult injectMotionEvent(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001687 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t source,
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001688 int32_t displayId, const PointF& position = {100, 200},
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001689 const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001690 AMOTION_EVENT_INVALID_CURSOR_POSITION},
1691 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001692 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Prabir Pradhan5735a322022-04-11 17:23:34 +00001693 nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC),
1694 std::optional<int32_t> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
Garfield Tandf26e862020-07-01 20:18:19 -07001695 MotionEvent event = MotionEventBuilder(action, source)
1696 .displayId(displayId)
1697 .eventTime(eventTime)
1698 .rawXCursorPosition(cursorPosition.x)
1699 .rawYCursorPosition(cursorPosition.y)
Harry Cutts33476232023-01-30 19:57:29 +00001700 .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER)
Garfield Tandf26e862020-07-01 20:18:19 -07001701 .x(position.x)
1702 .y(position.y))
1703 .build();
Arthur Hungb92218b2018-08-14 12:00:21 +08001704
1705 // Inject event until dispatch out.
Prabir Pradhan5735a322022-04-11 17:23:34 +00001706 return injectMotionEvent(dispatcher, event, injectionTimeout, injectionMode, targetUid,
1707 policyFlags);
Arthur Hungb92218b2018-08-14 12:00:21 +08001708}
1709
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001710static InputEventInjectionResult injectMotionDown(
1711 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t source, int32_t displayId,
1712 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001713 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location);
Garfield Tan00f511d2019-06-12 16:55:40 -07001714}
1715
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001716static InputEventInjectionResult injectMotionUp(const std::unique_ptr<InputDispatcher>& dispatcher,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001717 int32_t source, int32_t displayId,
1718 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001719 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location);
Michael Wright3a240c42019-12-10 20:53:41 +00001720}
1721
Jackal Guof9696682018-10-05 12:23:23 +08001722static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) {
1723 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1724 // Define a valid key event.
Harry Cutts33476232023-01-30 19:57:29 +00001725 NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001726 displayId, POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0, AKEYCODE_A,
1727 KEY_A, AMETA_NONE, currentTime);
Jackal Guof9696682018-10-05 12:23:23 +08001728
1729 return args;
1730}
1731
chaviwd1c23182019-12-20 18:44:56 -08001732static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId,
1733 const std::vector<PointF>& points) {
1734 size_t pointerCount = points.size();
chaviwaf87b3e2019-10-01 16:59:28 -07001735 if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) {
1736 EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer";
1737 }
1738
chaviwd1c23182019-12-20 18:44:56 -08001739 PointerProperties pointerProperties[pointerCount];
1740 PointerCoords pointerCoords[pointerCount];
Jackal Guof9696682018-10-05 12:23:23 +08001741
chaviwd1c23182019-12-20 18:44:56 -08001742 for (size_t i = 0; i < pointerCount; i++) {
1743 pointerProperties[i].clear();
1744 pointerProperties[i].id = i;
1745 pointerProperties[i].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jackal Guof9696682018-10-05 12:23:23 +08001746
chaviwd1c23182019-12-20 18:44:56 -08001747 pointerCoords[i].clear();
1748 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x);
1749 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y);
1750 }
Jackal Guof9696682018-10-05 12:23:23 +08001751
1752 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1753 // Define a valid motion event.
Harry Cutts33476232023-01-30 19:57:29 +00001754 NotifyMotionArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, source, displayId,
Garfield Tan00f511d2019-06-12 16:55:40 -07001755 POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0,
1756 AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
chaviwd1c23182019-12-20 18:44:56 -08001757 AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties,
1758 pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0,
Garfield Tan00f511d2019-06-12 16:55:40 -07001759 AMOTION_EVENT_INVALID_CURSOR_POSITION,
1760 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {});
Jackal Guof9696682018-10-05 12:23:23 +08001761
1762 return args;
1763}
1764
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08001765static NotifyMotionArgs generateTouchArgs(int32_t action, const std::vector<PointF>& points) {
1766 return generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, points);
1767}
1768
chaviwd1c23182019-12-20 18:44:56 -08001769static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) {
1770 return generateMotionArgs(action, source, displayId, {PointF{100, 200}});
1771}
1772
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001773static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs(
1774 const PointerCaptureRequest& request) {
Harry Cutts33476232023-01-30 19:57:29 +00001775 return NotifyPointerCaptureChangedArgs(/*id=*/0, systemTime(SYSTEM_TIME_MONOTONIC), request);
Prabir Pradhan99987712020-11-10 18:43:05 -08001776}
1777
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001778/**
1779 * When a window unexpectedly disposes of its input channel, policy should be notified about the
1780 * broken channel.
1781 */
1782TEST_F(InputDispatcherTest, WhenInputChannelBreaks_PolicyIsNotified) {
1783 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1784 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001785 sp<FakeWindowHandle>::make(application, mDispatcher,
1786 "Window that breaks its input channel", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001787
1788 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1789
1790 // Window closes its channel, but the window remains.
1791 window->destroyReceiver();
1792 mFakePolicy->assertNotifyInputChannelBrokenWasCalled(window->getInfo()->token);
1793}
1794
Arthur Hungb92218b2018-08-14 12:00:21 +08001795TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001796 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001797 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1798 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001799
Arthur Hung72d8dc32020-03-28 00:48:39 +00001800 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001801 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1802 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1803 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001804
1805 // Window should receive motion event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001806 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001807}
1808
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001809TEST_F(InputDispatcherTest, WhenDisplayNotSpecified_InjectMotionToDefaultDisplay) {
1810 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001811 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1812 "Fake Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001813
1814 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1815 // Inject a MotionEvent to an unknown display.
1816 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1817 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_NONE))
1818 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1819
1820 // Window should receive motion event.
1821 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1822}
1823
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001824/**
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08001825 * Calling setInputWindows once should not cause any issues.
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001826 * This test serves as a sanity check for the next test, where setInputWindows is
1827 * called twice.
1828 */
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08001829TEST_F(InputDispatcherTest, SetInputWindowOnceWithSingleTouchWindow) {
Chris Yea209fde2020-07-22 13:54:51 -07001830 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001831 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1832 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001833 window->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001834
1835 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001836 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001837 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1838 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001839 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001840
1841 // Window should receive motion event.
1842 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1843}
1844
1845/**
1846 * Calling setInputWindows twice, with the same info, should not cause any issues.
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001847 */
1848TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001849 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001850 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1851 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001852 window->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001853
1854 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1855 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001856 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001857 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1858 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001859 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001860
1861 // Window should receive motion event.
1862 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1863}
1864
Arthur Hungb92218b2018-08-14 12:00:21 +08001865// The foreground window should receive the first touch down event.
1866TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001867 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001868 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001869 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001870 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001871 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001872
Arthur Hung72d8dc32020-03-28 00:48:39 +00001873 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001874 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1875 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1876 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001877
1878 // Top window should receive the touch down event. Second window should not receive anything.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001879 windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001880 windowSecond->assertNoEvents();
1881}
1882
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001883/**
1884 * Two windows: A top window, and a wallpaper behind the window.
1885 * Touch goes to the top window, and then top window disappears. Ensure that wallpaper window
1886 * gets ACTION_CANCEL.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001887 * 1. foregroundWindow <-- dup touch to wallpaper
1888 * 2. wallpaperWindow <-- is wallpaper
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001889 */
1890TEST_F(InputDispatcherTest, WhenForegroundWindowDisappears_WallpaperTouchIsCanceled) {
1891 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1892 sp<FakeWindowHandle> foregroundWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001893 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001894 foregroundWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001895 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001896 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001897 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001898
1899 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}});
1900 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1901 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1902 {100, 200}))
1903 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1904
1905 // Both foreground window and its wallpaper should receive the touch down
1906 foregroundWindow->consumeMotionDown();
1907 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1908
1909 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1910 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
1911 ADISPLAY_ID_DEFAULT, {110, 200}))
1912 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1913
1914 foregroundWindow->consumeMotionMove();
1915 wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1916
1917 // Now the foreground window goes away, but the wallpaper stays
1918 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}});
1919 foregroundWindow->consumeMotionCancel();
1920 // Since the "parent" window of the wallpaper is gone, wallpaper should receive cancel, too.
1921 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1922}
1923
1924/**
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001925 * Same test as WhenForegroundWindowDisappears_WallpaperTouchIsCanceled above,
1926 * with the following differences:
1927 * After ACTION_DOWN, Wallpaper window hangs up its channel, which forces the dispatcher to
1928 * clean up the connection.
1929 * This later may crash dispatcher during ACTION_CANCEL synthesis, if the dispatcher is not careful.
1930 * Ensure that there's no crash in the dispatcher.
1931 */
1932TEST_F(InputDispatcherTest, WhenWallpaperDisappears_NoCrash) {
1933 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1934 sp<FakeWindowHandle> foregroundWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001935 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001936 foregroundWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001937 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001938 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001939 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001940
1941 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}});
1942 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1943 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1944 {100, 200}))
1945 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1946
1947 // Both foreground window and its wallpaper should receive the touch down
1948 foregroundWindow->consumeMotionDown();
1949 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1950
1951 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1952 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
1953 ADISPLAY_ID_DEFAULT, {110, 200}))
1954 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1955
1956 foregroundWindow->consumeMotionMove();
1957 wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1958
1959 // Wallpaper closes its channel, but the window remains.
1960 wallpaperWindow->destroyReceiver();
1961 mFakePolicy->assertNotifyInputChannelBrokenWasCalled(wallpaperWindow->getInfo()->token);
1962
1963 // Now the foreground window goes away, but the wallpaper stays, even though its channel
1964 // is no longer valid.
1965 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}});
1966 foregroundWindow->consumeMotionCancel();
1967}
1968
Arthur Hungc539dbb2022-12-08 07:45:36 +00001969class ShouldSplitTouchFixture : public InputDispatcherTest,
1970 public ::testing::WithParamInterface<bool> {};
1971INSTANTIATE_TEST_SUITE_P(InputDispatcherTest, ShouldSplitTouchFixture,
1972 ::testing::Values(true, false));
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001973/**
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001974 * A single window that receives touch (on top), and a wallpaper window underneath it.
1975 * The top window gets a multitouch gesture.
1976 * Ensure that wallpaper gets the same gesture.
1977 */
Arthur Hungc539dbb2022-12-08 07:45:36 +00001978TEST_P(ShouldSplitTouchFixture, WallpaperWindowReceivesMultiTouch) {
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001979 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Arthur Hungc539dbb2022-12-08 07:45:36 +00001980 sp<FakeWindowHandle> foregroundWindow =
1981 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
1982 foregroundWindow->setDupTouchToWallpaper(true);
1983 foregroundWindow->setPreventSplitting(GetParam());
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001984
1985 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001986 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001987 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001988
Arthur Hungc539dbb2022-12-08 07:45:36 +00001989 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}});
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001990
1991 // Touch down on top window
1992 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1993 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1994 {100, 100}))
1995 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1996
1997 // Both top window and its wallpaper should receive the touch down
Arthur Hungc539dbb2022-12-08 07:45:36 +00001998 foregroundWindow->consumeMotionDown();
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001999 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2000
2001 // Second finger down on the top window
2002 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002003 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002004 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Harry Cutts33476232023-01-30 19:57:29 +00002005 .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(100).y(100))
2006 .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(150))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002007 .build();
2008 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2009 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
2010 InputEventInjectionSync::WAIT_FOR_RESULT))
2011 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2012
Harry Cutts33476232023-01-30 19:57:29 +00002013 foregroundWindow->consumeMotionPointerDown(/*pointerIndex=*/1);
2014 wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002015 expectedWallpaperFlags);
Arthur Hungc539dbb2022-12-08 07:45:36 +00002016
2017 const MotionEvent secondFingerUpEvent =
2018 MotionEventBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
2019 .displayId(ADISPLAY_ID_DEFAULT)
2020 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Harry Cutts33476232023-01-30 19:57:29 +00002021 .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(100).y(100))
2022 .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(150))
Arthur Hungc539dbb2022-12-08 07:45:36 +00002023 .build();
2024 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2025 injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT,
2026 InputEventInjectionSync::WAIT_FOR_RESULT))
2027 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2028 foregroundWindow->consumeMotionPointerUp(0);
2029 wallpaperWindow->consumeMotionPointerUp(0, ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2030
2031 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2032 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2033 {100, 100}))
2034 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2035 foregroundWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
2036 wallpaperWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002037}
2038
2039/**
2040 * Two windows: a window on the left and window on the right.
2041 * A third window, wallpaper, is behind both windows, and spans both top windows.
2042 * The first touch down goes to the left window. A second pointer touches down on the right window.
2043 * The touch is split, so both left and right windows should receive ACTION_DOWN.
2044 * The wallpaper will get the full event, so it should receive ACTION_DOWN followed by
2045 * ACTION_POINTER_DOWN(1).
2046 */
2047TEST_F(InputDispatcherTest, TwoWindows_SplitWallpaperTouch) {
2048 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2049 sp<FakeWindowHandle> leftWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002050 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002051 leftWindow->setFrame(Rect(0, 0, 200, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002052 leftWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002053
2054 sp<FakeWindowHandle> rightWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002055 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002056 rightWindow->setFrame(Rect(200, 0, 400, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002057 rightWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002058
2059 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002060 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002061 wallpaperWindow->setFrame(Rect(0, 0, 400, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002062 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002063
2064 mDispatcher->setInputWindows(
2065 {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}});
2066
2067 // Touch down on left window
2068 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2069 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2070 {100, 100}))
2071 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2072
2073 // Both foreground window and its wallpaper should receive the touch down
2074 leftWindow->consumeMotionDown();
2075 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2076
2077 // Second finger down on the right window
2078 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002079 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002080 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Harry Cutts33476232023-01-30 19:57:29 +00002081 .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(100).y(100))
2082 .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(300).y(100))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002083 .build();
2084 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2085 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
2086 InputEventInjectionSync::WAIT_FOR_RESULT))
2087 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2088
2089 leftWindow->consumeMotionMove();
2090 // Since the touch is split, right window gets ACTION_DOWN
2091 rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Harry Cutts33476232023-01-30 19:57:29 +00002092 wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002093 expectedWallpaperFlags);
2094
2095 // Now, leftWindow, which received the first finger, disappears.
2096 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {rightWindow, wallpaperWindow}}});
2097 leftWindow->consumeMotionCancel();
2098 // Since a "parent" window of the wallpaper is gone, wallpaper should receive cancel, too.
2099 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2100
2101 // The pointer that's still down on the right window moves, and goes to the right window only.
2102 // As far as the dispatcher's concerned though, both pointers are still present.
2103 const MotionEvent secondFingerMoveEvent =
2104 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2105 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Harry Cutts33476232023-01-30 19:57:29 +00002106 .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(100).y(100))
2107 .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(310).y(110))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002108 .build();
2109 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2110 injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT,
2111 InputEventInjectionSync::WAIT_FOR_RESULT));
2112 rightWindow->consumeMotionMove();
2113
2114 leftWindow->assertNoEvents();
2115 rightWindow->assertNoEvents();
2116 wallpaperWindow->assertNoEvents();
2117}
2118
Arthur Hungc539dbb2022-12-08 07:45:36 +00002119/**
2120 * Two windows: a window on the left with dup touch to wallpaper and window on the right without it.
2121 * The touch slips to the right window. so left window and wallpaper should receive ACTION_CANCEL
2122 * The right window should receive ACTION_DOWN.
2123 */
2124TEST_F(InputDispatcherTest, WallpaperWindowWhenSlippery) {
Arthur Hung74c248d2022-11-23 07:09:59 +00002125 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Arthur Hungc539dbb2022-12-08 07:45:36 +00002126 sp<FakeWindowHandle> leftWindow =
2127 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2128 leftWindow->setFrame(Rect(0, 0, 200, 200));
2129 leftWindow->setDupTouchToWallpaper(true);
2130 leftWindow->setSlippery(true);
2131
2132 sp<FakeWindowHandle> rightWindow =
2133 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2134 rightWindow->setFrame(Rect(200, 0, 400, 200));
Arthur Hung74c248d2022-11-23 07:09:59 +00002135
2136 sp<FakeWindowHandle> wallpaperWindow =
2137 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
2138 wallpaperWindow->setIsWallpaper(true);
Arthur Hung74c248d2022-11-23 07:09:59 +00002139
Arthur Hungc539dbb2022-12-08 07:45:36 +00002140 mDispatcher->setInputWindows(
2141 {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}});
Arthur Hung74c248d2022-11-23 07:09:59 +00002142
Arthur Hungc539dbb2022-12-08 07:45:36 +00002143 // Touch down on left window
Arthur Hung74c248d2022-11-23 07:09:59 +00002144 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2145 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Arthur Hungc539dbb2022-12-08 07:45:36 +00002146 {100, 100}))
Arthur Hung74c248d2022-11-23 07:09:59 +00002147 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungc539dbb2022-12-08 07:45:36 +00002148
2149 // Both foreground window and its wallpaper should receive the touch down
2150 leftWindow->consumeMotionDown();
Arthur Hung74c248d2022-11-23 07:09:59 +00002151 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2152
Arthur Hungc539dbb2022-12-08 07:45:36 +00002153 // Move to right window, the left window should receive cancel.
Arthur Hung74c248d2022-11-23 07:09:59 +00002154 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Arthur Hungc539dbb2022-12-08 07:45:36 +00002155 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
2156 ADISPLAY_ID_DEFAULT, {201, 100}))
Arthur Hung74c248d2022-11-23 07:09:59 +00002157 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2158
Arthur Hungc539dbb2022-12-08 07:45:36 +00002159 leftWindow->consumeMotionCancel();
2160 rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2161 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Arthur Hung74c248d2022-11-23 07:09:59 +00002162}
2163
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002164/**
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002165 * The policy typically sets POLICY_FLAG_PASS_TO_USER to the events. But when the display is not
2166 * interactive, it might stop sending this flag.
2167 * In this test, we check that if the policy stops sending this flag mid-gesture, we still ensure
2168 * to have a consistent input stream.
2169 *
2170 * Test procedure:
2171 * DOWN -> POINTER_DOWN -> (stop sending POLICY_FLAG_PASS_TO_USER) -> CANCEL.
2172 * DOWN (new gesture).
2173 *
2174 * In the bad implementation, we could potentially drop the CANCEL event, and get an inconsistent
2175 * state in the dispatcher. This would cause the final DOWN event to not be delivered to the app.
2176 *
2177 * We technically just need a single window here, but we are using two windows (spy on top and a
2178 * regular window below) to emulate the actual situation where it happens on the device.
2179 */
2180TEST_F(InputDispatcherTest, TwoPointerCancelInconsistentPolicy) {
2181 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2182 sp<FakeWindowHandle> spyWindow =
2183 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
2184 spyWindow->setFrame(Rect(0, 0, 200, 200));
2185 spyWindow->setTrustedOverlay(true);
2186 spyWindow->setSpy(true);
2187
2188 sp<FakeWindowHandle> window =
2189 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2190 window->setFrame(Rect(0, 0, 200, 200));
2191
2192 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
2193 const int32_t touchDeviceId = 4;
2194 NotifyMotionArgs args;
2195
2196 // Two pointers down
2197 mDispatcher->notifyMotion(&(
2198 args = MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2199 .deviceId(touchDeviceId)
2200 .policyFlags(DEFAULT_POLICY_FLAGS)
2201 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(100).y(100))
2202 .build()));
2203
2204 mDispatcher->notifyMotion(&(
2205 args = MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2206 .deviceId(touchDeviceId)
2207 .policyFlags(DEFAULT_POLICY_FLAGS)
2208 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(100).y(100))
2209 .pointer(PointerBuilder(1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(120).y(120))
2210 .build()));
2211 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2212 spyWindow->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
2213 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2214 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
2215
2216 // Cancel the current gesture. Send the cancel without the default policy flags.
2217 mDispatcher->notifyMotion(&(
2218 args = MotionArgsBuilder(AMOTION_EVENT_ACTION_CANCEL, AINPUT_SOURCE_TOUCHSCREEN)
2219 .deviceId(touchDeviceId)
2220 .policyFlags(0)
2221 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(100).y(100))
2222 .pointer(PointerBuilder(1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(120).y(120))
2223 .build()));
2224 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL));
2225 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL));
2226
2227 // We don't need to reset the device to reproduce the issue, but the reset event typically
2228 // follows, so we keep it here to model the actual listener behaviour more closely.
2229 NotifyDeviceResetArgs resetArgs;
2230 resetArgs.id = 1; // arbitrary id
2231 resetArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
2232 resetArgs.deviceId = touchDeviceId;
2233 mDispatcher->notifyDeviceReset(&resetArgs);
2234
2235 // Start new gesture
2236 mDispatcher->notifyMotion(&(
2237 args = MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2238 .deviceId(touchDeviceId)
2239 .policyFlags(DEFAULT_POLICY_FLAGS)
2240 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(100).y(100))
2241 .build()));
2242 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2243 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2244
2245 // No more events
2246 spyWindow->assertNoEvents();
2247 window->assertNoEvents();
2248}
2249
2250/**
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002251 * Two windows: a window on the left and a window on the right.
2252 * Mouse is hovered from the right window into the left window.
2253 * Next, we tap on the left window, where the cursor was last seen.
2254 * The second tap is done onto the right window.
2255 * The mouse and tap are from two different devices.
2256 * We technically don't need to set the downtime / eventtime for these events, but setting these
2257 * explicitly helps during debugging.
2258 * This test reproduces a crash where there is a mismatch between the downTime and eventTime.
2259 * In the buggy implementation, a tap on the right window would cause a crash.
2260 */
2261TEST_F(InputDispatcherTest, HoverFromLeftToRightAndTap) {
2262 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2263 sp<FakeWindowHandle> leftWindow =
2264 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2265 leftWindow->setFrame(Rect(0, 0, 200, 200));
2266
2267 sp<FakeWindowHandle> rightWindow =
2268 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2269 rightWindow->setFrame(Rect(200, 0, 400, 200));
2270
2271 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}});
2272 // All times need to start at the current time, otherwise the dispatcher will drop the events as
2273 // stale.
2274 const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC);
2275 const int32_t mouseDeviceId = 6;
2276 const int32_t touchDeviceId = 4;
2277 // Move the cursor from right
2278 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2279 injectMotionEvent(mDispatcher,
2280 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2281 AINPUT_SOURCE_MOUSE)
2282 .deviceId(mouseDeviceId)
2283 .downTime(baseTime + 10)
2284 .eventTime(baseTime + 20)
2285 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2286 .x(300)
2287 .y(100))
2288 .build()));
2289 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2290
2291 // .. to the left window
2292 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2293 injectMotionEvent(mDispatcher,
2294 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2295 AINPUT_SOURCE_MOUSE)
2296 .deviceId(mouseDeviceId)
2297 .downTime(baseTime + 10)
2298 .eventTime(baseTime + 30)
2299 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2300 .x(110)
2301 .y(100))
2302 .build()));
2303 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2304 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2305 // Now tap the left window
2306 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2307 injectMotionEvent(mDispatcher,
2308 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2309 AINPUT_SOURCE_TOUCHSCREEN)
2310 .deviceId(touchDeviceId)
2311 .downTime(baseTime + 40)
2312 .eventTime(baseTime + 40)
2313 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER)
2314 .x(100)
2315 .y(100))
2316 .build()));
2317 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2318 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2319
2320 // release tap
2321 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2322 injectMotionEvent(mDispatcher,
2323 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2324 AINPUT_SOURCE_TOUCHSCREEN)
2325 .deviceId(touchDeviceId)
2326 .downTime(baseTime + 40)
2327 .eventTime(baseTime + 50)
2328 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER)
2329 .x(100)
2330 .y(100))
2331 .build()));
2332 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2333
2334 // Tap the window on the right
2335 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2336 injectMotionEvent(mDispatcher,
2337 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2338 AINPUT_SOURCE_TOUCHSCREEN)
2339 .deviceId(touchDeviceId)
2340 .downTime(baseTime + 60)
2341 .eventTime(baseTime + 60)
2342 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER)
2343 .x(300)
2344 .y(100))
2345 .build()));
2346 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2347
2348 // release tap
2349 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2350 injectMotionEvent(mDispatcher,
2351 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2352 AINPUT_SOURCE_TOUCHSCREEN)
2353 .deviceId(touchDeviceId)
2354 .downTime(baseTime + 60)
2355 .eventTime(baseTime + 70)
2356 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER)
2357 .x(300)
2358 .y(100))
2359 .build()));
2360 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2361
2362 // No more events
2363 leftWindow->assertNoEvents();
2364 rightWindow->assertNoEvents();
2365}
2366
2367/**
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002368 * On the display, have a single window, and also an area where there's no window.
2369 * First pointer touches the "no window" area of the screen. Second pointer touches the window.
2370 * Make sure that the window receives the second pointer, and first pointer is simply ignored.
2371 */
2372TEST_F(InputDispatcherTest, SplitWorksWhenEmptyAreaIsTouched) {
2373 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2374 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002375 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002376
2377 mDispatcher->setInputWindows({{DISPLAY_ID, {window}}});
2378 NotifyMotionArgs args;
2379
2380 // Touch down on the empty space
2381 mDispatcher->notifyMotion(&(args = generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{-1, -1}})));
2382
2383 mDispatcher->waitForIdle();
2384 window->assertNoEvents();
2385
2386 // Now touch down on the window with another pointer
2387 mDispatcher->notifyMotion(&(args = generateTouchArgs(POINTER_1_DOWN, {{-1, -1}, {10, 10}})));
2388 mDispatcher->waitForIdle();
2389 window->consumeMotionDown();
2390}
2391
2392/**
2393 * Same test as above, but instead of touching the empty space, the first touch goes to
2394 * non-touchable window.
2395 */
2396TEST_F(InputDispatcherTest, SplitWorksWhenNonTouchableWindowIsTouched) {
2397 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2398 sp<FakeWindowHandle> window1 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002399 sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002400 window1->setTouchableRegion(Region{{0, 0, 100, 100}});
2401 window1->setTouchable(false);
2402 sp<FakeWindowHandle> window2 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002403 sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002404 window2->setTouchableRegion(Region{{100, 0, 200, 100}});
2405
2406 mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}});
2407
2408 NotifyMotionArgs args;
2409 // Touch down on the non-touchable window
2410 mDispatcher->notifyMotion(&(args = generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}})));
2411
2412 mDispatcher->waitForIdle();
2413 window1->assertNoEvents();
2414 window2->assertNoEvents();
2415
2416 // Now touch down on the window with another pointer
2417 mDispatcher->notifyMotion(&(args = generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}})));
2418 mDispatcher->waitForIdle();
2419 window2->consumeMotionDown();
2420}
2421
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002422/**
2423 * When splitting touch events the downTime should be adjusted such that the downTime corresponds
2424 * to the event time of the first ACTION_DOWN sent to the particular window.
2425 */
2426TEST_F(InputDispatcherTest, SplitTouchesSendCorrectActionDownTime) {
2427 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2428 sp<FakeWindowHandle> window1 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002429 sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002430 window1->setTouchableRegion(Region{{0, 0, 100, 100}});
2431 sp<FakeWindowHandle> window2 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002432 sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002433 window2->setTouchableRegion(Region{{100, 0, 200, 100}});
2434
2435 mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}});
2436
2437 NotifyMotionArgs args;
2438 // Touch down on the first window
2439 mDispatcher->notifyMotion(&(args = generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}})));
2440
2441 mDispatcher->waitForIdle();
2442 InputEvent* inputEvent1 = window1->consume();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002443 ASSERT_NE(inputEvent1, nullptr);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002444 window2->assertNoEvents();
2445 MotionEvent& motionEvent1 = static_cast<MotionEvent&>(*inputEvent1);
2446 nsecs_t downTimeForWindow1 = motionEvent1.getDownTime();
2447 ASSERT_EQ(motionEvent1.getDownTime(), motionEvent1.getEventTime());
2448
2449 // Now touch down on the window with another pointer
2450 mDispatcher->notifyMotion(&(args = generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}})));
2451 mDispatcher->waitForIdle();
2452 InputEvent* inputEvent2 = window2->consume();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002453 ASSERT_NE(inputEvent2, nullptr);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002454 MotionEvent& motionEvent2 = static_cast<MotionEvent&>(*inputEvent2);
2455 nsecs_t downTimeForWindow2 = motionEvent2.getDownTime();
2456 ASSERT_NE(downTimeForWindow1, downTimeForWindow2);
2457 ASSERT_EQ(motionEvent2.getDownTime(), motionEvent2.getEventTime());
2458
2459 // Now move the pointer on the second window
2460 mDispatcher->notifyMotion(
2461 &(args = generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{50, 50}, {151, 51}})));
2462 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002463 window2->consumeMotionEvent(WithDownTime(downTimeForWindow2));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002464
2465 // Now add new touch down on the second window
2466 mDispatcher->notifyMotion(
2467 &(args = generateTouchArgs(POINTER_2_DOWN, {{50, 50}, {151, 51}, {150, 50}})));
2468 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002469 window2->consumeMotionEvent(WithDownTime(downTimeForWindow2));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002470
2471 // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line
2472 window1->consumeMotionMove();
2473 window1->assertNoEvents();
2474
2475 // Now move the pointer on the first window
2476 mDispatcher->notifyMotion(
2477 &(args = generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{51, 51}, {151, 51}})));
2478 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002479 window1->consumeMotionEvent(WithDownTime(downTimeForWindow1));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002480
2481 mDispatcher->notifyMotion(&(
2482 args = generateTouchArgs(POINTER_3_DOWN, {{51, 51}, {151, 51}, {150, 50}, {50, 50}})));
2483 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002484 window1->consumeMotionEvent(WithDownTime(downTimeForWindow1));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002485}
2486
Garfield Tandf26e862020-07-01 20:18:19 -07002487TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) {
Chris Yea209fde2020-07-22 13:54:51 -07002488 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07002489 sp<FakeWindowHandle> windowLeft =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002490 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07002491 windowLeft->setFrame(Rect(0, 0, 600, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07002492 sp<FakeWindowHandle> windowRight =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002493 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07002494 windowRight->setFrame(Rect(600, 0, 1200, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07002495
2496 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2497
2498 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
2499
2500 // Start cursor position in right window so that we can move the cursor to left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002501 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002502 injectMotionEvent(mDispatcher,
2503 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2504 AINPUT_SOURCE_MOUSE)
2505 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2506 .x(900)
2507 .y(400))
2508 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002509 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07002510
2511 // Move cursor into left window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002512 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002513 injectMotionEvent(mDispatcher,
2514 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2515 AINPUT_SOURCE_MOUSE)
2516 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2517 .x(300)
2518 .y(400))
2519 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002520 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2521 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07002522
2523 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002524 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002525 injectMotionEvent(mDispatcher,
2526 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2527 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2528 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2529 .x(300)
2530 .y(400))
2531 .build()));
2532 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2533
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002534 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002535 injectMotionEvent(mDispatcher,
2536 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
2537 AINPUT_SOURCE_MOUSE)
2538 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2539 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2540 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2541 .x(300)
2542 .y(400))
2543 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002544 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
Garfield Tandf26e862020-07-01 20:18:19 -07002545
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002546 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002547 injectMotionEvent(mDispatcher,
2548 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
2549 AINPUT_SOURCE_MOUSE)
2550 .buttonState(0)
2551 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2552 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2553 .x(300)
2554 .y(400))
2555 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002556 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE));
Garfield Tandf26e862020-07-01 20:18:19 -07002557
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002558 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002559 injectMotionEvent(mDispatcher,
2560 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
2561 .buttonState(0)
2562 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2563 .x(300)
2564 .y(400))
2565 .build()));
2566 windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT);
2567
2568 // Move mouse cursor back to right window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002569 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002570 injectMotionEvent(mDispatcher,
2571 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2572 AINPUT_SOURCE_MOUSE)
2573 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2574 .x(900)
2575 .y(400))
2576 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002577 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2578 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002579
2580 // No more events
2581 windowLeft->assertNoEvents();
2582 windowRight->assertNoEvents();
2583}
2584
2585TEST_F(InputDispatcherTest, HoverWithSpyWindows) {
2586 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2587
2588 sp<FakeWindowHandle> spyWindow =
2589 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
2590 spyWindow->setFrame(Rect(0, 0, 600, 800));
2591 spyWindow->setTrustedOverlay(true);
2592 spyWindow->setSpy(true);
2593 sp<FakeWindowHandle> window =
2594 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2595 window->setFrame(Rect(0, 0, 600, 800));
2596
2597 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2598 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
2599
2600 // Send mouse cursor to the window
2601 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2602 injectMotionEvent(mDispatcher,
2603 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
2604 AINPUT_SOURCE_MOUSE)
2605 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2606 .x(100)
2607 .y(100))
2608 .build()));
2609
2610 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2611 WithSource(AINPUT_SOURCE_MOUSE)));
2612 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2613 WithSource(AINPUT_SOURCE_MOUSE)));
2614
2615 window->assertNoEvents();
2616 spyWindow->assertNoEvents();
Garfield Tandf26e862020-07-01 20:18:19 -07002617}
2618
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08002619TEST_F(InputDispatcherTest, MouseAndTouchWithSpyWindows) {
2620 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2621
2622 sp<FakeWindowHandle> spyWindow =
2623 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
2624 spyWindow->setFrame(Rect(0, 0, 600, 800));
2625 spyWindow->setTrustedOverlay(true);
2626 spyWindow->setSpy(true);
2627 sp<FakeWindowHandle> window =
2628 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2629 window->setFrame(Rect(0, 0, 600, 800));
2630
2631 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2632 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
2633
2634 // Send mouse cursor to the window
2635 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2636 injectMotionEvent(mDispatcher,
2637 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
2638 AINPUT_SOURCE_MOUSE)
2639 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2640 .x(100)
2641 .y(100))
2642 .build()));
2643
2644 // Move mouse cursor
2645 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2646 injectMotionEvent(mDispatcher,
2647 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2648 AINPUT_SOURCE_MOUSE)
2649 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2650 .x(110)
2651 .y(110))
2652 .build()));
2653
2654 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2655 WithSource(AINPUT_SOURCE_MOUSE)));
2656 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2657 WithSource(AINPUT_SOURCE_MOUSE)));
2658 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2659 WithSource(AINPUT_SOURCE_MOUSE)));
2660 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2661 WithSource(AINPUT_SOURCE_MOUSE)));
2662 // Touch down on the window
2663 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2664 injectMotionEvent(mDispatcher,
2665 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2666 AINPUT_SOURCE_TOUCHSCREEN)
2667 .deviceId(SECOND_DEVICE_ID)
2668 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER)
2669 .x(200)
2670 .y(200))
2671 .build()));
2672 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2673 WithSource(AINPUT_SOURCE_MOUSE)));
2674 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2675 WithSource(AINPUT_SOURCE_MOUSE)));
2676 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2677 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
2678 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2679 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
2680
2681 // pilfer the motion, retaining the gesture on the spy window.
2682 EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken()));
2683 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
2684 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
2685
2686 // Touch UP on the window
2687 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2688 injectMotionEvent(mDispatcher,
2689 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2690 AINPUT_SOURCE_TOUCHSCREEN)
2691 .deviceId(SECOND_DEVICE_ID)
2692 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER)
2693 .x(200)
2694 .y(200))
2695 .build()));
2696 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
2697 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
2698
2699 // Previously, a touch was pilfered. However, that gesture was just finished. Now, we are going
2700 // to send a new gesture. It should again go to both windows (spy and the window below), just
2701 // like the first gesture did, before pilfering. The window configuration has not changed.
2702
2703 // One more tap - DOWN
2704 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2705 injectMotionEvent(mDispatcher,
2706 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2707 AINPUT_SOURCE_TOUCHSCREEN)
2708 .deviceId(SECOND_DEVICE_ID)
2709 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER)
2710 .x(250)
2711 .y(250))
2712 .build()));
2713 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2714 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
2715 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2716 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
2717
2718 // Touch UP on the window
2719 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2720 injectMotionEvent(mDispatcher,
2721 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2722 AINPUT_SOURCE_TOUCHSCREEN)
2723 .deviceId(SECOND_DEVICE_ID)
2724 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER)
2725 .x(250)
2726 .y(250))
2727 .build()));
2728 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
2729 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
2730 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
2731 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
2732
2733 window->assertNoEvents();
2734 spyWindow->assertNoEvents();
2735}
2736
Garfield Tandf26e862020-07-01 20:18:19 -07002737// This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected
2738// directly in this test.
2739TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) {
Chris Yea209fde2020-07-22 13:54:51 -07002740 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07002741 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002742 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07002743 window->setFrame(Rect(0, 0, 1200, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07002744
2745 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2746
2747 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2748
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002749 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002750 injectMotionEvent(mDispatcher,
2751 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
2752 AINPUT_SOURCE_MOUSE)
2753 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2754 .x(300)
2755 .y(400))
2756 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002757 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07002758 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002759 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002760 injectMotionEvent(mDispatcher,
2761 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2762 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2763 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2764 .x(300)
2765 .y(400))
2766 .build()));
2767 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2768
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002769 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002770 injectMotionEvent(mDispatcher,
2771 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
2772 AINPUT_SOURCE_MOUSE)
2773 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2774 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2775 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2776 .x(300)
2777 .y(400))
2778 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002779 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
Garfield Tandf26e862020-07-01 20:18:19 -07002780
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002781 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002782 injectMotionEvent(mDispatcher,
2783 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
2784 AINPUT_SOURCE_MOUSE)
2785 .buttonState(0)
2786 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2787 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2788 .x(300)
2789 .y(400))
2790 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002791 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE));
Garfield Tandf26e862020-07-01 20:18:19 -07002792
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002793 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002794 injectMotionEvent(mDispatcher,
2795 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
2796 .buttonState(0)
2797 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2798 .x(300)
2799 .y(400))
2800 .build()));
2801 window->consumeMotionUp(ADISPLAY_ID_DEFAULT);
2802
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002803 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002804 injectMotionEvent(mDispatcher,
2805 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT,
2806 AINPUT_SOURCE_MOUSE)
2807 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2808 .x(300)
2809 .y(400))
2810 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002811 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
Garfield Tandf26e862020-07-01 20:18:19 -07002812}
2813
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08002814/**
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00002815 * Hover over a window, and then remove that window. Make sure that HOVER_EXIT for that event
2816 * is generated.
2817 */
2818TEST_F(InputDispatcherTest, HoverExitIsSentToRemovedWindow) {
2819 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2820 sp<FakeWindowHandle> window =
2821 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2822 window->setFrame(Rect(0, 0, 1200, 800));
2823
2824 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2825
2826 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2827
2828 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2829 injectMotionEvent(mDispatcher,
2830 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
2831 AINPUT_SOURCE_MOUSE)
2832 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2833 .x(300)
2834 .y(400))
2835 .build()));
2836 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2837
2838 // Remove the window, but keep the channel.
2839 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
2840 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2841}
2842
2843/**
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08002844 * Inject a mouse hover event followed by a tap from touchscreen.
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00002845 * The tap causes a HOVER_EXIT event to be generated because the current event
2846 * stream's source has been switched.
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08002847 */
2848TEST_F(InputDispatcherTest, MouseHoverAndTouchTap) {
2849 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2850 sp<FakeWindowHandle> window =
2851 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2852 window->setFrame(Rect(0, 0, 100, 100));
2853
2854 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2855
2856 // Inject a hover_move from mouse.
2857 NotifyMotionArgs motionArgs =
2858 generateMotionArgs(AMOTION_EVENT_ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE,
2859 ADISPLAY_ID_DEFAULT, {{50, 50}});
2860 motionArgs.xCursorPosition = 50;
2861 motionArgs.yCursorPosition = 50;
2862 mDispatcher->notifyMotion(&motionArgs);
2863 ASSERT_NO_FATAL_FAILURE(
2864 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2865 WithSource(AINPUT_SOURCE_MOUSE))));
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08002866
2867 // Tap on the window
2868 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2869 ADISPLAY_ID_DEFAULT, {{10, 10}});
2870 mDispatcher->notifyMotion(&motionArgs);
2871 ASSERT_NO_FATAL_FAILURE(
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00002872 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2873 WithSource(AINPUT_SOURCE_MOUSE))));
2874
2875 ASSERT_NO_FATAL_FAILURE(
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08002876 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2877 WithSource(AINPUT_SOURCE_TOUCHSCREEN))));
2878
2879 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
2880 ADISPLAY_ID_DEFAULT, {{10, 10}});
2881 mDispatcher->notifyMotion(&motionArgs);
2882 ASSERT_NO_FATAL_FAILURE(
2883 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
2884 WithSource(AINPUT_SOURCE_TOUCHSCREEN))));
2885}
2886
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02002887TEST_F(InputDispatcherTest, HoverEnterMoveRemoveWindowsInSecondDisplay) {
2888 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2889 sp<FakeWindowHandle> windowDefaultDisplay =
2890 sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay",
2891 ADISPLAY_ID_DEFAULT);
2892 windowDefaultDisplay->setFrame(Rect(0, 0, 600, 800));
2893 sp<FakeWindowHandle> windowSecondDisplay =
2894 sp<FakeWindowHandle>::make(application, mDispatcher, "SecondDisplay",
2895 SECOND_DISPLAY_ID);
2896 windowSecondDisplay->setFrame(Rect(0, 0, 600, 800));
2897
2898 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}},
2899 {SECOND_DISPLAY_ID, {windowSecondDisplay}}});
2900
2901 // Set cursor position in window in default display and check that hover enter and move
2902 // events are generated.
2903 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2904 injectMotionEvent(mDispatcher,
2905 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2906 AINPUT_SOURCE_MOUSE)
2907 .displayId(ADISPLAY_ID_DEFAULT)
2908 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2909 .x(300)
2910 .y(600))
2911 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002912 windowDefaultDisplay->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02002913
2914 // Remove all windows in secondary display and check that no event happens on window in
2915 // primary display.
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002916 mDispatcher->setInputWindows(
2917 {{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}}, {SECOND_DISPLAY_ID, {}}});
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02002918 windowDefaultDisplay->assertNoEvents();
2919
2920 // Move cursor position in window in default display and check that only hover move
2921 // event is generated and not hover enter event.
2922 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}},
2923 {SECOND_DISPLAY_ID, {windowSecondDisplay}}});
2924 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2925 injectMotionEvent(mDispatcher,
2926 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2927 AINPUT_SOURCE_MOUSE)
2928 .displayId(ADISPLAY_ID_DEFAULT)
2929 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2930 .x(400)
2931 .y(700))
2932 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002933 windowDefaultDisplay->consumeMotionEvent(
2934 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2935 WithSource(AINPUT_SOURCE_MOUSE)));
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02002936 windowDefaultDisplay->assertNoEvents();
2937}
2938
Garfield Tan00f511d2019-06-12 16:55:40 -07002939TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) {
Chris Yea209fde2020-07-22 13:54:51 -07002940 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tan00f511d2019-06-12 16:55:40 -07002941
2942 sp<FakeWindowHandle> windowLeft =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002943 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07002944 windowLeft->setFrame(Rect(0, 0, 600, 800));
Garfield Tan00f511d2019-06-12 16:55:40 -07002945 sp<FakeWindowHandle> windowRight =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002946 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07002947 windowRight->setFrame(Rect(600, 0, 1200, 800));
Garfield Tan00f511d2019-06-12 16:55:40 -07002948
2949 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2950
Arthur Hung72d8dc32020-03-28 00:48:39 +00002951 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
Garfield Tan00f511d2019-06-12 16:55:40 -07002952
2953 // Inject an event with coordinate in the area of right window, with mouse cursor in the area of
2954 // left window. This event should be dispatched to the left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002955 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tan00f511d2019-06-12 16:55:40 -07002956 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07002957 ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400}));
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002958 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07002959 windowRight->assertNoEvents();
2960}
2961
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002962TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) {
Chris Yea209fde2020-07-22 13:54:51 -07002963 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002964 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
2965 "Fake Window", ADISPLAY_ID_DEFAULT);
Vishnu Nair47074b82020-08-14 11:54:47 -07002966 window->setFocusable(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002967
Arthur Hung72d8dc32020-03-28 00:48:39 +00002968 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002969 setFocusedWindow(window);
2970
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002971 window->consumeFocusEvent(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002972
2973 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
2974 mDispatcher->notifyKey(&keyArgs);
2975
2976 // Window should receive key down event.
2977 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2978
2979 // When device reset happens, that key stream should be terminated with FLAG_CANCELED
2980 // on the app side.
Harry Cutts33476232023-01-30 19:57:29 +00002981 NotifyDeviceResetArgs args(/*id=*/10, /*eventTime=*/20, DEVICE_ID);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002982 mDispatcher->notifyDeviceReset(&args);
2983 window->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
2984 AKEY_EVENT_FLAG_CANCELED);
2985}
2986
2987TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) {
Chris Yea209fde2020-07-22 13:54:51 -07002988 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002989 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
2990 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002991
Arthur Hung72d8dc32020-03-28 00:48:39 +00002992 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002993
2994 NotifyMotionArgs motionArgs =
2995 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2996 ADISPLAY_ID_DEFAULT);
2997 mDispatcher->notifyMotion(&motionArgs);
2998
2999 // Window should receive motion down event.
3000 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3001
3002 // When device reset happens, that motion stream should be terminated with ACTION_CANCEL
3003 // on the app side.
Harry Cutts33476232023-01-30 19:57:29 +00003004 NotifyDeviceResetArgs args(/*id=*/10, /*eventTime=*/20, DEVICE_ID);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003005 mDispatcher->notifyDeviceReset(&args);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08003006 window->consumeMotionEvent(
3007 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003008}
3009
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003010TEST_F(InputDispatcherTest, InterceptKeyByPolicy) {
3011 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003012 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3013 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003014 window->setFocusable(true);
3015
3016 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3017 setFocusedWindow(window);
3018
3019 window->consumeFocusEvent(true);
3020
3021 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
3022 const std::chrono::milliseconds interceptKeyTimeout = 50ms;
3023 const nsecs_t injectTime = keyArgs.eventTime;
3024 mFakePolicy->setInterceptKeyTimeout(interceptKeyTimeout);
3025 mDispatcher->notifyKey(&keyArgs);
3026 // The dispatching time should be always greater than or equal to intercept key timeout.
3027 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3028 ASSERT_TRUE((systemTime(SYSTEM_TIME_MONOTONIC) - injectTime) >=
3029 std::chrono::nanoseconds(interceptKeyTimeout).count());
3030}
3031
3032TEST_F(InputDispatcherTest, InterceptKeyIfKeyUp) {
3033 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003034 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3035 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003036 window->setFocusable(true);
3037
3038 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3039 setFocusedWindow(window);
3040
3041 window->consumeFocusEvent(true);
3042
3043 NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
3044 NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
3045 mFakePolicy->setInterceptKeyTimeout(150ms);
3046 mDispatcher->notifyKey(&keyDown);
3047 mDispatcher->notifyKey(&keyUp);
3048
3049 // Window should receive key event immediately when same key up.
3050 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3051 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
3052}
3053
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003054/**
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08003055 * Two windows. First is a regular window. Second does not overlap with the first, and has
3056 * WATCH_OUTSIDE_TOUCH.
3057 * Both windows are owned by the same UID.
3058 * Tap first window. Make sure that the second window receives ACTION_OUTSIDE with correct, non-zero
3059 * coordinates. The coordinates are not zeroed out because both windows are owned by the same UID.
3060 */
3061TEST_F(InputDispatcherTest, ActionOutsideForOwnedWindowHasValidCoordinates) {
3062 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3063 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3064 "First Window", ADISPLAY_ID_DEFAULT);
3065 window->setFrame(Rect{0, 0, 100, 100});
3066
3067 sp<FakeWindowHandle> outsideWindow =
3068 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3069 ADISPLAY_ID_DEFAULT);
3070 outsideWindow->setFrame(Rect{100, 100, 200, 200});
3071 outsideWindow->setWatchOutsideTouch(true);
3072 // outsideWindow must be above 'window' to receive ACTION_OUTSIDE events when 'window' is tapped
3073 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {outsideWindow, window}}});
3074
3075 // Tap on first window.
3076 NotifyMotionArgs motionArgs =
3077 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3078 ADISPLAY_ID_DEFAULT, {PointF{50, 50}});
3079 mDispatcher->notifyMotion(&motionArgs);
3080 window->consumeMotionDown();
3081 // The coordinates of the tap in 'outsideWindow' are relative to its top left corner.
3082 // Therefore, we should offset them by (100, 100) relative to the screen's top left corner.
3083 outsideWindow->consumeMotionEvent(
3084 AllOf(WithMotionAction(ACTION_OUTSIDE), WithCoords(-50, -50)));
3085}
3086
3087/**
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003088 * This test documents the behavior of WATCH_OUTSIDE_TOUCH. The window will get ACTION_OUTSIDE when
3089 * a another pointer causes ACTION_DOWN to be sent to another window for the first time. Only one
3090 * ACTION_OUTSIDE event is sent per gesture.
3091 */
3092TEST_F(InputDispatcherTest, ActionOutsideSentOnlyWhenAWindowIsTouched) {
3093 // There are three windows that do not overlap. `window` wants to WATCH_OUTSIDE_TOUCH.
3094 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003095 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3096 "First Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003097 window->setWatchOutsideTouch(true);
3098 window->setFrame(Rect{0, 0, 100, 100});
3099 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003100 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3101 ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003102 secondWindow->setFrame(Rect{100, 100, 200, 200});
3103 sp<FakeWindowHandle> thirdWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003104 sp<FakeWindowHandle>::make(application, mDispatcher, "Third Window",
3105 ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003106 thirdWindow->setFrame(Rect{200, 200, 300, 300});
3107 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, secondWindow, thirdWindow}}});
3108
3109 // First pointer lands outside all windows. `window` does not get ACTION_OUTSIDE.
3110 NotifyMotionArgs motionArgs =
3111 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3112 ADISPLAY_ID_DEFAULT, {PointF{-10, -10}});
3113 mDispatcher->notifyMotion(&motionArgs);
3114 window->assertNoEvents();
3115 secondWindow->assertNoEvents();
3116
3117 // The second pointer lands inside `secondWindow`, which should receive a DOWN event.
3118 // Now, `window` should get ACTION_OUTSIDE.
3119 motionArgs = generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3120 {PointF{-10, -10}, PointF{105, 105}});
3121 mDispatcher->notifyMotion(&motionArgs);
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08003122 const std::map<int32_t, PointF> expectedPointers{{0, PointF{-10, -10}}, {1, PointF{105, 105}}};
3123 window->consumeMotionEvent(
3124 AllOf(WithMotionAction(ACTION_OUTSIDE), WithPointers(expectedPointers)));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003125 secondWindow->consumeMotionDown();
3126 thirdWindow->assertNoEvents();
3127
3128 // The third pointer lands inside `thirdWindow`, which should receive a DOWN event. There is
3129 // no ACTION_OUTSIDE sent to `window` because one has already been sent for this gesture.
3130 motionArgs = generateMotionArgs(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3131 {PointF{-10, -10}, PointF{105, 105}, PointF{205, 205}});
3132 mDispatcher->notifyMotion(&motionArgs);
3133 window->assertNoEvents();
3134 secondWindow->consumeMotionMove();
3135 thirdWindow->consumeMotionDown();
3136}
3137
Prabir Pradhan814fe082022-07-22 20:22:18 +00003138TEST_F(InputDispatcherTest, OnWindowInfosChanged_RemoveAllWindowsOnDisplay) {
3139 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003140 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3141 "Fake Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan814fe082022-07-22 20:22:18 +00003142 window->setFocusable(true);
3143
3144 mDispatcher->onWindowInfosChanged({*window->getInfo()}, {});
3145 setFocusedWindow(window);
3146
3147 window->consumeFocusEvent(true);
3148
3149 NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
3150 NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
3151 mDispatcher->notifyKey(&keyDown);
3152 mDispatcher->notifyKey(&keyUp);
3153
3154 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3155 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
3156
3157 // All windows are removed from the display. Ensure that we can no longer dispatch to it.
3158 mDispatcher->onWindowInfosChanged({}, {});
3159
3160 window->consumeFocusEvent(false);
3161
3162 mDispatcher->notifyKey(&keyDown);
3163 mDispatcher->notifyKey(&keyUp);
3164 window->assertNoEvents();
3165}
3166
Arthur Hung96483742022-11-15 03:30:48 +00003167TEST_F(InputDispatcherTest, NonSplitTouchableWindowReceivesMultiTouch) {
3168 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3169 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3170 "Fake Window", ADISPLAY_ID_DEFAULT);
3171 // Ensure window is non-split and have some transform.
3172 window->setPreventSplitting(true);
3173 window->setWindowOffset(20, 40);
3174 mDispatcher->onWindowInfosChanged({*window->getInfo()}, {});
3175
3176 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3177 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3178 {50, 50}))
3179 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3180 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3181
3182 const MotionEvent secondFingerDownEvent =
3183 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3184 .displayId(ADISPLAY_ID_DEFAULT)
3185 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Harry Cutts33476232023-01-30 19:57:29 +00003186 .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
3187 .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(-30).y(-50))
Arthur Hung96483742022-11-15 03:30:48 +00003188 .build();
3189 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3190 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
3191 InputEventInjectionSync::WAIT_FOR_RESULT))
3192 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3193
3194 const MotionEvent* event = window->consumeMotion();
3195 EXPECT_EQ(POINTER_1_DOWN, event->getAction());
3196 EXPECT_EQ(70, event->getX(0)); // 50 + 20
3197 EXPECT_EQ(90, event->getY(0)); // 50 + 40
3198 EXPECT_EQ(-10, event->getX(1)); // -30 + 20
3199 EXPECT_EQ(-10, event->getY(1)); // -50 + 40
3200}
3201
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003202/**
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003203 * Ensure the correct coordinate spaces are used by InputDispatcher.
3204 *
3205 * InputDispatcher works in the display space, so its coordinate system is relative to the display
3206 * panel. Windows get events in the window space, and get raw coordinates in the logical display
3207 * space.
3208 */
3209class InputDispatcherDisplayProjectionTest : public InputDispatcherTest {
3210public:
3211 void SetUp() override {
3212 InputDispatcherTest::SetUp();
3213 mDisplayInfos.clear();
3214 mWindowInfos.clear();
3215 }
3216
3217 void addDisplayInfo(int displayId, const ui::Transform& transform) {
3218 gui::DisplayInfo info;
3219 info.displayId = displayId;
3220 info.transform = transform;
3221 mDisplayInfos.push_back(std::move(info));
3222 mDispatcher->onWindowInfosChanged(mWindowInfos, mDisplayInfos);
3223 }
3224
3225 void addWindow(const sp<WindowInfoHandle>& windowHandle) {
3226 mWindowInfos.push_back(*windowHandle->getInfo());
3227 mDispatcher->onWindowInfosChanged(mWindowInfos, mDisplayInfos);
3228 }
3229
3230 // Set up a test scenario where the display has a scaled projection and there are two windows
3231 // on the display.
3232 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupScaledDisplayScenario() {
3233 // The display has a projection that has a scale factor of 2 and 4 in the x and y directions
3234 // respectively.
3235 ui::Transform displayTransform;
3236 displayTransform.set(2, 0, 0, 4);
3237 addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform);
3238
3239 std::shared_ptr<FakeApplicationHandle> application =
3240 std::make_shared<FakeApplicationHandle>();
3241
3242 // Add two windows to the display. Their frames are represented in the display space.
3243 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003244 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
3245 ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003246 firstWindow->setFrame(Rect(0, 0, 100, 200), displayTransform);
3247 addWindow(firstWindow);
3248
3249 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003250 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3251 ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003252 secondWindow->setFrame(Rect(100, 200, 200, 400), displayTransform);
3253 addWindow(secondWindow);
3254 return {std::move(firstWindow), std::move(secondWindow)};
3255 }
3256
3257private:
3258 std::vector<gui::DisplayInfo> mDisplayInfos;
3259 std::vector<gui::WindowInfo> mWindowInfos;
3260};
3261
3262TEST_F(InputDispatcherDisplayProjectionTest, HitTestsInDisplaySpace) {
3263 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
3264 // Send down to the first window. The point is represented in the display space. The point is
3265 // selected so that if the hit test was done with the transform applied to it, then it would
3266 // end up in the incorrect window.
3267 NotifyMotionArgs downMotionArgs =
3268 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3269 ADISPLAY_ID_DEFAULT, {PointF{75, 55}});
3270 mDispatcher->notifyMotion(&downMotionArgs);
3271
3272 firstWindow->consumeMotionDown();
3273 secondWindow->assertNoEvents();
3274}
3275
3276// Ensure that when a MotionEvent is injected through the InputDispatcher::injectInputEvent() API,
3277// the event should be treated as being in the logical display space.
3278TEST_F(InputDispatcherDisplayProjectionTest, InjectionInLogicalDisplaySpace) {
3279 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
3280 // Send down to the first window. The point is represented in the logical display space. The
3281 // point is selected so that if the hit test was done in logical display space, then it would
3282 // end up in the incorrect window.
3283 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3284 PointF{75 * 2, 55 * 4});
3285
3286 firstWindow->consumeMotionDown();
3287 secondWindow->assertNoEvents();
3288}
3289
Prabir Pradhandaa2f142021-12-10 09:30:08 +00003290// Ensure that when a MotionEvent that has a custom transform is injected, the post-transformed
3291// event should be treated as being in the logical display space.
3292TEST_F(InputDispatcherDisplayProjectionTest, InjectionWithTransformInLogicalDisplaySpace) {
3293 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
3294
3295 const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0};
3296 ui::Transform injectedEventTransform;
3297 injectedEventTransform.set(matrix);
3298 const vec2 expectedPoint{75, 55}; // The injected point in the logical display space.
3299 const vec2 untransformedPoint = injectedEventTransform.inverse().transform(expectedPoint);
3300
3301 MotionEvent event = MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3302 .displayId(ADISPLAY_ID_DEFAULT)
3303 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Harry Cutts33476232023-01-30 19:57:29 +00003304 .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER)
Prabir Pradhandaa2f142021-12-10 09:30:08 +00003305 .x(untransformedPoint.x)
3306 .y(untransformedPoint.y))
3307 .build();
3308 event.transform(matrix);
3309
3310 injectMotionEvent(mDispatcher, event, INJECT_EVENT_TIMEOUT,
3311 InputEventInjectionSync::WAIT_FOR_RESULT);
3312
3313 firstWindow->consumeMotionDown();
3314 secondWindow->assertNoEvents();
3315}
3316
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003317TEST_F(InputDispatcherDisplayProjectionTest, WindowGetsEventsInCorrectCoordinateSpace) {
3318 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
3319
3320 // Send down to the second window.
3321 NotifyMotionArgs downMotionArgs =
3322 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3323 ADISPLAY_ID_DEFAULT, {PointF{150, 220}});
3324 mDispatcher->notifyMotion(&downMotionArgs);
3325
3326 firstWindow->assertNoEvents();
3327 const MotionEvent* event = secondWindow->consumeMotion();
3328 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, event->getAction());
3329
3330 // Ensure that the events from the "getRaw" API are in logical display coordinates.
3331 EXPECT_EQ(300, event->getRawX(0));
3332 EXPECT_EQ(880, event->getRawY(0));
3333
3334 // Ensure that the x and y values are in the window's coordinate space.
3335 // The left-top of the second window is at (100, 200) in display space, which is (200, 800) in
3336 // the logical display space. This will be the origin of the window space.
3337 EXPECT_EQ(100, event->getX(0));
3338 EXPECT_EQ(80, event->getY(0));
3339}
3340
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003341using TransferFunction = std::function<bool(const std::unique_ptr<InputDispatcher>& dispatcher,
3342 sp<IBinder>, sp<IBinder>)>;
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003343
3344class TransferTouchFixture : public InputDispatcherTest,
3345 public ::testing::WithParamInterface<TransferFunction> {};
3346
3347TEST_P(TransferTouchFixture, TransferTouch_OnePointer) {
Chris Yea209fde2020-07-22 13:54:51 -07003348 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08003349
3350 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003351 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003352 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
3353 ADISPLAY_ID_DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00003354 firstWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003355 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003356 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3357 ADISPLAY_ID_DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00003358 sp<FakeWindowHandle> wallpaper =
3359 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
3360 wallpaper->setIsWallpaper(true);
Svet Ganov5d3bc372020-01-26 23:11:07 -08003361 // Add the windows to the dispatcher
Arthur Hungc539dbb2022-12-08 07:45:36 +00003362 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow, wallpaper}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08003363
3364 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003365 NotifyMotionArgs downMotionArgs =
3366 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3367 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08003368 mDispatcher->notifyMotion(&downMotionArgs);
Arthur Hungc539dbb2022-12-08 07:45:36 +00003369
Svet Ganov5d3bc372020-01-26 23:11:07 -08003370 // Only the first window should get the down event
3371 firstWindow->consumeMotionDown();
3372 secondWindow->assertNoEvents();
Arthur Hungc539dbb2022-12-08 07:45:36 +00003373 wallpaper->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08003374
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003375 // Transfer touch to the second window
3376 TransferFunction f = GetParam();
3377 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
3378 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08003379 // The first window gets cancel and the second gets down
3380 firstWindow->consumeMotionCancel();
3381 secondWindow->consumeMotionDown();
Arthur Hungc539dbb2022-12-08 07:45:36 +00003382 wallpaper->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08003383
3384 // Send up event to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003385 NotifyMotionArgs upMotionArgs =
3386 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3387 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08003388 mDispatcher->notifyMotion(&upMotionArgs);
3389 // The first window gets no events and the second gets up
3390 firstWindow->assertNoEvents();
3391 secondWindow->consumeMotionUp();
Arthur Hungc539dbb2022-12-08 07:45:36 +00003392 wallpaper->assertNoEvents();
Svet Ganov5d3bc372020-01-26 23:11:07 -08003393}
3394
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07003395/**
3396 * When 'transferTouch' API is invoked, dispatcher needs to find the "best" window to take touch
3397 * from. When we have spy windows, there are several windows to choose from: either spy, or the
3398 * 'real' (non-spy) window. Always prefer the 'real' window because that's what would be most
3399 * natural to the user.
3400 * In this test, we are sending a pointer to both spy window and first window. We then try to
3401 * transfer touch to the second window. The dispatcher should identify the first window as the
3402 * one that should lose the gesture, and therefore the action should be to move the gesture from
3403 * the first window to the second.
3404 * The main goal here is to test the behaviour of 'transferTouch' API, but it's still valid to test
3405 * the other API, as well.
3406 */
3407TEST_P(TransferTouchFixture, TransferTouch_MultipleWindowsWithSpy) {
3408 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3409
3410 // Create a couple of windows + a spy window
3411 sp<FakeWindowHandle> spyWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003412 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07003413 spyWindow->setTrustedOverlay(true);
3414 spyWindow->setSpy(true);
3415 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003416 sp<FakeWindowHandle>::make(application, mDispatcher, "First", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07003417 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003418 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07003419
3420 // Add the windows to the dispatcher
3421 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, firstWindow, secondWindow}}});
3422
3423 // Send down to the first window
3424 NotifyMotionArgs downMotionArgs =
3425 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3426 ADISPLAY_ID_DEFAULT);
3427 mDispatcher->notifyMotion(&downMotionArgs);
3428 // Only the first window and spy should get the down event
3429 spyWindow->consumeMotionDown();
3430 firstWindow->consumeMotionDown();
3431
3432 // Transfer touch to the second window. Non-spy window should be preferred over the spy window
3433 // if f === 'transferTouch'.
3434 TransferFunction f = GetParam();
3435 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
3436 ASSERT_TRUE(success);
3437 // The first window gets cancel and the second gets down
3438 firstWindow->consumeMotionCancel();
3439 secondWindow->consumeMotionDown();
3440
3441 // Send up event to the second window
3442 NotifyMotionArgs upMotionArgs =
3443 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3444 ADISPLAY_ID_DEFAULT);
3445 mDispatcher->notifyMotion(&upMotionArgs);
3446 // The first window gets no events and the second+spy get up
3447 firstWindow->assertNoEvents();
3448 spyWindow->consumeMotionUp();
3449 secondWindow->consumeMotionUp();
3450}
3451
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003452TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07003453 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08003454
3455 PointF touchPoint = {10, 10};
3456
3457 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003458 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003459 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
3460 ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08003461 firstWindow->setPreventSplitting(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003462 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003463 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3464 ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08003465 secondWindow->setPreventSplitting(true);
Svet Ganov5d3bc372020-01-26 23:11:07 -08003466
3467 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00003468 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08003469
3470 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003471 NotifyMotionArgs downMotionArgs =
3472 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3473 ADISPLAY_ID_DEFAULT, {touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08003474 mDispatcher->notifyMotion(&downMotionArgs);
3475 // Only the first window should get the down event
3476 firstWindow->consumeMotionDown();
3477 secondWindow->assertNoEvents();
3478
3479 // Send pointer down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003480 NotifyMotionArgs pointerDownMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003481 generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003482 {touchPoint, touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08003483 mDispatcher->notifyMotion(&pointerDownMotionArgs);
3484 // Only the first window should get the pointer down event
3485 firstWindow->consumeMotionPointerDown(1);
3486 secondWindow->assertNoEvents();
3487
3488 // Transfer touch focus to the second window
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003489 TransferFunction f = GetParam();
3490 bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
3491 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08003492 // The first window gets cancel and the second gets down and pointer down
3493 firstWindow->consumeMotionCancel();
3494 secondWindow->consumeMotionDown();
3495 secondWindow->consumeMotionPointerDown(1);
3496
3497 // Send pointer up to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003498 NotifyMotionArgs pointerUpMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003499 generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003500 {touchPoint, touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08003501 mDispatcher->notifyMotion(&pointerUpMotionArgs);
3502 // The first window gets nothing and the second gets pointer up
3503 firstWindow->assertNoEvents();
3504 secondWindow->consumeMotionPointerUp(1);
3505
3506 // Send up event to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003507 NotifyMotionArgs upMotionArgs =
3508 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3509 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08003510 mDispatcher->notifyMotion(&upMotionArgs);
3511 // The first window gets nothing and the second gets up
3512 firstWindow->assertNoEvents();
3513 secondWindow->consumeMotionUp();
3514}
3515
Arthur Hungc539dbb2022-12-08 07:45:36 +00003516TEST_P(TransferTouchFixture, TransferTouch_MultipleWallpapers) {
3517 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3518
3519 // Create a couple of windows
3520 sp<FakeWindowHandle> firstWindow =
3521 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
3522 ADISPLAY_ID_DEFAULT);
3523 firstWindow->setDupTouchToWallpaper(true);
3524 sp<FakeWindowHandle> secondWindow =
3525 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3526 ADISPLAY_ID_DEFAULT);
3527 secondWindow->setDupTouchToWallpaper(true);
3528
3529 sp<FakeWindowHandle> wallpaper1 =
3530 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper1", ADISPLAY_ID_DEFAULT);
3531 wallpaper1->setIsWallpaper(true);
3532
3533 sp<FakeWindowHandle> wallpaper2 =
3534 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper2", ADISPLAY_ID_DEFAULT);
3535 wallpaper2->setIsWallpaper(true);
3536 // Add the windows to the dispatcher
3537 mDispatcher->setInputWindows(
3538 {{ADISPLAY_ID_DEFAULT, {firstWindow, wallpaper1, secondWindow, wallpaper2}}});
3539
3540 // Send down to the first window
3541 NotifyMotionArgs downMotionArgs =
3542 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3543 ADISPLAY_ID_DEFAULT);
3544 mDispatcher->notifyMotion(&downMotionArgs);
3545
3546 // Only the first window should get the down event
3547 firstWindow->consumeMotionDown();
3548 secondWindow->assertNoEvents();
3549 wallpaper1->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
3550 wallpaper2->assertNoEvents();
3551
3552 // Transfer touch focus to the second window
3553 TransferFunction f = GetParam();
3554 bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
3555 ASSERT_TRUE(success);
3556
3557 // The first window gets cancel and the second gets down
3558 firstWindow->consumeMotionCancel();
3559 secondWindow->consumeMotionDown();
3560 wallpaper1->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
3561 wallpaper2->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
3562
3563 // Send up event to the second window
3564 NotifyMotionArgs upMotionArgs =
3565 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3566 ADISPLAY_ID_DEFAULT);
3567 mDispatcher->notifyMotion(&upMotionArgs);
3568 // The first window gets no events and the second gets up
3569 firstWindow->assertNoEvents();
3570 secondWindow->consumeMotionUp();
3571 wallpaper1->assertNoEvents();
3572 wallpaper2->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
3573}
3574
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003575// For the cases of single pointer touch and two pointers non-split touch, the api's
3576// 'transferTouch' and 'transferTouchFocus' are equivalent in behaviour. They only differ
3577// for the case where there are multiple pointers split across several windows.
3578INSTANTIATE_TEST_SUITE_P(TransferFunctionTests, TransferTouchFixture,
3579 ::testing::Values(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003580 [&](const std::unique_ptr<InputDispatcher>& dispatcher,
3581 sp<IBinder> /*ignored*/, sp<IBinder> destChannelToken) {
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07003582 return dispatcher->transferTouch(destChannelToken,
3583 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003584 },
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003585 [&](const std::unique_ptr<InputDispatcher>& dispatcher,
3586 sp<IBinder> from, sp<IBinder> to) {
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003587 return dispatcher->transferTouchFocus(from, to,
Harry Cutts33476232023-01-30 19:57:29 +00003588 /*isDragAndDrop=*/false);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003589 }));
3590
Svet Ganov5d3bc372020-01-26 23:11:07 -08003591TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07003592 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08003593
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003594 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003595 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
3596 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08003597 firstWindow->setFrame(Rect(0, 0, 600, 400));
Svet Ganov5d3bc372020-01-26 23:11:07 -08003598
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003599 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003600 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3601 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08003602 secondWindow->setFrame(Rect(0, 400, 600, 800));
Svet Ganov5d3bc372020-01-26 23:11:07 -08003603
3604 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00003605 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08003606
3607 PointF pointInFirst = {300, 200};
3608 PointF pointInSecond = {300, 600};
3609
3610 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003611 NotifyMotionArgs firstDownMotionArgs =
3612 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3613 ADISPLAY_ID_DEFAULT, {pointInFirst});
Svet Ganov5d3bc372020-01-26 23:11:07 -08003614 mDispatcher->notifyMotion(&firstDownMotionArgs);
3615 // Only the first window should get the down event
3616 firstWindow->consumeMotionDown();
3617 secondWindow->assertNoEvents();
3618
3619 // Send down to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003620 NotifyMotionArgs secondDownMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003621 generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003622 {pointInFirst, pointInSecond});
Svet Ganov5d3bc372020-01-26 23:11:07 -08003623 mDispatcher->notifyMotion(&secondDownMotionArgs);
3624 // The first window gets a move and the second a down
3625 firstWindow->consumeMotionMove();
3626 secondWindow->consumeMotionDown();
3627
3628 // Transfer touch focus to the second window
3629 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
3630 // The first window gets cancel and the new gets pointer down (it already saw down)
3631 firstWindow->consumeMotionCancel();
3632 secondWindow->consumeMotionPointerDown(1);
3633
3634 // Send pointer up to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003635 NotifyMotionArgs pointerUpMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003636 generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003637 {pointInFirst, pointInSecond});
Svet Ganov5d3bc372020-01-26 23:11:07 -08003638 mDispatcher->notifyMotion(&pointerUpMotionArgs);
3639 // The first window gets nothing and the second gets pointer up
3640 firstWindow->assertNoEvents();
3641 secondWindow->consumeMotionPointerUp(1);
3642
3643 // Send up event to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003644 NotifyMotionArgs upMotionArgs =
3645 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3646 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08003647 mDispatcher->notifyMotion(&upMotionArgs);
3648 // The first window gets nothing and the second gets up
3649 firstWindow->assertNoEvents();
3650 secondWindow->consumeMotionUp();
3651}
3652
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003653// Same as TransferTouchFocus_TwoPointersSplitTouch, but using 'transferTouch' api.
3654// Unlike 'transferTouchFocus', calling 'transferTouch' when there are two windows receiving
3655// touch is not supported, so the touch should continue on those windows and the transferred-to
3656// window should get nothing.
3657TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) {
3658 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3659
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003660 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003661 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
3662 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003663 firstWindow->setFrame(Rect(0, 0, 600, 400));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003664
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003665 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003666 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3667 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003668 secondWindow->setFrame(Rect(0, 400, 600, 800));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003669
3670 // Add the windows to the dispatcher
3671 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
3672
3673 PointF pointInFirst = {300, 200};
3674 PointF pointInSecond = {300, 600};
3675
3676 // Send down to the first window
3677 NotifyMotionArgs firstDownMotionArgs =
3678 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3679 ADISPLAY_ID_DEFAULT, {pointInFirst});
3680 mDispatcher->notifyMotion(&firstDownMotionArgs);
3681 // Only the first window should get the down event
3682 firstWindow->consumeMotionDown();
3683 secondWindow->assertNoEvents();
3684
3685 // Send down to the second window
3686 NotifyMotionArgs secondDownMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003687 generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003688 {pointInFirst, pointInSecond});
3689 mDispatcher->notifyMotion(&secondDownMotionArgs);
3690 // The first window gets a move and the second a down
3691 firstWindow->consumeMotionMove();
3692 secondWindow->consumeMotionDown();
3693
3694 // Transfer touch focus to the second window
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07003695 const bool transferred =
3696 mDispatcher->transferTouch(secondWindow->getToken(), ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003697 // The 'transferTouch' call should not succeed, because there are 2 touched windows
3698 ASSERT_FALSE(transferred);
3699 firstWindow->assertNoEvents();
3700 secondWindow->assertNoEvents();
3701
3702 // The rest of the dispatch should proceed as normal
3703 // Send pointer up to the second window
3704 NotifyMotionArgs pointerUpMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003705 generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003706 {pointInFirst, pointInSecond});
3707 mDispatcher->notifyMotion(&pointerUpMotionArgs);
3708 // The first window gets MOVE and the second gets pointer up
3709 firstWindow->consumeMotionMove();
3710 secondWindow->consumeMotionUp();
3711
3712 // Send up event to the first window
3713 NotifyMotionArgs upMotionArgs =
3714 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3715 ADISPLAY_ID_DEFAULT);
3716 mDispatcher->notifyMotion(&upMotionArgs);
3717 // The first window gets nothing and the second gets up
3718 firstWindow->consumeMotionUp();
3719 secondWindow->assertNoEvents();
3720}
3721
Arthur Hungabbb9d82021-09-01 14:52:30 +00003722// This case will create two windows and one mirrored window on the default display and mirror
3723// two windows on the second display. It will test if 'transferTouchFocus' works fine if we put
3724// the windows info of second display before default display.
3725TEST_F(InputDispatcherTest, TransferTouchFocus_CloneSurface) {
3726 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3727 sp<FakeWindowHandle> firstWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003728 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00003729 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003730 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003731 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00003732 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003733
3734 sp<FakeWindowHandle> mirrorWindowInPrimary =
3735 firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT);
3736 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003737
3738 sp<FakeWindowHandle> firstWindowInSecondary =
3739 firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
3740 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003741
3742 sp<FakeWindowHandle> secondWindowInSecondary =
3743 secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
3744 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003745
3746 // Update window info, let it find window handle of second display first.
3747 mDispatcher->setInputWindows(
3748 {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}},
3749 {ADISPLAY_ID_DEFAULT,
3750 {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}});
3751
3752 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3753 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3754 {50, 50}))
3755 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3756
3757 // Window should receive motion event.
3758 firstWindowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3759
3760 // Transfer touch focus
3761 ASSERT_TRUE(mDispatcher->transferTouchFocus(firstWindowInPrimary->getToken(),
3762 secondWindowInPrimary->getToken()));
3763 // The first window gets cancel.
3764 firstWindowInPrimary->consumeMotionCancel();
3765 secondWindowInPrimary->consumeMotionDown();
3766
3767 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3768 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
3769 ADISPLAY_ID_DEFAULT, {150, 50}))
3770 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3771 firstWindowInPrimary->assertNoEvents();
3772 secondWindowInPrimary->consumeMotionMove();
3773
3774 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3775 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3776 {150, 50}))
3777 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3778 firstWindowInPrimary->assertNoEvents();
3779 secondWindowInPrimary->consumeMotionUp();
3780}
3781
3782// Same as TransferTouchFocus_CloneSurface, but this touch on the secondary display and use
3783// 'transferTouch' api.
3784TEST_F(InputDispatcherTest, TransferTouch_CloneSurface) {
3785 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3786 sp<FakeWindowHandle> firstWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003787 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00003788 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003789 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003790 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00003791 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003792
3793 sp<FakeWindowHandle> mirrorWindowInPrimary =
3794 firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT);
3795 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003796
3797 sp<FakeWindowHandle> firstWindowInSecondary =
3798 firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
3799 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003800
3801 sp<FakeWindowHandle> secondWindowInSecondary =
3802 secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
3803 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003804
3805 // Update window info, let it find window handle of second display first.
3806 mDispatcher->setInputWindows(
3807 {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}},
3808 {ADISPLAY_ID_DEFAULT,
3809 {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}});
3810
3811 // Touch on second display.
3812 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3813 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {50, 50}))
3814 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3815
3816 // Window should receive motion event.
3817 firstWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID);
3818
3819 // Transfer touch focus
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07003820 ASSERT_TRUE(mDispatcher->transferTouch(secondWindowInSecondary->getToken(), SECOND_DISPLAY_ID));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003821
3822 // The first window gets cancel.
3823 firstWindowInPrimary->consumeMotionCancel(SECOND_DISPLAY_ID);
3824 secondWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID);
3825
3826 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3827 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
3828 SECOND_DISPLAY_ID, {150, 50}))
3829 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3830 firstWindowInPrimary->assertNoEvents();
3831 secondWindowInPrimary->consumeMotionMove(SECOND_DISPLAY_ID);
3832
3833 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3834 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {150, 50}))
3835 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3836 firstWindowInPrimary->assertNoEvents();
3837 secondWindowInPrimary->consumeMotionUp(SECOND_DISPLAY_ID);
3838}
3839
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003840TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07003841 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003842 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3843 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003844
Vishnu Nair47074b82020-08-14 11:54:47 -07003845 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00003846 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003847 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003848
3849 window->consumeFocusEvent(true);
3850
3851 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
3852 mDispatcher->notifyKey(&keyArgs);
3853
3854 // Window should receive key down event.
3855 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3856}
3857
3858TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07003859 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003860 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3861 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003862
Arthur Hung72d8dc32020-03-28 00:48:39 +00003863 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003864
3865 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
3866 mDispatcher->notifyKey(&keyArgs);
3867 mDispatcher->waitForIdle();
3868
3869 window->assertNoEvents();
3870}
3871
3872// If a window is touchable, but does not have focus, it should receive motion events, but not keys
3873TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) {
Chris Yea209fde2020-07-22 13:54:51 -07003874 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003875 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3876 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003877
Arthur Hung72d8dc32020-03-28 00:48:39 +00003878 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003879
3880 // Send key
3881 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
3882 mDispatcher->notifyKey(&keyArgs);
3883 // Send motion
3884 NotifyMotionArgs motionArgs =
3885 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3886 ADISPLAY_ID_DEFAULT);
3887 mDispatcher->notifyMotion(&motionArgs);
3888
3889 // Window should receive only the motion event
3890 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3891 window->assertNoEvents(); // Key event or focus event will not be received
3892}
3893
arthurhungea3f4fc2020-12-21 23:18:53 +08003894TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) {
3895 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3896
arthurhungea3f4fc2020-12-21 23:18:53 +08003897 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003898 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
3899 ADISPLAY_ID_DEFAULT);
arthurhungea3f4fc2020-12-21 23:18:53 +08003900 firstWindow->setFrame(Rect(0, 0, 600, 400));
arthurhungea3f4fc2020-12-21 23:18:53 +08003901
arthurhungea3f4fc2020-12-21 23:18:53 +08003902 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003903 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3904 ADISPLAY_ID_DEFAULT);
arthurhungea3f4fc2020-12-21 23:18:53 +08003905 secondWindow->setFrame(Rect(0, 400, 600, 800));
arthurhungea3f4fc2020-12-21 23:18:53 +08003906
3907 // Add the windows to the dispatcher
3908 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
3909
3910 PointF pointInFirst = {300, 200};
3911 PointF pointInSecond = {300, 600};
3912
3913 // Send down to the first window
3914 NotifyMotionArgs firstDownMotionArgs =
3915 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3916 ADISPLAY_ID_DEFAULT, {pointInFirst});
3917 mDispatcher->notifyMotion(&firstDownMotionArgs);
3918 // Only the first window should get the down event
3919 firstWindow->consumeMotionDown();
3920 secondWindow->assertNoEvents();
3921
3922 // Send down to the second window
3923 NotifyMotionArgs secondDownMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003924 generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
arthurhungea3f4fc2020-12-21 23:18:53 +08003925 {pointInFirst, pointInSecond});
3926 mDispatcher->notifyMotion(&secondDownMotionArgs);
3927 // The first window gets a move and the second a down
3928 firstWindow->consumeMotionMove();
3929 secondWindow->consumeMotionDown();
3930
3931 // Send pointer cancel to the second window
3932 NotifyMotionArgs pointerUpMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003933 generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
arthurhungea3f4fc2020-12-21 23:18:53 +08003934 {pointInFirst, pointInSecond});
3935 pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED;
3936 mDispatcher->notifyMotion(&pointerUpMotionArgs);
3937 // The first window gets move and the second gets cancel.
3938 firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
3939 secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
3940
3941 // Send up event.
3942 NotifyMotionArgs upMotionArgs =
3943 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3944 ADISPLAY_ID_DEFAULT);
3945 mDispatcher->notifyMotion(&upMotionArgs);
3946 // The first window gets up and the second gets nothing.
3947 firstWindow->consumeMotionUp();
3948 secondWindow->assertNoEvents();
3949}
3950
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00003951TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) {
3952 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3953
3954 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003955 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00003956 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3957 std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline;
3958 graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2;
3959 graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3;
3960
Harry Cutts33476232023-01-30 19:57:29 +00003961 window->sendTimeline(/*inputEventId=*/1, graphicsTimeline);
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00003962 window->assertNoEvents();
3963 mDispatcher->waitForIdle();
3964}
3965
chaviwd1c23182019-12-20 18:44:56 -08003966class FakeMonitorReceiver {
Michael Wright3a240c42019-12-10 20:53:41 +00003967public:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003968 FakeMonitorReceiver(const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003969 int32_t displayId) {
Garfield Tan15601662020-09-22 15:32:38 -07003970 base::Result<std::unique_ptr<InputChannel>> channel =
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08003971 dispatcher->createInputMonitor(displayId, name, MONITOR_PID);
Garfield Tan15601662020-09-22 15:32:38 -07003972 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
Michael Wright3a240c42019-12-10 20:53:41 +00003973 }
3974
chaviwd1c23182019-12-20 18:44:56 -08003975 sp<IBinder> getToken() { return mInputReceiver->getToken(); }
3976
3977 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
3978 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN,
3979 expectedDisplayId, expectedFlags);
3980 }
3981
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003982 std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); }
3983
3984 void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); }
3985
chaviwd1c23182019-12-20 18:44:56 -08003986 void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
3987 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN,
3988 expectedDisplayId, expectedFlags);
3989 }
3990
Siarhei Vishniakouca205502021-07-16 21:31:58 +00003991 void consumeMotionMove(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
3992 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE,
3993 expectedDisplayId, expectedFlags);
3994 }
3995
chaviwd1c23182019-12-20 18:44:56 -08003996 void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
3997 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP,
3998 expectedDisplayId, expectedFlags);
3999 }
4000
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004001 void consumeMotionCancel(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08004002 mInputReceiver->consumeMotionEvent(
4003 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
4004 WithDisplayId(expectedDisplayId),
4005 WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED)));
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004006 }
4007
Arthur Hungfbfa5722021-11-16 02:45:54 +00004008 void consumeMotionPointerDown(int32_t pointerIdx) {
4009 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
4010 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
4011 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00004012 /*expectedFlags=*/0);
Arthur Hungfbfa5722021-11-16 02:45:54 +00004013 }
4014
Evan Rosky84f07f02021-04-16 10:42:42 -07004015 MotionEvent* consumeMotion() {
4016 InputEvent* event = mInputReceiver->consume();
4017 if (!event) {
4018 ADD_FAILURE() << "No event was produced";
4019 return nullptr;
4020 }
4021 if (event->getType() != AINPUT_EVENT_TYPE_MOTION) {
4022 ADD_FAILURE() << "Received event of type " << event->getType() << " instead of motion";
4023 return nullptr;
4024 }
4025 return static_cast<MotionEvent*>(event);
4026 }
4027
chaviwd1c23182019-12-20 18:44:56 -08004028 void assertNoEvents() { mInputReceiver->assertNoEvents(); }
4029
4030private:
4031 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Michael Wright3a240c42019-12-10 20:53:41 +00004032};
4033
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004034using InputDispatcherMonitorTest = InputDispatcherTest;
4035
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004036/**
4037 * Two entities that receive touch: A window, and a global monitor.
4038 * The touch goes to the window, and then the window disappears.
4039 * The monitor does not get cancel right away. But if more events come in, the touch gets canceled
4040 * for the monitor, as well.
4041 * 1. foregroundWindow
4042 * 2. monitor <-- global monitor (doesn't observe z order, receives all events)
4043 */
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004044TEST_F(InputDispatcherMonitorTest, MonitorTouchIsCanceledWhenForegroundWindowDisappears) {
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004045 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4046 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004047 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004048
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004049 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004050
4051 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4052 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4053 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4054 {100, 200}))
4055 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4056
4057 // Both the foreground window and the global monitor should receive the touch down
4058 window->consumeMotionDown();
4059 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
4060
4061 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4062 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4063 ADISPLAY_ID_DEFAULT, {110, 200}))
4064 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4065
4066 window->consumeMotionMove();
4067 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
4068
4069 // Now the foreground window goes away
4070 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
4071 window->consumeMotionCancel();
4072 monitor.assertNoEvents(); // Global monitor does not get a cancel yet
4073
4074 // If more events come in, there will be no more foreground window to send them to. This will
4075 // cause a cancel for the monitor, as well.
4076 ASSERT_EQ(InputEventInjectionResult::FAILED,
4077 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4078 ADISPLAY_ID_DEFAULT, {120, 200}))
4079 << "Injection should fail because the window was removed";
4080 window->assertNoEvents();
4081 // Global monitor now gets the cancel
4082 monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
4083}
4084
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004085TEST_F(InputDispatcherMonitorTest, ReceivesMotionEvents) {
Chris Yea209fde2020-07-22 13:54:51 -07004086 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004087 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4088 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004089 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00004090
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004091 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00004092
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004093 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00004094 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004095 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00004096 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08004097 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00004098}
4099
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004100TEST_F(InputDispatcherMonitorTest, MonitorCannotPilferPointers) {
4101 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00004102
Chris Yea209fde2020-07-22 13:54:51 -07004103 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004104 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4105 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004106 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00004107
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004108 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00004109 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004110 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
chaviwd1c23182019-12-20 18:44:56 -08004111 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004112 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00004113
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004114 // Pilfer pointers from the monitor.
4115 // This should not do anything and the window should continue to receive events.
4116 EXPECT_NE(OK, mDispatcher->pilferPointers(monitor.getToken()));
Michael Wright3a240c42019-12-10 20:53:41 +00004117
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004118 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004119 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4120 ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004121 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004122
4123 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
4124 window->consumeMotionMove(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00004125}
4126
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004127TEST_F(InputDispatcherMonitorTest, NoWindowTransform) {
Evan Rosky84f07f02021-04-16 10:42:42 -07004128 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004129 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4130 "Fake Window", ADISPLAY_ID_DEFAULT);
Evan Rosky84f07f02021-04-16 10:42:42 -07004131 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4132 window->setWindowOffset(20, 40);
4133 window->setWindowTransform(0, 1, -1, 0);
4134
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004135 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Evan Rosky84f07f02021-04-16 10:42:42 -07004136
4137 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4138 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
4139 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4140 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4141 MotionEvent* event = monitor.consumeMotion();
4142 // Even though window has transform, gesture monitor must not.
4143 ASSERT_EQ(ui::Transform(), event->getTransform());
4144}
4145
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004146TEST_F(InputDispatcherMonitorTest, InjectionFailsWithNoWindow) {
Arthur Hungb3307ee2021-10-14 10:57:37 +00004147 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004148 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Arthur Hungb3307ee2021-10-14 10:57:37 +00004149
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004150 ASSERT_EQ(InputEventInjectionResult::FAILED,
Arthur Hungb3307ee2021-10-14 10:57:37 +00004151 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004152 << "Injection should fail if there is a monitor, but no touchable window";
4153 monitor.assertNoEvents();
Arthur Hungb3307ee2021-10-14 10:57:37 +00004154}
4155
chaviw81e2bb92019-12-18 15:03:51 -08004156TEST_F(InputDispatcherTest, TestMoveEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07004157 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004158 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4159 "Fake Window", ADISPLAY_ID_DEFAULT);
chaviw81e2bb92019-12-18 15:03:51 -08004160
Arthur Hung72d8dc32020-03-28 00:48:39 +00004161 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
chaviw81e2bb92019-12-18 15:03:51 -08004162
4163 NotifyMotionArgs motionArgs =
4164 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4165 ADISPLAY_ID_DEFAULT);
4166
4167 mDispatcher->notifyMotion(&motionArgs);
4168 // Window should receive motion down event.
4169 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4170
4171 motionArgs.action = AMOTION_EVENT_ACTION_MOVE;
Garfield Tanc51d1ba2020-01-28 13:24:04 -08004172 motionArgs.id += 1;
chaviw81e2bb92019-12-18 15:03:51 -08004173 motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
4174 motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
4175 motionArgs.pointerCoords[0].getX() - 10);
4176
4177 mDispatcher->notifyMotion(&motionArgs);
4178 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00004179 /*expectedFlags=*/0);
chaviw81e2bb92019-12-18 15:03:51 -08004180}
4181
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004182/**
4183 * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to
4184 * the device default right away. In the test scenario, we check both the default value,
4185 * and the action of enabling / disabling.
4186 */
4187TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) {
Chris Yea209fde2020-07-22 13:54:51 -07004188 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004189 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4190 "Test window", ADISPLAY_ID_DEFAULT);
Antonio Kantekea47acb2021-12-23 12:41:25 -08004191 const WindowInfo& windowInfo = *window->getInfo();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004192
4193 // Set focused application.
4194 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07004195 window->setFocusable(true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004196
4197 SCOPED_TRACE("Check default value of touch mode");
Arthur Hung72d8dc32020-03-28 00:48:39 +00004198 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004199 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00004200 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004201
4202 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07004203 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004204 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Harry Cutts33476232023-01-30 19:57:29 +00004205 window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004206
4207 SCOPED_TRACE("Disable touch mode");
Antonio Kantekea47acb2021-12-23 12:41:25 -08004208 mDispatcher->setInTouchMode(false, windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00004209 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00004210 window->consumeTouchModeEvent(false);
Vishnu Nair47074b82020-08-14 11:54:47 -07004211 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004212 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004213 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00004214 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/false);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004215
4216 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07004217 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004218 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Harry Cutts33476232023-01-30 19:57:29 +00004219 window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/false);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004220
4221 SCOPED_TRACE("Enable touch mode again");
Antonio Kantekea47acb2021-12-23 12:41:25 -08004222 mDispatcher->setInTouchMode(true, windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00004223 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00004224 window->consumeTouchModeEvent(true);
Vishnu Nair47074b82020-08-14 11:54:47 -07004225 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004226 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004227 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00004228 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004229
4230 window->assertNoEvents();
4231}
4232
Gang Wange9087892020-01-07 12:17:14 -05004233TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07004234 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004235 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4236 "Test window", ADISPLAY_ID_DEFAULT);
Gang Wange9087892020-01-07 12:17:14 -05004237
4238 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07004239 window->setFocusable(true);
Gang Wange9087892020-01-07 12:17:14 -05004240
Arthur Hung72d8dc32020-03-28 00:48:39 +00004241 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004242 setFocusedWindow(window);
4243
Harry Cutts33476232023-01-30 19:57:29 +00004244 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Gang Wange9087892020-01-07 12:17:14 -05004245
4246 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
4247 mDispatcher->notifyKey(&keyArgs);
4248
4249 InputEvent* event = window->consume();
4250 ASSERT_NE(event, nullptr);
4251
4252 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
4253 ASSERT_NE(verified, nullptr);
4254 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY);
4255
4256 ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos);
4257 ASSERT_EQ(keyArgs.deviceId, verified->deviceId);
4258 ASSERT_EQ(keyArgs.source, verified->source);
4259 ASSERT_EQ(keyArgs.displayId, verified->displayId);
4260
4261 const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified);
4262
4263 ASSERT_EQ(keyArgs.action, verifiedKey.action);
Gang Wange9087892020-01-07 12:17:14 -05004264 ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08004265 ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos);
Gang Wange9087892020-01-07 12:17:14 -05004266 ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode);
4267 ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode);
4268 ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState);
4269 ASSERT_EQ(0, verifiedKey.repeatCount);
4270}
4271
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08004272TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07004273 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004274 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4275 "Test window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08004276
4277 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
4278
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07004279 ui::Transform transform;
4280 transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
4281
4282 gui::DisplayInfo displayInfo;
4283 displayInfo.displayId = ADISPLAY_ID_DEFAULT;
4284 displayInfo.transform = transform;
4285
4286 mDispatcher->onWindowInfosChanged({*window->getInfo()}, {displayInfo});
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08004287
4288 NotifyMotionArgs motionArgs =
4289 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4290 ADISPLAY_ID_DEFAULT);
4291 mDispatcher->notifyMotion(&motionArgs);
4292
4293 InputEvent* event = window->consume();
4294 ASSERT_NE(event, nullptr);
4295
4296 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
4297 ASSERT_NE(verified, nullptr);
4298 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION);
4299
4300 EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos);
4301 EXPECT_EQ(motionArgs.deviceId, verified->deviceId);
4302 EXPECT_EQ(motionArgs.source, verified->source);
4303 EXPECT_EQ(motionArgs.displayId, verified->displayId);
4304
4305 const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified);
4306
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07004307 const vec2 rawXY =
4308 MotionEvent::calculateTransformedXY(motionArgs.source, transform,
4309 motionArgs.pointerCoords[0].getXYValue());
4310 EXPECT_EQ(rawXY.x, verifiedMotion.rawX);
4311 EXPECT_EQ(rawXY.y, verifiedMotion.rawY);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08004312 EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08004313 EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08004314 EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08004315 EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState);
4316 EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState);
4317}
4318
chaviw09c8d2d2020-08-24 15:48:26 -07004319/**
4320 * Ensure that separate calls to sign the same data are generating the same key.
4321 * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance
4322 * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky
4323 * tests.
4324 */
4325TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) {
4326 KeyEvent event = getTestKeyEvent();
4327 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
4328
4329 std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent);
4330 std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent);
4331 ASSERT_EQ(hmac1, hmac2);
4332}
4333
4334/**
4335 * Ensure that changes in VerifiedKeyEvent produce a different hmac.
4336 */
4337TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) {
4338 KeyEvent event = getTestKeyEvent();
4339 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
4340 std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent);
4341
4342 verifiedEvent.deviceId += 1;
4343 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
4344
4345 verifiedEvent.source += 1;
4346 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
4347
4348 verifiedEvent.eventTimeNanos += 1;
4349 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
4350
4351 verifiedEvent.displayId += 1;
4352 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
4353
4354 verifiedEvent.action += 1;
4355 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
4356
4357 verifiedEvent.downTimeNanos += 1;
4358 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
4359
4360 verifiedEvent.flags += 1;
4361 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
4362
4363 verifiedEvent.keyCode += 1;
4364 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
4365
4366 verifiedEvent.scanCode += 1;
4367 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
4368
4369 verifiedEvent.metaState += 1;
4370 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
4371
4372 verifiedEvent.repeatCount += 1;
4373 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
4374}
4375
Vishnu Nair958da932020-08-21 17:12:37 -07004376TEST_F(InputDispatcherTest, SetFocusedWindow) {
4377 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4378 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004379 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07004380 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004381 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07004382 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
4383
4384 // Top window is also focusable but is not granted focus.
4385 windowTop->setFocusable(true);
4386 windowSecond->setFocusable(true);
4387 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
4388 setFocusedWindow(windowSecond);
4389
4390 windowSecond->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004391 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4392 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004393
4394 // Focused window should receive event.
4395 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
4396 windowTop->assertNoEvents();
4397}
4398
4399TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) {
4400 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4401 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004402 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07004403 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
4404
4405 window->setFocusable(true);
4406 // Release channel for window is no longer valid.
4407 window->releaseChannel();
4408 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4409 setFocusedWindow(window);
4410
4411 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004412 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
4413 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07004414
4415 // window channel is invalid, so it should not receive any input event.
4416 window->assertNoEvents();
4417}
4418
4419TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) {
4420 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4421 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004422 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08004423 window->setFocusable(false);
Vishnu Nair958da932020-08-21 17:12:37 -07004424 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
4425
Vishnu Nair958da932020-08-21 17:12:37 -07004426 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4427 setFocusedWindow(window);
4428
4429 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004430 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
4431 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07004432
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08004433 // window is not focusable, so it should not receive any input event.
Vishnu Nair958da932020-08-21 17:12:37 -07004434 window->assertNoEvents();
4435}
4436
4437TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) {
4438 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4439 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004440 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07004441 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004442 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07004443 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
4444
4445 windowTop->setFocusable(true);
4446 windowSecond->setFocusable(true);
4447 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
4448 setFocusedWindow(windowTop);
4449 windowTop->consumeFocusEvent(true);
4450
4451 setFocusedWindow(windowSecond, windowTop);
4452 windowSecond->consumeFocusEvent(true);
4453 windowTop->consumeFocusEvent(false);
4454
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004455 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4456 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004457
4458 // Focused window should receive event.
4459 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
4460}
4461
4462TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestFocusTokenNotFocused) {
4463 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4464 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004465 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07004466 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004467 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07004468 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
4469
4470 windowTop->setFocusable(true);
4471 windowSecond->setFocusable(true);
4472 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
4473 setFocusedWindow(windowSecond, windowTop);
4474
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004475 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
4476 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07004477
4478 // Event should be dropped.
4479 windowTop->assertNoEvents();
4480 windowSecond->assertNoEvents();
4481}
4482
4483TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) {
4484 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4485 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004486 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07004487 sp<FakeWindowHandle> previousFocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004488 sp<FakeWindowHandle>::make(application, mDispatcher, "previousFocusedWindow",
4489 ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07004490 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
4491
4492 window->setFocusable(true);
4493 previousFocusedWindow->setFocusable(true);
4494 window->setVisible(false);
4495 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}});
4496 setFocusedWindow(previousFocusedWindow);
4497 previousFocusedWindow->consumeFocusEvent(true);
4498
4499 // Requesting focus on invisible window takes focus from currently focused window.
4500 setFocusedWindow(window);
4501 previousFocusedWindow->consumeFocusEvent(false);
4502
4503 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004504 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00004505 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
4506 InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07004507
4508 // Window does not get focus event or key down.
4509 window->assertNoEvents();
4510
4511 // Window becomes visible.
4512 window->setVisible(true);
4513 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4514
4515 // Window receives focus event.
4516 window->consumeFocusEvent(true);
4517 // Focused window receives key down.
4518 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4519}
4520
Vishnu Nair599f1412021-06-21 10:39:58 -07004521TEST_F(InputDispatcherTest, DisplayRemoved) {
4522 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4523 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004524 sp<FakeWindowHandle>::make(application, mDispatcher, "window", ADISPLAY_ID_DEFAULT);
Vishnu Nair599f1412021-06-21 10:39:58 -07004525 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
4526
4527 // window is granted focus.
4528 window->setFocusable(true);
4529 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4530 setFocusedWindow(window);
4531 window->consumeFocusEvent(true);
4532
4533 // When a display is removed window loses focus.
4534 mDispatcher->displayRemoved(ADISPLAY_ID_DEFAULT);
4535 window->consumeFocusEvent(false);
4536}
4537
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004538/**
4539 * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY,
4540 * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top
4541 * of the 'slipperyEnterWindow'.
4542 *
4543 * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such
4544 * a way so that the touched location is no longer covered by the top window.
4545 *
4546 * Next, inject a MOVE event. Because the top window already moved earlier, this event is now
4547 * positioned over the bottom (slipperyEnterWindow) only. And because the top window had
4548 * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive
4549 * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting
4550 * with ACTION_DOWN).
4551 * Thus, the touch has been transferred from the top window into the bottom window, because the top
4552 * window moved itself away from the touched location and had Flag::SLIPPERY.
4553 *
4554 * Even though the top window moved away from the touched location, it is still obscuring the bottom
4555 * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_
4556 * OBSCURED should be set for the MotionEvent that reaches the bottom window.
4557 *
4558 * In this test, we ensure that the event received by the bottom window has
4559 * FLAG_WINDOW_IS_PARTIALLY_OBSCURED.
4560 */
4561TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) {
Prabir Pradhan5735a322022-04-11 17:23:34 +00004562 constexpr int32_t SLIPPERY_PID = WINDOW_PID + 1;
4563 constexpr int32_t SLIPPERY_UID = WINDOW_UID + 1;
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004564
4565 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4566 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
4567
4568 sp<FakeWindowHandle> slipperyExitWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004569 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08004570 slipperyExitWindow->setSlippery(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004571 // Make sure this one overlaps the bottom window
4572 slipperyExitWindow->setFrame(Rect(25, 25, 75, 75));
4573 // Change the owner uid/pid of the window so that it is considered to be occluding the bottom
4574 // one. Windows with the same owner are not considered to be occluding each other.
4575 slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID);
4576
4577 sp<FakeWindowHandle> slipperyEnterWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004578 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004579 slipperyExitWindow->setFrame(Rect(0, 0, 100, 100));
4580
4581 mDispatcher->setInputWindows(
4582 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
4583
4584 // Use notifyMotion instead of injecting to avoid dealing with injection permissions
4585 NotifyMotionArgs args = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4586 ADISPLAY_ID_DEFAULT, {{50, 50}});
4587 mDispatcher->notifyMotion(&args);
4588 slipperyExitWindow->consumeMotionDown();
4589 slipperyExitWindow->setFrame(Rect(70, 70, 100, 100));
4590 mDispatcher->setInputWindows(
4591 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
4592
4593 args = generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4594 ADISPLAY_ID_DEFAULT, {{51, 51}});
4595 mDispatcher->notifyMotion(&args);
4596
4597 slipperyExitWindow->consumeMotionCancel();
4598
4599 slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT,
4600 AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
4601}
4602
Garfield Tan1c7bc862020-01-28 13:24:04 -08004603class InputDispatcherKeyRepeatTest : public InputDispatcherTest {
4604protected:
4605 static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms
4606 static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms
4607
Chris Yea209fde2020-07-22 13:54:51 -07004608 std::shared_ptr<FakeApplicationHandle> mApp;
Garfield Tan1c7bc862020-01-28 13:24:04 -08004609 sp<FakeWindowHandle> mWindow;
4610
4611 virtual void SetUp() override {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004612 mFakePolicy = sp<FakeInputDispatcherPolicy>::make();
Garfield Tan1c7bc862020-01-28 13:24:04 -08004613 mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004614 mDispatcher = std::make_unique<InputDispatcher>(mFakePolicy);
Garfield Tan1c7bc862020-01-28 13:24:04 -08004615 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
4616 ASSERT_EQ(OK, mDispatcher->start());
4617
4618 setUpWindow();
4619 }
4620
4621 void setUpWindow() {
Chris Yea209fde2020-07-22 13:54:51 -07004622 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004623 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Garfield Tan1c7bc862020-01-28 13:24:04 -08004624
Vishnu Nair47074b82020-08-14 11:54:47 -07004625 mWindow->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004626 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004627 setFocusedWindow(mWindow);
Garfield Tan1c7bc862020-01-28 13:24:04 -08004628 mWindow->consumeFocusEvent(true);
4629 }
4630
Chris Ye2ad95392020-09-01 13:44:44 -07004631 void sendAndConsumeKeyDown(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08004632 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07004633 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08004634 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event
4635 mDispatcher->notifyKey(&keyArgs);
4636
4637 // Window should receive key down event.
4638 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4639 }
4640
4641 void expectKeyRepeatOnce(int32_t repeatCount) {
4642 SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount));
4643 InputEvent* repeatEvent = mWindow->consume();
4644 ASSERT_NE(nullptr, repeatEvent);
4645
4646 uint32_t eventType = repeatEvent->getType();
4647 ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, eventType);
4648
4649 KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent);
4650 uint32_t eventAction = repeatKeyEvent->getAction();
4651 EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction);
4652 EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount());
4653 }
4654
Chris Ye2ad95392020-09-01 13:44:44 -07004655 void sendAndConsumeKeyUp(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08004656 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07004657 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08004658 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event
4659 mDispatcher->notifyKey(&keyArgs);
4660
4661 // Window should receive key down event.
4662 mWindow->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00004663 /*expectedFlags=*/0);
Garfield Tan1c7bc862020-01-28 13:24:04 -08004664 }
4665};
4666
4667TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) {
Harry Cutts33476232023-01-30 19:57:29 +00004668 sendAndConsumeKeyDown(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07004669 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
4670 expectKeyRepeatOnce(repeatCount);
4671 }
4672}
4673
4674TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) {
Harry Cutts33476232023-01-30 19:57:29 +00004675 sendAndConsumeKeyDown(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07004676 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
4677 expectKeyRepeatOnce(repeatCount);
4678 }
Harry Cutts33476232023-01-30 19:57:29 +00004679 sendAndConsumeKeyDown(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07004680 /* repeatCount will start from 1 for deviceId 2 */
Garfield Tan1c7bc862020-01-28 13:24:04 -08004681 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
4682 expectKeyRepeatOnce(repeatCount);
4683 }
4684}
4685
4686TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) {
Harry Cutts33476232023-01-30 19:57:29 +00004687 sendAndConsumeKeyDown(/*deviceId=*/1);
4688 expectKeyRepeatOnce(/*repeatCount=*/1);
4689 sendAndConsumeKeyUp(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07004690 mWindow->assertNoEvents();
4691}
4692
4693TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) {
Harry Cutts33476232023-01-30 19:57:29 +00004694 sendAndConsumeKeyDown(/*deviceId=*/1);
4695 expectKeyRepeatOnce(/*repeatCount=*/1);
4696 sendAndConsumeKeyDown(/*deviceId=*/2);
4697 expectKeyRepeatOnce(/*repeatCount=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07004698 // Stale key up from device 1.
Harry Cutts33476232023-01-30 19:57:29 +00004699 sendAndConsumeKeyUp(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07004700 // Device 2 is still down, keep repeating
Harry Cutts33476232023-01-30 19:57:29 +00004701 expectKeyRepeatOnce(/*repeatCount=*/2);
4702 expectKeyRepeatOnce(/*repeatCount=*/3);
Chris Ye2ad95392020-09-01 13:44:44 -07004703 // Device 2 key up
Harry Cutts33476232023-01-30 19:57:29 +00004704 sendAndConsumeKeyUp(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07004705 mWindow->assertNoEvents();
4706}
4707
4708TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) {
Harry Cutts33476232023-01-30 19:57:29 +00004709 sendAndConsumeKeyDown(/*deviceId=*/1);
4710 expectKeyRepeatOnce(/*repeatCount=*/1);
4711 sendAndConsumeKeyDown(/*deviceId=*/2);
4712 expectKeyRepeatOnce(/*repeatCount=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07004713 // Device 2 which holds the key repeating goes up, expect the repeating to stop.
Harry Cutts33476232023-01-30 19:57:29 +00004714 sendAndConsumeKeyUp(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07004715 // Device 1 still holds key down, but the repeating was already stopped
Garfield Tan1c7bc862020-01-28 13:24:04 -08004716 mWindow->assertNoEvents();
4717}
4718
liushenxiang42232912021-05-21 20:24:09 +08004719TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) {
4720 sendAndConsumeKeyDown(DEVICE_ID);
Harry Cutts33476232023-01-30 19:57:29 +00004721 expectKeyRepeatOnce(/*repeatCount=*/1);
4722 NotifyDeviceResetArgs args(/*id=*/10, /*eventTime=*/20, DEVICE_ID);
liushenxiang42232912021-05-21 20:24:09 +08004723 mDispatcher->notifyDeviceReset(&args);
4724 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT,
4725 AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS);
4726 mWindow->assertNoEvents();
4727}
4728
Garfield Tan1c7bc862020-01-28 13:24:04 -08004729TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) {
Harry Cutts33476232023-01-30 19:57:29 +00004730 sendAndConsumeKeyDown(/*deviceId=*/1);
Garfield Tan1c7bc862020-01-28 13:24:04 -08004731 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
4732 InputEvent* repeatEvent = mWindow->consume();
4733 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
4734 EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER,
4735 IdGenerator::getSource(repeatEvent->getId()));
4736 }
4737}
4738
4739TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) {
Harry Cutts33476232023-01-30 19:57:29 +00004740 sendAndConsumeKeyDown(/*deviceId=*/1);
Garfield Tan1c7bc862020-01-28 13:24:04 -08004741
4742 std::unordered_set<int32_t> idSet;
4743 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
4744 InputEvent* repeatEvent = mWindow->consume();
4745 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
4746 int32_t id = repeatEvent->getId();
4747 EXPECT_EQ(idSet.end(), idSet.find(id));
4748 idSet.insert(id);
4749 }
4750}
4751
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004752/* Test InputDispatcher for MultiDisplay */
4753class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest {
4754public:
Prabir Pradhan3608aad2019-10-02 17:08:26 -07004755 virtual void SetUp() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004756 InputDispatcherTest::SetUp();
Arthur Hungb92218b2018-08-14 12:00:21 +08004757
Chris Yea209fde2020-07-22 13:54:51 -07004758 application1 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004759 windowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004760 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08004761
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004762 // Set focus window for primary display, but focused display would be second one.
4763 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1);
Vishnu Nair47074b82020-08-14 11:54:47 -07004764 windowInPrimary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004765 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004766 setFocusedWindow(windowInPrimary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004767 windowInPrimary->consumeFocusEvent(true);
Arthur Hungb92218b2018-08-14 12:00:21 +08004768
Chris Yea209fde2020-07-22 13:54:51 -07004769 application2 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004770 windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004771 sp<FakeWindowHandle>::make(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004772 // Set focus to second display window.
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004773 // Set focus display to second one.
4774 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
4775 // Set focus window for second display.
4776 mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2);
Vishnu Nair47074b82020-08-14 11:54:47 -07004777 windowInSecondary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004778 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004779 setFocusedWindow(windowInSecondary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004780 windowInSecondary->consumeFocusEvent(true);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004781 }
4782
Prabir Pradhan3608aad2019-10-02 17:08:26 -07004783 virtual void TearDown() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004784 InputDispatcherTest::TearDown();
4785
Chris Yea209fde2020-07-22 13:54:51 -07004786 application1.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004787 windowInPrimary.clear();
Chris Yea209fde2020-07-22 13:54:51 -07004788 application2.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004789 windowInSecondary.clear();
4790 }
4791
4792protected:
Chris Yea209fde2020-07-22 13:54:51 -07004793 std::shared_ptr<FakeApplicationHandle> application1;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004794 sp<FakeWindowHandle> windowInPrimary;
Chris Yea209fde2020-07-22 13:54:51 -07004795 std::shared_ptr<FakeApplicationHandle> application2;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004796 sp<FakeWindowHandle> windowInSecondary;
4797};
4798
4799TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) {
4800 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004801 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4802 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
4803 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004804 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08004805 windowInSecondary->assertNoEvents();
4806
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004807 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004808 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4809 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
4810 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08004811 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004812 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +08004813}
4814
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004815TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) {
Tiger Huang721e26f2018-07-24 22:26:19 +08004816 // Test inject a key down with display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08004817 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4818 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004819 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004820 windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Tiger Huang721e26f2018-07-24 22:26:19 +08004821 windowInSecondary->assertNoEvents();
4822
4823 // Test inject a key down without display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08004824 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004825 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08004826 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004827 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hungb92218b2018-08-14 12:00:21 +08004828
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08004829 // Remove all windows in secondary display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00004830 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}});
Arthur Hungb92218b2018-08-14 12:00:21 +08004831
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004832 // Old focus should receive a cancel event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004833 windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE,
4834 AKEY_EVENT_FLAG_CANCELED);
Arthur Hungb92218b2018-08-14 12:00:21 +08004835
4836 // Test inject a key down, should timeout because of no target window.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08004837 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004838 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Arthur Hungb92218b2018-08-14 12:00:21 +08004839 windowInPrimary->assertNoEvents();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004840 windowInSecondary->consumeFocusEvent(false);
Arthur Hungb92218b2018-08-14 12:00:21 +08004841 windowInSecondary->assertNoEvents();
4842}
4843
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004844// Test per-display input monitors for motion event.
4845TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) {
chaviwd1c23182019-12-20 18:44:56 -08004846 FakeMonitorReceiver monitorInPrimary =
4847 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
4848 FakeMonitorReceiver monitorInSecondary =
4849 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004850
4851 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004852 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4853 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
4854 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004855 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08004856 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004857 windowInSecondary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08004858 monitorInSecondary.assertNoEvents();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004859
4860 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004861 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4862 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
4863 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004864 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08004865 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004866 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
chaviwd1c23182019-12-20 18:44:56 -08004867 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004868
4869 // Test inject a non-pointer motion event.
4870 // If specific a display, it will dispatch to the focused window of particular display,
4871 // or it will dispatch to the focused window of focused display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004872 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4873 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE))
4874 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004875 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08004876 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004877 windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08004878 monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004879}
4880
4881// Test per-display input monitors for key event.
4882TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) {
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004883 // Input monitor per display.
chaviwd1c23182019-12-20 18:44:56 -08004884 FakeMonitorReceiver monitorInPrimary =
4885 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
4886 FakeMonitorReceiver monitorInSecondary =
4887 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004888
4889 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004890 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4891 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004892 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08004893 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004894 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08004895 monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004896}
4897
Vishnu Nair958da932020-08-21 17:12:37 -07004898TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) {
4899 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004900 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07004901 secondWindowInPrimary->setFocusable(true);
4902 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}});
4903 setFocusedWindow(secondWindowInPrimary);
4904 windowInPrimary->consumeFocusEvent(false);
4905 secondWindowInPrimary->consumeFocusEvent(true);
4906
4907 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004908 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
4909 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004910 windowInPrimary->assertNoEvents();
4911 windowInSecondary->assertNoEvents();
4912 secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4913}
4914
Arthur Hungdfd528e2021-12-08 13:23:04 +00004915TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CancelTouch_MultiDisplay) {
4916 FakeMonitorReceiver monitorInPrimary =
4917 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
4918 FakeMonitorReceiver monitorInSecondary =
4919 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
4920
4921 // Test touch down on primary display.
4922 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4923 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
4924 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4925 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4926 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
4927
4928 // Test touch down on second display.
4929 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4930 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
4931 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4932 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
4933 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
4934
4935 // Trigger cancel touch.
4936 mDispatcher->cancelCurrentTouch();
4937 windowInPrimary->consumeMotionCancel(ADISPLAY_ID_DEFAULT);
4938 monitorInPrimary.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
4939 windowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID);
4940 monitorInSecondary.consumeMotionCancel(SECOND_DISPLAY_ID);
4941
4942 // Test inject a move motion event, no window/monitor should receive the event.
4943 ASSERT_EQ(InputEventInjectionResult::FAILED,
4944 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4945 ADISPLAY_ID_DEFAULT, {110, 200}))
4946 << "Inject motion event should return InputEventInjectionResult::FAILED";
4947 windowInPrimary->assertNoEvents();
4948 monitorInPrimary.assertNoEvents();
4949
4950 ASSERT_EQ(InputEventInjectionResult::FAILED,
4951 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4952 SECOND_DISPLAY_ID, {110, 200}))
4953 << "Inject motion event should return InputEventInjectionResult::FAILED";
4954 windowInSecondary->assertNoEvents();
4955 monitorInSecondary.assertNoEvents();
4956}
4957
Jackal Guof9696682018-10-05 12:23:23 +08004958class InputFilterTest : public InputDispatcherTest {
4959protected:
Prabir Pradhan81420cc2021-09-06 10:28:50 -07004960 void testNotifyMotion(int32_t displayId, bool expectToBeFiltered,
4961 const ui::Transform& transform = ui::Transform()) {
Jackal Guof9696682018-10-05 12:23:23 +08004962 NotifyMotionArgs motionArgs;
4963
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004964 motionArgs =
4965 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Jackal Guof9696682018-10-05 12:23:23 +08004966 mDispatcher->notifyMotion(&motionArgs);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004967 motionArgs =
4968 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Jackal Guof9696682018-10-05 12:23:23 +08004969 mDispatcher->notifyMotion(&motionArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004970 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08004971 if (expectToBeFiltered) {
Prabir Pradhan81420cc2021-09-06 10:28:50 -07004972 const auto xy = transform.transform(motionArgs.pointerCoords->getXYValue());
4973 mFakePolicy->assertFilterInputEventWasCalled(motionArgs, xy);
Jackal Guof9696682018-10-05 12:23:23 +08004974 } else {
4975 mFakePolicy->assertFilterInputEventWasNotCalled();
4976 }
4977 }
4978
4979 void testNotifyKey(bool expectToBeFiltered) {
4980 NotifyKeyArgs keyArgs;
4981
4982 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
4983 mDispatcher->notifyKey(&keyArgs);
4984 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP);
4985 mDispatcher->notifyKey(&keyArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004986 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08004987
4988 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08004989 mFakePolicy->assertFilterInputEventWasCalled(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08004990 } else {
4991 mFakePolicy->assertFilterInputEventWasNotCalled();
4992 }
4993 }
4994};
4995
4996// Test InputFilter for MotionEvent
4997TEST_F(InputFilterTest, MotionEvent_InputFilter) {
4998 // Since the InputFilter is disabled by default, check if touch events aren't filtered.
4999 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
5000 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
5001
5002 // Enable InputFilter
5003 mDispatcher->setInputFilterEnabled(true);
5004 // Test touch on both primary and second display, and check if both events are filtered.
5005 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true);
5006 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true);
5007
5008 // Disable InputFilter
5009 mDispatcher->setInputFilterEnabled(false);
5010 // Test touch on both primary and second display, and check if both events aren't filtered.
5011 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
5012 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
5013}
5014
5015// Test InputFilter for KeyEvent
5016TEST_F(InputFilterTest, KeyEvent_InputFilter) {
5017 // Since the InputFilter is disabled by default, check if key event aren't filtered.
5018 testNotifyKey(/*expectToBeFiltered*/ false);
5019
5020 // Enable InputFilter
5021 mDispatcher->setInputFilterEnabled(true);
5022 // Send a key event, and check if it is filtered.
5023 testNotifyKey(/*expectToBeFiltered*/ true);
5024
5025 // Disable InputFilter
5026 mDispatcher->setInputFilterEnabled(false);
5027 // Send a key event, and check if it isn't filtered.
5028 testNotifyKey(/*expectToBeFiltered*/ false);
5029}
5030
Prabir Pradhan81420cc2021-09-06 10:28:50 -07005031// Ensure that MotionEvents sent to the InputFilter through InputListener are converted to the
5032// logical display coordinate space.
5033TEST_F(InputFilterTest, MotionEvent_UsesLogicalDisplayCoordinates_notifyMotion) {
5034 ui::Transform firstDisplayTransform;
5035 firstDisplayTransform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
5036 ui::Transform secondDisplayTransform;
5037 secondDisplayTransform.set({-6.6, -5.5, -4.4, -3.3, -2.2, -1.1, 0, 0, 1});
5038
5039 std::vector<gui::DisplayInfo> displayInfos(2);
5040 displayInfos[0].displayId = ADISPLAY_ID_DEFAULT;
5041 displayInfos[0].transform = firstDisplayTransform;
5042 displayInfos[1].displayId = SECOND_DISPLAY_ID;
5043 displayInfos[1].transform = secondDisplayTransform;
5044
5045 mDispatcher->onWindowInfosChanged({}, displayInfos);
5046
5047 // Enable InputFilter
5048 mDispatcher->setInputFilterEnabled(true);
5049
5050 // Ensure the correct transforms are used for the displays.
5051 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true, firstDisplayTransform);
5052 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true, secondDisplayTransform);
5053}
5054
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005055class InputFilterInjectionPolicyTest : public InputDispatcherTest {
5056protected:
5057 virtual void SetUp() override {
5058 InputDispatcherTest::SetUp();
5059
5060 /**
5061 * We don't need to enable input filter to test the injected event policy, but we enabled it
5062 * here to make the tests more realistic, since this policy only matters when inputfilter is
5063 * on.
5064 */
5065 mDispatcher->setInputFilterEnabled(true);
5066
5067 std::shared_ptr<InputApplicationHandle> application =
5068 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005069 mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Test Window",
5070 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005071
5072 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5073 mWindow->setFocusable(true);
5074 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
5075 setFocusedWindow(mWindow);
5076 mWindow->consumeFocusEvent(true);
5077 }
5078
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005079 void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
5080 int32_t flags) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005081 KeyEvent event;
5082
5083 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
5084 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD,
5085 ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A,
Harry Cutts33476232023-01-30 19:57:29 +00005086 KEY_A, AMETA_NONE, /*repeatCount=*/0, eventTime, eventTime);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005087 const int32_t additionalPolicyFlags =
5088 POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT;
5089 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00005090 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005091 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
5092 policyFlags | additionalPolicyFlags));
5093
5094 InputEvent* received = mWindow->consume();
5095 ASSERT_NE(nullptr, received);
5096 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005097 ASSERT_EQ(received->getType(), AINPUT_EVENT_TYPE_KEY);
5098 KeyEvent& keyEvent = static_cast<KeyEvent&>(*received);
5099 ASSERT_EQ(flags, keyEvent.getFlags());
5100 }
5101
5102 void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
5103 int32_t flags) {
5104 MotionEvent event;
5105 PointerProperties pointerProperties[1];
5106 PointerCoords pointerCoords[1];
5107 pointerProperties[0].clear();
5108 pointerProperties[0].id = 0;
5109 pointerCoords[0].clear();
5110 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300);
5111 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400);
5112
5113 ui::Transform identityTransform;
5114 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
5115 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN,
5116 DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0,
5117 AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE,
5118 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -07005119 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, eventTime,
Evan Rosky09576692021-07-01 12:22:09 -07005120 eventTime,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005121 /*pointerCount*/ 1, pointerProperties, pointerCoords);
5122
5123 const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER;
5124 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00005125 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005126 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
5127 policyFlags | additionalPolicyFlags));
5128
5129 InputEvent* received = mWindow->consume();
5130 ASSERT_NE(nullptr, received);
5131 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
5132 ASSERT_EQ(received->getType(), AINPUT_EVENT_TYPE_MOTION);
5133 MotionEvent& motionEvent = static_cast<MotionEvent&>(*received);
5134 ASSERT_EQ(flags, motionEvent.getFlags());
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005135 }
5136
5137private:
5138 sp<FakeWindowHandle> mWindow;
5139};
5140
5141TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) {
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005142 // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input
5143 // filter. Without it, the event will no different from a regularly injected event, and the
5144 // injected device id will be overwritten.
Harry Cutts33476232023-01-30 19:57:29 +00005145 testInjectedKey(POLICY_FLAG_FILTERED, /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
5146 /*flags=*/0);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005147}
5148
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005149TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005150 testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Harry Cutts33476232023-01-30 19:57:29 +00005151 /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005152 AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
5153}
5154
5155TEST_F(InputFilterInjectionPolicyTest,
5156 MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
5157 testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Harry Cutts33476232023-01-30 19:57:29 +00005158 /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005159 AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005160}
5161
5162TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) {
Harry Cutts33476232023-01-30 19:57:29 +00005163 testInjectedKey(/*policyFlags=*/0, /*injectedDeviceId=*/3,
5164 /*resolvedDeviceId=*/VIRTUAL_KEYBOARD_ID, /*flags=*/0);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005165}
5166
chaviwfd6d3512019-03-25 13:23:49 -07005167class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest {
Prabir Pradhan3608aad2019-10-02 17:08:26 -07005168 virtual void SetUp() override {
chaviwfd6d3512019-03-25 13:23:49 -07005169 InputDispatcherTest::SetUp();
5170
Chris Yea209fde2020-07-22 13:54:51 -07005171 std::shared_ptr<FakeApplicationHandle> application =
5172 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005173 mUnfocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005174 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07005175 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
chaviwfd6d3512019-03-25 13:23:49 -07005176
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005177 mFocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005178 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005179 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
chaviwfd6d3512019-03-25 13:23:49 -07005180
5181 // Set focused application.
5182 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07005183 mFocusedWindow->setFocusable(true);
chaviwfd6d3512019-03-25 13:23:49 -07005184
5185 // Expect one focus window exist in display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00005186 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005187 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005188 mFocusedWindow->consumeFocusEvent(true);
chaviwfd6d3512019-03-25 13:23:49 -07005189 }
5190
Prabir Pradhan3608aad2019-10-02 17:08:26 -07005191 virtual void TearDown() override {
chaviwfd6d3512019-03-25 13:23:49 -07005192 InputDispatcherTest::TearDown();
5193
5194 mUnfocusedWindow.clear();
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005195 mFocusedWindow.clear();
chaviwfd6d3512019-03-25 13:23:49 -07005196 }
5197
5198protected:
5199 sp<FakeWindowHandle> mUnfocusedWindow;
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005200 sp<FakeWindowHandle> mFocusedWindow;
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07005201 static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60};
chaviwfd6d3512019-03-25 13:23:49 -07005202};
5203
5204// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
5205// DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received
5206// the onPointerDownOutsideFocus callback.
5207TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005208 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07005209 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5210 {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005211 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07005212 mUnfocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07005213
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08005214 ASSERT_TRUE(mDispatcher->waitForIdle());
chaviwfd6d3512019-03-25 13:23:49 -07005215 mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken());
5216}
5217
5218// Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action
5219// DOWN on the window that doesn't have focus. Ensure no window received the
5220// onPointerDownOutsideFocus callback.
5221TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005222 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07005223 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005224 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07005225 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07005226
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08005227 ASSERT_TRUE(mDispatcher->waitForIdle());
5228 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07005229}
5230
5231// Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't
5232// have focus. Ensure no window received the onPointerDownOutsideFocus callback.
5233TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005234 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5235 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005236 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07005237 mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07005238
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08005239 ASSERT_TRUE(mDispatcher->waitForIdle());
5240 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07005241}
5242
5243// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
5244// DOWN on the window that already has focus. Ensure no window received the
5245// onPointerDownOutsideFocus callback.
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005246TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005247 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005248 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07005249 FOCUSED_WINDOW_TOUCH_POINT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005250 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07005251 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07005252
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08005253 ASSERT_TRUE(mDispatcher->waitForIdle());
5254 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07005255}
5256
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08005257// Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag
5258// NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback.
5259TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) {
5260 const MotionEvent event =
5261 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
5262 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Harry Cutts33476232023-01-30 19:57:29 +00005263 .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(20).y(20))
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08005264 .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE)
5265 .build();
5266 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(mDispatcher, event))
5267 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5268 mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
5269
5270 ASSERT_TRUE(mDispatcher->waitForIdle());
5271 mFakePolicy->assertOnPointerDownWasNotCalled();
5272 // Ensure that the unfocused window did not receive any FOCUS events.
5273 mUnfocusedWindow->assertNoEvents();
5274}
5275
chaviwaf87b3e2019-10-01 16:59:28 -07005276// These tests ensures we can send touch events to a single client when there are multiple input
5277// windows that point to the same client token.
5278class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest {
5279 virtual void SetUp() override {
5280 InputDispatcherTest::SetUp();
5281
Chris Yea209fde2020-07-22 13:54:51 -07005282 std::shared_ptr<FakeApplicationHandle> application =
5283 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005284 mWindow1 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 1",
5285 ADISPLAY_ID_DEFAULT);
chaviwaf87b3e2019-10-01 16:59:28 -07005286 mWindow1->setFrame(Rect(0, 0, 100, 100));
5287
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005288 mWindow2 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 2",
5289 ADISPLAY_ID_DEFAULT, mWindow1->getToken());
chaviwaf87b3e2019-10-01 16:59:28 -07005290 mWindow2->setFrame(Rect(100, 100, 200, 200));
5291
Arthur Hung72d8dc32020-03-28 00:48:39 +00005292 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
chaviwaf87b3e2019-10-01 16:59:28 -07005293 }
5294
5295protected:
5296 sp<FakeWindowHandle> mWindow1;
5297 sp<FakeWindowHandle> mWindow2;
5298
5299 // Helper function to convert the point from screen coordinates into the window's space
chaviw3277faf2021-05-19 16:45:23 -05005300 static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) {
chaviw1ff3d1e2020-07-01 15:53:47 -07005301 vec2 vals = windowInfo->transform.transform(point.x, point.y);
5302 return {vals.x, vals.y};
chaviwaf87b3e2019-10-01 16:59:28 -07005303 }
5304
5305 void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction,
5306 const std::vector<PointF>& points) {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005307 const std::string name = window->getName();
chaviwaf87b3e2019-10-01 16:59:28 -07005308 InputEvent* event = window->consume();
5309
5310 ASSERT_NE(nullptr, event) << name.c_str()
5311 << ": consumer should have returned non-NULL event.";
5312
5313 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType())
5314 << name.c_str() << "expected " << inputEventTypeToString(AINPUT_EVENT_TYPE_MOTION)
5315 << " event, got " << inputEventTypeToString(event->getType()) << " event";
5316
5317 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005318 assertMotionAction(expectedAction, motionEvent.getAction());
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08005319 ASSERT_EQ(points.size(), motionEvent.getPointerCount());
chaviwaf87b3e2019-10-01 16:59:28 -07005320
5321 for (size_t i = 0; i < points.size(); i++) {
5322 float expectedX = points[i].x;
5323 float expectedY = points[i].y;
5324
5325 EXPECT_EQ(expectedX, motionEvent.getX(i))
5326 << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str()
5327 << ", got " << motionEvent.getX(i);
5328 EXPECT_EQ(expectedY, motionEvent.getY(i))
5329 << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str()
5330 << ", got " << motionEvent.getY(i);
5331 }
5332 }
chaviw9eaa22c2020-07-01 16:21:27 -07005333
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08005334 void touchAndAssertPositions(int32_t action, const std::vector<PointF>& touchedPoints,
chaviw9eaa22c2020-07-01 16:21:27 -07005335 std::vector<PointF> expectedPoints) {
5336 NotifyMotionArgs motionArgs = generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN,
5337 ADISPLAY_ID_DEFAULT, touchedPoints);
5338 mDispatcher->notifyMotion(&motionArgs);
5339
5340 // Always consume from window1 since it's the window that has the InputReceiver
5341 consumeMotionEvent(mWindow1, action, expectedPoints);
5342 }
chaviwaf87b3e2019-10-01 16:59:28 -07005343};
5344
5345TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) {
5346 // Touch Window 1
5347 PointF touchedPoint = {10, 10};
5348 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07005349 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07005350
5351 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07005352 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07005353
5354 // Touch Window 2
5355 touchedPoint = {150, 150};
5356 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07005357 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07005358}
5359
chaviw9eaa22c2020-07-01 16:21:27 -07005360TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) {
5361 // Set scale value for window2
chaviwaf87b3e2019-10-01 16:59:28 -07005362 mWindow2->setWindowScale(0.5f, 0.5f);
5363
5364 // Touch Window 1
5365 PointF touchedPoint = {10, 10};
5366 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07005367 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07005368 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07005369 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07005370
5371 // Touch Window 2
5372 touchedPoint = {150, 150};
5373 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07005374 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
5375 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07005376
chaviw9eaa22c2020-07-01 16:21:27 -07005377 // Update the transform so rotation is set
5378 mWindow2->setWindowTransform(0, -1, 1, 0);
5379 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
5380 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07005381}
5382
chaviw9eaa22c2020-07-01 16:21:27 -07005383TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00005384 mWindow2->setWindowScale(0.5f, 0.5f);
5385
5386 // Touch Window 1
5387 std::vector<PointF> touchedPoints = {PointF{10, 10}};
5388 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07005389 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00005390
5391 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07005392 touchedPoints.push_back(PointF{150, 150});
5393 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08005394 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00005395
chaviw9eaa22c2020-07-01 16:21:27 -07005396 // Release Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08005397 touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07005398 expectedPoints.pop_back();
Chavi Weingarten65f98b82020-01-16 18:56:50 +00005399
chaviw9eaa22c2020-07-01 16:21:27 -07005400 // Update the transform so rotation is set for Window 2
5401 mWindow2->setWindowTransform(0, -1, 1, 0);
5402 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08005403 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00005404}
5405
chaviw9eaa22c2020-07-01 16:21:27 -07005406TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00005407 mWindow2->setWindowScale(0.5f, 0.5f);
5408
5409 // Touch Window 1
5410 std::vector<PointF> touchedPoints = {PointF{10, 10}};
5411 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07005412 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00005413
5414 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07005415 touchedPoints.push_back(PointF{150, 150});
5416 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00005417
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08005418 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00005419
5420 // Move both windows
5421 touchedPoints = {{20, 20}, {175, 175}};
5422 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
5423 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
5424
chaviw9eaa22c2020-07-01 16:21:27 -07005425 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00005426
chaviw9eaa22c2020-07-01 16:21:27 -07005427 // Release Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08005428 touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07005429 expectedPoints.pop_back();
5430
5431 // Touch Window 2
5432 mWindow2->setWindowTransform(0, -1, 1, 0);
5433 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08005434 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07005435
5436 // Move both windows
5437 touchedPoints = {{20, 20}, {175, 175}};
5438 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
5439 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
5440
5441 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00005442}
5443
5444TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) {
5445 mWindow1->setWindowScale(0.5f, 0.5f);
5446
5447 // Touch Window 1
5448 std::vector<PointF> touchedPoints = {PointF{10, 10}};
5449 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07005450 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00005451
5452 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07005453 touchedPoints.push_back(PointF{150, 150});
5454 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00005455
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08005456 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00005457
5458 // Move both windows
5459 touchedPoints = {{20, 20}, {175, 175}};
5460 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
5461 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
5462
chaviw9eaa22c2020-07-01 16:21:27 -07005463 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00005464}
5465
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005466class InputDispatcherSingleWindowAnr : public InputDispatcherTest {
5467 virtual void SetUp() override {
5468 InputDispatcherTest::SetUp();
5469
Chris Yea209fde2020-07-22 13:54:51 -07005470 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005471 mApplication->setDispatchingTimeout(20ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005472 mWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "TestWindow",
5473 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005474 mWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05005475 mWindow->setDispatchingTimeout(30ms);
Vishnu Nair47074b82020-08-14 11:54:47 -07005476 mWindow->setFocusable(true);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005477
5478 // Set focused application.
5479 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
5480
5481 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005482 setFocusedWindow(mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005483 mWindow->consumeFocusEvent(true);
5484 }
5485
5486 virtual void TearDown() override {
5487 InputDispatcherTest::TearDown();
5488 mWindow.clear();
5489 }
5490
5491protected:
Chris Yea209fde2020-07-22 13:54:51 -07005492 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005493 sp<FakeWindowHandle> mWindow;
5494 static constexpr PointF WINDOW_LOCATION = {20, 20};
5495
5496 void tapOnWindow() {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005497 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005498 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5499 WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005500 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005501 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5502 WINDOW_LOCATION));
5503 }
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005504
5505 sp<FakeWindowHandle> addSpyWindow() {
5506 sp<FakeWindowHandle> spy =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005507 sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005508 spy->setTrustedOverlay(true);
5509 spy->setFocusable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08005510 spy->setSpy(true);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005511 spy->setDispatchingTimeout(30ms);
5512 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, mWindow}}});
5513 return spy;
5514 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005515};
5516
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005517// Send a tap and respond, which should not cause an ANR.
5518TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) {
5519 tapOnWindow();
5520 mWindow->consumeMotionDown();
5521 mWindow->consumeMotionUp();
5522 ASSERT_TRUE(mDispatcher->waitForIdle());
5523 mFakePolicy->assertNotifyAnrWasNotCalled();
5524}
5525
5526// Send a regular key and respond, which should not cause an ANR.
5527TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005528 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005529 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
5530 ASSERT_TRUE(mDispatcher->waitForIdle());
5531 mFakePolicy->assertNotifyAnrWasNotCalled();
5532}
5533
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05005534TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) {
5535 mWindow->setFocusable(false);
5536 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
5537 mWindow->consumeFocusEvent(false);
5538
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005539 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00005540 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
5541 InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms,
5542 /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005543 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05005544 // Key will not go to window because we have no focused window.
5545 // The 'no focused window' ANR timer should start instead.
5546
5547 // Now, the focused application goes away.
5548 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr);
5549 // The key should get dropped and there should be no ANR.
5550
5551 ASSERT_TRUE(mDispatcher->waitForIdle());
5552 mFakePolicy->assertNotifyAnrWasNotCalled();
5553}
5554
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005555// Send an event to the app and have the app not respond right away.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005556// When ANR is raised, policy will tell the dispatcher to cancel the events for that window.
5557// So InputDispatcher will enqueue ACTION_CANCEL event as well.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005558TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005559 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005560 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5561 WINDOW_LOCATION));
5562
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005563 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN
5564 ASSERT_TRUE(sequenceNum);
5565 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005566 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005567
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005568 mWindow->finishEvent(*sequenceNum);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08005569 mWindow->consumeMotionEvent(
5570 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005571 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08005572 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005573}
5574
5575// Send a key to the app and have the app not respond right away.
5576TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) {
5577 // Inject a key, and don't respond - expect that ANR is called.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005578 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005579 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent();
5580 ASSERT_TRUE(sequenceNum);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005581 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005582 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005583 ASSERT_TRUE(mDispatcher->waitForIdle());
5584}
5585
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005586// We have a focused application, but no focused window
5587TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) {
Vishnu Nair47074b82020-08-14 11:54:47 -07005588 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005589 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
5590 mWindow->consumeFocusEvent(false);
5591
5592 // taps on the window work as normal
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005593 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005594 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5595 WINDOW_LOCATION));
5596 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
5597 mDispatcher->waitForIdle();
5598 mFakePolicy->assertNotifyAnrWasNotCalled();
5599
5600 // Once a focused event arrives, we get an ANR for this application
5601 // We specify the injection timeout to be smaller than the application timeout, to ensure that
5602 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005603 const InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00005604 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
5605 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005606 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005607 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Vishnu Naire4df8752022-09-08 09:17:55 -07005608 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005609 ASSERT_TRUE(mDispatcher->waitForIdle());
5610}
5611
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08005612/**
5613 * Make sure the stale key is dropped before causing an ANR. So even if there's no focused window,
5614 * there will not be an ANR.
5615 */
5616TEST_F(InputDispatcherSingleWindowAnr, StaleKeyEventDoesNotAnr) {
5617 mWindow->setFocusable(false);
5618 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
5619 mWindow->consumeFocusEvent(false);
5620
5621 KeyEvent event;
5622 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC) -
5623 std::chrono::nanoseconds(STALE_EVENT_TIMEOUT).count();
5624
5625 // Define a valid key down event that is stale (too old).
5626 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
5627 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, /* flags */ 0, AKEYCODE_A, KEY_A,
Harry Cutts33476232023-01-30 19:57:29 +00005628 AMETA_NONE, /*repeatCount=*/1, eventTime, eventTime);
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08005629
5630 const int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER;
5631
5632 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00005633 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08005634 InputEventInjectionSync::WAIT_FOR_RESULT,
5635 INJECT_EVENT_TIMEOUT, policyFlags);
5636 ASSERT_EQ(InputEventInjectionResult::FAILED, result)
5637 << "Injection should fail because the event is stale";
5638
5639 ASSERT_TRUE(mDispatcher->waitForIdle());
5640 mFakePolicy->assertNotifyAnrWasNotCalled();
5641 mWindow->assertNoEvents();
5642}
5643
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005644// We have a focused application, but no focused window
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005645// Make sure that we don't notify policy twice about the same ANR.
5646TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) {
Vishnu Nair47074b82020-08-14 11:54:47 -07005647 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005648 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
5649 mWindow->consumeFocusEvent(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005650
5651 // Once a focused event arrives, we get an ANR for this application
5652 // We specify the injection timeout to be smaller than the application timeout, to ensure that
5653 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005654 const InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00005655 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
5656 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005657 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Vishnu Naire4df8752022-09-08 09:17:55 -07005658 const std::chrono::duration appTimeout =
5659 mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
5660 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005661
Vishnu Naire4df8752022-09-08 09:17:55 -07005662 std::this_thread::sleep_for(appTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005663 // ANR should not be raised again. It is up to policy to do that if it desires.
5664 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005665
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005666 // If we now get a focused window, the ANR should stop, but the policy handles that via
5667 // 'notifyFocusChanged' callback. This is implemented in the policy so we can't test it here.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005668 ASSERT_TRUE(mDispatcher->waitForIdle());
5669}
5670
5671// We have a focused application, but no focused window
5672TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) {
Vishnu Nair47074b82020-08-14 11:54:47 -07005673 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005674 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
5675 mWindow->consumeFocusEvent(false);
5676
5677 // Once a focused event arrives, we get an ANR for this application
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005678 const InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00005679 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005680 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
5681 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005682
Vishnu Naire4df8752022-09-08 09:17:55 -07005683 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
5684 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005685
5686 // Future focused events get dropped right away
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005687 ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005688 ASSERT_TRUE(mDispatcher->waitForIdle());
5689 mWindow->assertNoEvents();
5690}
5691
5692/**
5693 * Ensure that the implementation is valid. Since we are using multiset to keep track of the
5694 * ANR timeouts, we are allowing entries with identical timestamps in the same connection.
5695 * If we process 1 of the events, but ANR on the second event with the same timestamp,
5696 * the ANR mechanism should still work.
5697 *
5698 * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the
5699 * DOWN event, while not responding on the second one.
5700 */
5701TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) {
5702 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
5703 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5704 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
5705 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
5706 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005707 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005708
5709 // Now send ACTION_UP, with identical timestamp
5710 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
5711 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
5712 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
5713 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005714 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005715
5716 // We have now sent down and up. Let's consume first event and then ANR on the second.
5717 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5718 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005719 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005720}
5721
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005722// A spy window can receive an ANR
5723TEST_F(InputDispatcherSingleWindowAnr, SpyWindowAnr) {
5724 sp<FakeWindowHandle> spy = addSpyWindow();
5725
5726 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5727 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5728 WINDOW_LOCATION));
5729 mWindow->consumeMotionDown();
5730
5731 std::optional<uint32_t> sequenceNum = spy->receiveEvent(); // ACTION_DOWN
5732 ASSERT_TRUE(sequenceNum);
5733 const std::chrono::duration timeout = spy->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005734 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, spy);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005735
5736 spy->finishEvent(*sequenceNum);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08005737 spy->consumeMotionEvent(
5738 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005739 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08005740 mFakePolicy->assertNotifyWindowResponsiveWasCalled(spy->getToken(), mWindow->getPid());
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005741}
5742
5743// If an app is not responding to a key event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005744// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005745TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnKey) {
5746 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005747
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005748 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5749 injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005750 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005751 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005752
5753 // Stuck on the ACTION_UP
5754 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005755 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005756
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005757 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005758 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005759 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5760 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005761
5762 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion
5763 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08005764 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005765 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005766 spy->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005767}
5768
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005769// If an app is not responding to a motion event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005770// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005771TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnMotion) {
5772 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005773
5774 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005775 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5776 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005777
5778 mWindow->consumeMotionDown();
5779 // Stuck on the ACTION_UP
5780 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005781 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005782
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005783 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005784 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005785 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5786 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005787
5788 mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion
5789 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08005790 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005791 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005792 spy->assertNoEvents();
5793}
5794
5795TEST_F(InputDispatcherSingleWindowAnr, UnresponsiveMonitorAnr) {
5796 mDispatcher->setMonitorDispatchingTimeoutForTest(30ms);
5797
5798 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
5799
5800 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5801 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5802 WINDOW_LOCATION));
5803
5804 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5805 const std::optional<uint32_t> consumeSeq = monitor.receiveEvent();
5806 ASSERT_TRUE(consumeSeq);
5807
Prabir Pradhanedd96402022-02-15 01:46:16 -08005808 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(30ms, monitor.getToken(), MONITOR_PID);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005809
5810 monitor.finishEvent(*consumeSeq);
5811 monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
5812
5813 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08005814 mFakePolicy->assertNotifyWindowResponsiveWasCalled(monitor.getToken(), MONITOR_PID);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005815}
5816
5817// If a window is unresponsive, then you get anr. if the window later catches up and starts to
5818// process events, you don't get an anr. When the window later becomes unresponsive again, you
5819// get an ANR again.
5820// 1. tap -> block on ACTION_UP -> receive ANR
5821// 2. consume all pending events (= queue becomes healthy again)
5822// 3. tap again -> block on ACTION_UP again -> receive ANR second time
5823TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) {
5824 tapOnWindow();
5825
5826 mWindow->consumeMotionDown();
5827 // Block on ACTION_UP
5828 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005829 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005830 mWindow->consumeMotionUp(); // Now the connection should be healthy again
5831 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08005832 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005833 mWindow->assertNoEvents();
5834
5835 tapOnWindow();
5836 mWindow->consumeMotionDown();
Prabir Pradhanedd96402022-02-15 01:46:16 -08005837 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005838 mWindow->consumeMotionUp();
5839
5840 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08005841 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005842 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005843 mWindow->assertNoEvents();
5844}
5845
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005846// If a connection remains unresponsive for a while, make sure policy is only notified once about
5847// it.
5848TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005849 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005850 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5851 WINDOW_LOCATION));
5852
5853 const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005854 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005855 std::this_thread::sleep_for(windowTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005856 // 'notifyConnectionUnresponsive' should only be called once per connection
5857 mFakePolicy->assertNotifyAnrWasNotCalled();
5858 // When the ANR happened, dispatcher should abort the current event stream via ACTION_CANCEL
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005859 mWindow->consumeMotionDown();
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08005860 mWindow->consumeMotionEvent(
5861 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005862 mWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005863 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08005864 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005865 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005866}
5867
5868/**
5869 * If a window is processing a motion event, and then a key event comes in, the key event should
5870 * not to to the focused window until the motion is processed.
5871 *
5872 * Warning!!!
5873 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
5874 * and the injection timeout that we specify when injecting the key.
5875 * We must have the injection timeout (10ms) be smaller than
5876 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
5877 *
5878 * If that value changes, this test should also change.
5879 */
5880TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) {
5881 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
5882 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
5883
5884 tapOnWindow();
5885 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
5886 ASSERT_TRUE(downSequenceNum);
5887 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
5888 ASSERT_TRUE(upSequenceNum);
5889 // Don't finish the events yet, and send a key
5890 // Injection will "succeed" because we will eventually give up and send the key to the focused
5891 // window even if motions are still being processed. But because the injection timeout is short,
5892 // we will receive INJECTION_TIMED_OUT as the result.
5893
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005894 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00005895 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005896 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
5897 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005898 // Key will not be sent to the window, yet, because the window is still processing events
5899 // and the key remains pending, waiting for the touch events to be processed
5900 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
5901 ASSERT_FALSE(keySequenceNum);
5902
5903 std::this_thread::sleep_for(500ms);
5904 // if we wait long enough though, dispatcher will give up, and still send the key
5905 // to the focused window, even though we have not yet finished the motion event
5906 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5907 mWindow->finishEvent(*downSequenceNum);
5908 mWindow->finishEvent(*upSequenceNum);
5909}
5910
5911/**
5912 * If a window is processing a motion event, and then a key event comes in, the key event should
5913 * not go to the focused window until the motion is processed.
5914 * If then a new motion comes in, then the pending key event should be going to the currently
5915 * focused window right away.
5916 */
5917TEST_F(InputDispatcherSingleWindowAnr,
5918 PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) {
5919 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
5920 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
5921
5922 tapOnWindow();
5923 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
5924 ASSERT_TRUE(downSequenceNum);
5925 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
5926 ASSERT_TRUE(upSequenceNum);
5927 // Don't finish the events yet, and send a key
5928 // Injection is async, so it will succeed
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005929 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00005930 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
5931 InputEventInjectionSync::NONE));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005932 // At this point, key is still pending, and should not be sent to the application yet.
5933 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
5934 ASSERT_FALSE(keySequenceNum);
5935
5936 // Now tap down again. It should cause the pending key to go to the focused window right away.
5937 tapOnWindow();
5938 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd
5939 // the other events yet. We can finish events in any order.
5940 mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN
5941 mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP
5942 mWindow->consumeMotionDown();
5943 mWindow->consumeMotionUp();
5944 mWindow->assertNoEvents();
5945}
5946
5947class InputDispatcherMultiWindowAnr : public InputDispatcherTest {
5948 virtual void SetUp() override {
5949 InputDispatcherTest::SetUp();
5950
Chris Yea209fde2020-07-22 13:54:51 -07005951 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005952 mApplication->setDispatchingTimeout(10ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005953 mUnfocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Unfocused",
5954 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005955 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005956 // Adding FLAG_WATCH_OUTSIDE_TOUCH to receive ACTION_OUTSIDE when another window is tapped
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08005957 mUnfocusedWindow->setWatchOutsideTouch(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005958
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005959 mFocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Focused",
5960 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05005961 mFocusedWindow->setDispatchingTimeout(30ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005962 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005963
5964 // Set focused application.
5965 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
Vishnu Nair47074b82020-08-14 11:54:47 -07005966 mFocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005967
5968 // Expect one focus window exist in display.
5969 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005970 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005971 mFocusedWindow->consumeFocusEvent(true);
5972 }
5973
5974 virtual void TearDown() override {
5975 InputDispatcherTest::TearDown();
5976
5977 mUnfocusedWindow.clear();
5978 mFocusedWindow.clear();
5979 }
5980
5981protected:
Chris Yea209fde2020-07-22 13:54:51 -07005982 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005983 sp<FakeWindowHandle> mUnfocusedWindow;
5984 sp<FakeWindowHandle> mFocusedWindow;
5985 static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20};
5986 static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75};
5987 static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40};
5988
5989 void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); }
5990
5991 void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); }
5992
5993private:
5994 void tap(const PointF& location) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005995 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005996 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5997 location));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005998 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005999 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6000 location));
6001 }
6002};
6003
6004// If we have 2 windows that are both unresponsive, the one with the shortest timeout
6005// should be ANR'd first.
6006TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006007 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006008 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6009 FOCUSED_WINDOW_LOCATION))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006010 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006011 mFocusedWindow->consumeMotionDown();
6012 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00006013 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006014 // We consumed all events, so no ANR
6015 ASSERT_TRUE(mDispatcher->waitForIdle());
6016 mFakePolicy->assertNotifyAnrWasNotCalled();
6017
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006018 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006019 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6020 FOCUSED_WINDOW_LOCATION));
6021 std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent();
6022 ASSERT_TRUE(unfocusedSequenceNum);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006023
6024 const std::chrono::duration timeout =
6025 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006026 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006027 // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event
6028 // sequence to make it consistent
6029 mFocusedWindow->consumeMotionCancel();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006030 mUnfocusedWindow->finishEvent(*unfocusedSequenceNum);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006031 mFocusedWindow->consumeMotionDown();
6032 // This cancel is generated because the connection was unresponsive
6033 mFocusedWindow->consumeMotionCancel();
6034 mFocusedWindow->assertNoEvents();
6035 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006036 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006037 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
6038 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006039 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006040}
6041
6042// If we have 2 windows with identical timeouts that are both unresponsive,
6043// it doesn't matter which order they should have ANR.
6044// But we should receive ANR for both.
6045TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) {
6046 // Set the timeout for unfocused window to match the focused window
6047 mUnfocusedWindow->setDispatchingTimeout(10ms);
6048 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
6049
6050 tapOnFocusedWindow();
6051 // we should have ACTION_DOWN/ACTION_UP on focused window and ACTION_OUTSIDE on unfocused window
Prabir Pradhanedd96402022-02-15 01:46:16 -08006052 sp<IBinder> anrConnectionToken1, anrConnectionToken2;
6053 ASSERT_NO_FATAL_FAILURE(anrConnectionToken1 = mFakePolicy->getUnresponsiveWindowToken(10ms));
6054 ASSERT_NO_FATAL_FAILURE(anrConnectionToken2 = mFakePolicy->getUnresponsiveWindowToken(0ms));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006055
6056 // We don't know which window will ANR first. But both of them should happen eventually.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006057 ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 ||
6058 mFocusedWindow->getToken() == anrConnectionToken2);
6059 ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 ||
6060 mUnfocusedWindow->getToken() == anrConnectionToken2);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006061
6062 ASSERT_TRUE(mDispatcher->waitForIdle());
6063 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006064
6065 mFocusedWindow->consumeMotionDown();
6066 mFocusedWindow->consumeMotionUp();
6067 mUnfocusedWindow->consumeMotionOutside();
6068
Prabir Pradhanedd96402022-02-15 01:46:16 -08006069 sp<IBinder> responsiveToken1, responsiveToken2;
6070 ASSERT_NO_FATAL_FAILURE(responsiveToken1 = mFakePolicy->getResponsiveWindowToken());
6071 ASSERT_NO_FATAL_FAILURE(responsiveToken2 = mFakePolicy->getResponsiveWindowToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006072
6073 // Both applications should be marked as responsive, in any order
6074 ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 ||
6075 mFocusedWindow->getToken() == responsiveToken2);
6076 ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 ||
6077 mUnfocusedWindow->getToken() == responsiveToken2);
6078 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006079}
6080
6081// If a window is already not responding, the second tap on the same window should be ignored.
6082// We should also log an error to account for the dropped event (not tested here).
6083// At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events.
6084TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) {
6085 tapOnFocusedWindow();
6086 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00006087 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006088 // Receive the events, but don't respond
6089 std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN
6090 ASSERT_TRUE(downEventSequenceNum);
6091 std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP
6092 ASSERT_TRUE(upEventSequenceNum);
6093 const std::chrono::duration timeout =
6094 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006095 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006096
6097 // Tap once again
6098 // We cannot use "tapOnFocusedWindow" because it asserts the injection result to be success
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006099 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006100 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6101 FOCUSED_WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006102 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006103 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6104 FOCUSED_WINDOW_LOCATION));
6105 // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a
6106 // valid touch target
6107 mUnfocusedWindow->assertNoEvents();
6108
6109 // Consume the first tap
6110 mFocusedWindow->finishEvent(*downEventSequenceNum);
6111 mFocusedWindow->finishEvent(*upEventSequenceNum);
6112 ASSERT_TRUE(mDispatcher->waitForIdle());
6113 // The second tap did not go to the focused window
6114 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006115 // Since all events are finished, connection should be deemed healthy again
Prabir Pradhanedd96402022-02-15 01:46:16 -08006116 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
6117 mFocusedWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006118 mFakePolicy->assertNotifyAnrWasNotCalled();
6119}
6120
6121// If you tap outside of all windows, there will not be ANR
6122TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006123 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006124 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6125 LOCATION_OUTSIDE_ALL_WINDOWS));
6126 ASSERT_TRUE(mDispatcher->waitForIdle());
6127 mFakePolicy->assertNotifyAnrWasNotCalled();
6128}
6129
6130// Since the focused window is paused, tapping on it should not produce any events
6131TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) {
6132 mFocusedWindow->setPaused(true);
6133 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
6134
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006135 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006136 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6137 FOCUSED_WINDOW_LOCATION));
6138
6139 std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT));
6140 ASSERT_TRUE(mDispatcher->waitForIdle());
6141 // Should not ANR because the window is paused, and touches shouldn't go to it
6142 mFakePolicy->assertNotifyAnrWasNotCalled();
6143
6144 mFocusedWindow->assertNoEvents();
6145 mUnfocusedWindow->assertNoEvents();
6146}
6147
6148/**
6149 * If a window is processing a motion event, and then a key event comes in, the key event should
6150 * not to to the focused window until the motion is processed.
6151 * If a different window becomes focused at this time, the key should go to that window instead.
6152 *
6153 * Warning!!!
6154 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
6155 * and the injection timeout that we specify when injecting the key.
6156 * We must have the injection timeout (10ms) be smaller than
6157 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
6158 *
6159 * If that value changes, this test should also change.
6160 */
6161TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) {
6162 // Set a long ANR timeout to prevent it from triggering
6163 mFocusedWindow->setDispatchingTimeout(2s);
6164 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
6165
6166 tapOnUnfocusedWindow();
6167 std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent();
6168 ASSERT_TRUE(downSequenceNum);
6169 std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent();
6170 ASSERT_TRUE(upSequenceNum);
6171 // Don't finish the events yet, and send a key
6172 // Injection will succeed because we will eventually give up and send the key to the focused
6173 // window even if motions are still being processed.
6174
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006175 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006176 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6177 InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006178 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006179 // Key will not be sent to the window, yet, because the window is still processing events
6180 // and the key remains pending, waiting for the touch events to be processed
6181 std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent();
6182 ASSERT_FALSE(keySequenceNum);
6183
6184 // Switch the focus to the "unfocused" window that we tapped. Expect the key to go there
Vishnu Nair47074b82020-08-14 11:54:47 -07006185 mFocusedWindow->setFocusable(false);
6186 mUnfocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006187 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07006188 setFocusedWindow(mUnfocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006189
6190 // Focus events should precede the key events
6191 mUnfocusedWindow->consumeFocusEvent(true);
6192 mFocusedWindow->consumeFocusEvent(false);
6193
6194 // Finish the tap events, which should unblock dispatcher
6195 mUnfocusedWindow->finishEvent(*downSequenceNum);
6196 mUnfocusedWindow->finishEvent(*upSequenceNum);
6197
6198 // Now that all queues are cleared and no backlog in the connections, the key event
6199 // can finally go to the newly focused "mUnfocusedWindow".
6200 mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
6201 mFocusedWindow->assertNoEvents();
6202 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006203 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006204}
6205
6206// When the touch stream is split across 2 windows, and one of them does not respond,
6207// then ANR should be raised and the touch should be canceled for the unresponsive window.
6208// The other window should not be affected by that.
6209TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) {
6210 // Touch Window 1
6211 NotifyMotionArgs motionArgs =
6212 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6213 ADISPLAY_ID_DEFAULT, {FOCUSED_WINDOW_LOCATION});
6214 mDispatcher->notifyMotion(&motionArgs);
6215 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00006216 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006217
6218 // Touch Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006219 motionArgs = generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6220 {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006221 mDispatcher->notifyMotion(&motionArgs);
6222
6223 const std::chrono::duration timeout =
6224 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006225 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006226
6227 mUnfocusedWindow->consumeMotionDown();
6228 mFocusedWindow->consumeMotionDown();
6229 // Focused window may or may not receive ACTION_MOVE
6230 // But it should definitely receive ACTION_CANCEL due to the ANR
6231 InputEvent* event;
6232 std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event);
6233 ASSERT_TRUE(moveOrCancelSequenceNum);
6234 mFocusedWindow->finishEvent(*moveOrCancelSequenceNum);
6235 ASSERT_NE(nullptr, event);
6236 ASSERT_EQ(event->getType(), AINPUT_EVENT_TYPE_MOTION);
6237 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
6238 if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) {
6239 mFocusedWindow->consumeMotionCancel();
6240 } else {
6241 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction());
6242 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006243 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006244 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
6245 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006246
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006247 mUnfocusedWindow->assertNoEvents();
6248 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006249 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006250}
6251
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05006252/**
6253 * If we have no focused window, and a key comes in, we start the ANR timer.
6254 * The focused application should add a focused window before the timer runs out to prevent ANR.
6255 *
6256 * If the user touches another application during this time, the key should be dropped.
6257 * Next, if a new focused window comes in, without toggling the focused application,
6258 * then no ANR should occur.
6259 *
6260 * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication',
6261 * but in some cases the policy may not update the focused application.
6262 */
6263TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) {
6264 std::shared_ptr<FakeApplicationHandle> focusedApplication =
6265 std::make_shared<FakeApplicationHandle>();
6266 focusedApplication->setDispatchingTimeout(60ms);
6267 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication);
6268 // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused.
6269 mFocusedWindow->setFocusable(false);
6270
6271 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
6272 mFocusedWindow->consumeFocusEvent(false);
6273
6274 // Send a key. The ANR timer should start because there is no focused window.
6275 // 'focusedApplication' will get blamed if this timer completes.
6276 // Key will not be sent anywhere because we have no focused window. It will remain pending.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006277 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006278 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6279 InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms,
6280 /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006281 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05006282
6283 // Wait until dispatcher starts the "no focused window" timer. If we don't wait here,
6284 // then the injected touches won't cause the focused event to get dropped.
6285 // The dispatcher only checks for whether the queue should be pruned upon queueing.
6286 // If we inject the touch right away and the ANR timer hasn't started, the touch event would
6287 // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'.
6288 // For this test, it means that the key would get delivered to the window once it becomes
6289 // focused.
6290 std::this_thread::sleep_for(10ms);
6291
6292 // Touch unfocused window. This should force the pending key to get dropped.
6293 NotifyMotionArgs motionArgs =
6294 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6295 ADISPLAY_ID_DEFAULT, {UNFOCUSED_WINDOW_LOCATION});
6296 mDispatcher->notifyMotion(&motionArgs);
6297
6298 // We do not consume the motion right away, because that would require dispatcher to first
6299 // process (== drop) the key event, and by that time, ANR will be raised.
6300 // Set the focused window first.
6301 mFocusedWindow->setFocusable(true);
6302 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
6303 setFocusedWindow(mFocusedWindow);
6304 mFocusedWindow->consumeFocusEvent(true);
6305 // We do not call "setFocusedApplication" here, even though the newly focused window belongs
6306 // to another application. This could be a bug / behaviour in the policy.
6307
6308 mUnfocusedWindow->consumeMotionDown();
6309
6310 ASSERT_TRUE(mDispatcher->waitForIdle());
6311 // Should not ANR because we actually have a focused window. It was just added too slowly.
6312 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled());
6313}
6314
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05006315// These tests ensure we cannot send touch events to a window that's positioned behind a window
6316// that has feature NO_INPUT_CHANNEL.
6317// Layout:
6318// Top (closest to user)
6319// mNoInputWindow (above all windows)
6320// mBottomWindow
6321// Bottom (furthest from user)
6322class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest {
6323 virtual void SetUp() override {
6324 InputDispatcherTest::SetUp();
6325
6326 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006327 mNoInputWindow =
6328 sp<FakeWindowHandle>::make(mApplication, mDispatcher,
6329 "Window without input channel", ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00006330 /*token=*/std::make_optional<sp<IBinder>>(nullptr));
Prabir Pradhan51e7db02022-02-07 06:02:57 -08006331 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05006332 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
6333 // It's perfectly valid for this window to not have an associated input channel
6334
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006335 mBottomWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Bottom window",
6336 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05006337 mBottomWindow->setFrame(Rect(0, 0, 100, 100));
6338
6339 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
6340 }
6341
6342protected:
6343 std::shared_ptr<FakeApplicationHandle> mApplication;
6344 sp<FakeWindowHandle> mNoInputWindow;
6345 sp<FakeWindowHandle> mBottomWindow;
6346};
6347
6348TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) {
6349 PointF touchedPoint = {10, 10};
6350
6351 NotifyMotionArgs motionArgs =
6352 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6353 ADISPLAY_ID_DEFAULT, {touchedPoint});
6354 mDispatcher->notifyMotion(&motionArgs);
6355
6356 mNoInputWindow->assertNoEvents();
6357 // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have
6358 // an input channel, it is not marked as FLAG_NOT_TOUCHABLE,
6359 // and therefore should prevent mBottomWindow from receiving touches
6360 mBottomWindow->assertNoEvents();
6361}
6362
6363/**
6364 * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel,
6365 * ensure that this window does not receive any touches, and blocks touches to windows underneath.
6366 */
6367TEST_F(InputDispatcherMultiWindowOcclusionTests,
6368 NoInputChannelFeature_DropsTouchesWithValidChannel) {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006369 mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher,
6370 "Window with input channel and NO_INPUT_CHANNEL",
6371 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05006372
Prabir Pradhan51e7db02022-02-07 06:02:57 -08006373 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05006374 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
6375 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
6376
6377 PointF touchedPoint = {10, 10};
6378
6379 NotifyMotionArgs motionArgs =
6380 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6381 ADISPLAY_ID_DEFAULT, {touchedPoint});
6382 mDispatcher->notifyMotion(&motionArgs);
6383
6384 mNoInputWindow->assertNoEvents();
6385 mBottomWindow->assertNoEvents();
6386}
6387
Vishnu Nair958da932020-08-21 17:12:37 -07006388class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest {
6389protected:
6390 std::shared_ptr<FakeApplicationHandle> mApp;
6391 sp<FakeWindowHandle> mWindow;
6392 sp<FakeWindowHandle> mMirror;
6393
6394 virtual void SetUp() override {
6395 InputDispatcherTest::SetUp();
6396 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006397 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
6398 mMirror = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindowMirror",
6399 ADISPLAY_ID_DEFAULT, mWindow->getToken());
Vishnu Nair958da932020-08-21 17:12:37 -07006400 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
6401 mWindow->setFocusable(true);
6402 mMirror->setFocusable(true);
6403 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
6404 }
6405};
6406
6407TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) {
6408 // Request focus on a mirrored window
6409 setFocusedWindow(mMirror);
6410
6411 // window gets focused
6412 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006413 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
6414 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07006415 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
6416}
6417
6418// A focused & mirrored window remains focused only if the window and its mirror are both
6419// focusable.
6420TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) {
6421 setFocusedWindow(mMirror);
6422
6423 // window gets focused
6424 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006425 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
6426 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07006427 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006428 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
6429 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07006430 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
6431
6432 mMirror->setFocusable(false);
6433 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
6434
6435 // window loses focus since one of the windows associated with the token in not focusable
6436 mWindow->consumeFocusEvent(false);
6437
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006438 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
6439 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07006440 mWindow->assertNoEvents();
6441}
6442
6443// A focused & mirrored window remains focused until the window and its mirror both become
6444// invisible.
6445TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) {
6446 setFocusedWindow(mMirror);
6447
6448 // window gets focused
6449 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006450 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
6451 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07006452 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006453 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
6454 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07006455 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
6456
6457 mMirror->setVisible(false);
6458 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
6459
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006460 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
6461 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07006462 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006463 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
6464 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07006465 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
6466
6467 mWindow->setVisible(false);
6468 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
6469
6470 // window loses focus only after all windows associated with the token become invisible.
6471 mWindow->consumeFocusEvent(false);
6472
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006473 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
6474 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07006475 mWindow->assertNoEvents();
6476}
6477
6478// A focused & mirrored window remains focused until both windows are removed.
6479TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) {
6480 setFocusedWindow(mMirror);
6481
6482 // window gets focused
6483 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006484 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
6485 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07006486 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006487 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
6488 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07006489 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
6490
6491 // single window is removed but the window token remains focused
6492 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}});
6493
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006494 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
6495 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07006496 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006497 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
6498 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07006499 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
6500
6501 // Both windows are removed
6502 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
6503 mWindow->consumeFocusEvent(false);
6504
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006505 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
6506 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07006507 mWindow->assertNoEvents();
6508}
6509
6510// Focus request can be pending until one window becomes visible.
6511TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) {
6512 // Request focus on an invisible mirror.
6513 mWindow->setVisible(false);
6514 mMirror->setVisible(false);
6515 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
6516 setFocusedWindow(mMirror);
6517
6518 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006519 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00006520 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6521 InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07006522
6523 mMirror->setVisible(true);
6524 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
6525
6526 // window gets focused
6527 mWindow->consumeFocusEvent(true);
6528 // window gets the pending key event
6529 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
6530}
Prabir Pradhan99987712020-11-10 18:43:05 -08006531
6532class InputDispatcherPointerCaptureTests : public InputDispatcherTest {
6533protected:
6534 std::shared_ptr<FakeApplicationHandle> mApp;
6535 sp<FakeWindowHandle> mWindow;
6536 sp<FakeWindowHandle> mSecondWindow;
6537
6538 void SetUp() override {
6539 InputDispatcherTest::SetUp();
6540 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006541 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -08006542 mWindow->setFocusable(true);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006543 mSecondWindow =
6544 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -08006545 mSecondWindow->setFocusable(true);
6546
6547 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
6548 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}});
6549
6550 setFocusedWindow(mWindow);
6551 mWindow->consumeFocusEvent(true);
6552 }
6553
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006554 void notifyPointerCaptureChanged(const PointerCaptureRequest& request) {
6555 const NotifyPointerCaptureChangedArgs args = generatePointerCaptureChangedArgs(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08006556 mDispatcher->notifyPointerCaptureChanged(&args);
6557 }
6558
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006559 PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window,
6560 bool enabled) {
Prabir Pradhan99987712020-11-10 18:43:05 -08006561 mDispatcher->requestPointerCapture(window->getToken(), enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006562 auto request = mFakePolicy->assertSetPointerCaptureCalled(enabled);
6563 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08006564 window->consumeCaptureEvent(enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006565 return request;
Prabir Pradhan99987712020-11-10 18:43:05 -08006566 }
6567};
6568
6569TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) {
6570 // Ensure that capture cannot be obtained for unfocused windows.
6571 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
6572 mFakePolicy->assertSetPointerCaptureNotCalled();
6573 mSecondWindow->assertNoEvents();
6574
6575 // Ensure that capture can be enabled from the focus window.
6576 requestAndVerifyPointerCapture(mWindow, true);
6577
6578 // Ensure that capture cannot be disabled from a window that does not have capture.
6579 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false);
6580 mFakePolicy->assertSetPointerCaptureNotCalled();
6581
6582 // Ensure that capture can be disabled from the window with capture.
6583 requestAndVerifyPointerCapture(mWindow, false);
6584}
6585
6586TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006587 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08006588
6589 setFocusedWindow(mSecondWindow);
6590
6591 // Ensure that the capture disabled event was sent first.
6592 mWindow->consumeCaptureEvent(false);
6593 mWindow->consumeFocusEvent(false);
6594 mSecondWindow->consumeFocusEvent(true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006595 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08006596
6597 // Ensure that additional state changes from InputReader are not sent to the window.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006598 notifyPointerCaptureChanged({});
6599 notifyPointerCaptureChanged(request);
6600 notifyPointerCaptureChanged({});
Prabir Pradhan99987712020-11-10 18:43:05 -08006601 mWindow->assertNoEvents();
6602 mSecondWindow->assertNoEvents();
6603 mFakePolicy->assertSetPointerCaptureNotCalled();
6604}
6605
6606TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006607 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08006608
6609 // InputReader unexpectedly disables and enables pointer capture.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006610 notifyPointerCaptureChanged({});
6611 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08006612
6613 // Ensure that Pointer Capture is disabled.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006614 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08006615 mWindow->consumeCaptureEvent(false);
6616 mWindow->assertNoEvents();
6617}
6618
Prabir Pradhan167e6d92021-02-04 16:18:17 -08006619TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) {
6620 requestAndVerifyPointerCapture(mWindow, true);
6621
6622 // The first window loses focus.
6623 setFocusedWindow(mSecondWindow);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006624 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08006625 mWindow->consumeCaptureEvent(false);
6626
6627 // Request Pointer Capture from the second window before the notification from InputReader
6628 // arrives.
6629 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006630 auto request = mFakePolicy->assertSetPointerCaptureCalled(true);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08006631
6632 // InputReader notifies Pointer Capture was disabled (because of the focus change).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006633 notifyPointerCaptureChanged({});
Prabir Pradhan167e6d92021-02-04 16:18:17 -08006634
6635 // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006636 notifyPointerCaptureChanged(request);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08006637
6638 mSecondWindow->consumeFocusEvent(true);
6639 mSecondWindow->consumeCaptureEvent(true);
6640}
6641
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006642TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) {
6643 // App repeatedly enables and disables capture.
6644 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
6645 auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
6646 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
6647 mFakePolicy->assertSetPointerCaptureCalled(false);
6648 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
6649 auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
6650
6651 // InputReader notifies that PointerCapture has been enabled for the first request. Since the
6652 // first request is now stale, this should do nothing.
6653 notifyPointerCaptureChanged(firstRequest);
6654 mWindow->assertNoEvents();
6655
6656 // InputReader notifies that the second request was enabled.
6657 notifyPointerCaptureChanged(secondRequest);
6658 mWindow->consumeCaptureEvent(true);
6659}
6660
Prabir Pradhan7092e262022-05-03 16:51:09 +00006661TEST_F(InputDispatcherPointerCaptureTests, RapidToggleRequests) {
6662 requestAndVerifyPointerCapture(mWindow, true);
6663
6664 // App toggles pointer capture off and on.
6665 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
6666 mFakePolicy->assertSetPointerCaptureCalled(false);
6667
6668 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
6669 auto enableRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
6670
6671 // InputReader notifies that the latest "enable" request was processed, while skipping over the
6672 // preceding "disable" request.
6673 notifyPointerCaptureChanged(enableRequest);
6674
6675 // Since pointer capture was never disabled during the rapid toggle, the window does not receive
6676 // any notifications.
6677 mWindow->assertNoEvents();
6678}
6679
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006680class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest {
6681protected:
6682 constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8;
Bernardo Rufino7393d172021-02-26 13:56:11 +00006683
6684 constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9;
6685 static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY);
6686
6687 constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7;
6688 static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
6689
6690 // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold
6691 constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5;
6692 static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
6693 static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) <
6694 MAXIMUM_OBSCURING_OPACITY);
6695
Bernardo Rufino6d52e542021-02-15 18:38:10 +00006696 static const int32_t TOUCHED_APP_UID = 10001;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006697 static const int32_t APP_B_UID = 10002;
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006698 static const int32_t APP_C_UID = 10003;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006699
6700 sp<FakeWindowHandle> mTouchWindow;
6701
6702 virtual void SetUp() override {
6703 InputDispatcherTest::SetUp();
Bernardo Rufino6d52e542021-02-15 18:38:10 +00006704 mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched");
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006705 mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY);
6706 }
6707
6708 virtual void TearDown() override {
6709 InputDispatcherTest::TearDown();
6710 mTouchWindow.clear();
6711 }
6712
chaviw3277faf2021-05-19 16:45:23 -05006713 sp<FakeWindowHandle> getOccludingWindow(int32_t uid, std::string name, TouchOcclusionMode mode,
6714 float alpha = 1.0f) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006715 sp<FakeWindowHandle> window = getWindow(uid, name);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08006716 window->setTouchable(false);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006717 window->setTouchOcclusionMode(mode);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006718 window->setAlpha(alpha);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006719 return window;
6720 }
6721
6722 sp<FakeWindowHandle> getWindow(int32_t uid, std::string name) {
6723 std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>();
6724 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006725 sp<FakeWindowHandle>::make(app, mDispatcher, name, ADISPLAY_ID_DEFAULT);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006726 // Generate an arbitrary PID based on the UID
6727 window->setOwnerInfo(1777 + (uid % 10000), uid);
6728 return window;
6729 }
6730
6731 void touch(const std::vector<PointF>& points = {PointF{100, 200}}) {
6732 NotifyMotionArgs args =
6733 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6734 ADISPLAY_ID_DEFAULT, points);
6735 mDispatcher->notifyMotion(&args);
6736 }
6737};
6738
6739TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006740 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006741 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006742 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006743
6744 touch();
6745
6746 mTouchWindow->assertNoEvents();
6747}
6748
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00006749TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufino7393d172021-02-26 13:56:11 +00006750 WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) {
6751 const sp<FakeWindowHandle>& w =
6752 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f);
6753 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6754
6755 touch();
6756
6757 mTouchWindow->assertNoEvents();
6758}
6759
6760TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00006761 WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) {
6762 const sp<FakeWindowHandle>& w =
6763 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
6764 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6765
6766 touch();
6767
6768 w->assertNoEvents();
6769}
6770
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006771TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006772 const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW);
6773 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006774
6775 touch();
6776
6777 mTouchWindow->consumeAnyMotionDown();
6778}
6779
6780TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006781 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006782 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006783 w->setFrame(Rect(0, 0, 50, 50));
6784 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006785
6786 touch({PointF{100, 100}});
6787
6788 mTouchWindow->consumeAnyMotionDown();
6789}
6790
6791TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006792 const sp<FakeWindowHandle>& w =
Bernardo Rufino6d52e542021-02-15 18:38:10 +00006793 getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006794 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6795
6796 touch();
6797
6798 mTouchWindow->consumeAnyMotionDown();
6799}
6800
6801TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) {
6802 const sp<FakeWindowHandle>& w =
6803 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
6804 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006805
6806 touch();
6807
6808 mTouchWindow->consumeAnyMotionDown();
6809}
6810
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00006811TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) {
6812 const sp<FakeWindowHandle>& w =
6813 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
6814 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6815
6816 touch();
6817
6818 w->assertNoEvents();
6819}
6820
6821/**
6822 * This is important to make sure apps can't indirectly learn the position of touches (outside vs
6823 * inside) while letting them pass-through. Note that even though touch passes through the occluding
6824 * window, the occluding window will still receive ACTION_OUTSIDE event.
6825 */
6826TEST_F(InputDispatcherUntrustedTouchesTest,
6827 WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) {
6828 const sp<FakeWindowHandle>& w =
6829 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08006830 w->setWatchOutsideTouch(true);
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00006831 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6832
6833 touch();
6834
6835 w->consumeMotionOutside();
6836}
6837
6838TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) {
6839 const sp<FakeWindowHandle>& w =
6840 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08006841 w->setWatchOutsideTouch(true);
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00006842 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6843
6844 touch();
6845
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08006846 w->consumeMotionOutsideWithZeroedCoords();
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00006847}
6848
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006849TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006850 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006851 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
6852 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006853 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6854
6855 touch();
6856
6857 mTouchWindow->consumeAnyMotionDown();
6858}
6859
6860TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) {
6861 const sp<FakeWindowHandle>& w =
6862 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
6863 MAXIMUM_OBSCURING_OPACITY);
6864 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006865
6866 touch();
6867
6868 mTouchWindow->consumeAnyMotionDown();
6869}
6870
6871TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006872 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006873 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
6874 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006875 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6876
6877 touch();
6878
6879 mTouchWindow->assertNoEvents();
6880}
6881
6882TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) {
6883 // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91
6884 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006885 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
6886 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006887 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006888 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
6889 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006890 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
6891
6892 touch();
6893
6894 mTouchWindow->assertNoEvents();
6895}
6896
6897TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) {
6898 // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75
6899 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006900 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
6901 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006902 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006903 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
6904 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006905 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
6906
6907 touch();
6908
6909 mTouchWindow->consumeAnyMotionDown();
6910}
6911
6912TEST_F(InputDispatcherUntrustedTouchesTest,
6913 WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) {
6914 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006915 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
6916 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006917 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006918 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
6919 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006920 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
6921
6922 touch();
6923
6924 mTouchWindow->consumeAnyMotionDown();
6925}
6926
6927TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) {
6928 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006929 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
6930 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006931 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006932 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
6933 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006934 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006935
6936 touch();
6937
6938 mTouchWindow->assertNoEvents();
6939}
6940
Bernardo Rufino6d52e542021-02-15 18:38:10 +00006941TEST_F(InputDispatcherUntrustedTouchesTest,
6942 WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) {
6943 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006944 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
6945 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00006946 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006947 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
6948 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00006949 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
6950
6951 touch();
6952
6953 mTouchWindow->assertNoEvents();
6954}
6955
6956TEST_F(InputDispatcherUntrustedTouchesTest,
6957 WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) {
6958 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006959 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
6960 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00006961 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006962 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
6963 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00006964 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
6965
6966 touch();
6967
6968 mTouchWindow->consumeAnyMotionDown();
6969}
6970
6971TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) {
6972 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006973 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
6974 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00006975 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6976
6977 touch();
6978
6979 mTouchWindow->consumeAnyMotionDown();
6980}
6981
6982TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) {
6983 const sp<FakeWindowHandle>& w =
6984 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED);
6985 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6986
6987 touch();
6988
6989 mTouchWindow->consumeAnyMotionDown();
6990}
6991
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00006992TEST_F(InputDispatcherUntrustedTouchesTest,
6993 OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) {
6994 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
6995 const sp<FakeWindowHandle>& w =
6996 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f);
6997 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6998
6999 touch();
7000
7001 mTouchWindow->assertNoEvents();
7002}
7003
7004TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) {
7005 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
7006 const sp<FakeWindowHandle>& w =
7007 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f);
7008 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7009
7010 touch();
7011
7012 mTouchWindow->consumeAnyMotionDown();
7013}
7014
7015TEST_F(InputDispatcherUntrustedTouchesTest,
7016 OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) {
7017 mDispatcher->setMaximumObscuringOpacityForTouch(1.0f);
7018 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007019 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7020 OPACITY_ABOVE_THRESHOLD);
7021 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7022
7023 touch();
7024
7025 mTouchWindow->consumeAnyMotionDown();
7026}
7027
7028TEST_F(InputDispatcherUntrustedTouchesTest,
7029 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) {
7030 const sp<FakeWindowHandle>& w1 =
7031 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
7032 OPACITY_BELOW_THRESHOLD);
7033 const sp<FakeWindowHandle>& w2 =
7034 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7035 OPACITY_BELOW_THRESHOLD);
7036 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
7037
7038 touch();
7039
7040 mTouchWindow->assertNoEvents();
7041}
7042
7043/**
7044 * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the
7045 * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold
7046 * (which alone would result in allowing touches) does not affect the blocking behavior.
7047 */
7048TEST_F(InputDispatcherUntrustedTouchesTest,
7049 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) {
7050 const sp<FakeWindowHandle>& wB =
7051 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
7052 OPACITY_BELOW_THRESHOLD);
7053 const sp<FakeWindowHandle>& wC =
7054 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
7055 OPACITY_BELOW_THRESHOLD);
7056 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
7057
7058 touch();
7059
7060 mTouchWindow->assertNoEvents();
7061}
7062
7063/**
7064 * This test is testing that a window from a different UID but with same application token doesn't
7065 * block the touch. Apps can share the application token for close UI collaboration for example.
7066 */
7067TEST_F(InputDispatcherUntrustedTouchesTest,
7068 WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) {
7069 const sp<FakeWindowHandle>& w =
7070 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
7071 w->setApplicationToken(mTouchWindow->getApplicationToken());
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00007072 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7073
7074 touch();
7075
7076 mTouchWindow->consumeAnyMotionDown();
7077}
7078
arthurhungb89ccb02020-12-30 16:19:01 +08007079class InputDispatcherDragTests : public InputDispatcherTest {
7080protected:
7081 std::shared_ptr<FakeApplicationHandle> mApp;
7082 sp<FakeWindowHandle> mWindow;
7083 sp<FakeWindowHandle> mSecondWindow;
7084 sp<FakeWindowHandle> mDragWindow;
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00007085 sp<FakeWindowHandle> mSpyWindow;
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007086 // Mouse would force no-split, set the id as non-zero to verify if drag state could track it.
7087 static constexpr int32_t MOUSE_POINTER_ID = 1;
arthurhungb89ccb02020-12-30 16:19:01 +08007088
7089 void SetUp() override {
7090 InputDispatcherTest::SetUp();
7091 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007092 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +08007093 mWindow->setFrame(Rect(0, 0, 100, 100));
arthurhungb89ccb02020-12-30 16:19:01 +08007094
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007095 mSecondWindow =
7096 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +08007097 mSecondWindow->setFrame(Rect(100, 0, 200, 100));
arthurhungb89ccb02020-12-30 16:19:01 +08007098
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007099 mSpyWindow =
7100 sp<FakeWindowHandle>::make(mApp, mDispatcher, "SpyWindow", ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00007101 mSpyWindow->setSpy(true);
7102 mSpyWindow->setTrustedOverlay(true);
7103 mSpyWindow->setFrame(Rect(0, 0, 200, 100));
7104
arthurhungb89ccb02020-12-30 16:19:01 +08007105 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00007106 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mSpyWindow, mWindow, mSecondWindow}}});
arthurhungb89ccb02020-12-30 16:19:01 +08007107 }
7108
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007109 void injectDown(int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
7110 switch (fromSource) {
7111 case AINPUT_SOURCE_TOUCHSCREEN:
7112 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7113 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
7114 ADISPLAY_ID_DEFAULT, {50, 50}))
7115 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7116 break;
7117 case AINPUT_SOURCE_STYLUS:
7118 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7119 injectMotionEvent(
7120 mDispatcher,
7121 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
7122 AINPUT_SOURCE_STYLUS)
7123 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
7124 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS)
7125 .x(50)
7126 .y(50))
7127 .build()));
7128 break;
7129 case AINPUT_SOURCE_MOUSE:
7130 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7131 injectMotionEvent(
7132 mDispatcher,
7133 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
7134 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
7135 .pointer(PointerBuilder(MOUSE_POINTER_ID,
7136 AMOTION_EVENT_TOOL_TYPE_MOUSE)
7137 .x(50)
7138 .y(50))
7139 .build()));
7140 break;
7141 default:
7142 FAIL() << "Source " << fromSource << " doesn't support drag and drop";
7143 }
arthurhungb89ccb02020-12-30 16:19:01 +08007144
7145 // Window should receive motion event.
7146 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00007147 // Spy window should also receive motion event
7148 mSpyWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung54745652022-04-20 07:17:41 +00007149 }
7150
7151 // Start performing drag, we will create a drag window and transfer touch to it.
7152 // @param sendDown : if true, send a motion down on first window before perform drag and drop.
7153 // Returns true on success.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007154 bool startDrag(bool sendDown = true, int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
Arthur Hung54745652022-04-20 07:17:41 +00007155 if (sendDown) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007156 injectDown(fromSource);
Arthur Hung54745652022-04-20 07:17:41 +00007157 }
arthurhungb89ccb02020-12-30 16:19:01 +08007158
7159 // The drag window covers the entire display
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007160 mDragWindow =
7161 sp<FakeWindowHandle>::make(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00007162 mDragWindow->setTouchableRegion(Region{{0, 0, 0, 0}});
arthurhungb89ccb02020-12-30 16:19:01 +08007163 mDispatcher->setInputWindows(
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00007164 {{ADISPLAY_ID_DEFAULT, {mDragWindow, mSpyWindow, mWindow, mSecondWindow}}});
arthurhungb89ccb02020-12-30 16:19:01 +08007165
7166 // Transfer touch focus to the drag window
Arthur Hung54745652022-04-20 07:17:41 +00007167 bool transferred =
7168 mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(),
Harry Cutts33476232023-01-30 19:57:29 +00007169 /*isDragDrop=*/true);
Arthur Hung54745652022-04-20 07:17:41 +00007170 if (transferred) {
7171 mWindow->consumeMotionCancel();
7172 mDragWindow->consumeMotionDown();
7173 }
7174 return transferred;
arthurhungb89ccb02020-12-30 16:19:01 +08007175 }
7176};
7177
7178TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007179 startDrag();
arthurhungb89ccb02020-12-30 16:19:01 +08007180
7181 // Move on window.
7182 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7183 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
7184 ADISPLAY_ID_DEFAULT, {50, 50}))
7185 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7186 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7187 mWindow->consumeDragEvent(false, 50, 50);
7188 mSecondWindow->assertNoEvents();
7189
7190 // Move to another window.
7191 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7192 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
7193 ADISPLAY_ID_DEFAULT, {150, 50}))
7194 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7195 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7196 mWindow->consumeDragEvent(true, 150, 50);
7197 mSecondWindow->consumeDragEvent(false, 50, 50);
7198
7199 // Move back to original window.
7200 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7201 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
7202 ADISPLAY_ID_DEFAULT, {50, 50}))
7203 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7204 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7205 mWindow->consumeDragEvent(false, 50, 50);
7206 mSecondWindow->consumeDragEvent(true, -50, 50);
7207
7208 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7209 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {50, 50}))
7210 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7211 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
7212 mWindow->assertNoEvents();
7213 mSecondWindow->assertNoEvents();
7214}
7215
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00007216TEST_F(InputDispatcherDragTests, DragEnterAndPointerDownPilfersPointers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007217 startDrag();
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00007218
7219 // No cancel event after drag start
7220 mSpyWindow->assertNoEvents();
7221
7222 const MotionEvent secondFingerDownEvent =
7223 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7224 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Harry Cutts33476232023-01-30 19:57:29 +00007225 .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
7226 .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(60).y(60))
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00007227 .build();
7228 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7229 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
7230 InputEventInjectionSync::WAIT_FOR_RESULT))
7231 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7232
7233 // Receives cancel for first pointer after next pointer down
7234 mSpyWindow->consumeMotionCancel();
7235 mSpyWindow->consumeMotionDown();
7236
7237 mSpyWindow->assertNoEvents();
7238}
7239
arthurhungf452d0b2021-01-06 00:19:52 +08007240TEST_F(InputDispatcherDragTests, DragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007241 startDrag();
arthurhungf452d0b2021-01-06 00:19:52 +08007242
7243 // Move on window.
7244 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7245 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
7246 ADISPLAY_ID_DEFAULT, {50, 50}))
7247 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7248 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7249 mWindow->consumeDragEvent(false, 50, 50);
7250 mSecondWindow->assertNoEvents();
7251
7252 // Move to another window.
7253 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7254 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
7255 ADISPLAY_ID_DEFAULT, {150, 50}))
7256 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7257 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7258 mWindow->consumeDragEvent(true, 150, 50);
7259 mSecondWindow->consumeDragEvent(false, 50, 50);
7260
7261 // drop to another window.
7262 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7263 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7264 {150, 50}))
7265 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7266 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
7267 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
7268 mWindow->assertNoEvents();
7269 mSecondWindow->assertNoEvents();
7270}
7271
arthurhung6d4bed92021-03-17 11:59:33 +08007272TEST_F(InputDispatcherDragTests, StylusDragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007273 startDrag(true, AINPUT_SOURCE_STYLUS);
arthurhung6d4bed92021-03-17 11:59:33 +08007274
7275 // Move on window and keep button pressed.
7276 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7277 injectMotionEvent(mDispatcher,
7278 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
7279 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
7280 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS)
7281 .x(50)
7282 .y(50))
7283 .build()))
7284 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7285 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7286 mWindow->consumeDragEvent(false, 50, 50);
7287 mSecondWindow->assertNoEvents();
7288
7289 // Move to another window and release button, expect to drop item.
7290 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7291 injectMotionEvent(mDispatcher,
7292 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
7293 .buttonState(0)
7294 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS)
7295 .x(150)
7296 .y(50))
7297 .build()))
7298 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7299 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7300 mWindow->assertNoEvents();
7301 mSecondWindow->assertNoEvents();
7302 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
7303
7304 // nothing to the window.
7305 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7306 injectMotionEvent(mDispatcher,
7307 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS)
7308 .buttonState(0)
7309 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS)
7310 .x(150)
7311 .y(50))
7312 .build()))
7313 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7314 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
7315 mWindow->assertNoEvents();
7316 mSecondWindow->assertNoEvents();
7317}
7318
Arthur Hung54745652022-04-20 07:17:41 +00007319TEST_F(InputDispatcherDragTests, DragAndDropOnInvalidWindow) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007320 startDrag();
Arthur Hung6d0571e2021-04-09 20:18:16 +08007321
7322 // Set second window invisible.
7323 mSecondWindow->setVisible(false);
7324 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}});
7325
7326 // Move on window.
7327 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7328 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
7329 ADISPLAY_ID_DEFAULT, {50, 50}))
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, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
7338 ADISPLAY_ID_DEFAULT, {150, 50}))
7339 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7340 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7341 mWindow->consumeDragEvent(true, 150, 50);
7342 mSecondWindow->assertNoEvents();
7343
7344 // drop to another window.
7345 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7346 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7347 {150, 50}))
7348 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7349 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
7350 mFakePolicy->assertDropTargetEquals(nullptr);
7351 mWindow->assertNoEvents();
7352 mSecondWindow->assertNoEvents();
7353}
7354
Arthur Hung54745652022-04-20 07:17:41 +00007355TEST_F(InputDispatcherDragTests, NoDragAndDropWhenMultiFingers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007356 // Ensure window could track pointerIds if it didn't support split touch.
7357 mWindow->setPreventSplitting(true);
7358
Arthur Hung54745652022-04-20 07:17:41 +00007359 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7360 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7361 {50, 50}))
7362 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7363 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7364
7365 const MotionEvent secondFingerDownEvent =
7366 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7367 .displayId(ADISPLAY_ID_DEFAULT)
7368 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Harry Cutts33476232023-01-30 19:57:29 +00007369 .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
7370 .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(75).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00007371 .build();
7372 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7373 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
7374 InputEventInjectionSync::WAIT_FOR_RESULT))
7375 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Harry Cutts33476232023-01-30 19:57:29 +00007376 mWindow->consumeMotionPointerDown(/*pointerIndex=*/1);
Arthur Hung54745652022-04-20 07:17:41 +00007377
7378 // Should not perform drag and drop when window has multi fingers.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007379 ASSERT_FALSE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +00007380}
7381
7382TEST_F(InputDispatcherDragTests, DragAndDropWhenSplitTouch) {
7383 // First down on second window.
7384 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7385 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7386 {150, 50}))
7387 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7388
7389 mSecondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7390
7391 // Second down on first window.
7392 const MotionEvent secondFingerDownEvent =
7393 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7394 .displayId(ADISPLAY_ID_DEFAULT)
7395 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Harry Cutts33476232023-01-30 19:57:29 +00007396 .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50))
7397 .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00007398 .build();
7399 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7400 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
7401 InputEventInjectionSync::WAIT_FOR_RESULT))
7402 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7403 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7404
7405 // Perform drag and drop from first window.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007406 ASSERT_TRUE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +00007407
7408 // Move on window.
7409 const MotionEvent secondFingerMoveEvent =
7410 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
7411 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Harry Cutts33476232023-01-30 19:57:29 +00007412 .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50))
7413 .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00007414 .build();
7415 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7416 injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT,
7417 InputEventInjectionSync::WAIT_FOR_RESULT));
7418 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7419 mWindow->consumeDragEvent(false, 50, 50);
7420 mSecondWindow->consumeMotionMove();
7421
7422 // Release the drag pointer should perform drop.
7423 const MotionEvent secondFingerUpEvent =
7424 MotionEventBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
7425 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Harry Cutts33476232023-01-30 19:57:29 +00007426 .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50))
7427 .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00007428 .build();
7429 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7430 injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT,
7431 InputEventInjectionSync::WAIT_FOR_RESULT));
7432 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
7433 mFakePolicy->assertDropTargetEquals(mWindow->getToken());
7434 mWindow->assertNoEvents();
7435 mSecondWindow->consumeMotionMove();
7436}
7437
Arthur Hung3915c1f2022-05-31 07:17:17 +00007438TEST_F(InputDispatcherDragTests, DragAndDropWhenMultiDisplays) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007439 startDrag();
Arthur Hung3915c1f2022-05-31 07:17:17 +00007440
7441 // Update window of second display.
7442 sp<FakeWindowHandle> windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007443 sp<FakeWindowHandle>::make(mApp, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung3915c1f2022-05-31 07:17:17 +00007444 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
7445
7446 // Let second display has a touch state.
7447 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7448 injectMotionEvent(mDispatcher,
7449 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
7450 AINPUT_SOURCE_TOUCHSCREEN)
7451 .displayId(SECOND_DISPLAY_ID)
7452 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER)
7453 .x(100)
7454 .y(100))
7455 .build()));
7456 windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN,
Harry Cutts33476232023-01-30 19:57:29 +00007457 SECOND_DISPLAY_ID, /*expectedFlag=*/0);
Arthur Hung3915c1f2022-05-31 07:17:17 +00007458 // Update window again.
7459 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
7460
7461 // Move on window.
7462 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7463 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
7464 ADISPLAY_ID_DEFAULT, {50, 50}))
7465 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7466 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7467 mWindow->consumeDragEvent(false, 50, 50);
7468 mSecondWindow->assertNoEvents();
7469
7470 // Move to another window.
7471 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7472 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
7473 ADISPLAY_ID_DEFAULT, {150, 50}))
7474 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7475 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7476 mWindow->consumeDragEvent(true, 150, 50);
7477 mSecondWindow->consumeDragEvent(false, 50, 50);
7478
7479 // drop to another window.
7480 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7481 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7482 {150, 50}))
7483 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7484 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
7485 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
7486 mWindow->assertNoEvents();
7487 mSecondWindow->assertNoEvents();
7488}
7489
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007490TEST_F(InputDispatcherDragTests, MouseDragAndDrop) {
7491 startDrag(true, AINPUT_SOURCE_MOUSE);
7492 // Move on window.
7493 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7494 injectMotionEvent(mDispatcher,
7495 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
7496 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
7497 .pointer(PointerBuilder(MOUSE_POINTER_ID,
7498 AMOTION_EVENT_TOOL_TYPE_MOUSE)
7499 .x(50)
7500 .y(50))
7501 .build()))
7502 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7503 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7504 mWindow->consumeDragEvent(false, 50, 50);
7505 mSecondWindow->assertNoEvents();
7506
7507 // Move to another window.
7508 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7509 injectMotionEvent(mDispatcher,
7510 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
7511 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
7512 .pointer(PointerBuilder(MOUSE_POINTER_ID,
7513 AMOTION_EVENT_TOOL_TYPE_MOUSE)
7514 .x(150)
7515 .y(50))
7516 .build()))
7517 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7518 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7519 mWindow->consumeDragEvent(true, 150, 50);
7520 mSecondWindow->consumeDragEvent(false, 50, 50);
7521
7522 // drop to another window.
7523 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7524 injectMotionEvent(mDispatcher,
7525 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
7526 .buttonState(0)
7527 .pointer(PointerBuilder(MOUSE_POINTER_ID,
7528 AMOTION_EVENT_TOOL_TYPE_MOUSE)
7529 .x(150)
7530 .y(50))
7531 .build()))
7532 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7533 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
7534 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
7535 mWindow->assertNoEvents();
7536 mSecondWindow->assertNoEvents();
7537}
7538
Vishnu Nair062a8672021-09-03 16:07:44 -07007539class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {};
7540
7541TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) {
7542 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007543 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
7544 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007545 window->setDropInput(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07007546 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
7547 window->setFocusable(true);
7548 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
7549 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00007550 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07007551
7552 // With the flag set, window should not get any input
7553 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
7554 mDispatcher->notifyKey(&keyArgs);
7555 window->assertNoEvents();
7556
7557 NotifyMotionArgs motionArgs =
7558 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
7559 ADISPLAY_ID_DEFAULT);
7560 mDispatcher->notifyMotion(&motionArgs);
7561 window->assertNoEvents();
7562
7563 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007564 window->setDropInput(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07007565 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
7566
7567 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
7568 mDispatcher->notifyKey(&keyArgs);
7569 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
7570
7571 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
7572 ADISPLAY_ID_DEFAULT);
7573 mDispatcher->notifyMotion(&motionArgs);
7574 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7575 window->assertNoEvents();
7576}
7577
7578TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) {
7579 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
7580 std::make_shared<FakeApplicationHandle>();
7581 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007582 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
7583 ADISPLAY_ID_DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -07007584 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
7585 obscuringWindow->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007586 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07007587 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007588 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
7589 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007590 window->setDropInputIfObscured(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07007591 window->setOwnerInfo(222, 222);
7592 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
7593 window->setFocusable(true);
7594 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
7595 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00007596 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07007597
7598 // With the flag set, window should not get any input
7599 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
7600 mDispatcher->notifyKey(&keyArgs);
7601 window->assertNoEvents();
7602
7603 NotifyMotionArgs motionArgs =
7604 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
7605 ADISPLAY_ID_DEFAULT);
7606 mDispatcher->notifyMotion(&motionArgs);
7607 window->assertNoEvents();
7608
7609 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007610 window->setDropInputIfObscured(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07007611 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
7612
7613 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
7614 mDispatcher->notifyKey(&keyArgs);
7615 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
7616
7617 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
7618 ADISPLAY_ID_DEFAULT);
7619 mDispatcher->notifyMotion(&motionArgs);
7620 window->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
7621 window->assertNoEvents();
7622}
7623
7624TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) {
7625 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
7626 std::make_shared<FakeApplicationHandle>();
7627 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007628 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
7629 ADISPLAY_ID_DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -07007630 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
7631 obscuringWindow->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007632 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07007633 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007634 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
7635 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007636 window->setDropInputIfObscured(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07007637 window->setOwnerInfo(222, 222);
7638 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
7639 window->setFocusable(true);
7640 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
7641 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00007642 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07007643
7644 // With the flag set, window should not get any input
7645 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
7646 mDispatcher->notifyKey(&keyArgs);
7647 window->assertNoEvents();
7648
7649 NotifyMotionArgs motionArgs =
7650 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
7651 ADISPLAY_ID_DEFAULT);
7652 mDispatcher->notifyMotion(&motionArgs);
7653 window->assertNoEvents();
7654
7655 // When the window is no longer obscured because it went on top, it should get input
7656 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, obscuringWindow}}});
7657
7658 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
7659 mDispatcher->notifyKey(&keyArgs);
7660 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
7661
7662 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
7663 ADISPLAY_ID_DEFAULT);
7664 mDispatcher->notifyMotion(&motionArgs);
7665 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7666 window->assertNoEvents();
7667}
7668
Antonio Kantekf16f2832021-09-28 04:39:20 +00007669class InputDispatcherTouchModeChangedTests : public InputDispatcherTest {
7670protected:
7671 std::shared_ptr<FakeApplicationHandle> mApp;
Antonio Kantek15beb512022-06-13 22:35:41 +00007672 std::shared_ptr<FakeApplicationHandle> mSecondaryApp;
Antonio Kantekf16f2832021-09-28 04:39:20 +00007673 sp<FakeWindowHandle> mWindow;
7674 sp<FakeWindowHandle> mSecondWindow;
Antonio Kantek15beb512022-06-13 22:35:41 +00007675 sp<FakeWindowHandle> mThirdWindow;
Antonio Kantekf16f2832021-09-28 04:39:20 +00007676
7677 void SetUp() override {
7678 InputDispatcherTest::SetUp();
7679
7680 mApp = std::make_shared<FakeApplicationHandle>();
Antonio Kantek15beb512022-06-13 22:35:41 +00007681 mSecondaryApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007682 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00007683 mWindow->setFocusable(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00007684 setFocusedWindow(mWindow);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007685 mSecondWindow =
7686 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00007687 mSecondWindow->setFocusable(true);
Antonio Kantek15beb512022-06-13 22:35:41 +00007688 mThirdWindow =
7689 sp<FakeWindowHandle>::make(mSecondaryApp, mDispatcher,
7690 "TestWindow3_SecondaryDisplay", SECOND_DISPLAY_ID);
7691 mThirdWindow->setFocusable(true);
Antonio Kantekf16f2832021-09-28 04:39:20 +00007692
7693 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Antonio Kantek15beb512022-06-13 22:35:41 +00007694 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}},
7695 {SECOND_DISPLAY_ID, {mThirdWindow}}});
7696 mThirdWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
Antonio Kantekf16f2832021-09-28 04:39:20 +00007697 mWindow->consumeFocusEvent(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00007698
Antonio Kantek15beb512022-06-13 22:35:41 +00007699 // Set main display initial touch mode to InputDispatcher::kDefaultInTouchMode.
Prabir Pradhan5735a322022-04-11 17:23:34 +00007700 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, WINDOW_PID,
Harry Cutts33476232023-01-30 19:57:29 +00007701 WINDOW_UID, /*hasPermission=*/true, ADISPLAY_ID_DEFAULT)) {
Antonio Kantek48710e42022-03-24 14:19:30 -07007702 mWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
7703 mSecondWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +00007704 mThirdWindow->assertNoEvents();
7705 }
7706
7707 // Set secondary display initial touch mode to InputDispatcher::kDefaultInTouchMode.
7708 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, SECONDARY_WINDOW_PID,
Harry Cutts33476232023-01-30 19:57:29 +00007709 SECONDARY_WINDOW_UID, /*hasPermission=*/true,
Antonio Kantek15beb512022-06-13 22:35:41 +00007710 SECOND_DISPLAY_ID)) {
7711 mWindow->assertNoEvents();
7712 mSecondWindow->assertNoEvents();
7713 mThirdWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek48710e42022-03-24 14:19:30 -07007714 }
Antonio Kantekf16f2832021-09-28 04:39:20 +00007715 }
7716
Antonio Kantek15beb512022-06-13 22:35:41 +00007717 void changeAndVerifyTouchModeInMainDisplayOnly(bool inTouchMode, int32_t pid, int32_t uid,
7718 bool hasPermission) {
Antonio Kanteka042c022022-07-06 16:51:07 -07007719 ASSERT_TRUE(mDispatcher->setInTouchMode(inTouchMode, pid, uid, hasPermission,
7720 ADISPLAY_ID_DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +00007721 mWindow->consumeTouchModeEvent(inTouchMode);
7722 mSecondWindow->consumeTouchModeEvent(inTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +00007723 mThirdWindow->assertNoEvents();
Antonio Kantekf16f2832021-09-28 04:39:20 +00007724 }
7725};
7726
Antonio Kantek26defcf2022-02-08 01:12:27 +00007727TEST_F(InputDispatcherTouchModeChangedTests, FocusedWindowCanChangeTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -08007728 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek15beb512022-06-13 22:35:41 +00007729 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode,
7730 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00007731 /* hasPermission=*/false);
Antonio Kantekf16f2832021-09-28 04:39:20 +00007732}
7733
Antonio Kantek26defcf2022-02-08 01:12:27 +00007734TEST_F(InputDispatcherTouchModeChangedTests, NonFocusedWindowOwnerCannotChangeTouchMode) {
7735 const WindowInfo& windowInfo = *mWindow->getInfo();
7736 int32_t ownerPid = windowInfo.ownerPid;
7737 int32_t ownerUid = windowInfo.ownerUid;
7738 mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1);
7739 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, ownerPid,
Harry Cutts33476232023-01-30 19:57:29 +00007740 ownerUid, /*hasPermission=*/false,
Antonio Kanteka042c022022-07-06 16:51:07 -07007741 ADISPLAY_ID_DEFAULT));
Antonio Kantek26defcf2022-02-08 01:12:27 +00007742 mWindow->assertNoEvents();
7743 mSecondWindow->assertNoEvents();
7744}
7745
7746TEST_F(InputDispatcherTouchModeChangedTests, NonWindowOwnerMayChangeTouchModeOnPermissionGranted) {
7747 const WindowInfo& windowInfo = *mWindow->getInfo();
7748 int32_t ownerPid = windowInfo.ownerPid;
7749 int32_t ownerUid = windowInfo.ownerUid;
7750 mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1);
Antonio Kantek15beb512022-06-13 22:35:41 +00007751 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, ownerPid,
Harry Cutts33476232023-01-30 19:57:29 +00007752 ownerUid, /*hasPermission=*/true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00007753}
7754
Antonio Kantekf16f2832021-09-28 04:39:20 +00007755TEST_F(InputDispatcherTouchModeChangedTests, EventIsNotGeneratedIfNotChangingTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -08007756 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek26defcf2022-02-08 01:12:27 +00007757 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode,
7758 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00007759 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +00007760 mWindow->assertNoEvents();
7761 mSecondWindow->assertNoEvents();
7762}
7763
Antonio Kantek15beb512022-06-13 22:35:41 +00007764TEST_F(InputDispatcherTouchModeChangedTests, ChangeTouchOnSecondaryDisplayOnly) {
7765 const WindowInfo& windowInfo = *mThirdWindow->getInfo();
7766 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
7767 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00007768 /*hasPermission=*/true, SECOND_DISPLAY_ID));
Antonio Kantek15beb512022-06-13 22:35:41 +00007769 mWindow->assertNoEvents();
7770 mSecondWindow->assertNoEvents();
7771 mThirdWindow->consumeTouchModeEvent(!InputDispatcher::kDefaultInTouchMode);
7772}
7773
Antonio Kantek48710e42022-03-24 14:19:30 -07007774TEST_F(InputDispatcherTouchModeChangedTests, CanChangeTouchModeWhenOwningLastInteractedWindow) {
7775 // Interact with the window first.
7776 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
7777 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
7778 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
7779
7780 // Then remove focus.
7781 mWindow->setFocusable(false);
7782 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
7783
7784 // Assert that caller can switch touch mode by owning one of the last interacted window.
7785 const WindowInfo& windowInfo = *mWindow->getInfo();
7786 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
7787 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00007788 /*hasPermission=*/false, ADISPLAY_ID_DEFAULT));
Antonio Kantek48710e42022-03-24 14:19:30 -07007789}
7790
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007791class InputDispatcherSpyWindowTest : public InputDispatcherTest {
7792public:
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007793 sp<FakeWindowHandle> createSpy() {
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007794 std::shared_ptr<FakeApplicationHandle> application =
7795 std::make_shared<FakeApplicationHandle>();
7796 std::string name = "Fake Spy ";
7797 name += std::to_string(mSpyCount++);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007798 sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher,
7799 name.c_str(), ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007800 spy->setSpy(true);
Prabir Pradhan5c85e052021-12-22 02:27:12 -08007801 spy->setTrustedOverlay(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007802 return spy;
7803 }
7804
7805 sp<FakeWindowHandle> createForeground() {
7806 std::shared_ptr<FakeApplicationHandle> application =
7807 std::make_shared<FakeApplicationHandle>();
7808 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007809 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
7810 ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007811 window->setFocusable(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007812 return window;
7813 }
7814
7815private:
7816 int mSpyCount{0};
7817};
7818
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08007819using InputDispatcherSpyWindowDeathTest = InputDispatcherSpyWindowTest;
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007820/**
Prabir Pradhan5c85e052021-12-22 02:27:12 -08007821 * Adding a spy window that is not a trusted overlay causes Dispatcher to abort.
7822 */
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08007823TEST_F(InputDispatcherSpyWindowDeathTest, UntrustedSpy_AbortsDispatcher) {
7824 ScopedSilentDeath _silentDeath;
7825
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007826 auto spy = createSpy();
Prabir Pradhan5c85e052021-12-22 02:27:12 -08007827 spy->setTrustedOverlay(false);
7828 ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}}),
7829 ".* not a trusted overlay");
7830}
7831
7832/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007833 * Input injection into a display with a spy window but no foreground windows should succeed.
7834 */
7835TEST_F(InputDispatcherSpyWindowTest, NoForegroundWindow) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007836 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007837 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}});
7838
7839 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7840 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
7841 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7842 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7843}
7844
7845/**
7846 * Verify the order in which different input windows receive events. The touched foreground window
7847 * (if there is one) should always receive the event first. When there are multiple spy windows, the
7848 * spy windows will receive the event according to their Z-order, where the top-most spy window will
7849 * receive events before ones belows it.
7850 *
7851 * Here, we set up a scenario with four windows in the following Z order from the top:
7852 * spy1, spy2, window, spy3.
7853 * We then inject an event and verify that the foreground "window" receives it first, followed by
7854 * "spy1" and "spy2". The "spy3" does not receive the event because it is underneath the foreground
7855 * window.
7856 */
7857TEST_F(InputDispatcherSpyWindowTest, ReceivesInputInOrder) {
7858 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007859 auto spy1 = createSpy();
7860 auto spy2 = createSpy();
7861 auto spy3 = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007862 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window, spy3}}});
7863 const std::vector<sp<FakeWindowHandle>> channels{spy1, spy2, window, spy3};
7864 const size_t numChannels = channels.size();
7865
Michael Wright8e9a8562022-02-09 13:44:29 +00007866 base::unique_fd epollFd(epoll_create1(EPOLL_CLOEXEC));
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007867 if (!epollFd.ok()) {
7868 FAIL() << "Failed to create epoll fd";
7869 }
7870
7871 for (size_t i = 0; i < numChannels; i++) {
7872 struct epoll_event event = {.events = EPOLLIN, .data.u64 = i};
7873 if (epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, channels[i]->getChannelFd(), &event) < 0) {
7874 FAIL() << "Failed to add fd to epoll";
7875 }
7876 }
7877
7878 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7879 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
7880 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7881
7882 std::vector<size_t> eventOrder;
7883 std::vector<struct epoll_event> events(numChannels);
7884 for (;;) {
7885 const int nFds = epoll_wait(epollFd.get(), events.data(), static_cast<int>(numChannels),
7886 (100ms).count());
7887 if (nFds < 0) {
7888 FAIL() << "Failed to call epoll_wait";
7889 }
7890 if (nFds == 0) {
7891 break; // epoll_wait timed out
7892 }
7893 for (int i = 0; i < nFds; i++) {
Colin Cross5b799302022-10-18 21:52:41 -07007894 ASSERT_EQ(static_cast<uint32_t>(EPOLLIN), events[i].events);
Siarhei Vishniakou31977182022-09-30 08:51:23 -07007895 eventOrder.push_back(static_cast<size_t>(events[i].data.u64));
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007896 channels[i]->consumeMotionDown();
7897 }
7898 }
7899
7900 // Verify the order in which the events were received.
7901 EXPECT_EQ(3u, eventOrder.size());
7902 EXPECT_EQ(2u, eventOrder[0]); // index 2: window
7903 EXPECT_EQ(0u, eventOrder[1]); // index 0: spy1
7904 EXPECT_EQ(1u, eventOrder[2]); // index 1: spy2
7905}
7906
7907/**
7908 * A spy window using the NOT_TOUCHABLE flag does not receive events.
7909 */
7910TEST_F(InputDispatcherSpyWindowTest, NotTouchable) {
7911 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007912 auto spy = createSpy();
7913 spy->setTouchable(false);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007914 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
7915
7916 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7917 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
7918 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7919 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7920 spy->assertNoEvents();
7921}
7922
7923/**
7924 * A spy window will only receive gestures that originate within its touchable region. Gestures that
7925 * have their ACTION_DOWN outside of the touchable region of the spy window will not be dispatched
7926 * to the window.
7927 */
7928TEST_F(InputDispatcherSpyWindowTest, TouchableRegion) {
7929 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007930 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007931 spy->setTouchableRegion(Region{{0, 0, 20, 20}});
7932 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
7933
7934 // Inject an event outside the spy window's touchable region.
7935 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7936 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
7937 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7938 window->consumeMotionDown();
7939 spy->assertNoEvents();
7940 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7941 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
7942 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7943 window->consumeMotionUp();
7944 spy->assertNoEvents();
7945
7946 // Inject an event inside the spy window's touchable region.
7947 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7948 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7949 {5, 10}))
7950 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7951 window->consumeMotionDown();
7952 spy->consumeMotionDown();
7953}
7954
7955/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007956 * A spy window can listen for touches outside its touchable region using the WATCH_OUTSIDE_TOUCHES
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08007957 * flag, but it will get zero-ed out coordinates if the foreground has a different owner.
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007958 */
7959TEST_F(InputDispatcherSpyWindowTest, WatchOutsideTouches) {
7960 auto window = createForeground();
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08007961 window->setOwnerInfo(12, 34);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007962 auto spy = createSpy();
7963 spy->setWatchOutsideTouch(true);
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08007964 spy->setOwnerInfo(56, 78);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007965 spy->setFrame(Rect{0, 0, 20, 20});
7966 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
7967
7968 // Inject an event outside the spy window's frame and touchable region.
7969 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08007970 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7971 {100, 200}))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007972 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7973 window->consumeMotionDown();
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08007974 spy->consumeMotionOutsideWithZeroedCoords();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007975}
7976
7977/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007978 * Even when a spy window spans over multiple foreground windows, the spy should receive all
7979 * pointers that are down within its bounds.
7980 */
7981TEST_F(InputDispatcherSpyWindowTest, ReceivesMultiplePointers) {
7982 auto windowLeft = createForeground();
7983 windowLeft->setFrame({0, 0, 100, 200});
7984 auto windowRight = createForeground();
7985 windowRight->setFrame({100, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007986 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007987 spy->setFrame({0, 0, 200, 200});
7988 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, windowLeft, windowRight}}});
7989
7990 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7991 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7992 {50, 50}))
7993 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7994 windowLeft->consumeMotionDown();
7995 spy->consumeMotionDown();
7996
7997 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08007998 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007999 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Harry Cutts33476232023-01-30 19:57:29 +00008000 .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
8001 .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008002 .build();
8003 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8004 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8005 InputEventInjectionSync::WAIT_FOR_RESULT))
8006 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8007 windowRight->consumeMotionDown();
Harry Cutts33476232023-01-30 19:57:29 +00008008 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008009}
8010
8011/**
8012 * When the first pointer lands outside the spy window and the second pointer lands inside it, the
8013 * the spy should receive the second pointer with ACTION_DOWN.
8014 */
8015TEST_F(InputDispatcherSpyWindowTest, ReceivesSecondPointerAsDown) {
8016 auto window = createForeground();
8017 window->setFrame({0, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008018 auto spyRight = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008019 spyRight->setFrame({100, 0, 200, 200});
8020 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyRight, window}}});
8021
8022 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8023 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8024 {50, 50}))
8025 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8026 window->consumeMotionDown();
8027 spyRight->assertNoEvents();
8028
8029 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08008030 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008031 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Harry Cutts33476232023-01-30 19:57:29 +00008032 .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
8033 .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008034 .build();
8035 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8036 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8037 InputEventInjectionSync::WAIT_FOR_RESULT))
8038 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Harry Cutts33476232023-01-30 19:57:29 +00008039 window->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008040 spyRight->consumeMotionDown();
8041}
8042
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008043/**
8044 * The spy window should not be able to affect whether or not touches are split. Only the foreground
8045 * windows should be allowed to control split touch.
8046 */
8047TEST_F(InputDispatcherSpyWindowTest, SplitIfNoForegroundWindowTouched) {
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08008048 // This spy window prevents touch splitting. However, we still expect to split touches
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008049 // because a foreground window has not disabled splitting.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008050 auto spy = createSpy();
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08008051 spy->setPreventSplitting(true);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008052
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008053 auto window = createForeground();
8054 window->setFrame(Rect(0, 0, 100, 100));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008055
8056 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8057
8058 // First finger down, no window touched.
8059 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8060 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8061 {100, 200}))
8062 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8063 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8064 window->assertNoEvents();
8065
8066 // Second finger down on window, the window should receive touch down.
8067 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08008068 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008069 .displayId(ADISPLAY_ID_DEFAULT)
8070 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Harry Cutts33476232023-01-30 19:57:29 +00008071 .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(100).y(200))
8072 .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008073 .build();
8074 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8075 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8076 InputEventInjectionSync::WAIT_FOR_RESULT))
8077 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8078
8079 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Harry Cutts33476232023-01-30 19:57:29 +00008080 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008081}
8082
8083/**
8084 * A spy window will usually be implemented as an un-focusable window. Verify that these windows
8085 * do not receive key events.
8086 */
8087TEST_F(InputDispatcherSpyWindowTest, UnfocusableSpyDoesNotReceiveKeyEvents) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008088 auto spy = createSpy();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008089 spy->setFocusable(false);
8090
8091 auto window = createForeground();
8092 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8093 setFocusedWindow(window);
8094 window->consumeFocusEvent(true);
8095
8096 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
8097 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
8098 window->consumeKeyDown(ADISPLAY_ID_NONE);
8099
8100 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
8101 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
8102 window->consumeKeyUp(ADISPLAY_ID_NONE);
8103
8104 spy->assertNoEvents();
8105}
8106
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00008107using InputDispatcherPilferPointersTest = InputDispatcherSpyWindowTest;
8108
8109/**
8110 * A spy window can pilfer pointers. When this happens, touch gestures used by the spy window that
8111 * are currently sent to any other windows - including other spy windows - will also be cancelled.
8112 */
8113TEST_F(InputDispatcherPilferPointersTest, PilferPointers) {
8114 auto window = createForeground();
8115 auto spy1 = createSpy();
8116 auto spy2 = createSpy();
8117 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window}}});
8118
8119 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8120 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8121 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8122 window->consumeMotionDown();
8123 spy1->consumeMotionDown();
8124 spy2->consumeMotionDown();
8125
8126 // Pilfer pointers from the second spy window.
8127 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy2->getToken()));
8128 spy2->assertNoEvents();
8129 spy1->consumeMotionCancel();
8130 window->consumeMotionCancel();
8131
8132 // The rest of the gesture should only be sent to the second spy window.
8133 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8134 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8135 ADISPLAY_ID_DEFAULT))
8136 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8137 spy2->consumeMotionMove();
8138 spy1->assertNoEvents();
8139 window->assertNoEvents();
8140}
8141
8142/**
8143 * A spy window can pilfer pointers for a gesture even after the foreground window has been removed
8144 * in the middle of the gesture.
8145 */
8146TEST_F(InputDispatcherPilferPointersTest, CanPilferAfterWindowIsRemovedMidStream) {
8147 auto window = createForeground();
8148 auto spy = createSpy();
8149 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8150
8151 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8152 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8153 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8154 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8155 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8156
8157 window->releaseChannel();
8158
8159 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
8160
8161 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8162 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8163 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8164 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8165}
8166
8167/**
8168 * After a spy window pilfers pointers, new pointers that go down in its bounds should be sent to
8169 * the spy, but not to any other windows.
8170 */
8171TEST_F(InputDispatcherPilferPointersTest, ContinuesToReceiveGestureAfterPilfer) {
8172 auto spy = createSpy();
8173 auto window = createForeground();
8174
8175 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8176
8177 // First finger down on the window and the spy.
8178 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8179 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8180 {100, 200}))
8181 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8182 spy->consumeMotionDown();
8183 window->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 and spy, but the window should not receive the pointer down.
8190 const MotionEvent secondFingerDownEvent =
8191 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8192 .displayId(ADISPLAY_ID_DEFAULT)
8193 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Harry Cutts33476232023-01-30 19:57:29 +00008194 .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(100).y(200))
8195 .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00008196 .build();
8197 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8198 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8199 InputEventInjectionSync::WAIT_FOR_RESULT))
8200 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8201
Harry Cutts33476232023-01-30 19:57:29 +00008202 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00008203
8204 // Third finger goes down outside all windows, so injection should fail.
8205 const MotionEvent thirdFingerDownEvent =
8206 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8207 .displayId(ADISPLAY_ID_DEFAULT)
8208 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Harry Cutts33476232023-01-30 19:57:29 +00008209 .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(100).y(200))
8210 .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
8211 .pointer(PointerBuilder(/*id=*/2, AMOTION_EVENT_TOOL_TYPE_FINGER).x(-5).y(-5))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00008212 .build();
8213 ASSERT_EQ(InputEventInjectionResult::FAILED,
8214 injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
8215 InputEventInjectionSync::WAIT_FOR_RESULT))
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08008216 << "Inject motion event should return InputEventInjectionResult::FAILED";
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00008217
8218 spy->assertNoEvents();
8219 window->assertNoEvents();
8220}
8221
8222/**
8223 * After a spy window pilfers pointers, only the pointers used by the spy should be canceled
8224 */
8225TEST_F(InputDispatcherPilferPointersTest, PartiallyPilferRequiredPointers) {
8226 auto spy = createSpy();
8227 spy->setFrame(Rect(0, 0, 100, 100));
8228 auto window = createForeground();
8229 window->setFrame(Rect(0, 0, 200, 200));
8230
8231 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8232
8233 // First finger down on the window only
8234 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8235 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8236 {150, 150}))
8237 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8238 window->consumeMotionDown();
8239
8240 // Second finger down on the spy and window
8241 const MotionEvent secondFingerDownEvent =
8242 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8243 .displayId(ADISPLAY_ID_DEFAULT)
8244 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Harry Cutts33476232023-01-30 19:57:29 +00008245 .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(150))
8246 .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(10).y(10))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00008247 .build();
8248 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8249 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8250 InputEventInjectionSync::WAIT_FOR_RESULT))
8251 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8252 spy->consumeMotionDown();
8253 window->consumeMotionPointerDown(1);
8254
8255 // Third finger down on the spy and window
8256 const MotionEvent thirdFingerDownEvent =
8257 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8258 .displayId(ADISPLAY_ID_DEFAULT)
8259 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Harry Cutts33476232023-01-30 19:57:29 +00008260 .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(150))
8261 .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(10).y(10))
8262 .pointer(PointerBuilder(/*id=*/2, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00008263 .build();
8264 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8265 injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
8266 InputEventInjectionSync::WAIT_FOR_RESULT))
8267 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8268 spy->consumeMotionPointerDown(1);
8269 window->consumeMotionPointerDown(2);
8270
8271 // Spy window pilfers the pointers.
8272 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
8273 window->consumeMotionPointerUp(/* idx */ 2, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
8274 window->consumeMotionPointerUp(/* idx */ 1, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
8275
8276 spy->assertNoEvents();
8277 window->assertNoEvents();
8278}
8279
8280/**
8281 * After a spy window pilfers pointers, all pilfered pointers that have already been dispatched to
8282 * other windows should be canceled. If this results in the cancellation of all pointers for some
8283 * window, then that window should receive ACTION_CANCEL.
8284 */
8285TEST_F(InputDispatcherPilferPointersTest, PilferAllRequiredPointers) {
8286 auto spy = createSpy();
8287 spy->setFrame(Rect(0, 0, 100, 100));
8288 auto window = createForeground();
8289 window->setFrame(Rect(0, 0, 200, 200));
8290
8291 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8292
8293 // First finger down on both spy and window
8294 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8295 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8296 {10, 10}))
8297 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8298 window->consumeMotionDown();
8299 spy->consumeMotionDown();
8300
8301 // Second finger down on the spy and window
8302 const MotionEvent secondFingerDownEvent =
8303 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8304 .displayId(ADISPLAY_ID_DEFAULT)
8305 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Harry Cutts33476232023-01-30 19:57:29 +00008306 .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(10).y(10))
8307 .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00008308 .build();
8309 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8310 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8311 InputEventInjectionSync::WAIT_FOR_RESULT))
8312 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8313 spy->consumeMotionPointerDown(1);
8314 window->consumeMotionPointerDown(1);
8315
8316 // Spy window pilfers the pointers.
8317 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
8318 window->consumeMotionCancel();
8319
8320 spy->assertNoEvents();
8321 window->assertNoEvents();
8322}
8323
8324/**
8325 * After a spy window pilfers pointers, new pointers that are not touching the spy window can still
8326 * be sent to other windows
8327 */
8328TEST_F(InputDispatcherPilferPointersTest, CanReceivePointersAfterPilfer) {
8329 auto spy = createSpy();
8330 spy->setFrame(Rect(0, 0, 100, 100));
8331 auto window = createForeground();
8332 window->setFrame(Rect(0, 0, 200, 200));
8333
8334 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8335
8336 // First finger down on both window and spy
8337 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8338 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8339 {10, 10}))
8340 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8341 window->consumeMotionDown();
8342 spy->consumeMotionDown();
8343
8344 // Spy window pilfers the pointers.
8345 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
8346 window->consumeMotionCancel();
8347
8348 // Second finger down on the window only
8349 const MotionEvent secondFingerDownEvent =
8350 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8351 .displayId(ADISPLAY_ID_DEFAULT)
8352 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Harry Cutts33476232023-01-30 19:57:29 +00008353 .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(10).y(10))
8354 .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(150))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00008355 .build();
8356 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8357 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8358 InputEventInjectionSync::WAIT_FOR_RESULT))
8359 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8360 window->consumeMotionDown();
8361 window->assertNoEvents();
8362
8363 // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line
8364 spy->consumeMotionMove();
8365 spy->assertNoEvents();
8366}
8367
Prabir Pradhand65552b2021-10-07 11:23:50 -07008368class InputDispatcherStylusInterceptorTest : public InputDispatcherTest {
8369public:
8370 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupStylusOverlayScenario() {
8371 std::shared_ptr<FakeApplicationHandle> overlayApplication =
8372 std::make_shared<FakeApplicationHandle>();
8373 sp<FakeWindowHandle> overlay =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008374 sp<FakeWindowHandle>::make(overlayApplication, mDispatcher,
8375 "Stylus interceptor window", ADISPLAY_ID_DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -07008376 overlay->setFocusable(false);
8377 overlay->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008378 overlay->setTouchable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008379 overlay->setInterceptsStylus(true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07008380 overlay->setTrustedOverlay(true);
8381
8382 std::shared_ptr<FakeApplicationHandle> application =
8383 std::make_shared<FakeApplicationHandle>();
8384 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008385 sp<FakeWindowHandle>::make(application, mDispatcher, "Application window",
8386 ADISPLAY_ID_DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -07008387 window->setFocusable(true);
8388 window->setOwnerInfo(222, 222);
Prabir Pradhand65552b2021-10-07 11:23:50 -07008389
8390 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
8391 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
8392 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00008393 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07008394 return {std::move(overlay), std::move(window)};
8395 }
8396
8397 void sendFingerEvent(int32_t action) {
8398 NotifyMotionArgs motionArgs =
8399 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
8400 ADISPLAY_ID_DEFAULT, {PointF{20, 20}});
8401 mDispatcher->notifyMotion(&motionArgs);
8402 }
8403
8404 void sendStylusEvent(int32_t action) {
8405 NotifyMotionArgs motionArgs =
8406 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
8407 ADISPLAY_ID_DEFAULT, {PointF{30, 40}});
8408 motionArgs.pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS;
8409 mDispatcher->notifyMotion(&motionArgs);
8410 }
8411};
8412
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08008413using InputDispatcherStylusInterceptorDeathTest = InputDispatcherStylusInterceptorTest;
8414
8415TEST_F(InputDispatcherStylusInterceptorDeathTest, UntrustedOverlay_AbortsDispatcher) {
8416 ScopedSilentDeath _silentDeath;
8417
Prabir Pradhand65552b2021-10-07 11:23:50 -07008418 auto [overlay, window] = setupStylusOverlayScenario();
8419 overlay->setTrustedOverlay(false);
8420 // Configuring an untrusted overlay as a stylus interceptor should cause Dispatcher to abort.
8421 ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}),
8422 ".* not a trusted overlay");
8423}
8424
8425TEST_F(InputDispatcherStylusInterceptorTest, ConsmesOnlyStylusEvents) {
8426 auto [overlay, window] = setupStylusOverlayScenario();
8427 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
8428
8429 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
8430 overlay->consumeMotionDown();
8431 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
8432 overlay->consumeMotionUp();
8433
8434 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
8435 window->consumeMotionDown();
8436 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
8437 window->consumeMotionUp();
8438
8439 overlay->assertNoEvents();
8440 window->assertNoEvents();
8441}
8442
8443TEST_F(InputDispatcherStylusInterceptorTest, SpyWindowStylusInterceptor) {
8444 auto [overlay, window] = setupStylusOverlayScenario();
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008445 overlay->setSpy(true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07008446 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
8447
8448 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
8449 overlay->consumeMotionDown();
8450 window->consumeMotionDown();
8451 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
8452 overlay->consumeMotionUp();
8453 window->consumeMotionUp();
8454
8455 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
8456 window->consumeMotionDown();
8457 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
8458 window->consumeMotionUp();
8459
8460 overlay->assertNoEvents();
8461 window->assertNoEvents();
8462}
8463
Prabir Pradhan6dfbf262022-03-14 15:24:30 +00008464/**
8465 * Set up a scenario to test the behavior used by the stylus handwriting detection feature.
8466 * The scenario is as follows:
8467 * - The stylus interceptor overlay is configured as a spy window.
8468 * - The stylus interceptor spy receives the start of a new stylus gesture.
8469 * - It pilfers pointers and then configures itself to no longer be a spy.
8470 * - The stylus interceptor continues to receive the rest of the gesture.
8471 */
8472TEST_F(InputDispatcherStylusInterceptorTest, StylusHandwritingScenario) {
8473 auto [overlay, window] = setupStylusOverlayScenario();
8474 overlay->setSpy(true);
8475 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
8476
8477 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
8478 overlay->consumeMotionDown();
8479 window->consumeMotionDown();
8480
8481 // The interceptor pilfers the pointers.
8482 EXPECT_EQ(OK, mDispatcher->pilferPointers(overlay->getToken()));
8483 window->consumeMotionCancel();
8484
8485 // The interceptor configures itself so that it is no longer a spy.
8486 overlay->setSpy(false);
8487 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
8488
8489 // It continues to receive the rest of the stylus gesture.
8490 sendStylusEvent(AMOTION_EVENT_ACTION_MOVE);
8491 overlay->consumeMotionMove();
8492 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
8493 overlay->consumeMotionUp();
8494
8495 window->assertNoEvents();
8496}
8497
Prabir Pradhan5735a322022-04-11 17:23:34 +00008498struct User {
8499 int32_t mPid;
8500 int32_t mUid;
8501 uint32_t mPolicyFlags{DEFAULT_POLICY_FLAGS};
8502 std::unique_ptr<InputDispatcher>& mDispatcher;
8503
8504 User(std::unique_ptr<InputDispatcher>& dispatcher, int32_t pid, int32_t uid)
8505 : mPid(pid), mUid(uid), mDispatcher(dispatcher) {}
8506
8507 InputEventInjectionResult injectTargetedMotion(int32_t action) const {
8508 return injectMotionEvent(mDispatcher, action, AINPUT_SOURCE_TOUCHSCREEN,
8509 ADISPLAY_ID_DEFAULT, {100, 200},
8510 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
8511 AMOTION_EVENT_INVALID_CURSOR_POSITION},
8512 INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT,
8513 systemTime(SYSTEM_TIME_MONOTONIC), {mUid}, mPolicyFlags);
8514 }
8515
8516 InputEventInjectionResult injectTargetedKey(int32_t action) const {
Harry Cutts33476232023-01-30 19:57:29 +00008517 return inputdispatcher::injectKey(mDispatcher, action, /*repeatCount=*/0, ADISPLAY_ID_NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +00008518 InputEventInjectionSync::WAIT_FOR_RESULT,
Harry Cutts33476232023-01-30 19:57:29 +00008519 INJECT_EVENT_TIMEOUT, /*allowKeyRepeat=*/false, {mUid},
Prabir Pradhan5735a322022-04-11 17:23:34 +00008520 mPolicyFlags);
8521 }
8522
8523 sp<FakeWindowHandle> createWindow() const {
8524 std::shared_ptr<FakeApplicationHandle> overlayApplication =
8525 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008526 sp<FakeWindowHandle> window =
8527 sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, "Owned Window",
8528 ADISPLAY_ID_DEFAULT);
Prabir Pradhan5735a322022-04-11 17:23:34 +00008529 window->setOwnerInfo(mPid, mUid);
8530 return window;
8531 }
8532};
8533
8534using InputDispatcherTargetedInjectionTest = InputDispatcherTest;
8535
8536TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedWindow) {
8537 auto owner = User(mDispatcher, 10, 11);
8538 auto window = owner.createWindow();
8539 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
8540
8541 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
8542 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
8543 window->consumeMotionDown();
8544
8545 setFocusedWindow(window);
8546 window->consumeFocusEvent(true);
8547
8548 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
8549 owner.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
8550 window->consumeKeyDown(ADISPLAY_ID_NONE);
8551}
8552
8553TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedWindow) {
8554 auto owner = User(mDispatcher, 10, 11);
8555 auto window = owner.createWindow();
8556 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
8557
8558 auto rando = User(mDispatcher, 20, 21);
8559 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
8560 rando.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
8561
8562 setFocusedWindow(window);
8563 window->consumeFocusEvent(true);
8564
8565 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
8566 rando.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
8567 window->assertNoEvents();
8568}
8569
8570TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedSpyWindow) {
8571 auto owner = User(mDispatcher, 10, 11);
8572 auto window = owner.createWindow();
8573 auto spy = owner.createWindow();
8574 spy->setSpy(true);
8575 spy->setTrustedOverlay(true);
8576 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8577
8578 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
8579 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
8580 spy->consumeMotionDown();
8581 window->consumeMotionDown();
8582}
8583
8584TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedSpyWindow) {
8585 auto owner = User(mDispatcher, 10, 11);
8586 auto window = owner.createWindow();
8587
8588 auto rando = User(mDispatcher, 20, 21);
8589 auto randosSpy = rando.createWindow();
8590 randosSpy->setSpy(true);
8591 randosSpy->setTrustedOverlay(true);
8592 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}});
8593
8594 // The event is targeted at owner's window, so injection should succeed, but the spy should
8595 // not receive the event.
8596 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
8597 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
8598 randosSpy->assertNoEvents();
8599 window->consumeMotionDown();
8600}
8601
8602TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoAnyWindowWhenNotTargeting) {
8603 auto owner = User(mDispatcher, 10, 11);
8604 auto window = owner.createWindow();
8605
8606 auto rando = User(mDispatcher, 20, 21);
8607 auto randosSpy = rando.createWindow();
8608 randosSpy->setSpy(true);
8609 randosSpy->setTrustedOverlay(true);
8610 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}});
8611
8612 // A user that has injection permission can inject into any window.
8613 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
8614 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
8615 ADISPLAY_ID_DEFAULT));
8616 randosSpy->consumeMotionDown();
8617 window->consumeMotionDown();
8618
8619 setFocusedWindow(randosSpy);
8620 randosSpy->consumeFocusEvent(true);
8621
8622 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher));
8623 randosSpy->consumeKeyDown(ADISPLAY_ID_NONE);
8624 window->assertNoEvents();
8625}
8626
8627TEST_F(InputDispatcherTargetedInjectionTest, CanGenerateActionOutsideToOtherUids) {
8628 auto owner = User(mDispatcher, 10, 11);
8629 auto window = owner.createWindow();
8630
8631 auto rando = User(mDispatcher, 20, 21);
8632 auto randosWindow = rando.createWindow();
8633 randosWindow->setFrame(Rect{-10, -10, -5, -5});
8634 randosWindow->setWatchOutsideTouch(true);
8635 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosWindow, window}}});
8636
8637 // We allow generation of ACTION_OUTSIDE events into windows owned by different uids.
8638 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
8639 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
8640 window->consumeMotionDown();
8641 randosWindow->consumeMotionOutside();
8642}
8643
Garfield Tane84e6f92019-08-29 17:28:41 -07008644} // namespace android::inputdispatcher