blob: e3bc5168a12fe104a535d27b7e3caf78a52400de [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 Vishniakou4cb50ca2020-05-26 21:43:02 -070032#include <thread>
Garfield Tan1c7bc862020-01-28 13:24:04 -080033#include <unordered_set>
chaviwd1c23182019-12-20 18:44:56 -080034#include <vector>
Michael Wrightd02c5b62014-02-10 15:10:22 -080035
Garfield Tan1c7bc862020-01-28 13:24:04 -080036using android::base::StringPrintf;
chaviw3277faf2021-05-19 16:45:23 -050037using android::gui::FocusRequest;
38using android::gui::TouchOcclusionMode;
39using android::gui::WindowInfo;
40using android::gui::WindowInfoHandle;
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080041using android::os::InputEventInjectionResult;
42using android::os::InputEventInjectionSync;
Garfield Tan1c7bc862020-01-28 13:24:04 -080043
Garfield Tane84e6f92019-08-29 17:28:41 -070044namespace android::inputdispatcher {
Michael Wrightd02c5b62014-02-10 15:10:22 -080045
Dominik Laskowski2f01d772022-03-23 16:01:29 -070046using namespace ftl::flag_operators;
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -080047using testing::AllOf;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070048
Michael Wrightd02c5b62014-02-10 15:10:22 -080049// An arbitrary time value.
Prabir Pradhan5735a322022-04-11 17:23:34 +000050static constexpr nsecs_t ARBITRARY_TIME = 1234;
Michael Wrightd02c5b62014-02-10 15:10:22 -080051
52// An arbitrary device id.
Prabir Pradhan5735a322022-04-11 17:23:34 +000053static constexpr int32_t DEVICE_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -080054
Jeff Brownf086ddb2014-02-11 14:28:48 -080055// An arbitrary display id.
Arthur Hungabbb9d82021-09-01 14:52:30 +000056static constexpr int32_t DISPLAY_ID = ADISPLAY_ID_DEFAULT;
57static constexpr int32_t SECOND_DISPLAY_ID = 1;
Jeff Brownf086ddb2014-02-11 14:28:48 -080058
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -080059static constexpr int32_t POINTER_1_DOWN =
60 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +000061static constexpr int32_t POINTER_2_DOWN =
62 AMOTION_EVENT_ACTION_POINTER_DOWN | (2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +000063static constexpr int32_t POINTER_3_DOWN =
64 AMOTION_EVENT_ACTION_POINTER_DOWN | (3 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Arthur Hungc539dbb2022-12-08 07:45:36 +000065static constexpr int32_t POINTER_0_UP =
66 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -080067static constexpr int32_t POINTER_1_UP =
68 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
69
Antonio Kantek15beb512022-06-13 22:35:41 +000070// The default pid and uid for windows created on the primary display by the test.
Prabir Pradhan5735a322022-04-11 17:23:34 +000071static constexpr int32_t WINDOW_PID = 999;
72static constexpr int32_t WINDOW_UID = 1001;
73
Antonio Kantek15beb512022-06-13 22:35:41 +000074// The default pid and uid for the windows created on the secondary display by the test.
75static constexpr int32_t SECONDARY_WINDOW_PID = 1010;
76static constexpr int32_t SECONDARY_WINDOW_UID = 1012;
77
Prabir Pradhan5735a322022-04-11 17:23:34 +000078// The default policy flags to use for event injection by tests.
79static constexpr uint32_t DEFAULT_POLICY_FLAGS = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER;
Michael Wrightd02c5b62014-02-10 15:10:22 -080080
Siarhei Vishniakou58cfc602020-12-14 23:21:30 +000081// An arbitrary pid of the gesture monitor window
82static constexpr int32_t MONITOR_PID = 2001;
83
Siarhei Vishniakou289e9242022-02-15 14:50:16 -080084static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 1000ms;
85
Arthur Hungc539dbb2022-12-08 07:45:36 +000086static constexpr int expectedWallpaperFlags =
87 AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED;
88
chaviwd1c23182019-12-20 18:44:56 -080089struct PointF {
90 float x;
91 float y;
92};
Michael Wrightd02c5b62014-02-10 15:10:22 -080093
Gang Wang342c9272020-01-13 13:15:04 -050094/**
95 * Return a DOWN key event with KEYCODE_A.
96 */
97static KeyEvent getTestKeyEvent() {
98 KeyEvent event;
99
Garfield Tanfbe732e2020-01-24 11:26:14 -0800100 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
101 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
102 ARBITRARY_TIME, ARBITRARY_TIME);
Gang Wang342c9272020-01-13 13:15:04 -0500103 return event;
104}
105
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000106static void assertMotionAction(int32_t expectedAction, int32_t receivedAction) {
107 ASSERT_EQ(expectedAction, receivedAction)
108 << "expected " << MotionEvent::actionToString(expectedAction) << ", got "
109 << MotionEvent::actionToString(receivedAction);
110}
111
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -0800112MATCHER_P(WithMotionAction, action, "MotionEvent with specified action") {
113 bool matches = action == arg.getAction();
114 if (!matches) {
115 *result_listener << "expected action " << MotionEvent::actionToString(action)
116 << ", but got " << MotionEvent::actionToString(arg.getAction());
117 }
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -0800118 if (action == AMOTION_EVENT_ACTION_DOWN) {
119 if (!matches) {
120 *result_listener << "; ";
121 }
122 *result_listener << "downTime should match eventTime for ACTION_DOWN events";
123 matches &= arg.getDownTime() == arg.getEventTime();
124 }
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -0800125 if (action == AMOTION_EVENT_ACTION_CANCEL) {
126 if (!matches) {
127 *result_listener << "; ";
128 }
129 *result_listener << "expected FLAG_CANCELED to be set with ACTION_CANCEL, but was not set";
130 matches &= (arg.getFlags() & AMOTION_EVENT_FLAG_CANCELED) != 0;
131 }
132 return matches;
133}
134
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -0800135MATCHER_P(WithDownTime, downTime, "InputEvent with specified downTime") {
136 return arg.getDownTime() == downTime;
137}
138
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -0800139MATCHER_P(WithSource, source, "InputEvent with specified source") {
140 *result_listener << "expected source " << inputEventSourceToString(source) << ", but got "
141 << inputEventSourceToString(arg.getSource());
142 return arg.getSource() == source;
143}
144
Michael Wrightd02c5b62014-02-10 15:10:22 -0800145// --- FakeInputDispatcherPolicy ---
146
147class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface {
148 InputDispatcherConfiguration mConfig;
149
Prabir Pradhanedd96402022-02-15 01:46:16 -0800150 using AnrResult = std::pair<sp<IBinder>, int32_t /*pid*/>;
151
Michael Wrightd02c5b62014-02-10 15:10:22 -0800152protected:
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000153 virtual ~FakeInputDispatcherPolicy() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800154
155public:
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000156 FakeInputDispatcherPolicy() {}
Jackal Guof9696682018-10-05 12:23:23 +0800157
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800158 void assertFilterInputEventWasCalled(const NotifyKeyArgs& args) {
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700159 assertFilterInputEventWasCalledInternal([&args](const InputEvent& event) {
160 ASSERT_EQ(event.getType(), AINPUT_EVENT_TYPE_KEY);
161 EXPECT_EQ(event.getDisplayId(), args.displayId);
162
163 const auto& keyEvent = static_cast<const KeyEvent&>(event);
164 EXPECT_EQ(keyEvent.getEventTime(), args.eventTime);
165 EXPECT_EQ(keyEvent.getAction(), args.action);
166 });
Jackal Guof9696682018-10-05 12:23:23 +0800167 }
168
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700169 void assertFilterInputEventWasCalled(const NotifyMotionArgs& args, vec2 point) {
170 assertFilterInputEventWasCalledInternal([&](const InputEvent& event) {
171 ASSERT_EQ(event.getType(), AINPUT_EVENT_TYPE_MOTION);
172 EXPECT_EQ(event.getDisplayId(), args.displayId);
173
174 const auto& motionEvent = static_cast<const MotionEvent&>(event);
175 EXPECT_EQ(motionEvent.getEventTime(), args.eventTime);
176 EXPECT_EQ(motionEvent.getAction(), args.action);
177 EXPECT_EQ(motionEvent.getX(0), point.x);
178 EXPECT_EQ(motionEvent.getY(0), point.y);
179 EXPECT_EQ(motionEvent.getRawX(0), point.x);
180 EXPECT_EQ(motionEvent.getRawY(0), point.y);
181 });
Jackal Guof9696682018-10-05 12:23:23 +0800182 }
183
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700184 void assertFilterInputEventWasNotCalled() {
185 std::scoped_lock lock(mLock);
186 ASSERT_EQ(nullptr, mFilteredEvent);
187 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800188
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800189 void assertNotifyConfigurationChangedWasCalled(nsecs_t when) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700190 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800191 ASSERT_TRUE(mConfigurationChangedTime)
192 << "Timed out waiting for configuration changed call";
193 ASSERT_EQ(*mConfigurationChangedTime, when);
194 mConfigurationChangedTime = std::nullopt;
195 }
196
197 void assertNotifySwitchWasCalled(const NotifySwitchArgs& args) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700198 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800199 ASSERT_TRUE(mLastNotifySwitch);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800200 // We do not check id because it is not exposed to the policy
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800201 EXPECT_EQ(args.eventTime, mLastNotifySwitch->eventTime);
202 EXPECT_EQ(args.policyFlags, mLastNotifySwitch->policyFlags);
203 EXPECT_EQ(args.switchValues, mLastNotifySwitch->switchValues);
204 EXPECT_EQ(args.switchMask, mLastNotifySwitch->switchMask);
205 mLastNotifySwitch = std::nullopt;
206 }
207
chaviwfd6d3512019-03-25 13:23:49 -0700208 void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700209 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800210 ASSERT_EQ(touchedToken, mOnPointerDownToken);
211 mOnPointerDownToken.clear();
212 }
213
214 void assertOnPointerDownWasNotCalled() {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700215 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800216 ASSERT_TRUE(mOnPointerDownToken == nullptr)
217 << "Expected onPointerDownOutsideFocus to not have been called";
chaviwfd6d3512019-03-25 13:23:49 -0700218 }
219
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700220 // This function must be called soon after the expected ANR timer starts,
221 // because we are also checking how much time has passed.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500222 void assertNotifyNoFocusedWindowAnrWasCalled(
Chris Yea209fde2020-07-22 13:54:51 -0700223 std::chrono::nanoseconds timeout,
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500224 const std::shared_ptr<InputApplicationHandle>& expectedApplication) {
Prabir Pradhanedd96402022-02-15 01:46:16 -0800225 std::unique_lock lock(mLock);
226 android::base::ScopedLockAssertion assumeLocked(mLock);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500227 std::shared_ptr<InputApplicationHandle> application;
Prabir Pradhanedd96402022-02-15 01:46:16 -0800228 ASSERT_NO_FATAL_FAILURE(
229 application = getAnrTokenLockedInterruptible(timeout, mAnrApplications, lock));
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500230 ASSERT_EQ(expectedApplication, application);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700231 }
232
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000233 void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout,
Prabir Pradhanedd96402022-02-15 01:46:16 -0800234 const sp<WindowInfoHandle>& window) {
235 LOG_ALWAYS_FATAL_IF(window == nullptr, "window should not be null");
236 assertNotifyWindowUnresponsiveWasCalled(timeout, window->getToken(),
237 window->getInfo()->ownerPid);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500238 }
239
Prabir Pradhanedd96402022-02-15 01:46:16 -0800240 void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout,
241 const sp<IBinder>& expectedToken,
242 int32_t expectedPid) {
243 std::unique_lock lock(mLock);
244 android::base::ScopedLockAssertion assumeLocked(mLock);
245 AnrResult result;
246 ASSERT_NO_FATAL_FAILURE(result =
247 getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock));
248 const auto& [token, pid] = result;
249 ASSERT_EQ(expectedToken, token);
250 ASSERT_EQ(expectedPid, pid);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500251 }
252
Prabir Pradhanedd96402022-02-15 01:46:16 -0800253 /** Wrap call with ASSERT_NO_FATAL_FAILURE() to ensure the return value is valid. */
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000254 sp<IBinder> getUnresponsiveWindowToken(std::chrono::nanoseconds timeout) {
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500255 std::unique_lock lock(mLock);
256 android::base::ScopedLockAssertion assumeLocked(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800257 AnrResult result = getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock);
258 const auto& [token, _] = result;
259 return token;
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000260 }
261
Prabir Pradhanedd96402022-02-15 01:46:16 -0800262 void assertNotifyWindowResponsiveWasCalled(const sp<IBinder>& expectedToken,
263 int32_t expectedPid) {
264 std::unique_lock lock(mLock);
265 android::base::ScopedLockAssertion assumeLocked(mLock);
266 AnrResult result;
267 ASSERT_NO_FATAL_FAILURE(
268 result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock));
269 const auto& [token, pid] = result;
270 ASSERT_EQ(expectedToken, token);
271 ASSERT_EQ(expectedPid, pid);
272 }
273
274 /** Wrap call with ASSERT_NO_FATAL_FAILURE() to ensure the return value is valid. */
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000275 sp<IBinder> getResponsiveWindowToken() {
276 std::unique_lock lock(mLock);
277 android::base::ScopedLockAssertion assumeLocked(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800278 AnrResult result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock);
279 const auto& [token, _] = result;
280 return token;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700281 }
282
283 void assertNotifyAnrWasNotCalled() {
284 std::scoped_lock lock(mLock);
285 ASSERT_TRUE(mAnrApplications.empty());
Prabir Pradhanedd96402022-02-15 01:46:16 -0800286 ASSERT_TRUE(mAnrWindows.empty());
287 ASSERT_TRUE(mResponsiveWindows.empty())
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500288 << "ANR was not called, but please also consume the 'connection is responsive' "
289 "signal";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700290 }
291
Garfield Tan1c7bc862020-01-28 13:24:04 -0800292 void setKeyRepeatConfiguration(nsecs_t timeout, nsecs_t delay) {
293 mConfig.keyRepeatTimeout = timeout;
294 mConfig.keyRepeatDelay = delay;
295 }
296
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000297 PointerCaptureRequest assertSetPointerCaptureCalled(bool enabled) {
Prabir Pradhan99987712020-11-10 18:43:05 -0800298 std::unique_lock lock(mLock);
299 base::ScopedLockAssertion assumeLocked(mLock);
300
301 if (!mPointerCaptureChangedCondition.wait_for(lock, 100ms,
302 [this, enabled]() REQUIRES(mLock) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000303 return mPointerCaptureRequest->enable ==
Prabir Pradhan99987712020-11-10 18:43:05 -0800304 enabled;
305 })) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000306 ADD_FAILURE() << "Timed out waiting for setPointerCapture(" << enabled
307 << ") to be called.";
308 return {};
Prabir Pradhan99987712020-11-10 18:43:05 -0800309 }
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000310 auto request = *mPointerCaptureRequest;
311 mPointerCaptureRequest.reset();
312 return request;
Prabir Pradhan99987712020-11-10 18:43:05 -0800313 }
314
315 void assertSetPointerCaptureNotCalled() {
316 std::unique_lock lock(mLock);
317 base::ScopedLockAssertion assumeLocked(mLock);
318
319 if (mPointerCaptureChangedCondition.wait_for(lock, 100ms) != std::cv_status::timeout) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000320 FAIL() << "Expected setPointerCapture(request) to not be called, but was called. "
Prabir Pradhan99987712020-11-10 18:43:05 -0800321 "enabled = "
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000322 << std::to_string(mPointerCaptureRequest->enable);
Prabir Pradhan99987712020-11-10 18:43:05 -0800323 }
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000324 mPointerCaptureRequest.reset();
Prabir Pradhan99987712020-11-10 18:43:05 -0800325 }
326
arthurhungf452d0b2021-01-06 00:19:52 +0800327 void assertDropTargetEquals(const sp<IBinder>& targetToken) {
328 std::scoped_lock lock(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800329 ASSERT_TRUE(mNotifyDropWindowWasCalled);
arthurhungf452d0b2021-01-06 00:19:52 +0800330 ASSERT_EQ(targetToken, mDropTargetWindowToken);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800331 mNotifyDropWindowWasCalled = false;
arthurhungf452d0b2021-01-06 00:19:52 +0800332 }
333
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800334 void assertNotifyInputChannelBrokenWasCalled(const sp<IBinder>& token) {
335 std::unique_lock lock(mLock);
336 base::ScopedLockAssertion assumeLocked(mLock);
337 std::optional<sp<IBinder>> receivedToken =
338 getItemFromStorageLockedInterruptible(100ms, mBrokenInputChannels, lock,
339 mNotifyInputChannelBroken);
340 ASSERT_TRUE(receivedToken.has_value());
341 ASSERT_EQ(token, *receivedToken);
342 }
343
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800344 /**
345 * Set policy timeout. A value of zero means next key will not be intercepted.
346 */
347 void setInterceptKeyTimeout(std::chrono::milliseconds timeout) {
348 mInterceptKeyTimeout = timeout;
349 }
350
Michael Wrightd02c5b62014-02-10 15:10:22 -0800351private:
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700352 std::mutex mLock;
353 std::unique_ptr<InputEvent> mFilteredEvent GUARDED_BY(mLock);
354 std::optional<nsecs_t> mConfigurationChangedTime GUARDED_BY(mLock);
355 sp<IBinder> mOnPointerDownToken GUARDED_BY(mLock);
356 std::optional<NotifySwitchArgs> mLastNotifySwitch GUARDED_BY(mLock);
Jackal Guof9696682018-10-05 12:23:23 +0800357
Prabir Pradhan99987712020-11-10 18:43:05 -0800358 std::condition_variable mPointerCaptureChangedCondition;
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000359
360 std::optional<PointerCaptureRequest> mPointerCaptureRequest GUARDED_BY(mLock);
Prabir Pradhan99987712020-11-10 18:43:05 -0800361
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700362 // ANR handling
Chris Yea209fde2020-07-22 13:54:51 -0700363 std::queue<std::shared_ptr<InputApplicationHandle>> mAnrApplications GUARDED_BY(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800364 std::queue<AnrResult> mAnrWindows GUARDED_BY(mLock);
365 std::queue<AnrResult> mResponsiveWindows GUARDED_BY(mLock);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700366 std::condition_variable mNotifyAnr;
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800367 std::queue<sp<IBinder>> mBrokenInputChannels GUARDED_BY(mLock);
368 std::condition_variable mNotifyInputChannelBroken;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700369
arthurhungf452d0b2021-01-06 00:19:52 +0800370 sp<IBinder> mDropTargetWindowToken GUARDED_BY(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800371 bool mNotifyDropWindowWasCalled GUARDED_BY(mLock) = false;
arthurhungf452d0b2021-01-06 00:19:52 +0800372
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800373 std::chrono::milliseconds mInterceptKeyTimeout = 0ms;
374
Prabir Pradhanedd96402022-02-15 01:46:16 -0800375 // All three ANR-related callbacks behave the same way, so we use this generic function to wait
376 // for a specific container to become non-empty. When the container is non-empty, return the
377 // first entry from the container and erase it.
378 template <class T>
379 T getAnrTokenLockedInterruptible(std::chrono::nanoseconds timeout, std::queue<T>& storage,
380 std::unique_lock<std::mutex>& lock) REQUIRES(mLock) {
381 // If there is an ANR, Dispatcher won't be idle because there are still events
382 // in the waitQueue that we need to check on. So we can't wait for dispatcher to be idle
383 // before checking if ANR was called.
384 // Since dispatcher is not guaranteed to call notifyNoFocusedWindowAnr right away, we need
385 // to provide it some time to act. 100ms seems reasonable.
386 std::chrono::duration timeToWait = timeout + 100ms; // provide some slack
387 const std::chrono::time_point start = std::chrono::steady_clock::now();
388 std::optional<T> token =
389 getItemFromStorageLockedInterruptible(timeToWait, storage, lock, mNotifyAnr);
390 if (!token.has_value()) {
391 ADD_FAILURE() << "Did not receive the ANR callback";
392 return {};
393 }
394
395 const std::chrono::duration waited = std::chrono::steady_clock::now() - start;
396 // Ensure that the ANR didn't get raised too early. We can't be too strict here because
397 // the dispatcher started counting before this function was called
398 if (std::chrono::abs(timeout - waited) > 100ms) {
399 ADD_FAILURE() << "ANR was raised too early or too late. Expected "
400 << std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count()
401 << "ms, but waited "
402 << std::chrono::duration_cast<std::chrono::milliseconds>(waited).count()
403 << "ms instead";
404 }
405 return *token;
406 }
407
408 template <class T>
409 std::optional<T> getItemFromStorageLockedInterruptible(std::chrono::nanoseconds timeout,
410 std::queue<T>& storage,
411 std::unique_lock<std::mutex>& lock,
412 std::condition_variable& condition)
413 REQUIRES(mLock) {
414 condition.wait_for(lock, timeout,
415 [&storage]() REQUIRES(mLock) { return !storage.empty(); });
416 if (storage.empty()) {
417 ADD_FAILURE() << "Did not receive the expected callback";
418 return std::nullopt;
419 }
420 T item = storage.front();
421 storage.pop();
422 return std::make_optional(item);
423 }
424
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600425 void notifyConfigurationChanged(nsecs_t when) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700426 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800427 mConfigurationChangedTime = when;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800428 }
429
Prabir Pradhanedd96402022-02-15 01:46:16 -0800430 void notifyWindowUnresponsive(const sp<IBinder>& connectionToken, std::optional<int32_t> pid,
431 const std::string&) override {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700432 std::scoped_lock lock(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800433 ASSERT_TRUE(pid.has_value());
434 mAnrWindows.push({connectionToken, *pid});
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700435 mNotifyAnr.notify_all();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500436 }
437
Prabir Pradhanedd96402022-02-15 01:46:16 -0800438 void notifyWindowResponsive(const sp<IBinder>& connectionToken,
439 std::optional<int32_t> pid) override {
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500440 std::scoped_lock lock(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800441 ASSERT_TRUE(pid.has_value());
442 mResponsiveWindows.push({connectionToken, *pid});
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500443 mNotifyAnr.notify_all();
444 }
445
446 void notifyNoFocusedWindowAnr(
447 const std::shared_ptr<InputApplicationHandle>& applicationHandle) override {
448 std::scoped_lock lock(mLock);
449 mAnrApplications.push(applicationHandle);
450 mNotifyAnr.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800451 }
452
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800453 void notifyInputChannelBroken(const sp<IBinder>& connectionToken) override {
454 std::scoped_lock lock(mLock);
455 mBrokenInputChannels.push(connectionToken);
456 mNotifyInputChannelBroken.notify_all();
457 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800458
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600459 void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {}
Robert Carr740167f2018-10-11 19:03:41 -0700460
Chris Yef59a2f42020-10-16 12:55:26 -0700461 void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType,
462 InputDeviceSensorAccuracy accuracy, nsecs_t timestamp,
463 const std::vector<float>& values) override {}
464
465 void notifySensorAccuracy(int deviceId, InputDeviceSensorType sensorType,
466 InputDeviceSensorAccuracy accuracy) override {}
Bernardo Rufino2e1f6512020-10-08 13:42:07 +0000467
Chris Yefb552902021-02-03 17:18:37 -0800468 void notifyVibratorState(int32_t deviceId, bool isOn) override {}
469
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600470 void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800471 *outConfig = mConfig;
472 }
473
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600474 bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700475 std::scoped_lock lock(mLock);
Jackal Guof9696682018-10-05 12:23:23 +0800476 switch (inputEvent->getType()) {
477 case AINPUT_EVENT_TYPE_KEY: {
478 const KeyEvent* keyEvent = static_cast<const KeyEvent*>(inputEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800479 mFilteredEvent = std::make_unique<KeyEvent>(*keyEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800480 break;
481 }
482
483 case AINPUT_EVENT_TYPE_MOTION: {
484 const MotionEvent* motionEvent = static_cast<const MotionEvent*>(inputEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800485 mFilteredEvent = std::make_unique<MotionEvent>(*motionEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800486 break;
487 }
488 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800489 return true;
490 }
491
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800492 void interceptKeyBeforeQueueing(const KeyEvent* inputEvent, uint32_t&) override {
493 if (inputEvent->getAction() == AKEY_EVENT_ACTION_UP) {
494 // Clear intercept state when we handled the event.
495 mInterceptKeyTimeout = 0ms;
496 }
497 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800498
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600499 void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800500
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600501 nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent*, uint32_t) override {
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800502 nsecs_t delay = std::chrono::nanoseconds(mInterceptKeyTimeout).count();
503 // Clear intercept state so we could dispatch the event in next wake.
504 mInterceptKeyTimeout = 0ms;
505 return delay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800506 }
507
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600508 bool dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent*, uint32_t, KeyEvent*) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800509 return false;
510 }
511
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600512 void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask,
513 uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700514 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800515 /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is
516 * essentially a passthrough for notifySwitch.
517 */
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800518 mLastNotifySwitch = NotifySwitchArgs(1 /*id*/, when, policyFlags, switchValues, switchMask);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800519 }
520
Sean Stoutb4e0a592021-02-23 07:34:53 -0800521 void pokeUserActivity(nsecs_t, int32_t, int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800522
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600523 void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700524 std::scoped_lock lock(mLock);
chaviwfd6d3512019-03-25 13:23:49 -0700525 mOnPointerDownToken = newToken;
526 }
527
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000528 void setPointerCapture(const PointerCaptureRequest& request) override {
Prabir Pradhan99987712020-11-10 18:43:05 -0800529 std::scoped_lock lock(mLock);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000530 mPointerCaptureRequest = {request};
Prabir Pradhan99987712020-11-10 18:43:05 -0800531 mPointerCaptureChangedCondition.notify_all();
532 }
533
arthurhungf452d0b2021-01-06 00:19:52 +0800534 void notifyDropWindow(const sp<IBinder>& token, float x, float y) override {
535 std::scoped_lock lock(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800536 mNotifyDropWindowWasCalled = true;
arthurhungf452d0b2021-01-06 00:19:52 +0800537 mDropTargetWindowToken = token;
538 }
539
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700540 void assertFilterInputEventWasCalledInternal(
541 const std::function<void(const InputEvent&)>& verify) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700542 std::scoped_lock lock(mLock);
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -0800543 ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called.";
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700544 verify(*mFilteredEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800545 mFilteredEvent = nullptr;
Jackal Guof9696682018-10-05 12:23:23 +0800546 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800547};
548
Michael Wrightd02c5b62014-02-10 15:10:22 -0800549// --- InputDispatcherTest ---
550
551class InputDispatcherTest : public testing::Test {
552protected:
553 sp<FakeInputDispatcherPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700554 std::unique_ptr<InputDispatcher> mDispatcher;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800555
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000556 void SetUp() override {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700557 mFakePolicy = sp<FakeInputDispatcherPolicy>::make();
Siarhei Vishniakou289e9242022-02-15 14:50:16 -0800558 mDispatcher = std::make_unique<InputDispatcher>(mFakePolicy, STALE_EVENT_TIMEOUT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800559 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000560 // Start InputDispatcher thread
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700561 ASSERT_EQ(OK, mDispatcher->start());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800562 }
563
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000564 void TearDown() override {
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700565 ASSERT_EQ(OK, mDispatcher->stop());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800566 mFakePolicy.clear();
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700567 mDispatcher.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800568 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700569
570 /**
571 * Used for debugging when writing the test
572 */
573 void dumpDispatcherState() {
574 std::string dump;
575 mDispatcher->dump(dump);
576 std::stringstream ss(dump);
577 std::string to;
578
579 while (std::getline(ss, to, '\n')) {
580 ALOGE("%s", to.c_str());
581 }
582 }
Vishnu Nair958da932020-08-21 17:12:37 -0700583
chaviw3277faf2021-05-19 16:45:23 -0500584 void setFocusedWindow(const sp<WindowInfoHandle>& window,
585 const sp<WindowInfoHandle>& focusedWindow = nullptr) {
Vishnu Nair958da932020-08-21 17:12:37 -0700586 FocusRequest request;
587 request.token = window->getToken();
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +0000588 request.windowName = window->getName();
Vishnu Nair958da932020-08-21 17:12:37 -0700589 if (focusedWindow) {
590 request.focusedToken = focusedWindow->getToken();
591 }
592 request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
593 request.displayId = window->getInfo()->displayId;
594 mDispatcher->setFocusedWindow(request);
595 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800596};
597
Michael Wrightd02c5b62014-02-10 15:10:22 -0800598TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) {
599 KeyEvent event;
600
601 // Rejects undefined key actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800602 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
603 INVALID_HMAC,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600604 /*action*/ -1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME,
605 ARBITRARY_TIME);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800606 ASSERT_EQ(InputEventInjectionResult::FAILED,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000607 mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE,
608 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800609 << "Should reject key events with undefined action.";
610
611 // Rejects ACTION_MULTIPLE since it is not supported despite being defined in the API.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800612 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
613 INVALID_HMAC, AKEY_EVENT_ACTION_MULTIPLE, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600614 ARBITRARY_TIME, ARBITRARY_TIME);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800615 ASSERT_EQ(InputEventInjectionResult::FAILED,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000616 mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE,
617 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800618 << "Should reject key events with ACTION_MULTIPLE.";
619}
620
621TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) {
622 MotionEvent event;
623 PointerProperties pointerProperties[MAX_POINTERS + 1];
624 PointerCoords pointerCoords[MAX_POINTERS + 1];
Siarhei Vishniakou01747382022-01-20 13:23:27 -0800625 for (size_t i = 0; i <= MAX_POINTERS; i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800626 pointerProperties[i].clear();
627 pointerProperties[i].id = i;
628 pointerCoords[i].clear();
629 }
630
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800631 // Some constants commonly used below
632 constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN;
633 constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE;
634 constexpr int32_t metaState = AMETA_NONE;
635 constexpr MotionClassification classification = MotionClassification::NONE;
636
chaviw9eaa22c2020-07-01 16:21:27 -0700637 ui::Transform identityTransform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800638 // Rejects undefined motion actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800639 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
chaviw9eaa22c2020-07-01 16:21:27 -0700640 /*action*/ -1, 0, 0, edgeFlags, metaState, 0, classification,
641 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700642 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
643 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700644 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800645 ASSERT_EQ(InputEventInjectionResult::FAILED,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000646 mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE,
647 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800648 << "Should reject motion events with undefined action.";
649
650 // Rejects pointer down with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800651 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -0800652 POINTER_1_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
653 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
654 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
655 ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500656 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800657 ASSERT_EQ(InputEventInjectionResult::FAILED,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000658 mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE,
659 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800660 << "Should reject motion events with pointer down index too large.";
661
Garfield Tanfbe732e2020-01-24 11:26:14 -0800662 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700663 AMOTION_EVENT_ACTION_POINTER_DOWN |
664 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700665 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
666 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700667 identityTransform, ARBITRARY_TIME, ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500668 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800669 ASSERT_EQ(InputEventInjectionResult::FAILED,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000670 mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE,
671 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800672 << "Should reject motion events with pointer down index too small.";
673
674 // Rejects pointer up with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800675 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -0800676 POINTER_1_UP, 0, 0, edgeFlags, metaState, 0, classification, identityTransform,
677 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
678 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
679 ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500680 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800681 ASSERT_EQ(InputEventInjectionResult::FAILED,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000682 mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE,
683 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800684 << "Should reject motion events with pointer up index too large.";
685
Garfield Tanfbe732e2020-01-24 11:26:14 -0800686 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700687 AMOTION_EVENT_ACTION_POINTER_UP |
688 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700689 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
690 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700691 identityTransform, ARBITRARY_TIME, ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500692 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800693 ASSERT_EQ(InputEventInjectionResult::FAILED,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000694 mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE,
695 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800696 << "Should reject motion events with pointer up index too small.";
697
698 // Rejects motion events with invalid number of pointers.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800699 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
700 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700701 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700702 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
703 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700704 /*pointerCount*/ 0, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800705 ASSERT_EQ(InputEventInjectionResult::FAILED,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000706 mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE,
707 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800708 << "Should reject motion events with 0 pointers.";
709
Garfield Tanfbe732e2020-01-24 11:26:14 -0800710 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
711 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700712 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700713 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
714 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700715 /*pointerCount*/ MAX_POINTERS + 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800716 ASSERT_EQ(InputEventInjectionResult::FAILED,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000717 mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE,
718 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800719 << "Should reject motion events with more than MAX_POINTERS pointers.";
720
721 // Rejects motion events with invalid pointer ids.
722 pointerProperties[0].id = -1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800723 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
724 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700725 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700726 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
727 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700728 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800729 ASSERT_EQ(InputEventInjectionResult::FAILED,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000730 mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE,
731 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800732 << "Should reject motion events with pointer ids less than 0.";
733
734 pointerProperties[0].id = MAX_POINTER_ID + 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800735 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
736 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700737 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700738 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
739 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700740 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800741 ASSERT_EQ(InputEventInjectionResult::FAILED,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000742 mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE,
743 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800744 << "Should reject motion events with pointer ids greater than MAX_POINTER_ID.";
745
746 // Rejects motion events with duplicate pointer ids.
747 pointerProperties[0].id = 1;
748 pointerProperties[1].id = 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800749 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
750 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700751 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700752 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
753 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700754 /*pointerCount*/ 2, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800755 ASSERT_EQ(InputEventInjectionResult::FAILED,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000756 mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE,
757 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800758 << "Should reject motion events with duplicate pointer ids.";
759}
760
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800761/* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */
762
763TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) {
764 constexpr nsecs_t eventTime = 20;
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800765 NotifyConfigurationChangedArgs args(10 /*id*/, eventTime);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800766 mDispatcher->notifyConfigurationChanged(&args);
767 ASSERT_TRUE(mDispatcher->waitForIdle());
768
769 mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime);
770}
771
772TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) {
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800773 NotifySwitchArgs args(10 /*id*/, 20 /*eventTime*/, 0 /*policyFlags*/, 1 /*switchValues*/,
774 2 /*switchMask*/);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800775 mDispatcher->notifySwitch(&args);
776
777 // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener
778 args.policyFlags |= POLICY_FLAG_TRUSTED;
779 mFakePolicy->assertNotifySwitchWasCalled(args);
780}
781
Arthur Hungb92218b2018-08-14 12:00:21 +0800782// --- InputDispatcherTest SetInputWindowTest ---
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700783static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms;
Siarhei Vishniakou1c494c52021-08-11 20:25:01 -0700784// Default input dispatching timeout if there is no focused application or paused window
785// from which to determine an appropriate dispatching timeout.
786static const std::chrono::duration DISPATCHING_TIMEOUT = std::chrono::milliseconds(
787 android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS *
788 android::base::HwTimeoutMultiplier());
Arthur Hungb92218b2018-08-14 12:00:21 +0800789
790class FakeApplicationHandle : public InputApplicationHandle {
791public:
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700792 FakeApplicationHandle() {
793 mInfo.name = "Fake Application";
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700794 mInfo.token = sp<BBinder>::make();
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500795 mInfo.dispatchingTimeoutMillis =
796 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700797 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800798 virtual ~FakeApplicationHandle() {}
799
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000800 virtual bool updateInfo() override { return true; }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700801
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500802 void setDispatchingTimeout(std::chrono::milliseconds timeout) {
803 mInfo.dispatchingTimeoutMillis = timeout.count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700804 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800805};
806
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800807class FakeInputReceiver {
Arthur Hungb92218b2018-08-14 12:00:21 +0800808public:
Garfield Tan15601662020-09-22 15:32:38 -0700809 explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name)
chaviwd1c23182019-12-20 18:44:56 -0800810 : mName(name) {
Garfield Tan15601662020-09-22 15:32:38 -0700811 mConsumer = std::make_unique<InputConsumer>(std::move(clientChannel));
chaviwd1c23182019-12-20 18:44:56 -0800812 }
813
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800814 InputEvent* consume() {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700815 InputEvent* event;
816 std::optional<uint32_t> consumeSeq = receiveEvent(&event);
817 if (!consumeSeq) {
818 return nullptr;
819 }
820 finishEvent(*consumeSeq);
821 return event;
822 }
823
824 /**
825 * Receive an event without acknowledging it.
826 * Return the sequence number that could later be used to send finished signal.
827 */
828 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Arthur Hungb92218b2018-08-14 12:00:21 +0800829 uint32_t consumeSeq;
830 InputEvent* event;
Arthur Hungb92218b2018-08-14 12:00:21 +0800831
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800832 std::chrono::time_point start = std::chrono::steady_clock::now();
833 status_t status = WOULD_BLOCK;
834 while (status == WOULD_BLOCK) {
chaviw81e2bb92019-12-18 15:03:51 -0800835 status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq,
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800836 &event);
837 std::chrono::duration elapsed = std::chrono::steady_clock::now() - start;
838 if (elapsed > 100ms) {
839 break;
840 }
841 }
842
843 if (status == WOULD_BLOCK) {
844 // Just means there's no event available.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700845 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800846 }
847
848 if (status != OK) {
849 ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK.";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700850 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800851 }
852 if (event == nullptr) {
853 ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700854 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800855 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700856 if (outEvent != nullptr) {
857 *outEvent = event;
858 }
859 return consumeSeq;
860 }
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800861
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700862 /**
863 * To be used together with "receiveEvent" to complete the consumption of an event.
864 */
865 void finishEvent(uint32_t consumeSeq) {
866 const status_t status = mConsumer->sendFinishedSignal(consumeSeq, true);
867 ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK.";
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800868 }
869
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000870 void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) {
871 const status_t status = mConsumer->sendTimeline(inputEventId, timeline);
872 ASSERT_EQ(OK, status);
873 }
874
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000875 void consumeEvent(int32_t expectedEventType, int32_t expectedAction,
876 std::optional<int32_t> expectedDisplayId,
877 std::optional<int32_t> expectedFlags) {
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800878 InputEvent* event = consume();
879
880 ASSERT_NE(nullptr, event) << mName.c_str()
881 << ": consumer should have returned non-NULL event.";
Arthur Hungb92218b2018-08-14 12:00:21 +0800882 ASSERT_EQ(expectedEventType, event->getType())
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700883 << mName.c_str() << " expected " << inputEventTypeToString(expectedEventType)
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800884 << " event, got " << inputEventTypeToString(event->getType()) << " event";
Arthur Hungb92218b2018-08-14 12:00:21 +0800885
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000886 if (expectedDisplayId.has_value()) {
887 EXPECT_EQ(expectedDisplayId, event->getDisplayId());
888 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800889
Tiger Huang8664f8c2018-10-11 19:14:35 +0800890 switch (expectedEventType) {
891 case AINPUT_EVENT_TYPE_KEY: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800892 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event);
893 EXPECT_EQ(expectedAction, keyEvent.getAction());
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000894 if (expectedFlags.has_value()) {
895 EXPECT_EQ(expectedFlags.value(), keyEvent.getFlags());
896 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800897 break;
898 }
899 case AINPUT_EVENT_TYPE_MOTION: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800900 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000901 assertMotionAction(expectedAction, motionEvent.getAction());
902
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000903 if (expectedFlags.has_value()) {
904 EXPECT_EQ(expectedFlags.value(), motionEvent.getFlags());
905 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800906 break;
907 }
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100908 case AINPUT_EVENT_TYPE_FOCUS: {
909 FAIL() << "Use 'consumeFocusEvent' for FOCUS events";
910 }
Prabir Pradhan99987712020-11-10 18:43:05 -0800911 case AINPUT_EVENT_TYPE_CAPTURE: {
912 FAIL() << "Use 'consumeCaptureEvent' for CAPTURE events";
913 }
Antonio Kantekf16f2832021-09-28 04:39:20 +0000914 case AINPUT_EVENT_TYPE_TOUCH_MODE: {
915 FAIL() << "Use 'consumeTouchModeEvent' for TOUCH_MODE events";
916 }
arthurhungb89ccb02020-12-30 16:19:01 +0800917 case AINPUT_EVENT_TYPE_DRAG: {
918 FAIL() << "Use 'consumeDragEvent' for DRAG events";
919 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800920 default: {
921 FAIL() << mName.c_str() << ": invalid event type: " << expectedEventType;
922 }
923 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800924 }
925
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100926 void consumeFocusEvent(bool hasFocus, bool inTouchMode) {
927 InputEvent* event = consume();
928 ASSERT_NE(nullptr, event) << mName.c_str()
929 << ": consumer should have returned non-NULL event.";
930 ASSERT_EQ(AINPUT_EVENT_TYPE_FOCUS, event->getType())
931 << "Got " << inputEventTypeToString(event->getType())
932 << " event instead of FOCUS event";
933
934 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
935 << mName.c_str() << ": event displayId should always be NONE.";
936
937 FocusEvent* focusEvent = static_cast<FocusEvent*>(event);
938 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100939 }
940
Prabir Pradhan99987712020-11-10 18:43:05 -0800941 void consumeCaptureEvent(bool hasCapture) {
942 const InputEvent* event = consume();
943 ASSERT_NE(nullptr, event) << mName.c_str()
944 << ": consumer should have returned non-NULL event.";
945 ASSERT_EQ(AINPUT_EVENT_TYPE_CAPTURE, event->getType())
946 << "Got " << inputEventTypeToString(event->getType())
947 << " event instead of CAPTURE event";
948
949 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
950 << mName.c_str() << ": event displayId should always be NONE.";
951
952 const auto& captureEvent = static_cast<const CaptureEvent&>(*event);
953 EXPECT_EQ(hasCapture, captureEvent.getPointerCaptureEnabled());
954 }
955
arthurhungb89ccb02020-12-30 16:19:01 +0800956 void consumeDragEvent(bool isExiting, float x, float y) {
957 const InputEvent* event = consume();
958 ASSERT_NE(nullptr, event) << mName.c_str()
959 << ": consumer should have returned non-NULL event.";
960 ASSERT_EQ(AINPUT_EVENT_TYPE_DRAG, event->getType())
961 << "Got " << inputEventTypeToString(event->getType())
962 << " event instead of DRAG event";
963
964 EXPECT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
965 << mName.c_str() << ": event displayId should always be NONE.";
966
967 const auto& dragEvent = static_cast<const DragEvent&>(*event);
968 EXPECT_EQ(isExiting, dragEvent.isExiting());
969 EXPECT_EQ(x, dragEvent.getX());
970 EXPECT_EQ(y, dragEvent.getY());
971 }
972
Antonio Kantekf16f2832021-09-28 04:39:20 +0000973 void consumeTouchModeEvent(bool inTouchMode) {
974 const InputEvent* event = consume();
975 ASSERT_NE(nullptr, event) << mName.c_str()
976 << ": consumer should have returned non-NULL event.";
977 ASSERT_EQ(AINPUT_EVENT_TYPE_TOUCH_MODE, event->getType())
978 << "Got " << inputEventTypeToString(event->getType())
979 << " event instead of TOUCH_MODE event";
980
981 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
982 << mName.c_str() << ": event displayId should always be NONE.";
983 const auto& touchModeEvent = static_cast<const TouchModeEvent&>(*event);
984 EXPECT_EQ(inTouchMode, touchModeEvent.isInTouchMode());
985 }
986
chaviwd1c23182019-12-20 18:44:56 -0800987 void assertNoEvents() {
988 InputEvent* event = consume();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700989 if (event == nullptr) {
990 return;
991 }
992 if (event->getType() == AINPUT_EVENT_TYPE_KEY) {
993 KeyEvent& keyEvent = static_cast<KeyEvent&>(*event);
994 ADD_FAILURE() << "Received key event "
995 << KeyEvent::actionToString(keyEvent.getAction());
996 } else if (event->getType() == AINPUT_EVENT_TYPE_MOTION) {
997 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
998 ADD_FAILURE() << "Received motion event "
999 << MotionEvent::actionToString(motionEvent.getAction());
1000 } else if (event->getType() == AINPUT_EVENT_TYPE_FOCUS) {
1001 FocusEvent& focusEvent = static_cast<FocusEvent&>(*event);
1002 ADD_FAILURE() << "Received focus event, hasFocus = "
1003 << (focusEvent.getHasFocus() ? "true" : "false");
Prabir Pradhan99987712020-11-10 18:43:05 -08001004 } else if (event->getType() == AINPUT_EVENT_TYPE_CAPTURE) {
1005 const auto& captureEvent = static_cast<CaptureEvent&>(*event);
1006 ADD_FAILURE() << "Received capture event, pointerCaptureEnabled = "
1007 << (captureEvent.getPointerCaptureEnabled() ? "true" : "false");
Antonio Kantekf16f2832021-09-28 04:39:20 +00001008 } else if (event->getType() == AINPUT_EVENT_TYPE_TOUCH_MODE) {
1009 const auto& touchModeEvent = static_cast<TouchModeEvent&>(*event);
1010 ADD_FAILURE() << "Received touch mode event, inTouchMode = "
1011 << (touchModeEvent.isInTouchMode() ? "true" : "false");
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001012 }
1013 FAIL() << mName.c_str()
1014 << ": should not have received any events, so consume() should return NULL";
chaviwd1c23182019-12-20 18:44:56 -08001015 }
1016
1017 sp<IBinder> getToken() { return mConsumer->getChannel()->getConnectionToken(); }
1018
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001019 int getChannelFd() { return mConsumer->getChannel()->getFd().get(); }
1020
chaviwd1c23182019-12-20 18:44:56 -08001021protected:
1022 std::unique_ptr<InputConsumer> mConsumer;
1023 PreallocatedInputEventFactory mEventFactory;
1024
1025 std::string mName;
1026};
1027
chaviw3277faf2021-05-19 16:45:23 -05001028class FakeWindowHandle : public WindowInfoHandle {
chaviwd1c23182019-12-20 18:44:56 -08001029public:
1030 static const int32_t WIDTH = 600;
1031 static const int32_t HEIGHT = 800;
chaviwd1c23182019-12-20 18:44:56 -08001032
Chris Yea209fde2020-07-22 13:54:51 -07001033 FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001034 const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name,
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001035 int32_t displayId, std::optional<sp<IBinder>> token = std::nullopt)
chaviwd1c23182019-12-20 18:44:56 -08001036 : mName(name) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001037 if (token == std::nullopt) {
Garfield Tan15601662020-09-22 15:32:38 -07001038 base::Result<std::unique_ptr<InputChannel>> channel =
1039 dispatcher->createInputChannel(name);
1040 token = (*channel)->getConnectionToken();
1041 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
chaviwd1c23182019-12-20 18:44:56 -08001042 }
1043
1044 inputApplicationHandle->updateInfo();
1045 mInfo.applicationInfo = *inputApplicationHandle->getInfo();
1046
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001047 mInfo.token = *token;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001048 mInfo.id = sId++;
chaviwd1c23182019-12-20 18:44:56 -08001049 mInfo.name = name;
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -05001050 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001051 mInfo.alpha = 1.0;
chaviwd1c23182019-12-20 18:44:56 -08001052 mInfo.frameLeft = 0;
1053 mInfo.frameTop = 0;
1054 mInfo.frameRight = WIDTH;
1055 mInfo.frameBottom = HEIGHT;
chaviw1ff3d1e2020-07-01 15:53:47 -07001056 mInfo.transform.set(0, 0);
chaviwd1c23182019-12-20 18:44:56 -08001057 mInfo.globalScaleFactor = 1.0;
1058 mInfo.touchableRegion.clear();
1059 mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT));
Prabir Pradhan5735a322022-04-11 17:23:34 +00001060 mInfo.ownerPid = WINDOW_PID;
1061 mInfo.ownerUid = WINDOW_UID;
chaviwd1c23182019-12-20 18:44:56 -08001062 mInfo.displayId = displayId;
Prabir Pradhan51e7db02022-02-07 06:02:57 -08001063 mInfo.inputConfig = WindowInfo::InputConfig::DEFAULT;
chaviwd1c23182019-12-20 18:44:56 -08001064 }
1065
Arthur Hungabbb9d82021-09-01 14:52:30 +00001066 sp<FakeWindowHandle> clone(
1067 const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001068 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t displayId) {
Arthur Hungabbb9d82021-09-01 14:52:30 +00001069 sp<FakeWindowHandle> handle =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001070 sp<FakeWindowHandle>::make(inputApplicationHandle, dispatcher,
1071 mInfo.name + "(Mirror)", displayId, mInfo.token);
Arthur Hungabbb9d82021-09-01 14:52:30 +00001072 return handle;
1073 }
1074
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001075 void setTouchable(bool touchable) {
1076 mInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, !touchable);
1077 }
chaviwd1c23182019-12-20 18:44:56 -08001078
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001079 void setFocusable(bool focusable) {
1080 mInfo.setInputConfig(WindowInfo::InputConfig::NOT_FOCUSABLE, !focusable);
1081 }
1082
1083 void setVisible(bool visible) {
1084 mInfo.setInputConfig(WindowInfo::InputConfig::NOT_VISIBLE, !visible);
1085 }
Vishnu Nair958da932020-08-21 17:12:37 -07001086
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001087 void setDispatchingTimeout(std::chrono::nanoseconds timeout) {
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -05001088 mInfo.dispatchingTimeout = timeout;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001089 }
1090
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001091 void setPaused(bool paused) {
1092 mInfo.setInputConfig(WindowInfo::InputConfig::PAUSE_DISPATCHING, paused);
1093 }
1094
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08001095 void setPreventSplitting(bool preventSplitting) {
1096 mInfo.setInputConfig(WindowInfo::InputConfig::PREVENT_SPLITTING, preventSplitting);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001097 }
1098
1099 void setSlippery(bool slippery) {
1100 mInfo.setInputConfig(WindowInfo::InputConfig::SLIPPERY, slippery);
1101 }
1102
1103 void setWatchOutsideTouch(bool watchOutside) {
1104 mInfo.setInputConfig(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH, watchOutside);
1105 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001106
Prabir Pradhan51e7db02022-02-07 06:02:57 -08001107 void setSpy(bool spy) { mInfo.setInputConfig(WindowInfo::InputConfig::SPY, spy); }
1108
1109 void setInterceptsStylus(bool interceptsStylus) {
1110 mInfo.setInputConfig(WindowInfo::InputConfig::INTERCEPTS_STYLUS, interceptsStylus);
1111 }
1112
1113 void setDropInput(bool dropInput) {
1114 mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT, dropInput);
1115 }
1116
1117 void setDropInputIfObscured(bool dropInputIfObscured) {
1118 mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED, dropInputIfObscured);
1119 }
1120
1121 void setNoInputChannel(bool noInputChannel) {
1122 mInfo.setInputConfig(WindowInfo::InputConfig::NO_INPUT_CHANNEL, noInputChannel);
1123 }
1124
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001125 void setAlpha(float alpha) { mInfo.alpha = alpha; }
1126
chaviw3277faf2021-05-19 16:45:23 -05001127 void setTouchOcclusionMode(TouchOcclusionMode mode) { mInfo.touchOcclusionMode = mode; }
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001128
Bernardo Rufino7393d172021-02-26 13:56:11 +00001129 void setApplicationToken(sp<IBinder> token) { mInfo.applicationInfo.token = token; }
1130
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001131 void setFrame(const Rect& frame, const ui::Transform& displayTransform = ui::Transform()) {
chaviwd1c23182019-12-20 18:44:56 -08001132 mInfo.frameLeft = frame.left;
1133 mInfo.frameTop = frame.top;
1134 mInfo.frameRight = frame.right;
1135 mInfo.frameBottom = frame.bottom;
1136 mInfo.touchableRegion.clear();
1137 mInfo.addTouchableRegion(frame);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001138
1139 const Rect logicalDisplayFrame = displayTransform.transform(frame);
1140 ui::Transform translate;
1141 translate.set(-logicalDisplayFrame.left, -logicalDisplayFrame.top);
1142 mInfo.transform = translate * displayTransform;
chaviwd1c23182019-12-20 18:44:56 -08001143 }
1144
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001145 void setTouchableRegion(const Region& region) { mInfo.touchableRegion = region; }
1146
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001147 void setIsWallpaper(bool isWallpaper) {
1148 mInfo.setInputConfig(WindowInfo::InputConfig::IS_WALLPAPER, isWallpaper);
1149 }
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001150
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001151 void setDupTouchToWallpaper(bool hasWallpaper) {
1152 mInfo.setInputConfig(WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER, hasWallpaper);
1153 }
chaviwd1c23182019-12-20 18:44:56 -08001154
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001155 void setTrustedOverlay(bool trustedOverlay) {
1156 mInfo.setInputConfig(WindowInfo::InputConfig::TRUSTED_OVERLAY, trustedOverlay);
1157 }
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001158
chaviw9eaa22c2020-07-01 16:21:27 -07001159 void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) {
1160 mInfo.transform.set(dsdx, dtdx, dtdy, dsdy);
1161 }
1162
1163 void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); }
chaviwaf87b3e2019-10-01 16:59:28 -07001164
yunho.shinf4a80b82020-11-16 21:13:57 +09001165 void setWindowOffset(float offsetX, float offsetY) { mInfo.transform.set(offsetX, offsetY); }
1166
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001167 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
1168 consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId,
1169 expectedFlags);
1170 }
1171
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001172 void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
1173 consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, expectedDisplayId, expectedFlags);
1174 }
1175
Svet Ganov5d3bc372020-01-26 23:11:07 -08001176 void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001177 int32_t expectedFlags = 0) {
Svet Ganov5d3bc372020-01-26 23:11:07 -08001178 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, expectedDisplayId,
1179 expectedFlags);
1180 }
1181
1182 void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001183 int32_t expectedFlags = 0) {
Svet Ganov5d3bc372020-01-26 23:11:07 -08001184 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, expectedDisplayId,
1185 expectedFlags);
1186 }
1187
1188 void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001189 int32_t expectedFlags = 0) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001190 consumeAnyMotionDown(expectedDisplayId, expectedFlags);
1191 }
1192
1193 void consumeAnyMotionDown(std::optional<int32_t> expectedDisplayId = std::nullopt,
1194 std::optional<int32_t> expectedFlags = std::nullopt) {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001195 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId,
1196 expectedFlags);
1197 }
1198
Svet Ganov5d3bc372020-01-26 23:11:07 -08001199 void consumeMotionPointerDown(int32_t pointerIdx,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001200 int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1201 int32_t expectedFlags = 0) {
1202 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
1203 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001204 consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags);
1205 }
1206
1207 void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001208 int32_t expectedFlags = 0) {
1209 int32_t action = AMOTION_EVENT_ACTION_POINTER_UP |
1210 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001211 consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags);
1212 }
1213
1214 void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001215 int32_t expectedFlags = 0) {
Michael Wright3a240c42019-12-10 20:53:41 +00001216 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId,
1217 expectedFlags);
1218 }
1219
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05001220 void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1221 int32_t expectedFlags = 0) {
1222 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, expectedDisplayId,
1223 expectedFlags);
1224 }
1225
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08001226 void consumeMotionOutsideWithZeroedCoords(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1227 int32_t expectedFlags = 0) {
1228 InputEvent* event = consume();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08001229 ASSERT_NE(nullptr, event);
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08001230 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType());
1231 const MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
1232 EXPECT_EQ(AMOTION_EVENT_ACTION_OUTSIDE, motionEvent.getActionMasked());
1233 EXPECT_EQ(0.f, motionEvent.getRawPointerCoords(0)->getX());
1234 EXPECT_EQ(0.f, motionEvent.getRawPointerCoords(0)->getY());
1235 }
1236
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001237 void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) {
1238 ASSERT_NE(mInputReceiver, nullptr)
1239 << "Cannot consume events from a window with no receiver";
1240 mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode);
1241 }
1242
Prabir Pradhan99987712020-11-10 18:43:05 -08001243 void consumeCaptureEvent(bool hasCapture) {
1244 ASSERT_NE(mInputReceiver, nullptr)
1245 << "Cannot consume events from a window with no receiver";
1246 mInputReceiver->consumeCaptureEvent(hasCapture);
1247 }
1248
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08001249 void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) {
1250 MotionEvent* motionEvent = consumeMotion();
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08001251 ASSERT_NE(nullptr, motionEvent) << "Did not get a motion event, but expected " << matcher;
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08001252 ASSERT_THAT(*motionEvent, matcher);
1253 }
1254
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001255 void consumeEvent(int32_t expectedEventType, int32_t expectedAction,
1256 std::optional<int32_t> expectedDisplayId,
1257 std::optional<int32_t> expectedFlags) {
chaviwd1c23182019-12-20 18:44:56 -08001258 ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver";
1259 mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId,
1260 expectedFlags);
1261 }
1262
arthurhungb89ccb02020-12-30 16:19:01 +08001263 void consumeDragEvent(bool isExiting, float x, float y) {
1264 mInputReceiver->consumeDragEvent(isExiting, x, y);
1265 }
1266
Antonio Kantekf16f2832021-09-28 04:39:20 +00001267 void consumeTouchModeEvent(bool inTouchMode) {
1268 ASSERT_NE(mInputReceiver, nullptr)
1269 << "Cannot consume events from a window with no receiver";
1270 mInputReceiver->consumeTouchModeEvent(inTouchMode);
1271 }
1272
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001273 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001274 if (mInputReceiver == nullptr) {
1275 ADD_FAILURE() << "Invalid receive event on window with no receiver";
1276 return std::nullopt;
1277 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001278 return mInputReceiver->receiveEvent(outEvent);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001279 }
1280
1281 void finishEvent(uint32_t sequenceNum) {
1282 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
1283 mInputReceiver->finishEvent(sequenceNum);
1284 }
1285
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00001286 void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) {
1287 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
1288 mInputReceiver->sendTimeline(inputEventId, timeline);
1289 }
1290
chaviwaf87b3e2019-10-01 16:59:28 -07001291 InputEvent* consume() {
1292 if (mInputReceiver == nullptr) {
1293 return nullptr;
1294 }
1295 return mInputReceiver->consume();
1296 }
1297
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00001298 MotionEvent* consumeMotion() {
1299 InputEvent* event = consume();
1300 if (event == nullptr) {
1301 ADD_FAILURE() << "Consume failed : no event";
1302 return nullptr;
1303 }
1304 if (event->getType() != AINPUT_EVENT_TYPE_MOTION) {
1305 ADD_FAILURE() << "Instead of motion event, got "
1306 << inputEventTypeToString(event->getType());
1307 return nullptr;
1308 }
1309 return static_cast<MotionEvent*>(event);
1310 }
1311
Arthur Hungb92218b2018-08-14 12:00:21 +08001312 void assertNoEvents() {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001313 if (mInputReceiver == nullptr &&
Prabir Pradhan51e7db02022-02-07 06:02:57 -08001314 mInfo.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001315 return; // Can't receive events if the window does not have input channel
1316 }
1317 ASSERT_NE(nullptr, mInputReceiver)
1318 << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL";
chaviwd1c23182019-12-20 18:44:56 -08001319 mInputReceiver->assertNoEvents();
Arthur Hungb92218b2018-08-14 12:00:21 +08001320 }
1321
chaviwaf87b3e2019-10-01 16:59:28 -07001322 sp<IBinder> getToken() { return mInfo.token; }
1323
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001324 const std::string& getName() { return mName; }
1325
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001326 void setOwnerInfo(int32_t ownerPid, int32_t ownerUid) {
1327 mInfo.ownerPid = ownerPid;
1328 mInfo.ownerUid = ownerUid;
1329 }
1330
Prabir Pradhanedd96402022-02-15 01:46:16 -08001331 int32_t getPid() const { return mInfo.ownerPid; }
1332
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001333 void destroyReceiver() { mInputReceiver = nullptr; }
1334
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001335 int getChannelFd() { return mInputReceiver->getChannelFd(); }
1336
chaviwd1c23182019-12-20 18:44:56 -08001337private:
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001338 const std::string mName;
chaviwd1c23182019-12-20 18:44:56 -08001339 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001340 static std::atomic<int32_t> sId; // each window gets a unique id, like in surfaceflinger
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001341};
1342
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001343std::atomic<int32_t> FakeWindowHandle::sId{1};
1344
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001345static InputEventInjectionResult injectKey(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001346 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t repeatCount,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001347 int32_t displayId = ADISPLAY_ID_NONE,
1348 InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001349 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Prabir Pradhan5735a322022-04-11 17:23:34 +00001350 bool allowKeyRepeat = true, std::optional<int32_t> targetUid = {},
1351 uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
Arthur Hungb92218b2018-08-14 12:00:21 +08001352 KeyEvent event;
1353 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1354
1355 // Define a valid key down event.
Garfield Tanfbe732e2020-01-24 11:26:14 -08001356 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001357 INVALID_HMAC, action, /* flags */ 0, AKEYCODE_A, KEY_A, AMETA_NONE,
1358 repeatCount, currentTime, currentTime);
Arthur Hungb92218b2018-08-14 12:00:21 +08001359
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001360 if (!allowKeyRepeat) {
1361 policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT;
1362 }
Arthur Hungb92218b2018-08-14 12:00:21 +08001363 // Inject event until dispatch out.
Prabir Pradhan5735a322022-04-11 17:23:34 +00001364 return dispatcher->injectInputEvent(&event, targetUid, syncMode, injectionTimeout, policyFlags);
Arthur Hungb92218b2018-08-14 12:00:21 +08001365}
1366
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001367static InputEventInjectionResult injectKeyDown(const std::unique_ptr<InputDispatcher>& dispatcher,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001368 int32_t displayId = ADISPLAY_ID_NONE) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001369 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /* repeatCount */ 0, displayId);
1370}
1371
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001372// Inject a down event that has key repeat disabled. This allows InputDispatcher to idle without
1373// sending a subsequent key up. When key repeat is enabled, the dispatcher cannot idle because it
1374// has to be woken up to process the repeating key.
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001375static InputEventInjectionResult injectKeyDownNoRepeat(
1376 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t displayId = ADISPLAY_ID_NONE) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001377 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /* repeatCount */ 0, displayId,
1378 InputEventInjectionSync::WAIT_FOR_RESULT, INJECT_EVENT_TIMEOUT,
1379 /* allowKeyRepeat */ false);
1380}
1381
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001382static InputEventInjectionResult injectKeyUp(const std::unique_ptr<InputDispatcher>& dispatcher,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001383 int32_t displayId = ADISPLAY_ID_NONE) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001384 return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /* repeatCount */ 0, displayId);
1385}
1386
Garfield Tandf26e862020-07-01 20:18:19 -07001387class PointerBuilder {
1388public:
1389 PointerBuilder(int32_t id, int32_t toolType) {
1390 mProperties.clear();
1391 mProperties.id = id;
1392 mProperties.toolType = toolType;
1393 mCoords.clear();
1394 }
1395
1396 PointerBuilder& x(float x) { return axis(AMOTION_EVENT_AXIS_X, x); }
1397
1398 PointerBuilder& y(float y) { return axis(AMOTION_EVENT_AXIS_Y, y); }
1399
1400 PointerBuilder& axis(int32_t axis, float value) {
1401 mCoords.setAxisValue(axis, value);
1402 return *this;
1403 }
1404
1405 PointerProperties buildProperties() const { return mProperties; }
1406
1407 PointerCoords buildCoords() const { return mCoords; }
1408
1409private:
1410 PointerProperties mProperties;
1411 PointerCoords mCoords;
1412};
1413
1414class MotionEventBuilder {
1415public:
1416 MotionEventBuilder(int32_t action, int32_t source) {
1417 mAction = action;
1418 mSource = source;
1419 mEventTime = systemTime(SYSTEM_TIME_MONOTONIC);
1420 }
1421
1422 MotionEventBuilder& eventTime(nsecs_t eventTime) {
1423 mEventTime = eventTime;
1424 return *this;
1425 }
1426
1427 MotionEventBuilder& displayId(int32_t displayId) {
1428 mDisplayId = displayId;
1429 return *this;
1430 }
1431
1432 MotionEventBuilder& actionButton(int32_t actionButton) {
1433 mActionButton = actionButton;
1434 return *this;
1435 }
1436
arthurhung6d4bed92021-03-17 11:59:33 +08001437 MotionEventBuilder& buttonState(int32_t buttonState) {
1438 mButtonState = buttonState;
Garfield Tandf26e862020-07-01 20:18:19 -07001439 return *this;
1440 }
1441
1442 MotionEventBuilder& rawXCursorPosition(float rawXCursorPosition) {
1443 mRawXCursorPosition = rawXCursorPosition;
1444 return *this;
1445 }
1446
1447 MotionEventBuilder& rawYCursorPosition(float rawYCursorPosition) {
1448 mRawYCursorPosition = rawYCursorPosition;
1449 return *this;
1450 }
1451
1452 MotionEventBuilder& pointer(PointerBuilder pointer) {
1453 mPointers.push_back(pointer);
1454 return *this;
1455 }
1456
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08001457 MotionEventBuilder& addFlag(uint32_t flags) {
1458 mFlags |= flags;
1459 return *this;
1460 }
1461
Garfield Tandf26e862020-07-01 20:18:19 -07001462 MotionEvent build() {
1463 std::vector<PointerProperties> pointerProperties;
1464 std::vector<PointerCoords> pointerCoords;
1465 for (const PointerBuilder& pointer : mPointers) {
1466 pointerProperties.push_back(pointer.buildProperties());
1467 pointerCoords.push_back(pointer.buildCoords());
1468 }
1469
1470 // Set mouse cursor position for the most common cases to avoid boilerplate.
1471 if (mSource == AINPUT_SOURCE_MOUSE &&
1472 !MotionEvent::isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition) &&
1473 mPointers.size() == 1) {
1474 mRawXCursorPosition = pointerCoords[0].getX();
1475 mRawYCursorPosition = pointerCoords[0].getY();
1476 }
1477
1478 MotionEvent event;
chaviw9eaa22c2020-07-01 16:21:27 -07001479 ui::Transform identityTransform;
Garfield Tandf26e862020-07-01 20:18:19 -07001480 event.initialize(InputEvent::nextId(), DEVICE_ID, mSource, mDisplayId, INVALID_HMAC,
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08001481 mAction, mActionButton, mFlags, /* edgeFlags */ 0, AMETA_NONE,
chaviw9eaa22c2020-07-01 16:21:27 -07001482 mButtonState, MotionClassification::NONE, identityTransform,
1483 /* xPrecision */ 0, /* yPrecision */ 0, mRawXCursorPosition,
Prabir Pradhanb9b18502021-08-26 12:30:32 -07001484 mRawYCursorPosition, identityTransform, mEventTime, mEventTime,
1485 mPointers.size(), pointerProperties.data(), pointerCoords.data());
Garfield Tandf26e862020-07-01 20:18:19 -07001486
1487 return event;
1488 }
1489
1490private:
1491 int32_t mAction;
1492 int32_t mSource;
1493 nsecs_t mEventTime;
1494 int32_t mDisplayId{ADISPLAY_ID_DEFAULT};
1495 int32_t mActionButton{0};
1496 int32_t mButtonState{0};
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08001497 int32_t mFlags{0};
Garfield Tandf26e862020-07-01 20:18:19 -07001498 float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
1499 float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
1500
1501 std::vector<PointerBuilder> mPointers;
1502};
1503
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001504static InputEventInjectionResult injectMotionEvent(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001505 const std::unique_ptr<InputDispatcher>& dispatcher, const MotionEvent& event,
Garfield Tandf26e862020-07-01 20:18:19 -07001506 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Prabir Pradhan5735a322022-04-11 17:23:34 +00001507 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT,
1508 std::optional<int32_t> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
1509 return dispatcher->injectInputEvent(&event, targetUid, injectionMode, injectionTimeout,
1510 policyFlags);
Garfield Tandf26e862020-07-01 20:18:19 -07001511}
1512
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001513static InputEventInjectionResult injectMotionEvent(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001514 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t source,
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001515 int32_t displayId, const PointF& position = {100, 200},
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001516 const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001517 AMOTION_EVENT_INVALID_CURSOR_POSITION},
1518 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001519 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Prabir Pradhan5735a322022-04-11 17:23:34 +00001520 nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC),
1521 std::optional<int32_t> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
Garfield Tandf26e862020-07-01 20:18:19 -07001522 MotionEvent event = MotionEventBuilder(action, source)
1523 .displayId(displayId)
1524 .eventTime(eventTime)
1525 .rawXCursorPosition(cursorPosition.x)
1526 .rawYCursorPosition(cursorPosition.y)
1527 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
1528 .x(position.x)
1529 .y(position.y))
1530 .build();
Arthur Hungb92218b2018-08-14 12:00:21 +08001531
1532 // Inject event until dispatch out.
Prabir Pradhan5735a322022-04-11 17:23:34 +00001533 return injectMotionEvent(dispatcher, event, injectionTimeout, injectionMode, targetUid,
1534 policyFlags);
Arthur Hungb92218b2018-08-14 12:00:21 +08001535}
1536
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001537static InputEventInjectionResult injectMotionDown(
1538 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t source, int32_t displayId,
1539 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001540 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location);
Garfield Tan00f511d2019-06-12 16:55:40 -07001541}
1542
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001543static InputEventInjectionResult injectMotionUp(const std::unique_ptr<InputDispatcher>& dispatcher,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001544 int32_t source, int32_t displayId,
1545 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001546 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location);
Michael Wright3a240c42019-12-10 20:53:41 +00001547}
1548
Jackal Guof9696682018-10-05 12:23:23 +08001549static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) {
1550 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1551 // Define a valid key event.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001552 NotifyKeyArgs args(/* id */ 0, currentTime, 0 /*readTime*/, DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
1553 displayId, POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0, AKEYCODE_A,
1554 KEY_A, AMETA_NONE, currentTime);
Jackal Guof9696682018-10-05 12:23:23 +08001555
1556 return args;
1557}
1558
chaviwd1c23182019-12-20 18:44:56 -08001559static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId,
1560 const std::vector<PointF>& points) {
1561 size_t pointerCount = points.size();
chaviwaf87b3e2019-10-01 16:59:28 -07001562 if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) {
1563 EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer";
1564 }
1565
chaviwd1c23182019-12-20 18:44:56 -08001566 PointerProperties pointerProperties[pointerCount];
1567 PointerCoords pointerCoords[pointerCount];
Jackal Guof9696682018-10-05 12:23:23 +08001568
chaviwd1c23182019-12-20 18:44:56 -08001569 for (size_t i = 0; i < pointerCount; i++) {
1570 pointerProperties[i].clear();
1571 pointerProperties[i].id = i;
1572 pointerProperties[i].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jackal Guof9696682018-10-05 12:23:23 +08001573
chaviwd1c23182019-12-20 18:44:56 -08001574 pointerCoords[i].clear();
1575 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x);
1576 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y);
1577 }
Jackal Guof9696682018-10-05 12:23:23 +08001578
1579 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1580 // Define a valid motion event.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001581 NotifyMotionArgs args(/* id */ 0, currentTime, 0 /*readTime*/, DEVICE_ID, source, displayId,
Garfield Tan00f511d2019-06-12 16:55:40 -07001582 POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0,
1583 AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
chaviwd1c23182019-12-20 18:44:56 -08001584 AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties,
1585 pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0,
Garfield Tan00f511d2019-06-12 16:55:40 -07001586 AMOTION_EVENT_INVALID_CURSOR_POSITION,
1587 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {});
Jackal Guof9696682018-10-05 12:23:23 +08001588
1589 return args;
1590}
1591
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08001592static NotifyMotionArgs generateTouchArgs(int32_t action, const std::vector<PointF>& points) {
1593 return generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, points);
1594}
1595
chaviwd1c23182019-12-20 18:44:56 -08001596static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) {
1597 return generateMotionArgs(action, source, displayId, {PointF{100, 200}});
1598}
1599
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001600static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs(
1601 const PointerCaptureRequest& request) {
1602 return NotifyPointerCaptureChangedArgs(/* id */ 0, systemTime(SYSTEM_TIME_MONOTONIC), request);
Prabir Pradhan99987712020-11-10 18:43:05 -08001603}
1604
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001605/**
1606 * When a window unexpectedly disposes of its input channel, policy should be notified about the
1607 * broken channel.
1608 */
1609TEST_F(InputDispatcherTest, WhenInputChannelBreaks_PolicyIsNotified) {
1610 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1611 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001612 sp<FakeWindowHandle>::make(application, mDispatcher,
1613 "Window that breaks its input channel", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001614
1615 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1616
1617 // Window closes its channel, but the window remains.
1618 window->destroyReceiver();
1619 mFakePolicy->assertNotifyInputChannelBrokenWasCalled(window->getInfo()->token);
1620}
1621
Arthur Hungb92218b2018-08-14 12:00:21 +08001622TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001623 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001624 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1625 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001626
Arthur Hung72d8dc32020-03-28 00:48:39 +00001627 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001628 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1629 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1630 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001631
1632 // Window should receive motion event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001633 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001634}
1635
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001636TEST_F(InputDispatcherTest, WhenDisplayNotSpecified_InjectMotionToDefaultDisplay) {
1637 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001638 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1639 "Fake Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001640
1641 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1642 // Inject a MotionEvent to an unknown display.
1643 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1644 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_NONE))
1645 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1646
1647 // Window should receive motion event.
1648 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1649}
1650
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001651/**
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08001652 * Calling setInputWindows once should not cause any issues.
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001653 * This test serves as a sanity check for the next test, where setInputWindows is
1654 * called twice.
1655 */
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08001656TEST_F(InputDispatcherTest, SetInputWindowOnceWithSingleTouchWindow) {
Chris Yea209fde2020-07-22 13:54:51 -07001657 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001658 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1659 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001660 window->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001661
1662 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001663 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001664 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1665 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001666 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001667
1668 // Window should receive motion event.
1669 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1670}
1671
1672/**
1673 * Calling setInputWindows twice, with the same info, should not cause any issues.
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001674 */
1675TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001676 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001677 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1678 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001679 window->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001680
1681 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1682 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001683 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001684 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1685 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001686 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001687
1688 // Window should receive motion event.
1689 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1690}
1691
Arthur Hungb92218b2018-08-14 12:00:21 +08001692// The foreground window should receive the first touch down event.
1693TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001694 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001695 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001696 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001697 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001698 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001699
Arthur Hung72d8dc32020-03-28 00:48:39 +00001700 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001701 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1702 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1703 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001704
1705 // Top window should receive the touch down event. Second window should not receive anything.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001706 windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001707 windowSecond->assertNoEvents();
1708}
1709
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001710/**
1711 * Two windows: A top window, and a wallpaper behind the window.
1712 * Touch goes to the top window, and then top window disappears. Ensure that wallpaper window
1713 * gets ACTION_CANCEL.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001714 * 1. foregroundWindow <-- dup touch to wallpaper
1715 * 2. wallpaperWindow <-- is wallpaper
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001716 */
1717TEST_F(InputDispatcherTest, WhenForegroundWindowDisappears_WallpaperTouchIsCanceled) {
1718 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1719 sp<FakeWindowHandle> foregroundWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001720 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001721 foregroundWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001722 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001723 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001724 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001725
1726 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}});
1727 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1728 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1729 {100, 200}))
1730 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1731
1732 // Both foreground window and its wallpaper should receive the touch down
1733 foregroundWindow->consumeMotionDown();
1734 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1735
1736 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1737 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
1738 ADISPLAY_ID_DEFAULT, {110, 200}))
1739 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1740
1741 foregroundWindow->consumeMotionMove();
1742 wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1743
1744 // Now the foreground window goes away, but the wallpaper stays
1745 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}});
1746 foregroundWindow->consumeMotionCancel();
1747 // Since the "parent" window of the wallpaper is gone, wallpaper should receive cancel, too.
1748 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1749}
1750
1751/**
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001752 * Same test as WhenForegroundWindowDisappears_WallpaperTouchIsCanceled above,
1753 * with the following differences:
1754 * After ACTION_DOWN, Wallpaper window hangs up its channel, which forces the dispatcher to
1755 * clean up the connection.
1756 * This later may crash dispatcher during ACTION_CANCEL synthesis, if the dispatcher is not careful.
1757 * Ensure that there's no crash in the dispatcher.
1758 */
1759TEST_F(InputDispatcherTest, WhenWallpaperDisappears_NoCrash) {
1760 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1761 sp<FakeWindowHandle> foregroundWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001762 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001763 foregroundWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001764 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001765 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001766 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001767
1768 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}});
1769 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1770 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1771 {100, 200}))
1772 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1773
1774 // Both foreground window and its wallpaper should receive the touch down
1775 foregroundWindow->consumeMotionDown();
1776 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1777
1778 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1779 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
1780 ADISPLAY_ID_DEFAULT, {110, 200}))
1781 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1782
1783 foregroundWindow->consumeMotionMove();
1784 wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1785
1786 // Wallpaper closes its channel, but the window remains.
1787 wallpaperWindow->destroyReceiver();
1788 mFakePolicy->assertNotifyInputChannelBrokenWasCalled(wallpaperWindow->getInfo()->token);
1789
1790 // Now the foreground window goes away, but the wallpaper stays, even though its channel
1791 // is no longer valid.
1792 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}});
1793 foregroundWindow->consumeMotionCancel();
1794}
1795
Arthur Hungc539dbb2022-12-08 07:45:36 +00001796class ShouldSplitTouchFixture : public InputDispatcherTest,
1797 public ::testing::WithParamInterface<bool> {};
1798INSTANTIATE_TEST_SUITE_P(InputDispatcherTest, ShouldSplitTouchFixture,
1799 ::testing::Values(true, false));
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001800/**
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001801 * A single window that receives touch (on top), and a wallpaper window underneath it.
1802 * The top window gets a multitouch gesture.
1803 * Ensure that wallpaper gets the same gesture.
1804 */
Arthur Hungc539dbb2022-12-08 07:45:36 +00001805TEST_P(ShouldSplitTouchFixture, WallpaperWindowReceivesMultiTouch) {
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001806 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Arthur Hungc539dbb2022-12-08 07:45:36 +00001807 sp<FakeWindowHandle> foregroundWindow =
1808 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
1809 foregroundWindow->setDupTouchToWallpaper(true);
1810 foregroundWindow->setPreventSplitting(GetParam());
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001811
1812 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001813 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001814 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001815
Arthur Hungc539dbb2022-12-08 07:45:36 +00001816 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}});
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001817
1818 // Touch down on top window
1819 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1820 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1821 {100, 100}))
1822 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1823
1824 // Both top window and its wallpaper should receive the touch down
Arthur Hungc539dbb2022-12-08 07:45:36 +00001825 foregroundWindow->consumeMotionDown();
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001826 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1827
1828 // Second finger down on the top window
1829 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08001830 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001831 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
1832 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
1833 .x(100)
1834 .y(100))
1835 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER)
1836 .x(150)
1837 .y(150))
1838 .build();
1839 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1840 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
1841 InputEventInjectionSync::WAIT_FOR_RESULT))
1842 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1843
Arthur Hungc539dbb2022-12-08 07:45:36 +00001844 foregroundWindow->consumeMotionPointerDown(1 /* pointerIndex */);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001845 wallpaperWindow->consumeMotionPointerDown(1 /* pointerIndex */, ADISPLAY_ID_DEFAULT,
1846 expectedWallpaperFlags);
Arthur Hungc539dbb2022-12-08 07:45:36 +00001847
1848 const MotionEvent secondFingerUpEvent =
1849 MotionEventBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
1850 .displayId(ADISPLAY_ID_DEFAULT)
1851 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
1852 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
1853 .x(100)
1854 .y(100))
1855 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER)
1856 .x(150)
1857 .y(150))
1858 .build();
1859 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1860 injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT,
1861 InputEventInjectionSync::WAIT_FOR_RESULT))
1862 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1863 foregroundWindow->consumeMotionPointerUp(0);
1864 wallpaperWindow->consumeMotionPointerUp(0, ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1865
1866 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1867 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1868 {100, 100}))
1869 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1870 foregroundWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
1871 wallpaperWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001872}
1873
1874/**
1875 * Two windows: a window on the left and window on the right.
1876 * A third window, wallpaper, is behind both windows, and spans both top windows.
1877 * The first touch down goes to the left window. A second pointer touches down on the right window.
1878 * The touch is split, so both left and right windows should receive ACTION_DOWN.
1879 * The wallpaper will get the full event, so it should receive ACTION_DOWN followed by
1880 * ACTION_POINTER_DOWN(1).
1881 */
1882TEST_F(InputDispatcherTest, TwoWindows_SplitWallpaperTouch) {
1883 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1884 sp<FakeWindowHandle> leftWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001885 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001886 leftWindow->setFrame(Rect(0, 0, 200, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001887 leftWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001888
1889 sp<FakeWindowHandle> rightWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001890 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001891 rightWindow->setFrame(Rect(200, 0, 400, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001892 rightWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001893
1894 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001895 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001896 wallpaperWindow->setFrame(Rect(0, 0, 400, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001897 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001898
1899 mDispatcher->setInputWindows(
1900 {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}});
1901
1902 // Touch down on left window
1903 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1904 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1905 {100, 100}))
1906 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1907
1908 // Both foreground window and its wallpaper should receive the touch down
1909 leftWindow->consumeMotionDown();
1910 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1911
1912 // Second finger down on the right window
1913 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08001914 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001915 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
1916 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
1917 .x(100)
1918 .y(100))
1919 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER)
1920 .x(300)
1921 .y(100))
1922 .build();
1923 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1924 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
1925 InputEventInjectionSync::WAIT_FOR_RESULT))
1926 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1927
1928 leftWindow->consumeMotionMove();
1929 // Since the touch is split, right window gets ACTION_DOWN
1930 rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1931 wallpaperWindow->consumeMotionPointerDown(1 /* pointerIndex */, ADISPLAY_ID_DEFAULT,
1932 expectedWallpaperFlags);
1933
1934 // Now, leftWindow, which received the first finger, disappears.
1935 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {rightWindow, wallpaperWindow}}});
1936 leftWindow->consumeMotionCancel();
1937 // Since a "parent" window of the wallpaper is gone, wallpaper should receive cancel, too.
1938 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1939
1940 // The pointer that's still down on the right window moves, and goes to the right window only.
1941 // As far as the dispatcher's concerned though, both pointers are still present.
1942 const MotionEvent secondFingerMoveEvent =
1943 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
1944 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
1945 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
1946 .x(100)
1947 .y(100))
1948 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER)
1949 .x(310)
1950 .y(110))
1951 .build();
1952 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1953 injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT,
1954 InputEventInjectionSync::WAIT_FOR_RESULT));
1955 rightWindow->consumeMotionMove();
1956
1957 leftWindow->assertNoEvents();
1958 rightWindow->assertNoEvents();
1959 wallpaperWindow->assertNoEvents();
1960}
1961
Arthur Hungc539dbb2022-12-08 07:45:36 +00001962/**
1963 * Two windows: a window on the left with dup touch to wallpaper and window on the right without it.
1964 * The touch slips to the right window. so left window and wallpaper should receive ACTION_CANCEL
1965 * The right window should receive ACTION_DOWN.
1966 */
1967TEST_F(InputDispatcherTest, WallpaperWindowWhenSlippery) {
Arthur Hung74c248d2022-11-23 07:09:59 +00001968 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Arthur Hungc539dbb2022-12-08 07:45:36 +00001969 sp<FakeWindowHandle> leftWindow =
1970 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
1971 leftWindow->setFrame(Rect(0, 0, 200, 200));
1972 leftWindow->setDupTouchToWallpaper(true);
1973 leftWindow->setSlippery(true);
1974
1975 sp<FakeWindowHandle> rightWindow =
1976 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
1977 rightWindow->setFrame(Rect(200, 0, 400, 200));
Arthur Hung74c248d2022-11-23 07:09:59 +00001978
1979 sp<FakeWindowHandle> wallpaperWindow =
1980 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
1981 wallpaperWindow->setIsWallpaper(true);
Arthur Hung74c248d2022-11-23 07:09:59 +00001982
Arthur Hungc539dbb2022-12-08 07:45:36 +00001983 mDispatcher->setInputWindows(
1984 {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}});
Arthur Hung74c248d2022-11-23 07:09:59 +00001985
Arthur Hungc539dbb2022-12-08 07:45:36 +00001986 // Touch down on left window
Arthur Hung74c248d2022-11-23 07:09:59 +00001987 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1988 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Arthur Hungc539dbb2022-12-08 07:45:36 +00001989 {100, 100}))
Arthur Hung74c248d2022-11-23 07:09:59 +00001990 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungc539dbb2022-12-08 07:45:36 +00001991
1992 // Both foreground window and its wallpaper should receive the touch down
1993 leftWindow->consumeMotionDown();
Arthur Hung74c248d2022-11-23 07:09:59 +00001994 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1995
Arthur Hungc539dbb2022-12-08 07:45:36 +00001996 // Move to right window, the left window should receive cancel.
Arthur Hung74c248d2022-11-23 07:09:59 +00001997 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Arthur Hungc539dbb2022-12-08 07:45:36 +00001998 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
1999 ADISPLAY_ID_DEFAULT, {201, 100}))
Arthur Hung74c248d2022-11-23 07:09:59 +00002000 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2001
Arthur Hungc539dbb2022-12-08 07:45:36 +00002002 leftWindow->consumeMotionCancel();
2003 rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2004 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Arthur Hung74c248d2022-11-23 07:09:59 +00002005}
2006
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002007/**
2008 * On the display, have a single window, and also an area where there's no window.
2009 * First pointer touches the "no window" area of the screen. Second pointer touches the window.
2010 * Make sure that the window receives the second pointer, and first pointer is simply ignored.
2011 */
2012TEST_F(InputDispatcherTest, SplitWorksWhenEmptyAreaIsTouched) {
2013 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2014 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002015 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002016
2017 mDispatcher->setInputWindows({{DISPLAY_ID, {window}}});
2018 NotifyMotionArgs args;
2019
2020 // Touch down on the empty space
2021 mDispatcher->notifyMotion(&(args = generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{-1, -1}})));
2022
2023 mDispatcher->waitForIdle();
2024 window->assertNoEvents();
2025
2026 // Now touch down on the window with another pointer
2027 mDispatcher->notifyMotion(&(args = generateTouchArgs(POINTER_1_DOWN, {{-1, -1}, {10, 10}})));
2028 mDispatcher->waitForIdle();
2029 window->consumeMotionDown();
2030}
2031
2032/**
2033 * Same test as above, but instead of touching the empty space, the first touch goes to
2034 * non-touchable window.
2035 */
2036TEST_F(InputDispatcherTest, SplitWorksWhenNonTouchableWindowIsTouched) {
2037 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2038 sp<FakeWindowHandle> window1 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002039 sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002040 window1->setTouchableRegion(Region{{0, 0, 100, 100}});
2041 window1->setTouchable(false);
2042 sp<FakeWindowHandle> window2 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002043 sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002044 window2->setTouchableRegion(Region{{100, 0, 200, 100}});
2045
2046 mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}});
2047
2048 NotifyMotionArgs args;
2049 // Touch down on the non-touchable window
2050 mDispatcher->notifyMotion(&(args = generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}})));
2051
2052 mDispatcher->waitForIdle();
2053 window1->assertNoEvents();
2054 window2->assertNoEvents();
2055
2056 // Now touch down on the window with another pointer
2057 mDispatcher->notifyMotion(&(args = generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}})));
2058 mDispatcher->waitForIdle();
2059 window2->consumeMotionDown();
2060}
2061
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002062/**
2063 * When splitting touch events the downTime should be adjusted such that the downTime corresponds
2064 * to the event time of the first ACTION_DOWN sent to the particular window.
2065 */
2066TEST_F(InputDispatcherTest, SplitTouchesSendCorrectActionDownTime) {
2067 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2068 sp<FakeWindowHandle> window1 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002069 sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002070 window1->setTouchableRegion(Region{{0, 0, 100, 100}});
2071 sp<FakeWindowHandle> window2 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002072 sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002073 window2->setTouchableRegion(Region{{100, 0, 200, 100}});
2074
2075 mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}});
2076
2077 NotifyMotionArgs args;
2078 // Touch down on the first window
2079 mDispatcher->notifyMotion(&(args = generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}})));
2080
2081 mDispatcher->waitForIdle();
2082 InputEvent* inputEvent1 = window1->consume();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002083 ASSERT_NE(inputEvent1, nullptr);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002084 window2->assertNoEvents();
2085 MotionEvent& motionEvent1 = static_cast<MotionEvent&>(*inputEvent1);
2086 nsecs_t downTimeForWindow1 = motionEvent1.getDownTime();
2087 ASSERT_EQ(motionEvent1.getDownTime(), motionEvent1.getEventTime());
2088
2089 // Now touch down on the window with another pointer
2090 mDispatcher->notifyMotion(&(args = generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}})));
2091 mDispatcher->waitForIdle();
2092 InputEvent* inputEvent2 = window2->consume();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002093 ASSERT_NE(inputEvent2, nullptr);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002094 MotionEvent& motionEvent2 = static_cast<MotionEvent&>(*inputEvent2);
2095 nsecs_t downTimeForWindow2 = motionEvent2.getDownTime();
2096 ASSERT_NE(downTimeForWindow1, downTimeForWindow2);
2097 ASSERT_EQ(motionEvent2.getDownTime(), motionEvent2.getEventTime());
2098
2099 // Now move the pointer on the second window
2100 mDispatcher->notifyMotion(
2101 &(args = generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{50, 50}, {151, 51}})));
2102 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002103 window2->consumeMotionEvent(WithDownTime(downTimeForWindow2));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002104
2105 // Now add new touch down on the second window
2106 mDispatcher->notifyMotion(
2107 &(args = generateTouchArgs(POINTER_2_DOWN, {{50, 50}, {151, 51}, {150, 50}})));
2108 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002109 window2->consumeMotionEvent(WithDownTime(downTimeForWindow2));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002110
2111 // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line
2112 window1->consumeMotionMove();
2113 window1->assertNoEvents();
2114
2115 // Now move the pointer on the first window
2116 mDispatcher->notifyMotion(
2117 &(args = generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{51, 51}, {151, 51}})));
2118 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002119 window1->consumeMotionEvent(WithDownTime(downTimeForWindow1));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002120
2121 mDispatcher->notifyMotion(&(
2122 args = generateTouchArgs(POINTER_3_DOWN, {{51, 51}, {151, 51}, {150, 50}, {50, 50}})));
2123 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002124 window1->consumeMotionEvent(WithDownTime(downTimeForWindow1));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002125}
2126
Garfield Tandf26e862020-07-01 20:18:19 -07002127TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) {
Chris Yea209fde2020-07-22 13:54:51 -07002128 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07002129 sp<FakeWindowHandle> windowLeft =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002130 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07002131 windowLeft->setFrame(Rect(0, 0, 600, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07002132 sp<FakeWindowHandle> windowRight =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002133 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07002134 windowRight->setFrame(Rect(600, 0, 1200, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07002135
2136 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2137
2138 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
2139
2140 // Start cursor position in right window so that we can move the cursor to left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002141 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002142 injectMotionEvent(mDispatcher,
2143 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2144 AINPUT_SOURCE_MOUSE)
2145 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2146 .x(900)
2147 .y(400))
2148 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002149 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Sam Dubey39d37cf2022-12-07 18:05:35 +00002150 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE));
Garfield Tandf26e862020-07-01 20:18:19 -07002151
2152 // Move cursor into left window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002153 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002154 injectMotionEvent(mDispatcher,
2155 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2156 AINPUT_SOURCE_MOUSE)
2157 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2158 .x(300)
2159 .y(400))
2160 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002161 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2162 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Sam Dubey39d37cf2022-12-07 18:05:35 +00002163 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE));
Garfield Tandf26e862020-07-01 20:18:19 -07002164
2165 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002166 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002167 injectMotionEvent(mDispatcher,
2168 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2169 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2170 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2171 .x(300)
2172 .y(400))
2173 .build()));
2174 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2175
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002176 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002177 injectMotionEvent(mDispatcher,
2178 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
2179 AINPUT_SOURCE_MOUSE)
2180 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2181 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2182 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2183 .x(300)
2184 .y(400))
2185 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002186 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
Garfield Tandf26e862020-07-01 20:18:19 -07002187
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002188 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002189 injectMotionEvent(mDispatcher,
2190 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
2191 AINPUT_SOURCE_MOUSE)
2192 .buttonState(0)
2193 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2194 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2195 .x(300)
2196 .y(400))
2197 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002198 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE));
Garfield Tandf26e862020-07-01 20:18:19 -07002199
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002200 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002201 injectMotionEvent(mDispatcher,
2202 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
2203 .buttonState(0)
2204 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2205 .x(300)
2206 .y(400))
2207 .build()));
2208 windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT);
2209
2210 // Move mouse cursor back to right window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002211 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002212 injectMotionEvent(mDispatcher,
2213 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2214 AINPUT_SOURCE_MOUSE)
2215 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2216 .x(900)
2217 .y(400))
2218 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002219 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2220 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Sam Dubey39d37cf2022-12-07 18:05:35 +00002221 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002222
2223 // No more events
2224 windowLeft->assertNoEvents();
2225 windowRight->assertNoEvents();
2226}
2227
2228TEST_F(InputDispatcherTest, HoverWithSpyWindows) {
2229 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2230
2231 sp<FakeWindowHandle> spyWindow =
2232 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
2233 spyWindow->setFrame(Rect(0, 0, 600, 800));
2234 spyWindow->setTrustedOverlay(true);
2235 spyWindow->setSpy(true);
2236 sp<FakeWindowHandle> window =
2237 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2238 window->setFrame(Rect(0, 0, 600, 800));
2239
2240 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2241 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
2242
2243 // Send mouse cursor to the window
2244 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2245 injectMotionEvent(mDispatcher,
2246 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
2247 AINPUT_SOURCE_MOUSE)
2248 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2249 .x(100)
2250 .y(100))
2251 .build()));
2252
2253 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2254 WithSource(AINPUT_SOURCE_MOUSE)));
2255 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2256 WithSource(AINPUT_SOURCE_MOUSE)));
2257
2258 window->assertNoEvents();
2259 spyWindow->assertNoEvents();
Garfield Tandf26e862020-07-01 20:18:19 -07002260}
2261
2262// This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected
2263// directly in this test.
2264TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) {
Chris Yea209fde2020-07-22 13:54:51 -07002265 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07002266 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002267 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07002268 window->setFrame(Rect(0, 0, 1200, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07002269
2270 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2271
2272 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2273
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002274 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002275 injectMotionEvent(mDispatcher,
2276 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
2277 AINPUT_SOURCE_MOUSE)
2278 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2279 .x(300)
2280 .y(400))
2281 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002282 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Sam Dubey39d37cf2022-12-07 18:05:35 +00002283
Garfield Tandf26e862020-07-01 20:18:19 -07002284 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002285 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002286 injectMotionEvent(mDispatcher,
2287 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2288 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2289 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2290 .x(300)
2291 .y(400))
2292 .build()));
2293 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2294
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002295 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002296 injectMotionEvent(mDispatcher,
2297 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
2298 AINPUT_SOURCE_MOUSE)
2299 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2300 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2301 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2302 .x(300)
2303 .y(400))
2304 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002305 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
Garfield Tandf26e862020-07-01 20:18:19 -07002306
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002307 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002308 injectMotionEvent(mDispatcher,
2309 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
2310 AINPUT_SOURCE_MOUSE)
2311 .buttonState(0)
2312 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2313 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2314 .x(300)
2315 .y(400))
2316 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002317 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE));
Garfield Tandf26e862020-07-01 20:18:19 -07002318
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002319 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002320 injectMotionEvent(mDispatcher,
2321 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
2322 .buttonState(0)
2323 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2324 .x(300)
2325 .y(400))
2326 .build()));
2327 window->consumeMotionUp(ADISPLAY_ID_DEFAULT);
2328
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002329 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002330 injectMotionEvent(mDispatcher,
2331 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT,
2332 AINPUT_SOURCE_MOUSE)
2333 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2334 .x(300)
2335 .y(400))
2336 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002337 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
Garfield Tandf26e862020-07-01 20:18:19 -07002338}
2339
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08002340/**
2341 * Inject a mouse hover event followed by a tap from touchscreen.
Sam Dubey39d37cf2022-12-07 18:05:35 +00002342 * In the current implementation, the tap does not cause a HOVER_EXIT event.
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08002343 */
2344TEST_F(InputDispatcherTest, MouseHoverAndTouchTap) {
2345 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2346 sp<FakeWindowHandle> window =
2347 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2348 window->setFrame(Rect(0, 0, 100, 100));
2349
2350 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2351
2352 // Inject a hover_move from mouse.
2353 NotifyMotionArgs motionArgs =
2354 generateMotionArgs(AMOTION_EVENT_ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE,
2355 ADISPLAY_ID_DEFAULT, {{50, 50}});
2356 motionArgs.xCursorPosition = 50;
2357 motionArgs.yCursorPosition = 50;
2358 mDispatcher->notifyMotion(&motionArgs);
2359 ASSERT_NO_FATAL_FAILURE(
2360 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2361 WithSource(AINPUT_SOURCE_MOUSE))));
Sam Dubey39d37cf2022-12-07 18:05:35 +00002362 ASSERT_NO_FATAL_FAILURE(
2363 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2364 WithSource(AINPUT_SOURCE_MOUSE))));
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08002365
2366 // Tap on the window
2367 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2368 ADISPLAY_ID_DEFAULT, {{10, 10}});
2369 mDispatcher->notifyMotion(&motionArgs);
2370 ASSERT_NO_FATAL_FAILURE(
2371 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2372 WithSource(AINPUT_SOURCE_TOUCHSCREEN))));
2373
2374 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
2375 ADISPLAY_ID_DEFAULT, {{10, 10}});
2376 mDispatcher->notifyMotion(&motionArgs);
2377 ASSERT_NO_FATAL_FAILURE(
2378 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
2379 WithSource(AINPUT_SOURCE_TOUCHSCREEN))));
2380}
2381
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02002382TEST_F(InputDispatcherTest, HoverEnterMoveRemoveWindowsInSecondDisplay) {
2383 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2384 sp<FakeWindowHandle> windowDefaultDisplay =
2385 sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay",
2386 ADISPLAY_ID_DEFAULT);
2387 windowDefaultDisplay->setFrame(Rect(0, 0, 600, 800));
2388 sp<FakeWindowHandle> windowSecondDisplay =
2389 sp<FakeWindowHandle>::make(application, mDispatcher, "SecondDisplay",
2390 SECOND_DISPLAY_ID);
2391 windowSecondDisplay->setFrame(Rect(0, 0, 600, 800));
2392
2393 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}},
2394 {SECOND_DISPLAY_ID, {windowSecondDisplay}}});
2395
2396 // Set cursor position in window in default display and check that hover enter and move
2397 // events are generated.
2398 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2399 injectMotionEvent(mDispatcher,
2400 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2401 AINPUT_SOURCE_MOUSE)
2402 .displayId(ADISPLAY_ID_DEFAULT)
2403 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2404 .x(300)
2405 .y(600))
2406 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002407 windowDefaultDisplay->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Sam Dubey39d37cf2022-12-07 18:05:35 +00002408 windowDefaultDisplay->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE));
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02002409
2410 // Remove all windows in secondary display and check that no event happens on window in
2411 // primary display.
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002412 mDispatcher->setInputWindows(
2413 {{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}}, {SECOND_DISPLAY_ID, {}}});
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02002414 windowDefaultDisplay->assertNoEvents();
2415
2416 // Move cursor position in window in default display and check that only hover move
2417 // event is generated and not hover enter event.
2418 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}},
2419 {SECOND_DISPLAY_ID, {windowSecondDisplay}}});
2420 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2421 injectMotionEvent(mDispatcher,
2422 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2423 AINPUT_SOURCE_MOUSE)
2424 .displayId(ADISPLAY_ID_DEFAULT)
2425 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2426 .x(400)
2427 .y(700))
2428 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002429 windowDefaultDisplay->consumeMotionEvent(
2430 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2431 WithSource(AINPUT_SOURCE_MOUSE)));
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02002432 windowDefaultDisplay->assertNoEvents();
2433}
2434
Garfield Tan00f511d2019-06-12 16:55:40 -07002435TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) {
Chris Yea209fde2020-07-22 13:54:51 -07002436 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tan00f511d2019-06-12 16:55:40 -07002437
2438 sp<FakeWindowHandle> windowLeft =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002439 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07002440 windowLeft->setFrame(Rect(0, 0, 600, 800));
Garfield Tan00f511d2019-06-12 16:55:40 -07002441 sp<FakeWindowHandle> windowRight =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002442 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07002443 windowRight->setFrame(Rect(600, 0, 1200, 800));
Garfield Tan00f511d2019-06-12 16:55:40 -07002444
2445 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2446
Arthur Hung72d8dc32020-03-28 00:48:39 +00002447 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
Garfield Tan00f511d2019-06-12 16:55:40 -07002448
2449 // Inject an event with coordinate in the area of right window, with mouse cursor in the area of
2450 // left window. This event should be dispatched to the left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002451 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tan00f511d2019-06-12 16:55:40 -07002452 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07002453 ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400}));
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002454 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07002455 windowRight->assertNoEvents();
2456}
2457
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002458TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) {
Chris Yea209fde2020-07-22 13:54:51 -07002459 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002460 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
2461 "Fake Window", ADISPLAY_ID_DEFAULT);
Vishnu Nair47074b82020-08-14 11:54:47 -07002462 window->setFocusable(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002463
Arthur Hung72d8dc32020-03-28 00:48:39 +00002464 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002465 setFocusedWindow(window);
2466
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002467 window->consumeFocusEvent(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002468
2469 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
2470 mDispatcher->notifyKey(&keyArgs);
2471
2472 // Window should receive key down event.
2473 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2474
2475 // When device reset happens, that key stream should be terminated with FLAG_CANCELED
2476 // on the app side.
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002477 NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002478 mDispatcher->notifyDeviceReset(&args);
2479 window->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
2480 AKEY_EVENT_FLAG_CANCELED);
2481}
2482
2483TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) {
Chris Yea209fde2020-07-22 13:54:51 -07002484 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002485 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
2486 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002487
Arthur Hung72d8dc32020-03-28 00:48:39 +00002488 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002489
2490 NotifyMotionArgs motionArgs =
2491 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2492 ADISPLAY_ID_DEFAULT);
2493 mDispatcher->notifyMotion(&motionArgs);
2494
2495 // Window should receive motion down event.
2496 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2497
2498 // When device reset happens, that motion stream should be terminated with ACTION_CANCEL
2499 // on the app side.
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002500 NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002501 mDispatcher->notifyDeviceReset(&args);
2502 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, ADISPLAY_ID_DEFAULT,
2503 0 /*expectedFlags*/);
2504}
2505
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08002506TEST_F(InputDispatcherTest, InterceptKeyByPolicy) {
2507 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002508 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
2509 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08002510 window->setFocusable(true);
2511
2512 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2513 setFocusedWindow(window);
2514
2515 window->consumeFocusEvent(true);
2516
2517 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
2518 const std::chrono::milliseconds interceptKeyTimeout = 50ms;
2519 const nsecs_t injectTime = keyArgs.eventTime;
2520 mFakePolicy->setInterceptKeyTimeout(interceptKeyTimeout);
2521 mDispatcher->notifyKey(&keyArgs);
2522 // The dispatching time should be always greater than or equal to intercept key timeout.
2523 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2524 ASSERT_TRUE((systemTime(SYSTEM_TIME_MONOTONIC) - injectTime) >=
2525 std::chrono::nanoseconds(interceptKeyTimeout).count());
2526}
2527
2528TEST_F(InputDispatcherTest, InterceptKeyIfKeyUp) {
2529 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002530 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
2531 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08002532 window->setFocusable(true);
2533
2534 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2535 setFocusedWindow(window);
2536
2537 window->consumeFocusEvent(true);
2538
2539 NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
2540 NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
2541 mFakePolicy->setInterceptKeyTimeout(150ms);
2542 mDispatcher->notifyKey(&keyDown);
2543 mDispatcher->notifyKey(&keyUp);
2544
2545 // Window should receive key event immediately when same key up.
2546 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2547 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
2548}
2549
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07002550/**
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00002551 * This test documents the behavior of WATCH_OUTSIDE_TOUCH. The window will get ACTION_OUTSIDE when
2552 * a another pointer causes ACTION_DOWN to be sent to another window for the first time. Only one
2553 * ACTION_OUTSIDE event is sent per gesture.
2554 */
2555TEST_F(InputDispatcherTest, ActionOutsideSentOnlyWhenAWindowIsTouched) {
2556 // There are three windows that do not overlap. `window` wants to WATCH_OUTSIDE_TOUCH.
2557 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002558 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
2559 "First Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00002560 window->setWatchOutsideTouch(true);
2561 window->setFrame(Rect{0, 0, 100, 100});
2562 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002563 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
2564 ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00002565 secondWindow->setFrame(Rect{100, 100, 200, 200});
2566 sp<FakeWindowHandle> thirdWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002567 sp<FakeWindowHandle>::make(application, mDispatcher, "Third Window",
2568 ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00002569 thirdWindow->setFrame(Rect{200, 200, 300, 300});
2570 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, secondWindow, thirdWindow}}});
2571
2572 // First pointer lands outside all windows. `window` does not get ACTION_OUTSIDE.
2573 NotifyMotionArgs motionArgs =
2574 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2575 ADISPLAY_ID_DEFAULT, {PointF{-10, -10}});
2576 mDispatcher->notifyMotion(&motionArgs);
2577 window->assertNoEvents();
2578 secondWindow->assertNoEvents();
2579
2580 // The second pointer lands inside `secondWindow`, which should receive a DOWN event.
2581 // Now, `window` should get ACTION_OUTSIDE.
2582 motionArgs = generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2583 {PointF{-10, -10}, PointF{105, 105}});
2584 mDispatcher->notifyMotion(&motionArgs);
2585 window->consumeMotionOutside();
2586 secondWindow->consumeMotionDown();
2587 thirdWindow->assertNoEvents();
2588
2589 // The third pointer lands inside `thirdWindow`, which should receive a DOWN event. There is
2590 // no ACTION_OUTSIDE sent to `window` because one has already been sent for this gesture.
2591 motionArgs = generateMotionArgs(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2592 {PointF{-10, -10}, PointF{105, 105}, PointF{205, 205}});
2593 mDispatcher->notifyMotion(&motionArgs);
2594 window->assertNoEvents();
2595 secondWindow->consumeMotionMove();
2596 thirdWindow->consumeMotionDown();
2597}
2598
Prabir Pradhan814fe082022-07-22 20:22:18 +00002599TEST_F(InputDispatcherTest, OnWindowInfosChanged_RemoveAllWindowsOnDisplay) {
2600 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002601 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
2602 "Fake Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan814fe082022-07-22 20:22:18 +00002603 window->setFocusable(true);
2604
2605 mDispatcher->onWindowInfosChanged({*window->getInfo()}, {});
2606 setFocusedWindow(window);
2607
2608 window->consumeFocusEvent(true);
2609
2610 NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
2611 NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
2612 mDispatcher->notifyKey(&keyDown);
2613 mDispatcher->notifyKey(&keyUp);
2614
2615 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2616 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
2617
2618 // All windows are removed from the display. Ensure that we can no longer dispatch to it.
2619 mDispatcher->onWindowInfosChanged({}, {});
2620
2621 window->consumeFocusEvent(false);
2622
2623 mDispatcher->notifyKey(&keyDown);
2624 mDispatcher->notifyKey(&keyUp);
2625 window->assertNoEvents();
2626}
2627
Arthur Hung96483742022-11-15 03:30:48 +00002628TEST_F(InputDispatcherTest, NonSplitTouchableWindowReceivesMultiTouch) {
2629 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2630 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
2631 "Fake Window", ADISPLAY_ID_DEFAULT);
2632 // Ensure window is non-split and have some transform.
2633 window->setPreventSplitting(true);
2634 window->setWindowOffset(20, 40);
2635 mDispatcher->onWindowInfosChanged({*window->getInfo()}, {});
2636
2637 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2638 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2639 {50, 50}))
2640 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2641 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2642
2643 const MotionEvent secondFingerDownEvent =
2644 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2645 .displayId(ADISPLAY_ID_DEFAULT)
2646 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
2647 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
2648 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER)
2649 .x(-30)
2650 .y(-50))
2651 .build();
2652 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2653 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
2654 InputEventInjectionSync::WAIT_FOR_RESULT))
2655 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2656
2657 const MotionEvent* event = window->consumeMotion();
2658 EXPECT_EQ(POINTER_1_DOWN, event->getAction());
2659 EXPECT_EQ(70, event->getX(0)); // 50 + 20
2660 EXPECT_EQ(90, event->getY(0)); // 50 + 40
2661 EXPECT_EQ(-10, event->getX(1)); // -30 + 20
2662 EXPECT_EQ(-10, event->getY(1)); // -50 + 40
2663}
2664
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00002665/**
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07002666 * Ensure the correct coordinate spaces are used by InputDispatcher.
2667 *
2668 * InputDispatcher works in the display space, so its coordinate system is relative to the display
2669 * panel. Windows get events in the window space, and get raw coordinates in the logical display
2670 * space.
2671 */
2672class InputDispatcherDisplayProjectionTest : public InputDispatcherTest {
2673public:
2674 void SetUp() override {
2675 InputDispatcherTest::SetUp();
2676 mDisplayInfos.clear();
2677 mWindowInfos.clear();
2678 }
2679
2680 void addDisplayInfo(int displayId, const ui::Transform& transform) {
2681 gui::DisplayInfo info;
2682 info.displayId = displayId;
2683 info.transform = transform;
2684 mDisplayInfos.push_back(std::move(info));
2685 mDispatcher->onWindowInfosChanged(mWindowInfos, mDisplayInfos);
2686 }
2687
2688 void addWindow(const sp<WindowInfoHandle>& windowHandle) {
2689 mWindowInfos.push_back(*windowHandle->getInfo());
2690 mDispatcher->onWindowInfosChanged(mWindowInfos, mDisplayInfos);
2691 }
2692
2693 // Set up a test scenario where the display has a scaled projection and there are two windows
2694 // on the display.
2695 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupScaledDisplayScenario() {
2696 // The display has a projection that has a scale factor of 2 and 4 in the x and y directions
2697 // respectively.
2698 ui::Transform displayTransform;
2699 displayTransform.set(2, 0, 0, 4);
2700 addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform);
2701
2702 std::shared_ptr<FakeApplicationHandle> application =
2703 std::make_shared<FakeApplicationHandle>();
2704
2705 // Add two windows to the display. Their frames are represented in the display space.
2706 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002707 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
2708 ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07002709 firstWindow->setFrame(Rect(0, 0, 100, 200), displayTransform);
2710 addWindow(firstWindow);
2711
2712 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002713 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
2714 ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07002715 secondWindow->setFrame(Rect(100, 200, 200, 400), displayTransform);
2716 addWindow(secondWindow);
2717 return {std::move(firstWindow), std::move(secondWindow)};
2718 }
2719
2720private:
2721 std::vector<gui::DisplayInfo> mDisplayInfos;
2722 std::vector<gui::WindowInfo> mWindowInfos;
2723};
2724
2725TEST_F(InputDispatcherDisplayProjectionTest, HitTestsInDisplaySpace) {
2726 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
2727 // Send down to the first window. The point is represented in the display space. The point is
2728 // selected so that if the hit test was done with the transform applied to it, then it would
2729 // end up in the incorrect window.
2730 NotifyMotionArgs downMotionArgs =
2731 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2732 ADISPLAY_ID_DEFAULT, {PointF{75, 55}});
2733 mDispatcher->notifyMotion(&downMotionArgs);
2734
2735 firstWindow->consumeMotionDown();
2736 secondWindow->assertNoEvents();
2737}
2738
2739// Ensure that when a MotionEvent is injected through the InputDispatcher::injectInputEvent() API,
2740// the event should be treated as being in the logical display space.
2741TEST_F(InputDispatcherDisplayProjectionTest, InjectionInLogicalDisplaySpace) {
2742 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
2743 // Send down to the first window. The point is represented in the logical display space. The
2744 // point is selected so that if the hit test was done in logical display space, then it would
2745 // end up in the incorrect window.
2746 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2747 PointF{75 * 2, 55 * 4});
2748
2749 firstWindow->consumeMotionDown();
2750 secondWindow->assertNoEvents();
2751}
2752
Prabir Pradhandaa2f142021-12-10 09:30:08 +00002753// Ensure that when a MotionEvent that has a custom transform is injected, the post-transformed
2754// event should be treated as being in the logical display space.
2755TEST_F(InputDispatcherDisplayProjectionTest, InjectionWithTransformInLogicalDisplaySpace) {
2756 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
2757
2758 const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0};
2759 ui::Transform injectedEventTransform;
2760 injectedEventTransform.set(matrix);
2761 const vec2 expectedPoint{75, 55}; // The injected point in the logical display space.
2762 const vec2 untransformedPoint = injectedEventTransform.inverse().transform(expectedPoint);
2763
2764 MotionEvent event = MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2765 .displayId(ADISPLAY_ID_DEFAULT)
2766 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
2767 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
2768 .x(untransformedPoint.x)
2769 .y(untransformedPoint.y))
2770 .build();
2771 event.transform(matrix);
2772
2773 injectMotionEvent(mDispatcher, event, INJECT_EVENT_TIMEOUT,
2774 InputEventInjectionSync::WAIT_FOR_RESULT);
2775
2776 firstWindow->consumeMotionDown();
2777 secondWindow->assertNoEvents();
2778}
2779
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07002780TEST_F(InputDispatcherDisplayProjectionTest, WindowGetsEventsInCorrectCoordinateSpace) {
2781 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
2782
2783 // Send down to the second window.
2784 NotifyMotionArgs downMotionArgs =
2785 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2786 ADISPLAY_ID_DEFAULT, {PointF{150, 220}});
2787 mDispatcher->notifyMotion(&downMotionArgs);
2788
2789 firstWindow->assertNoEvents();
2790 const MotionEvent* event = secondWindow->consumeMotion();
2791 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, event->getAction());
2792
2793 // Ensure that the events from the "getRaw" API are in logical display coordinates.
2794 EXPECT_EQ(300, event->getRawX(0));
2795 EXPECT_EQ(880, event->getRawY(0));
2796
2797 // Ensure that the x and y values are in the window's coordinate space.
2798 // The left-top of the second window is at (100, 200) in display space, which is (200, 800) in
2799 // the logical display space. This will be the origin of the window space.
2800 EXPECT_EQ(100, event->getX(0));
2801 EXPECT_EQ(80, event->getY(0));
2802}
2803
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002804using TransferFunction = std::function<bool(const std::unique_ptr<InputDispatcher>& dispatcher,
2805 sp<IBinder>, sp<IBinder>)>;
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00002806
2807class TransferTouchFixture : public InputDispatcherTest,
2808 public ::testing::WithParamInterface<TransferFunction> {};
2809
2810TEST_P(TransferTouchFixture, TransferTouch_OnePointer) {
Chris Yea209fde2020-07-22 13:54:51 -07002811 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08002812
2813 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002814 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002815 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
2816 ADISPLAY_ID_DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00002817 firstWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002818 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002819 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
2820 ADISPLAY_ID_DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00002821 sp<FakeWindowHandle> wallpaper =
2822 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
2823 wallpaper->setIsWallpaper(true);
Svet Ganov5d3bc372020-01-26 23:11:07 -08002824 // Add the windows to the dispatcher
Arthur Hungc539dbb2022-12-08 07:45:36 +00002825 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow, wallpaper}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08002826
2827 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002828 NotifyMotionArgs downMotionArgs =
2829 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2830 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08002831 mDispatcher->notifyMotion(&downMotionArgs);
Arthur Hungc539dbb2022-12-08 07:45:36 +00002832
Svet Ganov5d3bc372020-01-26 23:11:07 -08002833 // Only the first window should get the down event
2834 firstWindow->consumeMotionDown();
2835 secondWindow->assertNoEvents();
Arthur Hungc539dbb2022-12-08 07:45:36 +00002836 wallpaper->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08002837
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00002838 // Transfer touch to the second window
2839 TransferFunction f = GetParam();
2840 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
2841 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08002842 // The first window gets cancel and the second gets down
2843 firstWindow->consumeMotionCancel();
2844 secondWindow->consumeMotionDown();
Arthur Hungc539dbb2022-12-08 07:45:36 +00002845 wallpaper->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08002846
2847 // Send up event to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002848 NotifyMotionArgs upMotionArgs =
2849 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
2850 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08002851 mDispatcher->notifyMotion(&upMotionArgs);
2852 // The first window gets no events and the second gets up
2853 firstWindow->assertNoEvents();
2854 secondWindow->consumeMotionUp();
Arthur Hungc539dbb2022-12-08 07:45:36 +00002855 wallpaper->assertNoEvents();
Svet Ganov5d3bc372020-01-26 23:11:07 -08002856}
2857
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07002858/**
2859 * When 'transferTouch' API is invoked, dispatcher needs to find the "best" window to take touch
2860 * from. When we have spy windows, there are several windows to choose from: either spy, or the
2861 * 'real' (non-spy) window. Always prefer the 'real' window because that's what would be most
2862 * natural to the user.
2863 * In this test, we are sending a pointer to both spy window and first window. We then try to
2864 * transfer touch to the second window. The dispatcher should identify the first window as the
2865 * one that should lose the gesture, and therefore the action should be to move the gesture from
2866 * the first window to the second.
2867 * The main goal here is to test the behaviour of 'transferTouch' API, but it's still valid to test
2868 * the other API, as well.
2869 */
2870TEST_P(TransferTouchFixture, TransferTouch_MultipleWindowsWithSpy) {
2871 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2872
2873 // Create a couple of windows + a spy window
2874 sp<FakeWindowHandle> spyWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002875 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07002876 spyWindow->setTrustedOverlay(true);
2877 spyWindow->setSpy(true);
2878 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002879 sp<FakeWindowHandle>::make(application, mDispatcher, "First", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07002880 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002881 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07002882
2883 // Add the windows to the dispatcher
2884 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, firstWindow, secondWindow}}});
2885
2886 // Send down to the first window
2887 NotifyMotionArgs downMotionArgs =
2888 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2889 ADISPLAY_ID_DEFAULT);
2890 mDispatcher->notifyMotion(&downMotionArgs);
2891 // Only the first window and spy should get the down event
2892 spyWindow->consumeMotionDown();
2893 firstWindow->consumeMotionDown();
2894
2895 // Transfer touch to the second window. Non-spy window should be preferred over the spy window
2896 // if f === 'transferTouch'.
2897 TransferFunction f = GetParam();
2898 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
2899 ASSERT_TRUE(success);
2900 // The first window gets cancel and the second gets down
2901 firstWindow->consumeMotionCancel();
2902 secondWindow->consumeMotionDown();
2903
2904 // Send up event to the second window
2905 NotifyMotionArgs upMotionArgs =
2906 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
2907 ADISPLAY_ID_DEFAULT);
2908 mDispatcher->notifyMotion(&upMotionArgs);
2909 // The first window gets no events and the second+spy get up
2910 firstWindow->assertNoEvents();
2911 spyWindow->consumeMotionUp();
2912 secondWindow->consumeMotionUp();
2913}
2914
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00002915TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07002916 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08002917
2918 PointF touchPoint = {10, 10};
2919
2920 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002921 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002922 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
2923 ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08002924 firstWindow->setPreventSplitting(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002925 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002926 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
2927 ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08002928 secondWindow->setPreventSplitting(true);
Svet Ganov5d3bc372020-01-26 23:11:07 -08002929
2930 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00002931 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08002932
2933 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002934 NotifyMotionArgs downMotionArgs =
2935 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2936 ADISPLAY_ID_DEFAULT, {touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08002937 mDispatcher->notifyMotion(&downMotionArgs);
2938 // Only the first window should get the down event
2939 firstWindow->consumeMotionDown();
2940 secondWindow->assertNoEvents();
2941
2942 // Send pointer down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002943 NotifyMotionArgs pointerDownMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002944 generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002945 {touchPoint, touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08002946 mDispatcher->notifyMotion(&pointerDownMotionArgs);
2947 // Only the first window should get the pointer down event
2948 firstWindow->consumeMotionPointerDown(1);
2949 secondWindow->assertNoEvents();
2950
2951 // Transfer touch focus to the second window
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00002952 TransferFunction f = GetParam();
2953 bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
2954 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08002955 // The first window gets cancel and the second gets down and pointer down
2956 firstWindow->consumeMotionCancel();
2957 secondWindow->consumeMotionDown();
2958 secondWindow->consumeMotionPointerDown(1);
2959
2960 // Send pointer up to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002961 NotifyMotionArgs pointerUpMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002962 generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002963 {touchPoint, touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08002964 mDispatcher->notifyMotion(&pointerUpMotionArgs);
2965 // The first window gets nothing and the second gets pointer up
2966 firstWindow->assertNoEvents();
2967 secondWindow->consumeMotionPointerUp(1);
2968
2969 // Send up event to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002970 NotifyMotionArgs upMotionArgs =
2971 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
2972 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08002973 mDispatcher->notifyMotion(&upMotionArgs);
2974 // The first window gets nothing and the second gets up
2975 firstWindow->assertNoEvents();
2976 secondWindow->consumeMotionUp();
2977}
2978
Arthur Hungc539dbb2022-12-08 07:45:36 +00002979TEST_P(TransferTouchFixture, TransferTouch_MultipleWallpapers) {
2980 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2981
2982 // Create a couple of windows
2983 sp<FakeWindowHandle> firstWindow =
2984 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
2985 ADISPLAY_ID_DEFAULT);
2986 firstWindow->setDupTouchToWallpaper(true);
2987 sp<FakeWindowHandle> secondWindow =
2988 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
2989 ADISPLAY_ID_DEFAULT);
2990 secondWindow->setDupTouchToWallpaper(true);
2991
2992 sp<FakeWindowHandle> wallpaper1 =
2993 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper1", ADISPLAY_ID_DEFAULT);
2994 wallpaper1->setIsWallpaper(true);
2995
2996 sp<FakeWindowHandle> wallpaper2 =
2997 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper2", ADISPLAY_ID_DEFAULT);
2998 wallpaper2->setIsWallpaper(true);
2999 // Add the windows to the dispatcher
3000 mDispatcher->setInputWindows(
3001 {{ADISPLAY_ID_DEFAULT, {firstWindow, wallpaper1, secondWindow, wallpaper2}}});
3002
3003 // Send down to the first window
3004 NotifyMotionArgs downMotionArgs =
3005 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3006 ADISPLAY_ID_DEFAULT);
3007 mDispatcher->notifyMotion(&downMotionArgs);
3008
3009 // Only the first window should get the down event
3010 firstWindow->consumeMotionDown();
3011 secondWindow->assertNoEvents();
3012 wallpaper1->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
3013 wallpaper2->assertNoEvents();
3014
3015 // Transfer touch focus to the second window
3016 TransferFunction f = GetParam();
3017 bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
3018 ASSERT_TRUE(success);
3019
3020 // The first window gets cancel and the second gets down
3021 firstWindow->consumeMotionCancel();
3022 secondWindow->consumeMotionDown();
3023 wallpaper1->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
3024 wallpaper2->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
3025
3026 // Send up event to the second window
3027 NotifyMotionArgs upMotionArgs =
3028 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3029 ADISPLAY_ID_DEFAULT);
3030 mDispatcher->notifyMotion(&upMotionArgs);
3031 // The first window gets no events and the second gets up
3032 firstWindow->assertNoEvents();
3033 secondWindow->consumeMotionUp();
3034 wallpaper1->assertNoEvents();
3035 wallpaper2->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
3036}
3037
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003038// For the cases of single pointer touch and two pointers non-split touch, the api's
3039// 'transferTouch' and 'transferTouchFocus' are equivalent in behaviour. They only differ
3040// for the case where there are multiple pointers split across several windows.
3041INSTANTIATE_TEST_SUITE_P(TransferFunctionTests, TransferTouchFixture,
3042 ::testing::Values(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003043 [&](const std::unique_ptr<InputDispatcher>& dispatcher,
3044 sp<IBinder> /*ignored*/, sp<IBinder> destChannelToken) {
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07003045 return dispatcher->transferTouch(destChannelToken,
3046 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003047 },
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003048 [&](const std::unique_ptr<InputDispatcher>& dispatcher,
3049 sp<IBinder> from, sp<IBinder> to) {
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003050 return dispatcher->transferTouchFocus(from, to,
3051 false /*isDragAndDrop*/);
3052 }));
3053
Svet Ganov5d3bc372020-01-26 23:11:07 -08003054TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07003055 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08003056
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003057 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003058 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
3059 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08003060 firstWindow->setFrame(Rect(0, 0, 600, 400));
Svet Ganov5d3bc372020-01-26 23:11:07 -08003061
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003062 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003063 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3064 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08003065 secondWindow->setFrame(Rect(0, 400, 600, 800));
Svet Ganov5d3bc372020-01-26 23:11:07 -08003066
3067 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00003068 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08003069
3070 PointF pointInFirst = {300, 200};
3071 PointF pointInSecond = {300, 600};
3072
3073 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003074 NotifyMotionArgs firstDownMotionArgs =
3075 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3076 ADISPLAY_ID_DEFAULT, {pointInFirst});
Svet Ganov5d3bc372020-01-26 23:11:07 -08003077 mDispatcher->notifyMotion(&firstDownMotionArgs);
3078 // Only the first window should get the down event
3079 firstWindow->consumeMotionDown();
3080 secondWindow->assertNoEvents();
3081
3082 // Send down to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003083 NotifyMotionArgs secondDownMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003084 generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003085 {pointInFirst, pointInSecond});
Svet Ganov5d3bc372020-01-26 23:11:07 -08003086 mDispatcher->notifyMotion(&secondDownMotionArgs);
3087 // The first window gets a move and the second a down
3088 firstWindow->consumeMotionMove();
3089 secondWindow->consumeMotionDown();
3090
3091 // Transfer touch focus to the second window
3092 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
3093 // The first window gets cancel and the new gets pointer down (it already saw down)
3094 firstWindow->consumeMotionCancel();
3095 secondWindow->consumeMotionPointerDown(1);
3096
3097 // Send pointer up to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003098 NotifyMotionArgs pointerUpMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003099 generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003100 {pointInFirst, pointInSecond});
Svet Ganov5d3bc372020-01-26 23:11:07 -08003101 mDispatcher->notifyMotion(&pointerUpMotionArgs);
3102 // The first window gets nothing and the second gets pointer up
3103 firstWindow->assertNoEvents();
3104 secondWindow->consumeMotionPointerUp(1);
3105
3106 // Send up event to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003107 NotifyMotionArgs upMotionArgs =
3108 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3109 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08003110 mDispatcher->notifyMotion(&upMotionArgs);
3111 // The first window gets nothing and the second gets up
3112 firstWindow->assertNoEvents();
3113 secondWindow->consumeMotionUp();
3114}
3115
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003116// Same as TransferTouchFocus_TwoPointersSplitTouch, but using 'transferTouch' api.
3117// Unlike 'transferTouchFocus', calling 'transferTouch' when there are two windows receiving
3118// touch is not supported, so the touch should continue on those windows and the transferred-to
3119// window should get nothing.
3120TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) {
3121 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3122
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003123 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003124 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
3125 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003126 firstWindow->setFrame(Rect(0, 0, 600, 400));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003127
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003128 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003129 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3130 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003131 secondWindow->setFrame(Rect(0, 400, 600, 800));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003132
3133 // Add the windows to the dispatcher
3134 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
3135
3136 PointF pointInFirst = {300, 200};
3137 PointF pointInSecond = {300, 600};
3138
3139 // Send down to the first window
3140 NotifyMotionArgs firstDownMotionArgs =
3141 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3142 ADISPLAY_ID_DEFAULT, {pointInFirst});
3143 mDispatcher->notifyMotion(&firstDownMotionArgs);
3144 // Only the first window should get the down event
3145 firstWindow->consumeMotionDown();
3146 secondWindow->assertNoEvents();
3147
3148 // Send down to the second window
3149 NotifyMotionArgs secondDownMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003150 generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003151 {pointInFirst, pointInSecond});
3152 mDispatcher->notifyMotion(&secondDownMotionArgs);
3153 // The first window gets a move and the second a down
3154 firstWindow->consumeMotionMove();
3155 secondWindow->consumeMotionDown();
3156
3157 // Transfer touch focus to the second window
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07003158 const bool transferred =
3159 mDispatcher->transferTouch(secondWindow->getToken(), ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003160 // The 'transferTouch' call should not succeed, because there are 2 touched windows
3161 ASSERT_FALSE(transferred);
3162 firstWindow->assertNoEvents();
3163 secondWindow->assertNoEvents();
3164
3165 // The rest of the dispatch should proceed as normal
3166 // Send pointer up to the second window
3167 NotifyMotionArgs pointerUpMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003168 generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003169 {pointInFirst, pointInSecond});
3170 mDispatcher->notifyMotion(&pointerUpMotionArgs);
3171 // The first window gets MOVE and the second gets pointer up
3172 firstWindow->consumeMotionMove();
3173 secondWindow->consumeMotionUp();
3174
3175 // Send up event to the first window
3176 NotifyMotionArgs upMotionArgs =
3177 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3178 ADISPLAY_ID_DEFAULT);
3179 mDispatcher->notifyMotion(&upMotionArgs);
3180 // The first window gets nothing and the second gets up
3181 firstWindow->consumeMotionUp();
3182 secondWindow->assertNoEvents();
3183}
3184
Arthur Hungabbb9d82021-09-01 14:52:30 +00003185// This case will create two windows and one mirrored window on the default display and mirror
3186// two windows on the second display. It will test if 'transferTouchFocus' works fine if we put
3187// the windows info of second display before default display.
3188TEST_F(InputDispatcherTest, TransferTouchFocus_CloneSurface) {
3189 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3190 sp<FakeWindowHandle> firstWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003191 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00003192 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003193 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003194 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00003195 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003196
3197 sp<FakeWindowHandle> mirrorWindowInPrimary =
3198 firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT);
3199 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003200
3201 sp<FakeWindowHandle> firstWindowInSecondary =
3202 firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
3203 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003204
3205 sp<FakeWindowHandle> secondWindowInSecondary =
3206 secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
3207 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003208
3209 // Update window info, let it find window handle of second display first.
3210 mDispatcher->setInputWindows(
3211 {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}},
3212 {ADISPLAY_ID_DEFAULT,
3213 {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}});
3214
3215 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3216 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3217 {50, 50}))
3218 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3219
3220 // Window should receive motion event.
3221 firstWindowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3222
3223 // Transfer touch focus
3224 ASSERT_TRUE(mDispatcher->transferTouchFocus(firstWindowInPrimary->getToken(),
3225 secondWindowInPrimary->getToken()));
3226 // The first window gets cancel.
3227 firstWindowInPrimary->consumeMotionCancel();
3228 secondWindowInPrimary->consumeMotionDown();
3229
3230 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3231 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
3232 ADISPLAY_ID_DEFAULT, {150, 50}))
3233 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3234 firstWindowInPrimary->assertNoEvents();
3235 secondWindowInPrimary->consumeMotionMove();
3236
3237 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3238 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3239 {150, 50}))
3240 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3241 firstWindowInPrimary->assertNoEvents();
3242 secondWindowInPrimary->consumeMotionUp();
3243}
3244
3245// Same as TransferTouchFocus_CloneSurface, but this touch on the secondary display and use
3246// 'transferTouch' api.
3247TEST_F(InputDispatcherTest, TransferTouch_CloneSurface) {
3248 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3249 sp<FakeWindowHandle> firstWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003250 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00003251 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003252 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003253 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00003254 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003255
3256 sp<FakeWindowHandle> mirrorWindowInPrimary =
3257 firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT);
3258 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003259
3260 sp<FakeWindowHandle> firstWindowInSecondary =
3261 firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
3262 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003263
3264 sp<FakeWindowHandle> secondWindowInSecondary =
3265 secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
3266 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003267
3268 // Update window info, let it find window handle of second display first.
3269 mDispatcher->setInputWindows(
3270 {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}},
3271 {ADISPLAY_ID_DEFAULT,
3272 {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}});
3273
3274 // Touch on second display.
3275 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3276 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {50, 50}))
3277 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3278
3279 // Window should receive motion event.
3280 firstWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID);
3281
3282 // Transfer touch focus
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07003283 ASSERT_TRUE(mDispatcher->transferTouch(secondWindowInSecondary->getToken(), SECOND_DISPLAY_ID));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003284
3285 // The first window gets cancel.
3286 firstWindowInPrimary->consumeMotionCancel(SECOND_DISPLAY_ID);
3287 secondWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID);
3288
3289 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3290 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
3291 SECOND_DISPLAY_ID, {150, 50}))
3292 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3293 firstWindowInPrimary->assertNoEvents();
3294 secondWindowInPrimary->consumeMotionMove(SECOND_DISPLAY_ID);
3295
3296 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3297 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {150, 50}))
3298 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3299 firstWindowInPrimary->assertNoEvents();
3300 secondWindowInPrimary->consumeMotionUp(SECOND_DISPLAY_ID);
3301}
3302
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003303TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07003304 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003305 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3306 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003307
Vishnu Nair47074b82020-08-14 11:54:47 -07003308 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00003309 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003310 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003311
3312 window->consumeFocusEvent(true);
3313
3314 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
3315 mDispatcher->notifyKey(&keyArgs);
3316
3317 // Window should receive key down event.
3318 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3319}
3320
3321TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07003322 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003323 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3324 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003325
Arthur Hung72d8dc32020-03-28 00:48:39 +00003326 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003327
3328 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
3329 mDispatcher->notifyKey(&keyArgs);
3330 mDispatcher->waitForIdle();
3331
3332 window->assertNoEvents();
3333}
3334
3335// If a window is touchable, but does not have focus, it should receive motion events, but not keys
3336TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) {
Chris Yea209fde2020-07-22 13:54:51 -07003337 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003338 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3339 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003340
Arthur Hung72d8dc32020-03-28 00:48:39 +00003341 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003342
3343 // Send key
3344 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
3345 mDispatcher->notifyKey(&keyArgs);
3346 // Send motion
3347 NotifyMotionArgs motionArgs =
3348 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3349 ADISPLAY_ID_DEFAULT);
3350 mDispatcher->notifyMotion(&motionArgs);
3351
3352 // Window should receive only the motion event
3353 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3354 window->assertNoEvents(); // Key event or focus event will not be received
3355}
3356
arthurhungea3f4fc2020-12-21 23:18:53 +08003357TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) {
3358 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3359
arthurhungea3f4fc2020-12-21 23:18:53 +08003360 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003361 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
3362 ADISPLAY_ID_DEFAULT);
arthurhungea3f4fc2020-12-21 23:18:53 +08003363 firstWindow->setFrame(Rect(0, 0, 600, 400));
arthurhungea3f4fc2020-12-21 23:18:53 +08003364
arthurhungea3f4fc2020-12-21 23:18:53 +08003365 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003366 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3367 ADISPLAY_ID_DEFAULT);
arthurhungea3f4fc2020-12-21 23:18:53 +08003368 secondWindow->setFrame(Rect(0, 400, 600, 800));
arthurhungea3f4fc2020-12-21 23:18:53 +08003369
3370 // Add the windows to the dispatcher
3371 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
3372
3373 PointF pointInFirst = {300, 200};
3374 PointF pointInSecond = {300, 600};
3375
3376 // Send down to the first window
3377 NotifyMotionArgs firstDownMotionArgs =
3378 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3379 ADISPLAY_ID_DEFAULT, {pointInFirst});
3380 mDispatcher->notifyMotion(&firstDownMotionArgs);
3381 // Only the first window should get the down event
3382 firstWindow->consumeMotionDown();
3383 secondWindow->assertNoEvents();
3384
3385 // Send down to the second window
3386 NotifyMotionArgs secondDownMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003387 generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
arthurhungea3f4fc2020-12-21 23:18:53 +08003388 {pointInFirst, pointInSecond});
3389 mDispatcher->notifyMotion(&secondDownMotionArgs);
3390 // The first window gets a move and the second a down
3391 firstWindow->consumeMotionMove();
3392 secondWindow->consumeMotionDown();
3393
3394 // Send pointer cancel to the second window
3395 NotifyMotionArgs pointerUpMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003396 generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
arthurhungea3f4fc2020-12-21 23:18:53 +08003397 {pointInFirst, pointInSecond});
3398 pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED;
3399 mDispatcher->notifyMotion(&pointerUpMotionArgs);
3400 // The first window gets move and the second gets cancel.
3401 firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
3402 secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
3403
3404 // Send up event.
3405 NotifyMotionArgs upMotionArgs =
3406 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3407 ADISPLAY_ID_DEFAULT);
3408 mDispatcher->notifyMotion(&upMotionArgs);
3409 // The first window gets up and the second gets nothing.
3410 firstWindow->consumeMotionUp();
3411 secondWindow->assertNoEvents();
3412}
3413
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00003414TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) {
3415 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3416
3417 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003418 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00003419 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3420 std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline;
3421 graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2;
3422 graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3;
3423
3424 window->sendTimeline(1 /*inputEventId*/, graphicsTimeline);
3425 window->assertNoEvents();
3426 mDispatcher->waitForIdle();
3427}
3428
chaviwd1c23182019-12-20 18:44:56 -08003429class FakeMonitorReceiver {
Michael Wright3a240c42019-12-10 20:53:41 +00003430public:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003431 FakeMonitorReceiver(const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003432 int32_t displayId) {
Garfield Tan15601662020-09-22 15:32:38 -07003433 base::Result<std::unique_ptr<InputChannel>> channel =
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08003434 dispatcher->createInputMonitor(displayId, name, MONITOR_PID);
Garfield Tan15601662020-09-22 15:32:38 -07003435 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
Michael Wright3a240c42019-12-10 20:53:41 +00003436 }
3437
chaviwd1c23182019-12-20 18:44:56 -08003438 sp<IBinder> getToken() { return mInputReceiver->getToken(); }
3439
3440 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
3441 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN,
3442 expectedDisplayId, expectedFlags);
3443 }
3444
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003445 std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); }
3446
3447 void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); }
3448
chaviwd1c23182019-12-20 18:44:56 -08003449 void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
3450 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN,
3451 expectedDisplayId, expectedFlags);
3452 }
3453
Siarhei Vishniakouca205502021-07-16 21:31:58 +00003454 void consumeMotionMove(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
3455 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE,
3456 expectedDisplayId, expectedFlags);
3457 }
3458
chaviwd1c23182019-12-20 18:44:56 -08003459 void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
3460 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP,
3461 expectedDisplayId, expectedFlags);
3462 }
3463
Siarhei Vishniakouca205502021-07-16 21:31:58 +00003464 void consumeMotionCancel(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
3465 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL,
3466 expectedDisplayId, expectedFlags);
3467 }
3468
Arthur Hungfbfa5722021-11-16 02:45:54 +00003469 void consumeMotionPointerDown(int32_t pointerIdx) {
3470 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
3471 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
3472 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, ADISPLAY_ID_DEFAULT,
3473 0 /*expectedFlags*/);
3474 }
3475
Evan Rosky84f07f02021-04-16 10:42:42 -07003476 MotionEvent* consumeMotion() {
3477 InputEvent* event = mInputReceiver->consume();
3478 if (!event) {
3479 ADD_FAILURE() << "No event was produced";
3480 return nullptr;
3481 }
3482 if (event->getType() != AINPUT_EVENT_TYPE_MOTION) {
3483 ADD_FAILURE() << "Received event of type " << event->getType() << " instead of motion";
3484 return nullptr;
3485 }
3486 return static_cast<MotionEvent*>(event);
3487 }
3488
chaviwd1c23182019-12-20 18:44:56 -08003489 void assertNoEvents() { mInputReceiver->assertNoEvents(); }
3490
3491private:
3492 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Michael Wright3a240c42019-12-10 20:53:41 +00003493};
3494
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003495using InputDispatcherMonitorTest = InputDispatcherTest;
3496
Siarhei Vishniakouca205502021-07-16 21:31:58 +00003497/**
3498 * Two entities that receive touch: A window, and a global monitor.
3499 * The touch goes to the window, and then the window disappears.
3500 * The monitor does not get cancel right away. But if more events come in, the touch gets canceled
3501 * for the monitor, as well.
3502 * 1. foregroundWindow
3503 * 2. monitor <-- global monitor (doesn't observe z order, receives all events)
3504 */
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003505TEST_F(InputDispatcherMonitorTest, MonitorTouchIsCanceledWhenForegroundWindowDisappears) {
Siarhei Vishniakouca205502021-07-16 21:31:58 +00003506 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3507 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003508 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00003509
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003510 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00003511
3512 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3513 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3514 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3515 {100, 200}))
3516 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3517
3518 // Both the foreground window and the global monitor should receive the touch down
3519 window->consumeMotionDown();
3520 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
3521
3522 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3523 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
3524 ADISPLAY_ID_DEFAULT, {110, 200}))
3525 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3526
3527 window->consumeMotionMove();
3528 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
3529
3530 // Now the foreground window goes away
3531 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
3532 window->consumeMotionCancel();
3533 monitor.assertNoEvents(); // Global monitor does not get a cancel yet
3534
3535 // If more events come in, there will be no more foreground window to send them to. This will
3536 // cause a cancel for the monitor, as well.
3537 ASSERT_EQ(InputEventInjectionResult::FAILED,
3538 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
3539 ADISPLAY_ID_DEFAULT, {120, 200}))
3540 << "Injection should fail because the window was removed";
3541 window->assertNoEvents();
3542 // Global monitor now gets the cancel
3543 monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
3544}
3545
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003546TEST_F(InputDispatcherMonitorTest, ReceivesMotionEvents) {
Chris Yea209fde2020-07-22 13:54:51 -07003547 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003548 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3549 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00003550 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00003551
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003552 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00003553
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003554 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00003555 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003556 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00003557 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08003558 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00003559}
3560
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003561TEST_F(InputDispatcherMonitorTest, MonitorCannotPilferPointers) {
3562 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00003563
Chris Yea209fde2020-07-22 13:54:51 -07003564 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003565 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3566 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00003567 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00003568
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003569 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00003570 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003571 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
chaviwd1c23182019-12-20 18:44:56 -08003572 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003573 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00003574
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003575 // Pilfer pointers from the monitor.
3576 // This should not do anything and the window should continue to receive events.
3577 EXPECT_NE(OK, mDispatcher->pilferPointers(monitor.getToken()));
Michael Wright3a240c42019-12-10 20:53:41 +00003578
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003579 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003580 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
3581 ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003582 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003583
3584 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
3585 window->consumeMotionMove(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00003586}
3587
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003588TEST_F(InputDispatcherMonitorTest, NoWindowTransform) {
Evan Rosky84f07f02021-04-16 10:42:42 -07003589 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003590 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3591 "Fake Window", ADISPLAY_ID_DEFAULT);
Evan Rosky84f07f02021-04-16 10:42:42 -07003592 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3593 window->setWindowOffset(20, 40);
3594 window->setWindowTransform(0, 1, -1, 0);
3595
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003596 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Evan Rosky84f07f02021-04-16 10:42:42 -07003597
3598 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3599 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
3600 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3601 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3602 MotionEvent* event = monitor.consumeMotion();
3603 // Even though window has transform, gesture monitor must not.
3604 ASSERT_EQ(ui::Transform(), event->getTransform());
3605}
3606
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003607TEST_F(InputDispatcherMonitorTest, InjectionFailsWithNoWindow) {
Arthur Hungb3307ee2021-10-14 10:57:37 +00003608 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003609 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Arthur Hungb3307ee2021-10-14 10:57:37 +00003610
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003611 ASSERT_EQ(InputEventInjectionResult::FAILED,
Arthur Hungb3307ee2021-10-14 10:57:37 +00003612 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003613 << "Injection should fail if there is a monitor, but no touchable window";
3614 monitor.assertNoEvents();
Arthur Hungb3307ee2021-10-14 10:57:37 +00003615}
3616
chaviw81e2bb92019-12-18 15:03:51 -08003617TEST_F(InputDispatcherTest, TestMoveEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07003618 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003619 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3620 "Fake Window", ADISPLAY_ID_DEFAULT);
chaviw81e2bb92019-12-18 15:03:51 -08003621
Arthur Hung72d8dc32020-03-28 00:48:39 +00003622 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
chaviw81e2bb92019-12-18 15:03:51 -08003623
3624 NotifyMotionArgs motionArgs =
3625 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3626 ADISPLAY_ID_DEFAULT);
3627
3628 mDispatcher->notifyMotion(&motionArgs);
3629 // Window should receive motion down event.
3630 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3631
3632 motionArgs.action = AMOTION_EVENT_ACTION_MOVE;
Garfield Tanc51d1ba2020-01-28 13:24:04 -08003633 motionArgs.id += 1;
chaviw81e2bb92019-12-18 15:03:51 -08003634 motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
3635 motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
3636 motionArgs.pointerCoords[0].getX() - 10);
3637
3638 mDispatcher->notifyMotion(&motionArgs);
3639 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT,
3640 0 /*expectedFlags*/);
3641}
3642
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003643/**
3644 * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to
3645 * the device default right away. In the test scenario, we check both the default value,
3646 * and the action of enabling / disabling.
3647 */
3648TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) {
Chris Yea209fde2020-07-22 13:54:51 -07003649 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003650 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3651 "Test window", ADISPLAY_ID_DEFAULT);
Antonio Kantekea47acb2021-12-23 12:41:25 -08003652 const WindowInfo& windowInfo = *window->getInfo();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003653
3654 // Set focused application.
3655 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07003656 window->setFocusable(true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003657
3658 SCOPED_TRACE("Check default value of touch mode");
Arthur Hung72d8dc32020-03-28 00:48:39 +00003659 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003660 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003661 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
3662
3663 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07003664 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00003665 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003666 window->consumeFocusEvent(false /*hasFocus*/, true /*inTouchMode*/);
3667
3668 SCOPED_TRACE("Disable touch mode");
Antonio Kantekea47acb2021-12-23 12:41:25 -08003669 mDispatcher->setInTouchMode(false, windowInfo.ownerPid, windowInfo.ownerUid,
Antonio Kanteka042c022022-07-06 16:51:07 -07003670 true /*hasPermission*/, ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00003671 window->consumeTouchModeEvent(false);
Vishnu Nair47074b82020-08-14 11:54:47 -07003672 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00003673 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003674 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003675 window->consumeFocusEvent(true /*hasFocus*/, false /*inTouchMode*/);
3676
3677 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07003678 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00003679 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003680 window->consumeFocusEvent(false /*hasFocus*/, false /*inTouchMode*/);
3681
3682 SCOPED_TRACE("Enable touch mode again");
Antonio Kantekea47acb2021-12-23 12:41:25 -08003683 mDispatcher->setInTouchMode(true, windowInfo.ownerPid, windowInfo.ownerUid,
Antonio Kanteka042c022022-07-06 16:51:07 -07003684 true /*hasPermission*/, ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00003685 window->consumeTouchModeEvent(true);
Vishnu Nair47074b82020-08-14 11:54:47 -07003686 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00003687 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003688 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003689 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
3690
3691 window->assertNoEvents();
3692}
3693
Gang Wange9087892020-01-07 12:17:14 -05003694TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07003695 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003696 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3697 "Test window", ADISPLAY_ID_DEFAULT);
Gang Wange9087892020-01-07 12:17:14 -05003698
3699 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07003700 window->setFocusable(true);
Gang Wange9087892020-01-07 12:17:14 -05003701
Arthur Hung72d8dc32020-03-28 00:48:39 +00003702 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003703 setFocusedWindow(window);
3704
Gang Wange9087892020-01-07 12:17:14 -05003705 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
3706
3707 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
3708 mDispatcher->notifyKey(&keyArgs);
3709
3710 InputEvent* event = window->consume();
3711 ASSERT_NE(event, nullptr);
3712
3713 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
3714 ASSERT_NE(verified, nullptr);
3715 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY);
3716
3717 ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos);
3718 ASSERT_EQ(keyArgs.deviceId, verified->deviceId);
3719 ASSERT_EQ(keyArgs.source, verified->source);
3720 ASSERT_EQ(keyArgs.displayId, verified->displayId);
3721
3722 const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified);
3723
3724 ASSERT_EQ(keyArgs.action, verifiedKey.action);
Gang Wange9087892020-01-07 12:17:14 -05003725 ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08003726 ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos);
Gang Wange9087892020-01-07 12:17:14 -05003727 ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode);
3728 ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode);
3729 ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState);
3730 ASSERT_EQ(0, verifiedKey.repeatCount);
3731}
3732
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08003733TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07003734 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003735 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3736 "Test window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08003737
3738 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3739
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07003740 ui::Transform transform;
3741 transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
3742
3743 gui::DisplayInfo displayInfo;
3744 displayInfo.displayId = ADISPLAY_ID_DEFAULT;
3745 displayInfo.transform = transform;
3746
3747 mDispatcher->onWindowInfosChanged({*window->getInfo()}, {displayInfo});
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08003748
3749 NotifyMotionArgs motionArgs =
3750 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3751 ADISPLAY_ID_DEFAULT);
3752 mDispatcher->notifyMotion(&motionArgs);
3753
3754 InputEvent* event = window->consume();
3755 ASSERT_NE(event, nullptr);
3756
3757 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
3758 ASSERT_NE(verified, nullptr);
3759 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION);
3760
3761 EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos);
3762 EXPECT_EQ(motionArgs.deviceId, verified->deviceId);
3763 EXPECT_EQ(motionArgs.source, verified->source);
3764 EXPECT_EQ(motionArgs.displayId, verified->displayId);
3765
3766 const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified);
3767
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07003768 const vec2 rawXY =
3769 MotionEvent::calculateTransformedXY(motionArgs.source, transform,
3770 motionArgs.pointerCoords[0].getXYValue());
3771 EXPECT_EQ(rawXY.x, verifiedMotion.rawX);
3772 EXPECT_EQ(rawXY.y, verifiedMotion.rawY);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08003773 EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08003774 EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08003775 EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08003776 EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState);
3777 EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState);
3778}
3779
chaviw09c8d2d2020-08-24 15:48:26 -07003780/**
3781 * Ensure that separate calls to sign the same data are generating the same key.
3782 * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance
3783 * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky
3784 * tests.
3785 */
3786TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) {
3787 KeyEvent event = getTestKeyEvent();
3788 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
3789
3790 std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent);
3791 std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent);
3792 ASSERT_EQ(hmac1, hmac2);
3793}
3794
3795/**
3796 * Ensure that changes in VerifiedKeyEvent produce a different hmac.
3797 */
3798TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) {
3799 KeyEvent event = getTestKeyEvent();
3800 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
3801 std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent);
3802
3803 verifiedEvent.deviceId += 1;
3804 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
3805
3806 verifiedEvent.source += 1;
3807 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
3808
3809 verifiedEvent.eventTimeNanos += 1;
3810 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
3811
3812 verifiedEvent.displayId += 1;
3813 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
3814
3815 verifiedEvent.action += 1;
3816 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
3817
3818 verifiedEvent.downTimeNanos += 1;
3819 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
3820
3821 verifiedEvent.flags += 1;
3822 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
3823
3824 verifiedEvent.keyCode += 1;
3825 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
3826
3827 verifiedEvent.scanCode += 1;
3828 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
3829
3830 verifiedEvent.metaState += 1;
3831 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
3832
3833 verifiedEvent.repeatCount += 1;
3834 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
3835}
3836
Vishnu Nair958da932020-08-21 17:12:37 -07003837TEST_F(InputDispatcherTest, SetFocusedWindow) {
3838 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3839 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003840 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07003841 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003842 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07003843 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3844
3845 // Top window is also focusable but is not granted focus.
3846 windowTop->setFocusable(true);
3847 windowSecond->setFocusable(true);
3848 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
3849 setFocusedWindow(windowSecond);
3850
3851 windowSecond->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003852 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
3853 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07003854
3855 // Focused window should receive event.
3856 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
3857 windowTop->assertNoEvents();
3858}
3859
3860TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) {
3861 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3862 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003863 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07003864 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3865
3866 window->setFocusable(true);
3867 // Release channel for window is no longer valid.
3868 window->releaseChannel();
3869 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3870 setFocusedWindow(window);
3871
3872 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003873 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
3874 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07003875
3876 // window channel is invalid, so it should not receive any input event.
3877 window->assertNoEvents();
3878}
3879
3880TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) {
3881 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3882 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003883 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08003884 window->setFocusable(false);
Vishnu Nair958da932020-08-21 17:12:37 -07003885 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3886
Vishnu Nair958da932020-08-21 17:12:37 -07003887 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3888 setFocusedWindow(window);
3889
3890 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003891 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
3892 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07003893
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08003894 // window is not focusable, so it should not receive any input event.
Vishnu Nair958da932020-08-21 17:12:37 -07003895 window->assertNoEvents();
3896}
3897
3898TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) {
3899 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3900 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003901 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07003902 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003903 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07003904 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3905
3906 windowTop->setFocusable(true);
3907 windowSecond->setFocusable(true);
3908 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
3909 setFocusedWindow(windowTop);
3910 windowTop->consumeFocusEvent(true);
3911
3912 setFocusedWindow(windowSecond, windowTop);
3913 windowSecond->consumeFocusEvent(true);
3914 windowTop->consumeFocusEvent(false);
3915
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003916 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
3917 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07003918
3919 // Focused window should receive event.
3920 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
3921}
3922
3923TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestFocusTokenNotFocused) {
3924 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3925 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003926 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07003927 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003928 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07003929 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3930
3931 windowTop->setFocusable(true);
3932 windowSecond->setFocusable(true);
3933 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
3934 setFocusedWindow(windowSecond, windowTop);
3935
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003936 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
3937 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07003938
3939 // Event should be dropped.
3940 windowTop->assertNoEvents();
3941 windowSecond->assertNoEvents();
3942}
3943
3944TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) {
3945 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3946 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003947 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07003948 sp<FakeWindowHandle> previousFocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003949 sp<FakeWindowHandle>::make(application, mDispatcher, "previousFocusedWindow",
3950 ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07003951 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3952
3953 window->setFocusable(true);
3954 previousFocusedWindow->setFocusable(true);
3955 window->setVisible(false);
3956 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}});
3957 setFocusedWindow(previousFocusedWindow);
3958 previousFocusedWindow->consumeFocusEvent(true);
3959
3960 // Requesting focus on invisible window takes focus from currently focused window.
3961 setFocusedWindow(window);
3962 previousFocusedWindow->consumeFocusEvent(false);
3963
3964 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003965 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Vishnu Nair958da932020-08-21 17:12:37 -07003966 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003967 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07003968
3969 // Window does not get focus event or key down.
3970 window->assertNoEvents();
3971
3972 // Window becomes visible.
3973 window->setVisible(true);
3974 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3975
3976 // Window receives focus event.
3977 window->consumeFocusEvent(true);
3978 // Focused window receives key down.
3979 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3980}
3981
Vishnu Nair599f1412021-06-21 10:39:58 -07003982TEST_F(InputDispatcherTest, DisplayRemoved) {
3983 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3984 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003985 sp<FakeWindowHandle>::make(application, mDispatcher, "window", ADISPLAY_ID_DEFAULT);
Vishnu Nair599f1412021-06-21 10:39:58 -07003986 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3987
3988 // window is granted focus.
3989 window->setFocusable(true);
3990 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3991 setFocusedWindow(window);
3992 window->consumeFocusEvent(true);
3993
3994 // When a display is removed window loses focus.
3995 mDispatcher->displayRemoved(ADISPLAY_ID_DEFAULT);
3996 window->consumeFocusEvent(false);
3997}
3998
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003999/**
4000 * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY,
4001 * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top
4002 * of the 'slipperyEnterWindow'.
4003 *
4004 * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such
4005 * a way so that the touched location is no longer covered by the top window.
4006 *
4007 * Next, inject a MOVE event. Because the top window already moved earlier, this event is now
4008 * positioned over the bottom (slipperyEnterWindow) only. And because the top window had
4009 * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive
4010 * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting
4011 * with ACTION_DOWN).
4012 * Thus, the touch has been transferred from the top window into the bottom window, because the top
4013 * window moved itself away from the touched location and had Flag::SLIPPERY.
4014 *
4015 * Even though the top window moved away from the touched location, it is still obscuring the bottom
4016 * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_
4017 * OBSCURED should be set for the MotionEvent that reaches the bottom window.
4018 *
4019 * In this test, we ensure that the event received by the bottom window has
4020 * FLAG_WINDOW_IS_PARTIALLY_OBSCURED.
4021 */
4022TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) {
Prabir Pradhan5735a322022-04-11 17:23:34 +00004023 constexpr int32_t SLIPPERY_PID = WINDOW_PID + 1;
4024 constexpr int32_t SLIPPERY_UID = WINDOW_UID + 1;
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004025
4026 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4027 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
4028
4029 sp<FakeWindowHandle> slipperyExitWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004030 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08004031 slipperyExitWindow->setSlippery(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004032 // Make sure this one overlaps the bottom window
4033 slipperyExitWindow->setFrame(Rect(25, 25, 75, 75));
4034 // Change the owner uid/pid of the window so that it is considered to be occluding the bottom
4035 // one. Windows with the same owner are not considered to be occluding each other.
4036 slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID);
4037
4038 sp<FakeWindowHandle> slipperyEnterWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004039 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004040 slipperyExitWindow->setFrame(Rect(0, 0, 100, 100));
4041
4042 mDispatcher->setInputWindows(
4043 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
4044
4045 // Use notifyMotion instead of injecting to avoid dealing with injection permissions
4046 NotifyMotionArgs args = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4047 ADISPLAY_ID_DEFAULT, {{50, 50}});
4048 mDispatcher->notifyMotion(&args);
4049 slipperyExitWindow->consumeMotionDown();
4050 slipperyExitWindow->setFrame(Rect(70, 70, 100, 100));
4051 mDispatcher->setInputWindows(
4052 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
4053
4054 args = generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4055 ADISPLAY_ID_DEFAULT, {{51, 51}});
4056 mDispatcher->notifyMotion(&args);
4057
4058 slipperyExitWindow->consumeMotionCancel();
4059
4060 slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT,
4061 AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
4062}
4063
Garfield Tan1c7bc862020-01-28 13:24:04 -08004064class InputDispatcherKeyRepeatTest : public InputDispatcherTest {
4065protected:
4066 static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms
4067 static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms
4068
Chris Yea209fde2020-07-22 13:54:51 -07004069 std::shared_ptr<FakeApplicationHandle> mApp;
Garfield Tan1c7bc862020-01-28 13:24:04 -08004070 sp<FakeWindowHandle> mWindow;
4071
4072 virtual void SetUp() override {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004073 mFakePolicy = sp<FakeInputDispatcherPolicy>::make();
Garfield Tan1c7bc862020-01-28 13:24:04 -08004074 mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004075 mDispatcher = std::make_unique<InputDispatcher>(mFakePolicy);
Garfield Tan1c7bc862020-01-28 13:24:04 -08004076 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
4077 ASSERT_EQ(OK, mDispatcher->start());
4078
4079 setUpWindow();
4080 }
4081
4082 void setUpWindow() {
Chris Yea209fde2020-07-22 13:54:51 -07004083 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004084 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Garfield Tan1c7bc862020-01-28 13:24:04 -08004085
Vishnu Nair47074b82020-08-14 11:54:47 -07004086 mWindow->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004087 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004088 setFocusedWindow(mWindow);
Garfield Tan1c7bc862020-01-28 13:24:04 -08004089 mWindow->consumeFocusEvent(true);
4090 }
4091
Chris Ye2ad95392020-09-01 13:44:44 -07004092 void sendAndConsumeKeyDown(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08004093 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07004094 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08004095 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event
4096 mDispatcher->notifyKey(&keyArgs);
4097
4098 // Window should receive key down event.
4099 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4100 }
4101
4102 void expectKeyRepeatOnce(int32_t repeatCount) {
4103 SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount));
4104 InputEvent* repeatEvent = mWindow->consume();
4105 ASSERT_NE(nullptr, repeatEvent);
4106
4107 uint32_t eventType = repeatEvent->getType();
4108 ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, eventType);
4109
4110 KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent);
4111 uint32_t eventAction = repeatKeyEvent->getAction();
4112 EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction);
4113 EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount());
4114 }
4115
Chris Ye2ad95392020-09-01 13:44:44 -07004116 void sendAndConsumeKeyUp(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08004117 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07004118 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08004119 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event
4120 mDispatcher->notifyKey(&keyArgs);
4121
4122 // Window should receive key down event.
4123 mWindow->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
4124 0 /*expectedFlags*/);
4125 }
4126};
4127
4128TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) {
Chris Ye2ad95392020-09-01 13:44:44 -07004129 sendAndConsumeKeyDown(1 /* deviceId */);
4130 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
4131 expectKeyRepeatOnce(repeatCount);
4132 }
4133}
4134
4135TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) {
4136 sendAndConsumeKeyDown(1 /* deviceId */);
4137 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
4138 expectKeyRepeatOnce(repeatCount);
4139 }
4140 sendAndConsumeKeyDown(2 /* deviceId */);
4141 /* repeatCount will start from 1 for deviceId 2 */
Garfield Tan1c7bc862020-01-28 13:24:04 -08004142 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
4143 expectKeyRepeatOnce(repeatCount);
4144 }
4145}
4146
4147TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) {
Chris Ye2ad95392020-09-01 13:44:44 -07004148 sendAndConsumeKeyDown(1 /* deviceId */);
Garfield Tan1c7bc862020-01-28 13:24:04 -08004149 expectKeyRepeatOnce(1 /*repeatCount*/);
Chris Ye2ad95392020-09-01 13:44:44 -07004150 sendAndConsumeKeyUp(1 /* deviceId */);
4151 mWindow->assertNoEvents();
4152}
4153
4154TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) {
4155 sendAndConsumeKeyDown(1 /* deviceId */);
4156 expectKeyRepeatOnce(1 /*repeatCount*/);
4157 sendAndConsumeKeyDown(2 /* deviceId */);
4158 expectKeyRepeatOnce(1 /*repeatCount*/);
4159 // Stale key up from device 1.
4160 sendAndConsumeKeyUp(1 /* deviceId */);
4161 // Device 2 is still down, keep repeating
4162 expectKeyRepeatOnce(2 /*repeatCount*/);
4163 expectKeyRepeatOnce(3 /*repeatCount*/);
4164 // Device 2 key up
4165 sendAndConsumeKeyUp(2 /* deviceId */);
4166 mWindow->assertNoEvents();
4167}
4168
4169TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) {
4170 sendAndConsumeKeyDown(1 /* deviceId */);
4171 expectKeyRepeatOnce(1 /*repeatCount*/);
4172 sendAndConsumeKeyDown(2 /* deviceId */);
4173 expectKeyRepeatOnce(1 /*repeatCount*/);
4174 // Device 2 which holds the key repeating goes up, expect the repeating to stop.
4175 sendAndConsumeKeyUp(2 /* deviceId */);
4176 // Device 1 still holds key down, but the repeating was already stopped
Garfield Tan1c7bc862020-01-28 13:24:04 -08004177 mWindow->assertNoEvents();
4178}
4179
liushenxiang42232912021-05-21 20:24:09 +08004180TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) {
4181 sendAndConsumeKeyDown(DEVICE_ID);
4182 expectKeyRepeatOnce(1 /*repeatCount*/);
4183 NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID);
4184 mDispatcher->notifyDeviceReset(&args);
4185 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT,
4186 AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS);
4187 mWindow->assertNoEvents();
4188}
4189
Garfield Tan1c7bc862020-01-28 13:24:04 -08004190TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) {
Chris Ye2ad95392020-09-01 13:44:44 -07004191 sendAndConsumeKeyDown(1 /* deviceId */);
Garfield Tan1c7bc862020-01-28 13:24:04 -08004192 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
4193 InputEvent* repeatEvent = mWindow->consume();
4194 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
4195 EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER,
4196 IdGenerator::getSource(repeatEvent->getId()));
4197 }
4198}
4199
4200TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) {
Chris Ye2ad95392020-09-01 13:44:44 -07004201 sendAndConsumeKeyDown(1 /* deviceId */);
Garfield Tan1c7bc862020-01-28 13:24:04 -08004202
4203 std::unordered_set<int32_t> idSet;
4204 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
4205 InputEvent* repeatEvent = mWindow->consume();
4206 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
4207 int32_t id = repeatEvent->getId();
4208 EXPECT_EQ(idSet.end(), idSet.find(id));
4209 idSet.insert(id);
4210 }
4211}
4212
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004213/* Test InputDispatcher for MultiDisplay */
4214class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest {
4215public:
Prabir Pradhan3608aad2019-10-02 17:08:26 -07004216 virtual void SetUp() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004217 InputDispatcherTest::SetUp();
Arthur Hungb92218b2018-08-14 12:00:21 +08004218
Chris Yea209fde2020-07-22 13:54:51 -07004219 application1 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004220 windowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004221 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08004222
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004223 // Set focus window for primary display, but focused display would be second one.
4224 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1);
Vishnu Nair47074b82020-08-14 11:54:47 -07004225 windowInPrimary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004226 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004227 setFocusedWindow(windowInPrimary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004228 windowInPrimary->consumeFocusEvent(true);
Arthur Hungb92218b2018-08-14 12:00:21 +08004229
Chris Yea209fde2020-07-22 13:54:51 -07004230 application2 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004231 windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004232 sp<FakeWindowHandle>::make(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004233 // Set focus to second display window.
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004234 // Set focus display to second one.
4235 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
4236 // Set focus window for second display.
4237 mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2);
Vishnu Nair47074b82020-08-14 11:54:47 -07004238 windowInSecondary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004239 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004240 setFocusedWindow(windowInSecondary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004241 windowInSecondary->consumeFocusEvent(true);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004242 }
4243
Prabir Pradhan3608aad2019-10-02 17:08:26 -07004244 virtual void TearDown() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004245 InputDispatcherTest::TearDown();
4246
Chris Yea209fde2020-07-22 13:54:51 -07004247 application1.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004248 windowInPrimary.clear();
Chris Yea209fde2020-07-22 13:54:51 -07004249 application2.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004250 windowInSecondary.clear();
4251 }
4252
4253protected:
Chris Yea209fde2020-07-22 13:54:51 -07004254 std::shared_ptr<FakeApplicationHandle> application1;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004255 sp<FakeWindowHandle> windowInPrimary;
Chris Yea209fde2020-07-22 13:54:51 -07004256 std::shared_ptr<FakeApplicationHandle> application2;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004257 sp<FakeWindowHandle> windowInSecondary;
4258};
4259
4260TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) {
4261 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004262 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4263 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
4264 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004265 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08004266 windowInSecondary->assertNoEvents();
4267
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004268 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004269 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4270 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
4271 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08004272 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004273 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +08004274}
4275
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004276TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) {
Tiger Huang721e26f2018-07-24 22:26:19 +08004277 // Test inject a key down with display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08004278 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4279 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004280 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004281 windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Tiger Huang721e26f2018-07-24 22:26:19 +08004282 windowInSecondary->assertNoEvents();
4283
4284 // Test inject a key down without display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08004285 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004286 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08004287 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004288 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hungb92218b2018-08-14 12:00:21 +08004289
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08004290 // Remove all windows in secondary display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00004291 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}});
Arthur Hungb92218b2018-08-14 12:00:21 +08004292
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004293 // Old focus should receive a cancel event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004294 windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE,
4295 AKEY_EVENT_FLAG_CANCELED);
Arthur Hungb92218b2018-08-14 12:00:21 +08004296
4297 // Test inject a key down, should timeout because of no target window.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08004298 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004299 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Arthur Hungb92218b2018-08-14 12:00:21 +08004300 windowInPrimary->assertNoEvents();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004301 windowInSecondary->consumeFocusEvent(false);
Arthur Hungb92218b2018-08-14 12:00:21 +08004302 windowInSecondary->assertNoEvents();
4303}
4304
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004305// Test per-display input monitors for motion event.
4306TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) {
chaviwd1c23182019-12-20 18:44:56 -08004307 FakeMonitorReceiver monitorInPrimary =
4308 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
4309 FakeMonitorReceiver monitorInSecondary =
4310 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004311
4312 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004313 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4314 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
4315 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004316 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08004317 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004318 windowInSecondary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08004319 monitorInSecondary.assertNoEvents();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004320
4321 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004322 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4323 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
4324 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004325 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08004326 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004327 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
chaviwd1c23182019-12-20 18:44:56 -08004328 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004329
4330 // Test inject a non-pointer motion event.
4331 // If specific a display, it will dispatch to the focused window of particular display,
4332 // or it will dispatch to the focused window of focused display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004333 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4334 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE))
4335 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004336 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08004337 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004338 windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08004339 monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004340}
4341
4342// Test per-display input monitors for key event.
4343TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) {
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004344 // Input monitor per display.
chaviwd1c23182019-12-20 18:44:56 -08004345 FakeMonitorReceiver monitorInPrimary =
4346 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
4347 FakeMonitorReceiver monitorInSecondary =
4348 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004349
4350 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004351 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4352 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004353 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08004354 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004355 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08004356 monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004357}
4358
Vishnu Nair958da932020-08-21 17:12:37 -07004359TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) {
4360 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004361 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07004362 secondWindowInPrimary->setFocusable(true);
4363 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}});
4364 setFocusedWindow(secondWindowInPrimary);
4365 windowInPrimary->consumeFocusEvent(false);
4366 secondWindowInPrimary->consumeFocusEvent(true);
4367
4368 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004369 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
4370 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004371 windowInPrimary->assertNoEvents();
4372 windowInSecondary->assertNoEvents();
4373 secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4374}
4375
Arthur Hungdfd528e2021-12-08 13:23:04 +00004376TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CancelTouch_MultiDisplay) {
4377 FakeMonitorReceiver monitorInPrimary =
4378 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
4379 FakeMonitorReceiver monitorInSecondary =
4380 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
4381
4382 // Test touch down on primary display.
4383 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4384 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
4385 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4386 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4387 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
4388
4389 // Test touch down on second display.
4390 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4391 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
4392 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4393 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
4394 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
4395
4396 // Trigger cancel touch.
4397 mDispatcher->cancelCurrentTouch();
4398 windowInPrimary->consumeMotionCancel(ADISPLAY_ID_DEFAULT);
4399 monitorInPrimary.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
4400 windowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID);
4401 monitorInSecondary.consumeMotionCancel(SECOND_DISPLAY_ID);
4402
4403 // Test inject a move motion event, no window/monitor should receive the event.
4404 ASSERT_EQ(InputEventInjectionResult::FAILED,
4405 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4406 ADISPLAY_ID_DEFAULT, {110, 200}))
4407 << "Inject motion event should return InputEventInjectionResult::FAILED";
4408 windowInPrimary->assertNoEvents();
4409 monitorInPrimary.assertNoEvents();
4410
4411 ASSERT_EQ(InputEventInjectionResult::FAILED,
4412 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4413 SECOND_DISPLAY_ID, {110, 200}))
4414 << "Inject motion event should return InputEventInjectionResult::FAILED";
4415 windowInSecondary->assertNoEvents();
4416 monitorInSecondary.assertNoEvents();
4417}
4418
Jackal Guof9696682018-10-05 12:23:23 +08004419class InputFilterTest : public InputDispatcherTest {
4420protected:
Prabir Pradhan81420cc2021-09-06 10:28:50 -07004421 void testNotifyMotion(int32_t displayId, bool expectToBeFiltered,
4422 const ui::Transform& transform = ui::Transform()) {
Jackal Guof9696682018-10-05 12:23:23 +08004423 NotifyMotionArgs motionArgs;
4424
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004425 motionArgs =
4426 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Jackal Guof9696682018-10-05 12:23:23 +08004427 mDispatcher->notifyMotion(&motionArgs);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004428 motionArgs =
4429 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Jackal Guof9696682018-10-05 12:23:23 +08004430 mDispatcher->notifyMotion(&motionArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004431 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08004432 if (expectToBeFiltered) {
Prabir Pradhan81420cc2021-09-06 10:28:50 -07004433 const auto xy = transform.transform(motionArgs.pointerCoords->getXYValue());
4434 mFakePolicy->assertFilterInputEventWasCalled(motionArgs, xy);
Jackal Guof9696682018-10-05 12:23:23 +08004435 } else {
4436 mFakePolicy->assertFilterInputEventWasNotCalled();
4437 }
4438 }
4439
4440 void testNotifyKey(bool expectToBeFiltered) {
4441 NotifyKeyArgs keyArgs;
4442
4443 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
4444 mDispatcher->notifyKey(&keyArgs);
4445 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP);
4446 mDispatcher->notifyKey(&keyArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004447 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08004448
4449 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08004450 mFakePolicy->assertFilterInputEventWasCalled(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08004451 } else {
4452 mFakePolicy->assertFilterInputEventWasNotCalled();
4453 }
4454 }
4455};
4456
4457// Test InputFilter for MotionEvent
4458TEST_F(InputFilterTest, MotionEvent_InputFilter) {
4459 // Since the InputFilter is disabled by default, check if touch events aren't filtered.
4460 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
4461 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
4462
4463 // Enable InputFilter
4464 mDispatcher->setInputFilterEnabled(true);
4465 // Test touch on both primary and second display, and check if both events are filtered.
4466 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true);
4467 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true);
4468
4469 // Disable InputFilter
4470 mDispatcher->setInputFilterEnabled(false);
4471 // Test touch on both primary and second display, and check if both events aren't filtered.
4472 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
4473 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
4474}
4475
4476// Test InputFilter for KeyEvent
4477TEST_F(InputFilterTest, KeyEvent_InputFilter) {
4478 // Since the InputFilter is disabled by default, check if key event aren't filtered.
4479 testNotifyKey(/*expectToBeFiltered*/ false);
4480
4481 // Enable InputFilter
4482 mDispatcher->setInputFilterEnabled(true);
4483 // Send a key event, and check if it is filtered.
4484 testNotifyKey(/*expectToBeFiltered*/ true);
4485
4486 // Disable InputFilter
4487 mDispatcher->setInputFilterEnabled(false);
4488 // Send a key event, and check if it isn't filtered.
4489 testNotifyKey(/*expectToBeFiltered*/ false);
4490}
4491
Prabir Pradhan81420cc2021-09-06 10:28:50 -07004492// Ensure that MotionEvents sent to the InputFilter through InputListener are converted to the
4493// logical display coordinate space.
4494TEST_F(InputFilterTest, MotionEvent_UsesLogicalDisplayCoordinates_notifyMotion) {
4495 ui::Transform firstDisplayTransform;
4496 firstDisplayTransform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
4497 ui::Transform secondDisplayTransform;
4498 secondDisplayTransform.set({-6.6, -5.5, -4.4, -3.3, -2.2, -1.1, 0, 0, 1});
4499
4500 std::vector<gui::DisplayInfo> displayInfos(2);
4501 displayInfos[0].displayId = ADISPLAY_ID_DEFAULT;
4502 displayInfos[0].transform = firstDisplayTransform;
4503 displayInfos[1].displayId = SECOND_DISPLAY_ID;
4504 displayInfos[1].transform = secondDisplayTransform;
4505
4506 mDispatcher->onWindowInfosChanged({}, displayInfos);
4507
4508 // Enable InputFilter
4509 mDispatcher->setInputFilterEnabled(true);
4510
4511 // Ensure the correct transforms are used for the displays.
4512 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true, firstDisplayTransform);
4513 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true, secondDisplayTransform);
4514}
4515
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004516class InputFilterInjectionPolicyTest : public InputDispatcherTest {
4517protected:
4518 virtual void SetUp() override {
4519 InputDispatcherTest::SetUp();
4520
4521 /**
4522 * We don't need to enable input filter to test the injected event policy, but we enabled it
4523 * here to make the tests more realistic, since this policy only matters when inputfilter is
4524 * on.
4525 */
4526 mDispatcher->setInputFilterEnabled(true);
4527
4528 std::shared_ptr<InputApplicationHandle> application =
4529 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004530 mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Test Window",
4531 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004532
4533 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
4534 mWindow->setFocusable(true);
4535 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
4536 setFocusedWindow(mWindow);
4537 mWindow->consumeFocusEvent(true);
4538 }
4539
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00004540 void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
4541 int32_t flags) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004542 KeyEvent event;
4543
4544 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
4545 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD,
4546 ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A,
4547 KEY_A, AMETA_NONE, 0 /*repeatCount*/, eventTime, eventTime);
4548 const int32_t additionalPolicyFlags =
4549 POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT;
4550 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Prabir Pradhan5735a322022-04-11 17:23:34 +00004551 mDispatcher->injectInputEvent(&event, {} /*targetUid*/,
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004552 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
4553 policyFlags | additionalPolicyFlags));
4554
4555 InputEvent* received = mWindow->consume();
4556 ASSERT_NE(nullptr, received);
4557 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00004558 ASSERT_EQ(received->getType(), AINPUT_EVENT_TYPE_KEY);
4559 KeyEvent& keyEvent = static_cast<KeyEvent&>(*received);
4560 ASSERT_EQ(flags, keyEvent.getFlags());
4561 }
4562
4563 void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
4564 int32_t flags) {
4565 MotionEvent event;
4566 PointerProperties pointerProperties[1];
4567 PointerCoords pointerCoords[1];
4568 pointerProperties[0].clear();
4569 pointerProperties[0].id = 0;
4570 pointerCoords[0].clear();
4571 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300);
4572 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400);
4573
4574 ui::Transform identityTransform;
4575 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
4576 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN,
4577 DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0,
4578 AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE,
4579 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -07004580 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, eventTime,
Evan Rosky09576692021-07-01 12:22:09 -07004581 eventTime,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00004582 /*pointerCount*/ 1, pointerProperties, pointerCoords);
4583
4584 const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER;
4585 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Prabir Pradhan5735a322022-04-11 17:23:34 +00004586 mDispatcher->injectInputEvent(&event, {} /*targetUid*/,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00004587 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
4588 policyFlags | additionalPolicyFlags));
4589
4590 InputEvent* received = mWindow->consume();
4591 ASSERT_NE(nullptr, received);
4592 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
4593 ASSERT_EQ(received->getType(), AINPUT_EVENT_TYPE_MOTION);
4594 MotionEvent& motionEvent = static_cast<MotionEvent&>(*received);
4595 ASSERT_EQ(flags, motionEvent.getFlags());
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004596 }
4597
4598private:
4599 sp<FakeWindowHandle> mWindow;
4600};
4601
4602TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) {
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00004603 // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input
4604 // filter. Without it, the event will no different from a regularly injected event, and the
4605 // injected device id will be overwritten.
4606 testInjectedKey(POLICY_FLAG_FILTERED, 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/,
4607 0 /*flags*/);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004608}
4609
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00004610TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004611 testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00004612 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/,
4613 AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
4614}
4615
4616TEST_F(InputFilterInjectionPolicyTest,
4617 MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
4618 testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
4619 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/,
4620 AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004621}
4622
4623TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) {
4624 testInjectedKey(0 /*policyFlags*/, 3 /*injectedDeviceId*/,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00004625 VIRTUAL_KEYBOARD_ID /*resolvedDeviceId*/, 0 /*flags*/);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004626}
4627
chaviwfd6d3512019-03-25 13:23:49 -07004628class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest {
Prabir Pradhan3608aad2019-10-02 17:08:26 -07004629 virtual void SetUp() override {
chaviwfd6d3512019-03-25 13:23:49 -07004630 InputDispatcherTest::SetUp();
4631
Chris Yea209fde2020-07-22 13:54:51 -07004632 std::shared_ptr<FakeApplicationHandle> application =
4633 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004634 mUnfocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004635 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07004636 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
chaviwfd6d3512019-03-25 13:23:49 -07004637
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08004638 mFocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004639 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08004640 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
chaviwfd6d3512019-03-25 13:23:49 -07004641
4642 // Set focused application.
4643 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07004644 mFocusedWindow->setFocusable(true);
chaviwfd6d3512019-03-25 13:23:49 -07004645
4646 // Expect one focus window exist in display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00004647 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004648 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004649 mFocusedWindow->consumeFocusEvent(true);
chaviwfd6d3512019-03-25 13:23:49 -07004650 }
4651
Prabir Pradhan3608aad2019-10-02 17:08:26 -07004652 virtual void TearDown() override {
chaviwfd6d3512019-03-25 13:23:49 -07004653 InputDispatcherTest::TearDown();
4654
4655 mUnfocusedWindow.clear();
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08004656 mFocusedWindow.clear();
chaviwfd6d3512019-03-25 13:23:49 -07004657 }
4658
4659protected:
4660 sp<FakeWindowHandle> mUnfocusedWindow;
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08004661 sp<FakeWindowHandle> mFocusedWindow;
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07004662 static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60};
chaviwfd6d3512019-03-25 13:23:49 -07004663};
4664
4665// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
4666// DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received
4667// the onPointerDownOutsideFocus callback.
4668TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004669 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07004670 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4671 {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004672 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07004673 mUnfocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07004674
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004675 ASSERT_TRUE(mDispatcher->waitForIdle());
chaviwfd6d3512019-03-25 13:23:49 -07004676 mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken());
4677}
4678
4679// Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action
4680// DOWN on the window that doesn't have focus. Ensure no window received the
4681// onPointerDownOutsideFocus callback.
4682TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004683 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07004684 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004685 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07004686 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07004687
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004688 ASSERT_TRUE(mDispatcher->waitForIdle());
4689 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07004690}
4691
4692// Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't
4693// have focus. Ensure no window received the onPointerDownOutsideFocus callback.
4694TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08004695 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4696 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004697 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07004698 mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07004699
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004700 ASSERT_TRUE(mDispatcher->waitForIdle());
4701 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07004702}
4703
4704// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
4705// DOWN on the window that already has focus. Ensure no window received the
4706// onPointerDownOutsideFocus callback.
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004707TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004708 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08004709 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07004710 FOCUSED_WINDOW_TOUCH_POINT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004711 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07004712 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07004713
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004714 ASSERT_TRUE(mDispatcher->waitForIdle());
4715 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07004716}
4717
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08004718// Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag
4719// NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback.
4720TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) {
4721 const MotionEvent event =
4722 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
4723 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
4724 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(20).y(20))
4725 .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE)
4726 .build();
4727 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(mDispatcher, event))
4728 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4729 mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
4730
4731 ASSERT_TRUE(mDispatcher->waitForIdle());
4732 mFakePolicy->assertOnPointerDownWasNotCalled();
4733 // Ensure that the unfocused window did not receive any FOCUS events.
4734 mUnfocusedWindow->assertNoEvents();
4735}
4736
chaviwaf87b3e2019-10-01 16:59:28 -07004737// These tests ensures we can send touch events to a single client when there are multiple input
4738// windows that point to the same client token.
4739class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest {
4740 virtual void SetUp() override {
4741 InputDispatcherTest::SetUp();
4742
Chris Yea209fde2020-07-22 13:54:51 -07004743 std::shared_ptr<FakeApplicationHandle> application =
4744 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004745 mWindow1 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 1",
4746 ADISPLAY_ID_DEFAULT);
chaviwaf87b3e2019-10-01 16:59:28 -07004747 mWindow1->setFrame(Rect(0, 0, 100, 100));
4748
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004749 mWindow2 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 2",
4750 ADISPLAY_ID_DEFAULT, mWindow1->getToken());
chaviwaf87b3e2019-10-01 16:59:28 -07004751 mWindow2->setFrame(Rect(100, 100, 200, 200));
4752
Arthur Hung72d8dc32020-03-28 00:48:39 +00004753 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
chaviwaf87b3e2019-10-01 16:59:28 -07004754 }
4755
4756protected:
4757 sp<FakeWindowHandle> mWindow1;
4758 sp<FakeWindowHandle> mWindow2;
4759
4760 // Helper function to convert the point from screen coordinates into the window's space
chaviw3277faf2021-05-19 16:45:23 -05004761 static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) {
chaviw1ff3d1e2020-07-01 15:53:47 -07004762 vec2 vals = windowInfo->transform.transform(point.x, point.y);
4763 return {vals.x, vals.y};
chaviwaf87b3e2019-10-01 16:59:28 -07004764 }
4765
4766 void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction,
4767 const std::vector<PointF>& points) {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004768 const std::string name = window->getName();
chaviwaf87b3e2019-10-01 16:59:28 -07004769 InputEvent* event = window->consume();
4770
4771 ASSERT_NE(nullptr, event) << name.c_str()
4772 << ": consumer should have returned non-NULL event.";
4773
4774 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType())
4775 << name.c_str() << "expected " << inputEventTypeToString(AINPUT_EVENT_TYPE_MOTION)
4776 << " event, got " << inputEventTypeToString(event->getType()) << " event";
4777
4778 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004779 assertMotionAction(expectedAction, motionEvent.getAction());
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08004780 ASSERT_EQ(points.size(), motionEvent.getPointerCount());
chaviwaf87b3e2019-10-01 16:59:28 -07004781
4782 for (size_t i = 0; i < points.size(); i++) {
4783 float expectedX = points[i].x;
4784 float expectedY = points[i].y;
4785
4786 EXPECT_EQ(expectedX, motionEvent.getX(i))
4787 << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str()
4788 << ", got " << motionEvent.getX(i);
4789 EXPECT_EQ(expectedY, motionEvent.getY(i))
4790 << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str()
4791 << ", got " << motionEvent.getY(i);
4792 }
4793 }
chaviw9eaa22c2020-07-01 16:21:27 -07004794
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08004795 void touchAndAssertPositions(int32_t action, const std::vector<PointF>& touchedPoints,
chaviw9eaa22c2020-07-01 16:21:27 -07004796 std::vector<PointF> expectedPoints) {
4797 NotifyMotionArgs motionArgs = generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN,
4798 ADISPLAY_ID_DEFAULT, touchedPoints);
4799 mDispatcher->notifyMotion(&motionArgs);
4800
4801 // Always consume from window1 since it's the window that has the InputReceiver
4802 consumeMotionEvent(mWindow1, action, expectedPoints);
4803 }
chaviwaf87b3e2019-10-01 16:59:28 -07004804};
4805
4806TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) {
4807 // Touch Window 1
4808 PointF touchedPoint = {10, 10};
4809 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07004810 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07004811
4812 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07004813 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07004814
4815 // Touch Window 2
4816 touchedPoint = {150, 150};
4817 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07004818 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07004819}
4820
chaviw9eaa22c2020-07-01 16:21:27 -07004821TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) {
4822 // Set scale value for window2
chaviwaf87b3e2019-10-01 16:59:28 -07004823 mWindow2->setWindowScale(0.5f, 0.5f);
4824
4825 // Touch Window 1
4826 PointF touchedPoint = {10, 10};
4827 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07004828 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07004829 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07004830 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07004831
4832 // Touch Window 2
4833 touchedPoint = {150, 150};
4834 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07004835 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
4836 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07004837
chaviw9eaa22c2020-07-01 16:21:27 -07004838 // Update the transform so rotation is set
4839 mWindow2->setWindowTransform(0, -1, 1, 0);
4840 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
4841 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07004842}
4843
chaviw9eaa22c2020-07-01 16:21:27 -07004844TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004845 mWindow2->setWindowScale(0.5f, 0.5f);
4846
4847 // Touch Window 1
4848 std::vector<PointF> touchedPoints = {PointF{10, 10}};
4849 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07004850 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004851
4852 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07004853 touchedPoints.push_back(PointF{150, 150});
4854 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08004855 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004856
chaviw9eaa22c2020-07-01 16:21:27 -07004857 // Release Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08004858 touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07004859 expectedPoints.pop_back();
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004860
chaviw9eaa22c2020-07-01 16:21:27 -07004861 // Update the transform so rotation is set for Window 2
4862 mWindow2->setWindowTransform(0, -1, 1, 0);
4863 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08004864 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004865}
4866
chaviw9eaa22c2020-07-01 16:21:27 -07004867TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004868 mWindow2->setWindowScale(0.5f, 0.5f);
4869
4870 // Touch Window 1
4871 std::vector<PointF> touchedPoints = {PointF{10, 10}};
4872 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07004873 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004874
4875 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07004876 touchedPoints.push_back(PointF{150, 150});
4877 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004878
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08004879 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004880
4881 // Move both windows
4882 touchedPoints = {{20, 20}, {175, 175}};
4883 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
4884 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
4885
chaviw9eaa22c2020-07-01 16:21:27 -07004886 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004887
chaviw9eaa22c2020-07-01 16:21:27 -07004888 // Release Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08004889 touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07004890 expectedPoints.pop_back();
4891
4892 // Touch Window 2
4893 mWindow2->setWindowTransform(0, -1, 1, 0);
4894 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08004895 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07004896
4897 // Move both windows
4898 touchedPoints = {{20, 20}, {175, 175}};
4899 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
4900 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
4901
4902 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004903}
4904
4905TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) {
4906 mWindow1->setWindowScale(0.5f, 0.5f);
4907
4908 // Touch Window 1
4909 std::vector<PointF> touchedPoints = {PointF{10, 10}};
4910 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07004911 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004912
4913 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07004914 touchedPoints.push_back(PointF{150, 150});
4915 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004916
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08004917 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004918
4919 // Move both windows
4920 touchedPoints = {{20, 20}, {175, 175}};
4921 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
4922 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
4923
chaviw9eaa22c2020-07-01 16:21:27 -07004924 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004925}
4926
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004927class InputDispatcherSingleWindowAnr : public InputDispatcherTest {
4928 virtual void SetUp() override {
4929 InputDispatcherTest::SetUp();
4930
Chris Yea209fde2020-07-22 13:54:51 -07004931 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004932 mApplication->setDispatchingTimeout(20ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004933 mWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "TestWindow",
4934 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004935 mWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05004936 mWindow->setDispatchingTimeout(30ms);
Vishnu Nair47074b82020-08-14 11:54:47 -07004937 mWindow->setFocusable(true);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004938
4939 // Set focused application.
4940 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
4941
4942 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004943 setFocusedWindow(mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004944 mWindow->consumeFocusEvent(true);
4945 }
4946
4947 virtual void TearDown() override {
4948 InputDispatcherTest::TearDown();
4949 mWindow.clear();
4950 }
4951
4952protected:
Chris Yea209fde2020-07-22 13:54:51 -07004953 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004954 sp<FakeWindowHandle> mWindow;
4955 static constexpr PointF WINDOW_LOCATION = {20, 20};
4956
4957 void tapOnWindow() {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004958 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004959 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4960 WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004961 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004962 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4963 WINDOW_LOCATION));
4964 }
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004965
4966 sp<FakeWindowHandle> addSpyWindow() {
4967 sp<FakeWindowHandle> spy =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004968 sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004969 spy->setTrustedOverlay(true);
4970 spy->setFocusable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08004971 spy->setSpy(true);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004972 spy->setDispatchingTimeout(30ms);
4973 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, mWindow}}});
4974 return spy;
4975 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004976};
4977
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004978// Send a tap and respond, which should not cause an ANR.
4979TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) {
4980 tapOnWindow();
4981 mWindow->consumeMotionDown();
4982 mWindow->consumeMotionUp();
4983 ASSERT_TRUE(mDispatcher->waitForIdle());
4984 mFakePolicy->assertNotifyAnrWasNotCalled();
4985}
4986
4987// Send a regular key and respond, which should not cause an ANR.
4988TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08004989 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004990 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
4991 ASSERT_TRUE(mDispatcher->waitForIdle());
4992 mFakePolicy->assertNotifyAnrWasNotCalled();
4993}
4994
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05004995TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) {
4996 mWindow->setFocusable(false);
4997 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
4998 mWindow->consumeFocusEvent(false);
4999
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005000 InputEventInjectionResult result =
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05005001 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005002 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/,
5003 false /* allowKeyRepeat */);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005004 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05005005 // Key will not go to window because we have no focused window.
5006 // The 'no focused window' ANR timer should start instead.
5007
5008 // Now, the focused application goes away.
5009 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr);
5010 // The key should get dropped and there should be no ANR.
5011
5012 ASSERT_TRUE(mDispatcher->waitForIdle());
5013 mFakePolicy->assertNotifyAnrWasNotCalled();
5014}
5015
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005016// Send an event to the app and have the app not respond right away.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005017// When ANR is raised, policy will tell the dispatcher to cancel the events for that window.
5018// So InputDispatcher will enqueue ACTION_CANCEL event as well.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005019TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005020 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005021 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5022 WINDOW_LOCATION));
5023
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005024 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN
5025 ASSERT_TRUE(sequenceNum);
5026 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005027 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005028
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005029 mWindow->finishEvent(*sequenceNum);
5030 mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL,
5031 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005032 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08005033 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005034}
5035
5036// Send a key to the app and have the app not respond right away.
5037TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) {
5038 // Inject a key, and don't respond - expect that ANR is called.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005039 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005040 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent();
5041 ASSERT_TRUE(sequenceNum);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005042 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005043 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005044 ASSERT_TRUE(mDispatcher->waitForIdle());
5045}
5046
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005047// We have a focused application, but no focused window
5048TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) {
Vishnu Nair47074b82020-08-14 11:54:47 -07005049 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005050 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
5051 mWindow->consumeFocusEvent(false);
5052
5053 // taps on the window work as normal
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005054 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005055 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5056 WINDOW_LOCATION));
5057 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
5058 mDispatcher->waitForIdle();
5059 mFakePolicy->assertNotifyAnrWasNotCalled();
5060
5061 // Once a focused event arrives, we get an ANR for this application
5062 // We specify the injection timeout to be smaller than the application timeout, to ensure that
5063 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005064 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005065 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005066 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, false /* allowKeyRepeat */);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005067 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005068 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Vishnu Naire4df8752022-09-08 09:17:55 -07005069 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005070 ASSERT_TRUE(mDispatcher->waitForIdle());
5071}
5072
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08005073/**
5074 * Make sure the stale key is dropped before causing an ANR. So even if there's no focused window,
5075 * there will not be an ANR.
5076 */
5077TEST_F(InputDispatcherSingleWindowAnr, StaleKeyEventDoesNotAnr) {
5078 mWindow->setFocusable(false);
5079 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
5080 mWindow->consumeFocusEvent(false);
5081
5082 KeyEvent event;
5083 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC) -
5084 std::chrono::nanoseconds(STALE_EVENT_TIMEOUT).count();
5085
5086 // Define a valid key down event that is stale (too old).
5087 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
5088 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, /* flags */ 0, AKEYCODE_A, KEY_A,
5089 AMETA_NONE, 1 /*repeatCount*/, eventTime, eventTime);
5090
5091 const int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER;
5092
5093 InputEventInjectionResult result =
Prabir Pradhan5735a322022-04-11 17:23:34 +00005094 mDispatcher->injectInputEvent(&event, {} /* targetUid */,
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08005095 InputEventInjectionSync::WAIT_FOR_RESULT,
5096 INJECT_EVENT_TIMEOUT, policyFlags);
5097 ASSERT_EQ(InputEventInjectionResult::FAILED, result)
5098 << "Injection should fail because the event is stale";
5099
5100 ASSERT_TRUE(mDispatcher->waitForIdle());
5101 mFakePolicy->assertNotifyAnrWasNotCalled();
5102 mWindow->assertNoEvents();
5103}
5104
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005105// We have a focused application, but no focused window
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005106// Make sure that we don't notify policy twice about the same ANR.
5107TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) {
Vishnu Nair47074b82020-08-14 11:54:47 -07005108 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005109 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
5110 mWindow->consumeFocusEvent(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005111
5112 // Once a focused event arrives, we get an ANR for this application
5113 // We specify the injection timeout to be smaller than the application timeout, to ensure that
5114 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005115 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005116 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005117 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, false /* allowKeyRepeat */);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005118 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Vishnu Naire4df8752022-09-08 09:17:55 -07005119 const std::chrono::duration appTimeout =
5120 mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
5121 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005122
Vishnu Naire4df8752022-09-08 09:17:55 -07005123 std::this_thread::sleep_for(appTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005124 // ANR should not be raised again. It is up to policy to do that if it desires.
5125 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005126
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005127 // If we now get a focused window, the ANR should stop, but the policy handles that via
5128 // 'notifyFocusChanged' callback. This is implemented in the policy so we can't test it here.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005129 ASSERT_TRUE(mDispatcher->waitForIdle());
5130}
5131
5132// We have a focused application, but no focused window
5133TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) {
Vishnu Nair47074b82020-08-14 11:54:47 -07005134 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005135 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
5136 mWindow->consumeFocusEvent(false);
5137
5138 // Once a focused event arrives, we get an ANR for this application
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005139 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005140 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005141 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
5142 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005143
Vishnu Naire4df8752022-09-08 09:17:55 -07005144 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
5145 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005146
5147 // Future focused events get dropped right away
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005148 ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005149 ASSERT_TRUE(mDispatcher->waitForIdle());
5150 mWindow->assertNoEvents();
5151}
5152
5153/**
5154 * Ensure that the implementation is valid. Since we are using multiset to keep track of the
5155 * ANR timeouts, we are allowing entries with identical timestamps in the same connection.
5156 * If we process 1 of the events, but ANR on the second event with the same timestamp,
5157 * the ANR mechanism should still work.
5158 *
5159 * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the
5160 * DOWN event, while not responding on the second one.
5161 */
5162TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) {
5163 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
5164 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5165 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
5166 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
5167 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005168 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005169
5170 // Now send ACTION_UP, with identical timestamp
5171 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
5172 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
5173 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
5174 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005175 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005176
5177 // We have now sent down and up. Let's consume first event and then ANR on the second.
5178 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5179 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005180 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005181}
5182
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005183// A spy window can receive an ANR
5184TEST_F(InputDispatcherSingleWindowAnr, SpyWindowAnr) {
5185 sp<FakeWindowHandle> spy = addSpyWindow();
5186
5187 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5188 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5189 WINDOW_LOCATION));
5190 mWindow->consumeMotionDown();
5191
5192 std::optional<uint32_t> sequenceNum = spy->receiveEvent(); // ACTION_DOWN
5193 ASSERT_TRUE(sequenceNum);
5194 const std::chrono::duration timeout = spy->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005195 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, spy);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005196
5197 spy->finishEvent(*sequenceNum);
5198 spy->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, ADISPLAY_ID_DEFAULT,
5199 0 /*flags*/);
5200 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08005201 mFakePolicy->assertNotifyWindowResponsiveWasCalled(spy->getToken(), mWindow->getPid());
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005202}
5203
5204// If an app is not responding to a key event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005205// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005206TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnKey) {
5207 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005208
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005209 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5210 injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005211 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005212 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005213
5214 // Stuck on the ACTION_UP
5215 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005216 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005217
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005218 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005219 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005220 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5221 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005222
5223 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion
5224 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08005225 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005226 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005227 spy->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005228}
5229
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005230// If an app is not responding to a motion event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005231// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005232TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnMotion) {
5233 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005234
5235 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005236 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5237 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005238
5239 mWindow->consumeMotionDown();
5240 // Stuck on the ACTION_UP
5241 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005242 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005243
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005244 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005245 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005246 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5247 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005248
5249 mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion
5250 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08005251 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005252 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005253 spy->assertNoEvents();
5254}
5255
5256TEST_F(InputDispatcherSingleWindowAnr, UnresponsiveMonitorAnr) {
5257 mDispatcher->setMonitorDispatchingTimeoutForTest(30ms);
5258
5259 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
5260
5261 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5262 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5263 WINDOW_LOCATION));
5264
5265 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5266 const std::optional<uint32_t> consumeSeq = monitor.receiveEvent();
5267 ASSERT_TRUE(consumeSeq);
5268
Prabir Pradhanedd96402022-02-15 01:46:16 -08005269 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(30ms, monitor.getToken(), MONITOR_PID);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005270
5271 monitor.finishEvent(*consumeSeq);
5272 monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
5273
5274 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08005275 mFakePolicy->assertNotifyWindowResponsiveWasCalled(monitor.getToken(), MONITOR_PID);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005276}
5277
5278// If a window is unresponsive, then you get anr. if the window later catches up and starts to
5279// process events, you don't get an anr. When the window later becomes unresponsive again, you
5280// get an ANR again.
5281// 1. tap -> block on ACTION_UP -> receive ANR
5282// 2. consume all pending events (= queue becomes healthy again)
5283// 3. tap again -> block on ACTION_UP again -> receive ANR second time
5284TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) {
5285 tapOnWindow();
5286
5287 mWindow->consumeMotionDown();
5288 // Block on ACTION_UP
5289 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005290 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005291 mWindow->consumeMotionUp(); // Now the connection should be healthy again
5292 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08005293 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005294 mWindow->assertNoEvents();
5295
5296 tapOnWindow();
5297 mWindow->consumeMotionDown();
Prabir Pradhanedd96402022-02-15 01:46:16 -08005298 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005299 mWindow->consumeMotionUp();
5300
5301 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08005302 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005303 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005304 mWindow->assertNoEvents();
5305}
5306
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005307// If a connection remains unresponsive for a while, make sure policy is only notified once about
5308// it.
5309TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005310 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005311 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5312 WINDOW_LOCATION));
5313
5314 const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005315 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005316 std::this_thread::sleep_for(windowTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005317 // 'notifyConnectionUnresponsive' should only be called once per connection
5318 mFakePolicy->assertNotifyAnrWasNotCalled();
5319 // When the ANR happened, dispatcher should abort the current event stream via ACTION_CANCEL
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005320 mWindow->consumeMotionDown();
5321 mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL,
5322 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
5323 mWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005324 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08005325 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005326 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005327}
5328
5329/**
5330 * If a window is processing a motion event, and then a key event comes in, the key event should
5331 * not to to the focused window until the motion is processed.
5332 *
5333 * Warning!!!
5334 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
5335 * and the injection timeout that we specify when injecting the key.
5336 * We must have the injection timeout (10ms) be smaller than
5337 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
5338 *
5339 * If that value changes, this test should also change.
5340 */
5341TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) {
5342 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
5343 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
5344
5345 tapOnWindow();
5346 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
5347 ASSERT_TRUE(downSequenceNum);
5348 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
5349 ASSERT_TRUE(upSequenceNum);
5350 // Don't finish the events yet, and send a key
5351 // Injection will "succeed" because we will eventually give up and send the key to the focused
5352 // window even if motions are still being processed. But because the injection timeout is short,
5353 // we will receive INJECTION_TIMED_OUT as the result.
5354
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005355 InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005356 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005357 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
5358 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005359 // Key will not be sent to the window, yet, because the window is still processing events
5360 // and the key remains pending, waiting for the touch events to be processed
5361 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
5362 ASSERT_FALSE(keySequenceNum);
5363
5364 std::this_thread::sleep_for(500ms);
5365 // if we wait long enough though, dispatcher will give up, and still send the key
5366 // to the focused window, even though we have not yet finished the motion event
5367 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5368 mWindow->finishEvent(*downSequenceNum);
5369 mWindow->finishEvent(*upSequenceNum);
5370}
5371
5372/**
5373 * If a window is processing a motion event, and then a key event comes in, the key event should
5374 * not go to the focused window until the motion is processed.
5375 * If then a new motion comes in, then the pending key event should be going to the currently
5376 * focused window right away.
5377 */
5378TEST_F(InputDispatcherSingleWindowAnr,
5379 PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) {
5380 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
5381 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
5382
5383 tapOnWindow();
5384 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
5385 ASSERT_TRUE(downSequenceNum);
5386 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
5387 ASSERT_TRUE(upSequenceNum);
5388 // Don't finish the events yet, and send a key
5389 // Injection is async, so it will succeed
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005390 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005391 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005392 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005393 // At this point, key is still pending, and should not be sent to the application yet.
5394 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
5395 ASSERT_FALSE(keySequenceNum);
5396
5397 // Now tap down again. It should cause the pending key to go to the focused window right away.
5398 tapOnWindow();
5399 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd
5400 // the other events yet. We can finish events in any order.
5401 mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN
5402 mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP
5403 mWindow->consumeMotionDown();
5404 mWindow->consumeMotionUp();
5405 mWindow->assertNoEvents();
5406}
5407
5408class InputDispatcherMultiWindowAnr : public InputDispatcherTest {
5409 virtual void SetUp() override {
5410 InputDispatcherTest::SetUp();
5411
Chris Yea209fde2020-07-22 13:54:51 -07005412 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005413 mApplication->setDispatchingTimeout(10ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005414 mUnfocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Unfocused",
5415 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005416 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005417 // Adding FLAG_WATCH_OUTSIDE_TOUCH to receive ACTION_OUTSIDE when another window is tapped
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08005418 mUnfocusedWindow->setWatchOutsideTouch(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005419
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005420 mFocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Focused",
5421 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05005422 mFocusedWindow->setDispatchingTimeout(30ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005423 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005424
5425 // Set focused application.
5426 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
Vishnu Nair47074b82020-08-14 11:54:47 -07005427 mFocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005428
5429 // Expect one focus window exist in display.
5430 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005431 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005432 mFocusedWindow->consumeFocusEvent(true);
5433 }
5434
5435 virtual void TearDown() override {
5436 InputDispatcherTest::TearDown();
5437
5438 mUnfocusedWindow.clear();
5439 mFocusedWindow.clear();
5440 }
5441
5442protected:
Chris Yea209fde2020-07-22 13:54:51 -07005443 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005444 sp<FakeWindowHandle> mUnfocusedWindow;
5445 sp<FakeWindowHandle> mFocusedWindow;
5446 static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20};
5447 static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75};
5448 static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40};
5449
5450 void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); }
5451
5452 void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); }
5453
5454private:
5455 void tap(const PointF& location) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005456 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005457 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5458 location));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005459 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005460 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5461 location));
5462 }
5463};
5464
5465// If we have 2 windows that are both unresponsive, the one with the shortest timeout
5466// should be ANR'd first.
5467TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005468 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005469 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5470 FOCUSED_WINDOW_LOCATION))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005471 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005472 mFocusedWindow->consumeMotionDown();
5473 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
5474 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
5475 // We consumed all events, so no ANR
5476 ASSERT_TRUE(mDispatcher->waitForIdle());
5477 mFakePolicy->assertNotifyAnrWasNotCalled();
5478
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005479 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005480 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5481 FOCUSED_WINDOW_LOCATION));
5482 std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent();
5483 ASSERT_TRUE(unfocusedSequenceNum);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005484
5485 const std::chrono::duration timeout =
5486 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005487 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005488 // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event
5489 // sequence to make it consistent
5490 mFocusedWindow->consumeMotionCancel();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005491 mUnfocusedWindow->finishEvent(*unfocusedSequenceNum);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005492 mFocusedWindow->consumeMotionDown();
5493 // This cancel is generated because the connection was unresponsive
5494 mFocusedWindow->consumeMotionCancel();
5495 mFocusedWindow->assertNoEvents();
5496 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005497 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08005498 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
5499 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005500 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005501}
5502
5503// If we have 2 windows with identical timeouts that are both unresponsive,
5504// it doesn't matter which order they should have ANR.
5505// But we should receive ANR for both.
5506TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) {
5507 // Set the timeout for unfocused window to match the focused window
5508 mUnfocusedWindow->setDispatchingTimeout(10ms);
5509 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
5510
5511 tapOnFocusedWindow();
5512 // we should have ACTION_DOWN/ACTION_UP on focused window and ACTION_OUTSIDE on unfocused window
Prabir Pradhanedd96402022-02-15 01:46:16 -08005513 sp<IBinder> anrConnectionToken1, anrConnectionToken2;
5514 ASSERT_NO_FATAL_FAILURE(anrConnectionToken1 = mFakePolicy->getUnresponsiveWindowToken(10ms));
5515 ASSERT_NO_FATAL_FAILURE(anrConnectionToken2 = mFakePolicy->getUnresponsiveWindowToken(0ms));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005516
5517 // We don't know which window will ANR first. But both of them should happen eventually.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005518 ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 ||
5519 mFocusedWindow->getToken() == anrConnectionToken2);
5520 ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 ||
5521 mUnfocusedWindow->getToken() == anrConnectionToken2);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005522
5523 ASSERT_TRUE(mDispatcher->waitForIdle());
5524 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005525
5526 mFocusedWindow->consumeMotionDown();
5527 mFocusedWindow->consumeMotionUp();
5528 mUnfocusedWindow->consumeMotionOutside();
5529
Prabir Pradhanedd96402022-02-15 01:46:16 -08005530 sp<IBinder> responsiveToken1, responsiveToken2;
5531 ASSERT_NO_FATAL_FAILURE(responsiveToken1 = mFakePolicy->getResponsiveWindowToken());
5532 ASSERT_NO_FATAL_FAILURE(responsiveToken2 = mFakePolicy->getResponsiveWindowToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005533
5534 // Both applications should be marked as responsive, in any order
5535 ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 ||
5536 mFocusedWindow->getToken() == responsiveToken2);
5537 ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 ||
5538 mUnfocusedWindow->getToken() == responsiveToken2);
5539 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005540}
5541
5542// If a window is already not responding, the second tap on the same window should be ignored.
5543// We should also log an error to account for the dropped event (not tested here).
5544// At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events.
5545TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) {
5546 tapOnFocusedWindow();
5547 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
5548 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
5549 // Receive the events, but don't respond
5550 std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN
5551 ASSERT_TRUE(downEventSequenceNum);
5552 std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP
5553 ASSERT_TRUE(upEventSequenceNum);
5554 const std::chrono::duration timeout =
5555 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005556 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005557
5558 // Tap once again
5559 // We cannot use "tapOnFocusedWindow" because it asserts the injection result to be success
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005560 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005561 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5562 FOCUSED_WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005563 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005564 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5565 FOCUSED_WINDOW_LOCATION));
5566 // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a
5567 // valid touch target
5568 mUnfocusedWindow->assertNoEvents();
5569
5570 // Consume the first tap
5571 mFocusedWindow->finishEvent(*downEventSequenceNum);
5572 mFocusedWindow->finishEvent(*upEventSequenceNum);
5573 ASSERT_TRUE(mDispatcher->waitForIdle());
5574 // The second tap did not go to the focused window
5575 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005576 // Since all events are finished, connection should be deemed healthy again
Prabir Pradhanedd96402022-02-15 01:46:16 -08005577 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
5578 mFocusedWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005579 mFakePolicy->assertNotifyAnrWasNotCalled();
5580}
5581
5582// If you tap outside of all windows, there will not be ANR
5583TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005584 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005585 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5586 LOCATION_OUTSIDE_ALL_WINDOWS));
5587 ASSERT_TRUE(mDispatcher->waitForIdle());
5588 mFakePolicy->assertNotifyAnrWasNotCalled();
5589}
5590
5591// Since the focused window is paused, tapping on it should not produce any events
5592TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) {
5593 mFocusedWindow->setPaused(true);
5594 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
5595
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005596 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005597 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5598 FOCUSED_WINDOW_LOCATION));
5599
5600 std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT));
5601 ASSERT_TRUE(mDispatcher->waitForIdle());
5602 // Should not ANR because the window is paused, and touches shouldn't go to it
5603 mFakePolicy->assertNotifyAnrWasNotCalled();
5604
5605 mFocusedWindow->assertNoEvents();
5606 mUnfocusedWindow->assertNoEvents();
5607}
5608
5609/**
5610 * If a window is processing a motion event, and then a key event comes in, the key event should
5611 * not to to the focused window until the motion is processed.
5612 * If a different window becomes focused at this time, the key should go to that window instead.
5613 *
5614 * Warning!!!
5615 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
5616 * and the injection timeout that we specify when injecting the key.
5617 * We must have the injection timeout (10ms) be smaller than
5618 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
5619 *
5620 * If that value changes, this test should also change.
5621 */
5622TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) {
5623 // Set a long ANR timeout to prevent it from triggering
5624 mFocusedWindow->setDispatchingTimeout(2s);
5625 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
5626
5627 tapOnUnfocusedWindow();
5628 std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent();
5629 ASSERT_TRUE(downSequenceNum);
5630 std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent();
5631 ASSERT_TRUE(upSequenceNum);
5632 // Don't finish the events yet, and send a key
5633 // Injection will succeed because we will eventually give up and send the key to the focused
5634 // window even if motions are still being processed.
5635
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005636 InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005637 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005638 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/);
5639 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005640 // Key will not be sent to the window, yet, because the window is still processing events
5641 // and the key remains pending, waiting for the touch events to be processed
5642 std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent();
5643 ASSERT_FALSE(keySequenceNum);
5644
5645 // Switch the focus to the "unfocused" window that we tapped. Expect the key to go there
Vishnu Nair47074b82020-08-14 11:54:47 -07005646 mFocusedWindow->setFocusable(false);
5647 mUnfocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005648 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005649 setFocusedWindow(mUnfocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005650
5651 // Focus events should precede the key events
5652 mUnfocusedWindow->consumeFocusEvent(true);
5653 mFocusedWindow->consumeFocusEvent(false);
5654
5655 // Finish the tap events, which should unblock dispatcher
5656 mUnfocusedWindow->finishEvent(*downSequenceNum);
5657 mUnfocusedWindow->finishEvent(*upSequenceNum);
5658
5659 // Now that all queues are cleared and no backlog in the connections, the key event
5660 // can finally go to the newly focused "mUnfocusedWindow".
5661 mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5662 mFocusedWindow->assertNoEvents();
5663 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005664 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005665}
5666
5667// When the touch stream is split across 2 windows, and one of them does not respond,
5668// then ANR should be raised and the touch should be canceled for the unresponsive window.
5669// The other window should not be affected by that.
5670TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) {
5671 // Touch Window 1
5672 NotifyMotionArgs motionArgs =
5673 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5674 ADISPLAY_ID_DEFAULT, {FOCUSED_WINDOW_LOCATION});
5675 mDispatcher->notifyMotion(&motionArgs);
5676 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
5677 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
5678
5679 // Touch Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08005680 motionArgs = generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5681 {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005682 mDispatcher->notifyMotion(&motionArgs);
5683
5684 const std::chrono::duration timeout =
5685 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005686 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005687
5688 mUnfocusedWindow->consumeMotionDown();
5689 mFocusedWindow->consumeMotionDown();
5690 // Focused window may or may not receive ACTION_MOVE
5691 // But it should definitely receive ACTION_CANCEL due to the ANR
5692 InputEvent* event;
5693 std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event);
5694 ASSERT_TRUE(moveOrCancelSequenceNum);
5695 mFocusedWindow->finishEvent(*moveOrCancelSequenceNum);
5696 ASSERT_NE(nullptr, event);
5697 ASSERT_EQ(event->getType(), AINPUT_EVENT_TYPE_MOTION);
5698 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
5699 if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) {
5700 mFocusedWindow->consumeMotionCancel();
5701 } else {
5702 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction());
5703 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005704 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08005705 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
5706 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005707
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005708 mUnfocusedWindow->assertNoEvents();
5709 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005710 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005711}
5712
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05005713/**
5714 * If we have no focused window, and a key comes in, we start the ANR timer.
5715 * The focused application should add a focused window before the timer runs out to prevent ANR.
5716 *
5717 * If the user touches another application during this time, the key should be dropped.
5718 * Next, if a new focused window comes in, without toggling the focused application,
5719 * then no ANR should occur.
5720 *
5721 * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication',
5722 * but in some cases the policy may not update the focused application.
5723 */
5724TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) {
5725 std::shared_ptr<FakeApplicationHandle> focusedApplication =
5726 std::make_shared<FakeApplicationHandle>();
5727 focusedApplication->setDispatchingTimeout(60ms);
5728 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication);
5729 // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused.
5730 mFocusedWindow->setFocusable(false);
5731
5732 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
5733 mFocusedWindow->consumeFocusEvent(false);
5734
5735 // Send a key. The ANR timer should start because there is no focused window.
5736 // 'focusedApplication' will get blamed if this timer completes.
5737 // Key will not be sent anywhere because we have no focused window. It will remain pending.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005738 InputEventInjectionResult result =
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05005739 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005740 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/,
5741 false /* allowKeyRepeat */);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005742 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05005743
5744 // Wait until dispatcher starts the "no focused window" timer. If we don't wait here,
5745 // then the injected touches won't cause the focused event to get dropped.
5746 // The dispatcher only checks for whether the queue should be pruned upon queueing.
5747 // If we inject the touch right away and the ANR timer hasn't started, the touch event would
5748 // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'.
5749 // For this test, it means that the key would get delivered to the window once it becomes
5750 // focused.
5751 std::this_thread::sleep_for(10ms);
5752
5753 // Touch unfocused window. This should force the pending key to get dropped.
5754 NotifyMotionArgs motionArgs =
5755 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5756 ADISPLAY_ID_DEFAULT, {UNFOCUSED_WINDOW_LOCATION});
5757 mDispatcher->notifyMotion(&motionArgs);
5758
5759 // We do not consume the motion right away, because that would require dispatcher to first
5760 // process (== drop) the key event, and by that time, ANR will be raised.
5761 // Set the focused window first.
5762 mFocusedWindow->setFocusable(true);
5763 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
5764 setFocusedWindow(mFocusedWindow);
5765 mFocusedWindow->consumeFocusEvent(true);
5766 // We do not call "setFocusedApplication" here, even though the newly focused window belongs
5767 // to another application. This could be a bug / behaviour in the policy.
5768
5769 mUnfocusedWindow->consumeMotionDown();
5770
5771 ASSERT_TRUE(mDispatcher->waitForIdle());
5772 // Should not ANR because we actually have a focused window. It was just added too slowly.
5773 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled());
5774}
5775
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05005776// These tests ensure we cannot send touch events to a window that's positioned behind a window
5777// that has feature NO_INPUT_CHANNEL.
5778// Layout:
5779// Top (closest to user)
5780// mNoInputWindow (above all windows)
5781// mBottomWindow
5782// Bottom (furthest from user)
5783class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest {
5784 virtual void SetUp() override {
5785 InputDispatcherTest::SetUp();
5786
5787 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005788 mNoInputWindow =
5789 sp<FakeWindowHandle>::make(mApplication, mDispatcher,
5790 "Window without input channel", ADISPLAY_ID_DEFAULT,
5791 std::make_optional<sp<IBinder>>(nullptr) /*token*/);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08005792 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05005793 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
5794 // It's perfectly valid for this window to not have an associated input channel
5795
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005796 mBottomWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Bottom window",
5797 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05005798 mBottomWindow->setFrame(Rect(0, 0, 100, 100));
5799
5800 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
5801 }
5802
5803protected:
5804 std::shared_ptr<FakeApplicationHandle> mApplication;
5805 sp<FakeWindowHandle> mNoInputWindow;
5806 sp<FakeWindowHandle> mBottomWindow;
5807};
5808
5809TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) {
5810 PointF touchedPoint = {10, 10};
5811
5812 NotifyMotionArgs motionArgs =
5813 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5814 ADISPLAY_ID_DEFAULT, {touchedPoint});
5815 mDispatcher->notifyMotion(&motionArgs);
5816
5817 mNoInputWindow->assertNoEvents();
5818 // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have
5819 // an input channel, it is not marked as FLAG_NOT_TOUCHABLE,
5820 // and therefore should prevent mBottomWindow from receiving touches
5821 mBottomWindow->assertNoEvents();
5822}
5823
5824/**
5825 * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel,
5826 * ensure that this window does not receive any touches, and blocks touches to windows underneath.
5827 */
5828TEST_F(InputDispatcherMultiWindowOcclusionTests,
5829 NoInputChannelFeature_DropsTouchesWithValidChannel) {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005830 mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher,
5831 "Window with input channel and NO_INPUT_CHANNEL",
5832 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05005833
Prabir Pradhan51e7db02022-02-07 06:02:57 -08005834 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05005835 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
5836 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
5837
5838 PointF touchedPoint = {10, 10};
5839
5840 NotifyMotionArgs motionArgs =
5841 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5842 ADISPLAY_ID_DEFAULT, {touchedPoint});
5843 mDispatcher->notifyMotion(&motionArgs);
5844
5845 mNoInputWindow->assertNoEvents();
5846 mBottomWindow->assertNoEvents();
5847}
5848
Vishnu Nair958da932020-08-21 17:12:37 -07005849class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest {
5850protected:
5851 std::shared_ptr<FakeApplicationHandle> mApp;
5852 sp<FakeWindowHandle> mWindow;
5853 sp<FakeWindowHandle> mMirror;
5854
5855 virtual void SetUp() override {
5856 InputDispatcherTest::SetUp();
5857 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005858 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
5859 mMirror = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindowMirror",
5860 ADISPLAY_ID_DEFAULT, mWindow->getToken());
Vishnu Nair958da932020-08-21 17:12:37 -07005861 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
5862 mWindow->setFocusable(true);
5863 mMirror->setFocusable(true);
5864 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
5865 }
5866};
5867
5868TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) {
5869 // Request focus on a mirrored window
5870 setFocusedWindow(mMirror);
5871
5872 // window gets focused
5873 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005874 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5875 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005876 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
5877}
5878
5879// A focused & mirrored window remains focused only if the window and its mirror are both
5880// focusable.
5881TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) {
5882 setFocusedWindow(mMirror);
5883
5884 // window gets focused
5885 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005886 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5887 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005888 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005889 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
5890 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005891 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
5892
5893 mMirror->setFocusable(false);
5894 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
5895
5896 // window loses focus since one of the windows associated with the token in not focusable
5897 mWindow->consumeFocusEvent(false);
5898
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005899 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
5900 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07005901 mWindow->assertNoEvents();
5902}
5903
5904// A focused & mirrored window remains focused until the window and its mirror both become
5905// invisible.
5906TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) {
5907 setFocusedWindow(mMirror);
5908
5909 // window gets focused
5910 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005911 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5912 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005913 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005914 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
5915 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005916 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
5917
5918 mMirror->setVisible(false);
5919 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
5920
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005921 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5922 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005923 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005924 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
5925 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005926 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
5927
5928 mWindow->setVisible(false);
5929 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
5930
5931 // window loses focus only after all windows associated with the token become invisible.
5932 mWindow->consumeFocusEvent(false);
5933
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005934 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
5935 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07005936 mWindow->assertNoEvents();
5937}
5938
5939// A focused & mirrored window remains focused until both windows are removed.
5940TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) {
5941 setFocusedWindow(mMirror);
5942
5943 // window gets focused
5944 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005945 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5946 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005947 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005948 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
5949 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005950 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
5951
5952 // single window is removed but the window token remains focused
5953 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}});
5954
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005955 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5956 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005957 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005958 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
5959 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005960 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
5961
5962 // Both windows are removed
5963 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
5964 mWindow->consumeFocusEvent(false);
5965
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005966 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
5967 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07005968 mWindow->assertNoEvents();
5969}
5970
5971// Focus request can be pending until one window becomes visible.
5972TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) {
5973 // Request focus on an invisible mirror.
5974 mWindow->setVisible(false);
5975 mMirror->setVisible(false);
5976 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
5977 setFocusedWindow(mMirror);
5978
5979 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005980 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Vishnu Nair958da932020-08-21 17:12:37 -07005981 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005982 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07005983
5984 mMirror->setVisible(true);
5985 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
5986
5987 // window gets focused
5988 mWindow->consumeFocusEvent(true);
5989 // window gets the pending key event
5990 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5991}
Prabir Pradhan99987712020-11-10 18:43:05 -08005992
5993class InputDispatcherPointerCaptureTests : public InputDispatcherTest {
5994protected:
5995 std::shared_ptr<FakeApplicationHandle> mApp;
5996 sp<FakeWindowHandle> mWindow;
5997 sp<FakeWindowHandle> mSecondWindow;
5998
5999 void SetUp() override {
6000 InputDispatcherTest::SetUp();
6001 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006002 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -08006003 mWindow->setFocusable(true);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006004 mSecondWindow =
6005 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -08006006 mSecondWindow->setFocusable(true);
6007
6008 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
6009 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}});
6010
6011 setFocusedWindow(mWindow);
6012 mWindow->consumeFocusEvent(true);
6013 }
6014
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006015 void notifyPointerCaptureChanged(const PointerCaptureRequest& request) {
6016 const NotifyPointerCaptureChangedArgs args = generatePointerCaptureChangedArgs(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08006017 mDispatcher->notifyPointerCaptureChanged(&args);
6018 }
6019
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006020 PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window,
6021 bool enabled) {
Prabir Pradhan99987712020-11-10 18:43:05 -08006022 mDispatcher->requestPointerCapture(window->getToken(), enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006023 auto request = mFakePolicy->assertSetPointerCaptureCalled(enabled);
6024 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08006025 window->consumeCaptureEvent(enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006026 return request;
Prabir Pradhan99987712020-11-10 18:43:05 -08006027 }
6028};
6029
6030TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) {
6031 // Ensure that capture cannot be obtained for unfocused windows.
6032 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
6033 mFakePolicy->assertSetPointerCaptureNotCalled();
6034 mSecondWindow->assertNoEvents();
6035
6036 // Ensure that capture can be enabled from the focus window.
6037 requestAndVerifyPointerCapture(mWindow, true);
6038
6039 // Ensure that capture cannot be disabled from a window that does not have capture.
6040 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false);
6041 mFakePolicy->assertSetPointerCaptureNotCalled();
6042
6043 // Ensure that capture can be disabled from the window with capture.
6044 requestAndVerifyPointerCapture(mWindow, false);
6045}
6046
6047TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006048 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08006049
6050 setFocusedWindow(mSecondWindow);
6051
6052 // Ensure that the capture disabled event was sent first.
6053 mWindow->consumeCaptureEvent(false);
6054 mWindow->consumeFocusEvent(false);
6055 mSecondWindow->consumeFocusEvent(true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006056 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08006057
6058 // Ensure that additional state changes from InputReader are not sent to the window.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006059 notifyPointerCaptureChanged({});
6060 notifyPointerCaptureChanged(request);
6061 notifyPointerCaptureChanged({});
Prabir Pradhan99987712020-11-10 18:43:05 -08006062 mWindow->assertNoEvents();
6063 mSecondWindow->assertNoEvents();
6064 mFakePolicy->assertSetPointerCaptureNotCalled();
6065}
6066
6067TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006068 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08006069
6070 // InputReader unexpectedly disables and enables pointer capture.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006071 notifyPointerCaptureChanged({});
6072 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08006073
6074 // Ensure that Pointer Capture is disabled.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006075 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08006076 mWindow->consumeCaptureEvent(false);
6077 mWindow->assertNoEvents();
6078}
6079
Prabir Pradhan167e6d92021-02-04 16:18:17 -08006080TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) {
6081 requestAndVerifyPointerCapture(mWindow, true);
6082
6083 // The first window loses focus.
6084 setFocusedWindow(mSecondWindow);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006085 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08006086 mWindow->consumeCaptureEvent(false);
6087
6088 // Request Pointer Capture from the second window before the notification from InputReader
6089 // arrives.
6090 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006091 auto request = mFakePolicy->assertSetPointerCaptureCalled(true);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08006092
6093 // InputReader notifies Pointer Capture was disabled (because of the focus change).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006094 notifyPointerCaptureChanged({});
Prabir Pradhan167e6d92021-02-04 16:18:17 -08006095
6096 // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006097 notifyPointerCaptureChanged(request);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08006098
6099 mSecondWindow->consumeFocusEvent(true);
6100 mSecondWindow->consumeCaptureEvent(true);
6101}
6102
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006103TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) {
6104 // App repeatedly enables and disables capture.
6105 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
6106 auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
6107 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
6108 mFakePolicy->assertSetPointerCaptureCalled(false);
6109 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
6110 auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
6111
6112 // InputReader notifies that PointerCapture has been enabled for the first request. Since the
6113 // first request is now stale, this should do nothing.
6114 notifyPointerCaptureChanged(firstRequest);
6115 mWindow->assertNoEvents();
6116
6117 // InputReader notifies that the second request was enabled.
6118 notifyPointerCaptureChanged(secondRequest);
6119 mWindow->consumeCaptureEvent(true);
6120}
6121
Prabir Pradhan7092e262022-05-03 16:51:09 +00006122TEST_F(InputDispatcherPointerCaptureTests, RapidToggleRequests) {
6123 requestAndVerifyPointerCapture(mWindow, true);
6124
6125 // App toggles pointer capture off and on.
6126 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
6127 mFakePolicy->assertSetPointerCaptureCalled(false);
6128
6129 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
6130 auto enableRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
6131
6132 // InputReader notifies that the latest "enable" request was processed, while skipping over the
6133 // preceding "disable" request.
6134 notifyPointerCaptureChanged(enableRequest);
6135
6136 // Since pointer capture was never disabled during the rapid toggle, the window does not receive
6137 // any notifications.
6138 mWindow->assertNoEvents();
6139}
6140
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006141class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest {
6142protected:
6143 constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8;
Bernardo Rufino7393d172021-02-26 13:56:11 +00006144
6145 constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9;
6146 static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY);
6147
6148 constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7;
6149 static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
6150
6151 // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold
6152 constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5;
6153 static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
6154 static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) <
6155 MAXIMUM_OBSCURING_OPACITY);
6156
Bernardo Rufino6d52e542021-02-15 18:38:10 +00006157 static const int32_t TOUCHED_APP_UID = 10001;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006158 static const int32_t APP_B_UID = 10002;
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006159 static const int32_t APP_C_UID = 10003;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006160
6161 sp<FakeWindowHandle> mTouchWindow;
6162
6163 virtual void SetUp() override {
6164 InputDispatcherTest::SetUp();
Bernardo Rufino6d52e542021-02-15 18:38:10 +00006165 mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched");
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006166 mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY);
6167 }
6168
6169 virtual void TearDown() override {
6170 InputDispatcherTest::TearDown();
6171 mTouchWindow.clear();
6172 }
6173
chaviw3277faf2021-05-19 16:45:23 -05006174 sp<FakeWindowHandle> getOccludingWindow(int32_t uid, std::string name, TouchOcclusionMode mode,
6175 float alpha = 1.0f) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006176 sp<FakeWindowHandle> window = getWindow(uid, name);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08006177 window->setTouchable(false);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006178 window->setTouchOcclusionMode(mode);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006179 window->setAlpha(alpha);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006180 return window;
6181 }
6182
6183 sp<FakeWindowHandle> getWindow(int32_t uid, std::string name) {
6184 std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>();
6185 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006186 sp<FakeWindowHandle>::make(app, mDispatcher, name, ADISPLAY_ID_DEFAULT);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006187 // Generate an arbitrary PID based on the UID
6188 window->setOwnerInfo(1777 + (uid % 10000), uid);
6189 return window;
6190 }
6191
6192 void touch(const std::vector<PointF>& points = {PointF{100, 200}}) {
6193 NotifyMotionArgs args =
6194 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6195 ADISPLAY_ID_DEFAULT, points);
6196 mDispatcher->notifyMotion(&args);
6197 }
6198};
6199
6200TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006201 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006202 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006203 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006204
6205 touch();
6206
6207 mTouchWindow->assertNoEvents();
6208}
6209
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00006210TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufino7393d172021-02-26 13:56:11 +00006211 WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) {
6212 const sp<FakeWindowHandle>& w =
6213 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f);
6214 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6215
6216 touch();
6217
6218 mTouchWindow->assertNoEvents();
6219}
6220
6221TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00006222 WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) {
6223 const sp<FakeWindowHandle>& w =
6224 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
6225 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6226
6227 touch();
6228
6229 w->assertNoEvents();
6230}
6231
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006232TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006233 const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW);
6234 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006235
6236 touch();
6237
6238 mTouchWindow->consumeAnyMotionDown();
6239}
6240
6241TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006242 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006243 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006244 w->setFrame(Rect(0, 0, 50, 50));
6245 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006246
6247 touch({PointF{100, 100}});
6248
6249 mTouchWindow->consumeAnyMotionDown();
6250}
6251
6252TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006253 const sp<FakeWindowHandle>& w =
Bernardo Rufino6d52e542021-02-15 18:38:10 +00006254 getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006255 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6256
6257 touch();
6258
6259 mTouchWindow->consumeAnyMotionDown();
6260}
6261
6262TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) {
6263 const sp<FakeWindowHandle>& w =
6264 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
6265 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006266
6267 touch();
6268
6269 mTouchWindow->consumeAnyMotionDown();
6270}
6271
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00006272TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) {
6273 const sp<FakeWindowHandle>& w =
6274 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
6275 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6276
6277 touch();
6278
6279 w->assertNoEvents();
6280}
6281
6282/**
6283 * This is important to make sure apps can't indirectly learn the position of touches (outside vs
6284 * inside) while letting them pass-through. Note that even though touch passes through the occluding
6285 * window, the occluding window will still receive ACTION_OUTSIDE event.
6286 */
6287TEST_F(InputDispatcherUntrustedTouchesTest,
6288 WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) {
6289 const sp<FakeWindowHandle>& w =
6290 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08006291 w->setWatchOutsideTouch(true);
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00006292 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6293
6294 touch();
6295
6296 w->consumeMotionOutside();
6297}
6298
6299TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) {
6300 const sp<FakeWindowHandle>& w =
6301 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08006302 w->setWatchOutsideTouch(true);
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00006303 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6304
6305 touch();
6306
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08006307 w->consumeMotionOutsideWithZeroedCoords();
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00006308}
6309
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006310TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006311 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006312 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
6313 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006314 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6315
6316 touch();
6317
6318 mTouchWindow->consumeAnyMotionDown();
6319}
6320
6321TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) {
6322 const sp<FakeWindowHandle>& w =
6323 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
6324 MAXIMUM_OBSCURING_OPACITY);
6325 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006326
6327 touch();
6328
6329 mTouchWindow->consumeAnyMotionDown();
6330}
6331
6332TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006333 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006334 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
6335 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006336 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6337
6338 touch();
6339
6340 mTouchWindow->assertNoEvents();
6341}
6342
6343TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) {
6344 // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91
6345 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006346 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
6347 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006348 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006349 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
6350 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006351 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
6352
6353 touch();
6354
6355 mTouchWindow->assertNoEvents();
6356}
6357
6358TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) {
6359 // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75
6360 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006361 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
6362 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006363 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006364 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
6365 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006366 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
6367
6368 touch();
6369
6370 mTouchWindow->consumeAnyMotionDown();
6371}
6372
6373TEST_F(InputDispatcherUntrustedTouchesTest,
6374 WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) {
6375 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006376 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
6377 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006378 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006379 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
6380 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006381 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
6382
6383 touch();
6384
6385 mTouchWindow->consumeAnyMotionDown();
6386}
6387
6388TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) {
6389 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006390 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
6391 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006392 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006393 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
6394 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006395 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006396
6397 touch();
6398
6399 mTouchWindow->assertNoEvents();
6400}
6401
Bernardo Rufino6d52e542021-02-15 18:38:10 +00006402TEST_F(InputDispatcherUntrustedTouchesTest,
6403 WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) {
6404 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006405 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
6406 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00006407 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006408 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
6409 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00006410 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
6411
6412 touch();
6413
6414 mTouchWindow->assertNoEvents();
6415}
6416
6417TEST_F(InputDispatcherUntrustedTouchesTest,
6418 WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) {
6419 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006420 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
6421 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00006422 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006423 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
6424 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00006425 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
6426
6427 touch();
6428
6429 mTouchWindow->consumeAnyMotionDown();
6430}
6431
6432TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) {
6433 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006434 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
6435 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00006436 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6437
6438 touch();
6439
6440 mTouchWindow->consumeAnyMotionDown();
6441}
6442
6443TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) {
6444 const sp<FakeWindowHandle>& w =
6445 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED);
6446 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6447
6448 touch();
6449
6450 mTouchWindow->consumeAnyMotionDown();
6451}
6452
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00006453TEST_F(InputDispatcherUntrustedTouchesTest,
6454 OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) {
6455 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
6456 const sp<FakeWindowHandle>& w =
6457 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f);
6458 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6459
6460 touch();
6461
6462 mTouchWindow->assertNoEvents();
6463}
6464
6465TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) {
6466 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
6467 const sp<FakeWindowHandle>& w =
6468 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f);
6469 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6470
6471 touch();
6472
6473 mTouchWindow->consumeAnyMotionDown();
6474}
6475
6476TEST_F(InputDispatcherUntrustedTouchesTest,
6477 OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) {
6478 mDispatcher->setMaximumObscuringOpacityForTouch(1.0f);
6479 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006480 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
6481 OPACITY_ABOVE_THRESHOLD);
6482 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6483
6484 touch();
6485
6486 mTouchWindow->consumeAnyMotionDown();
6487}
6488
6489TEST_F(InputDispatcherUntrustedTouchesTest,
6490 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) {
6491 const sp<FakeWindowHandle>& w1 =
6492 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
6493 OPACITY_BELOW_THRESHOLD);
6494 const sp<FakeWindowHandle>& w2 =
6495 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
6496 OPACITY_BELOW_THRESHOLD);
6497 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
6498
6499 touch();
6500
6501 mTouchWindow->assertNoEvents();
6502}
6503
6504/**
6505 * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the
6506 * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold
6507 * (which alone would result in allowing touches) does not affect the blocking behavior.
6508 */
6509TEST_F(InputDispatcherUntrustedTouchesTest,
6510 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) {
6511 const sp<FakeWindowHandle>& wB =
6512 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
6513 OPACITY_BELOW_THRESHOLD);
6514 const sp<FakeWindowHandle>& wC =
6515 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
6516 OPACITY_BELOW_THRESHOLD);
6517 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
6518
6519 touch();
6520
6521 mTouchWindow->assertNoEvents();
6522}
6523
6524/**
6525 * This test is testing that a window from a different UID but with same application token doesn't
6526 * block the touch. Apps can share the application token for close UI collaboration for example.
6527 */
6528TEST_F(InputDispatcherUntrustedTouchesTest,
6529 WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) {
6530 const sp<FakeWindowHandle>& w =
6531 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
6532 w->setApplicationToken(mTouchWindow->getApplicationToken());
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00006533 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6534
6535 touch();
6536
6537 mTouchWindow->consumeAnyMotionDown();
6538}
6539
arthurhungb89ccb02020-12-30 16:19:01 +08006540class InputDispatcherDragTests : public InputDispatcherTest {
6541protected:
6542 std::shared_ptr<FakeApplicationHandle> mApp;
6543 sp<FakeWindowHandle> mWindow;
6544 sp<FakeWindowHandle> mSecondWindow;
6545 sp<FakeWindowHandle> mDragWindow;
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00006546 sp<FakeWindowHandle> mSpyWindow;
Arthur Hungb75c2aa2022-07-15 09:35:36 +00006547 // Mouse would force no-split, set the id as non-zero to verify if drag state could track it.
6548 static constexpr int32_t MOUSE_POINTER_ID = 1;
arthurhungb89ccb02020-12-30 16:19:01 +08006549
6550 void SetUp() override {
6551 InputDispatcherTest::SetUp();
6552 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006553 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +08006554 mWindow->setFrame(Rect(0, 0, 100, 100));
arthurhungb89ccb02020-12-30 16:19:01 +08006555
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006556 mSecondWindow =
6557 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +08006558 mSecondWindow->setFrame(Rect(100, 0, 200, 100));
arthurhungb89ccb02020-12-30 16:19:01 +08006559
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006560 mSpyWindow =
6561 sp<FakeWindowHandle>::make(mApp, mDispatcher, "SpyWindow", ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00006562 mSpyWindow->setSpy(true);
6563 mSpyWindow->setTrustedOverlay(true);
6564 mSpyWindow->setFrame(Rect(0, 0, 200, 100));
6565
arthurhungb89ccb02020-12-30 16:19:01 +08006566 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00006567 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mSpyWindow, mWindow, mSecondWindow}}});
arthurhungb89ccb02020-12-30 16:19:01 +08006568 }
6569
Arthur Hungb75c2aa2022-07-15 09:35:36 +00006570 void injectDown(int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
6571 switch (fromSource) {
6572 case AINPUT_SOURCE_TOUCHSCREEN:
6573 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6574 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
6575 ADISPLAY_ID_DEFAULT, {50, 50}))
6576 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6577 break;
6578 case AINPUT_SOURCE_STYLUS:
6579 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6580 injectMotionEvent(
6581 mDispatcher,
6582 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
6583 AINPUT_SOURCE_STYLUS)
6584 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
6585 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS)
6586 .x(50)
6587 .y(50))
6588 .build()));
6589 break;
6590 case AINPUT_SOURCE_MOUSE:
6591 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6592 injectMotionEvent(
6593 mDispatcher,
6594 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
6595 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
6596 .pointer(PointerBuilder(MOUSE_POINTER_ID,
6597 AMOTION_EVENT_TOOL_TYPE_MOUSE)
6598 .x(50)
6599 .y(50))
6600 .build()));
6601 break;
6602 default:
6603 FAIL() << "Source " << fromSource << " doesn't support drag and drop";
6604 }
arthurhungb89ccb02020-12-30 16:19:01 +08006605
6606 // Window should receive motion event.
6607 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00006608 // Spy window should also receive motion event
6609 mSpyWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung54745652022-04-20 07:17:41 +00006610 }
6611
6612 // Start performing drag, we will create a drag window and transfer touch to it.
6613 // @param sendDown : if true, send a motion down on first window before perform drag and drop.
6614 // Returns true on success.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00006615 bool startDrag(bool sendDown = true, int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
Arthur Hung54745652022-04-20 07:17:41 +00006616 if (sendDown) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00006617 injectDown(fromSource);
Arthur Hung54745652022-04-20 07:17:41 +00006618 }
arthurhungb89ccb02020-12-30 16:19:01 +08006619
6620 // The drag window covers the entire display
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006621 mDragWindow =
6622 sp<FakeWindowHandle>::make(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00006623 mDragWindow->setTouchableRegion(Region{{0, 0, 0, 0}});
arthurhungb89ccb02020-12-30 16:19:01 +08006624 mDispatcher->setInputWindows(
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00006625 {{ADISPLAY_ID_DEFAULT, {mDragWindow, mSpyWindow, mWindow, mSecondWindow}}});
arthurhungb89ccb02020-12-30 16:19:01 +08006626
6627 // Transfer touch focus to the drag window
Arthur Hung54745652022-04-20 07:17:41 +00006628 bool transferred =
6629 mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(),
6630 true /* isDragDrop */);
6631 if (transferred) {
6632 mWindow->consumeMotionCancel();
6633 mDragWindow->consumeMotionDown();
6634 }
6635 return transferred;
arthurhungb89ccb02020-12-30 16:19:01 +08006636 }
6637};
6638
6639TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00006640 startDrag();
arthurhungb89ccb02020-12-30 16:19:01 +08006641
6642 // Move on window.
6643 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6644 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6645 ADISPLAY_ID_DEFAULT, {50, 50}))
6646 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6647 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
6648 mWindow->consumeDragEvent(false, 50, 50);
6649 mSecondWindow->assertNoEvents();
6650
6651 // Move to another window.
6652 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6653 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6654 ADISPLAY_ID_DEFAULT, {150, 50}))
6655 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6656 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
6657 mWindow->consumeDragEvent(true, 150, 50);
6658 mSecondWindow->consumeDragEvent(false, 50, 50);
6659
6660 // Move back to original window.
6661 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6662 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6663 ADISPLAY_ID_DEFAULT, {50, 50}))
6664 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6665 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
6666 mWindow->consumeDragEvent(false, 50, 50);
6667 mSecondWindow->consumeDragEvent(true, -50, 50);
6668
6669 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6670 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {50, 50}))
6671 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6672 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
6673 mWindow->assertNoEvents();
6674 mSecondWindow->assertNoEvents();
6675}
6676
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00006677TEST_F(InputDispatcherDragTests, DragEnterAndPointerDownPilfersPointers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00006678 startDrag();
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00006679
6680 // No cancel event after drag start
6681 mSpyWindow->assertNoEvents();
6682
6683 const MotionEvent secondFingerDownEvent =
6684 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
6685 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
6686 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
6687 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(60).y(60))
6688 .build();
6689 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6690 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
6691 InputEventInjectionSync::WAIT_FOR_RESULT))
6692 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6693
6694 // Receives cancel for first pointer after next pointer down
6695 mSpyWindow->consumeMotionCancel();
6696 mSpyWindow->consumeMotionDown();
6697
6698 mSpyWindow->assertNoEvents();
6699}
6700
arthurhungf452d0b2021-01-06 00:19:52 +08006701TEST_F(InputDispatcherDragTests, DragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00006702 startDrag();
arthurhungf452d0b2021-01-06 00:19:52 +08006703
6704 // Move on window.
6705 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6706 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6707 ADISPLAY_ID_DEFAULT, {50, 50}))
6708 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6709 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
6710 mWindow->consumeDragEvent(false, 50, 50);
6711 mSecondWindow->assertNoEvents();
6712
6713 // Move to another window.
6714 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6715 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6716 ADISPLAY_ID_DEFAULT, {150, 50}))
6717 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6718 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
6719 mWindow->consumeDragEvent(true, 150, 50);
6720 mSecondWindow->consumeDragEvent(false, 50, 50);
6721
6722 // drop to another window.
6723 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6724 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6725 {150, 50}))
6726 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6727 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
6728 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
6729 mWindow->assertNoEvents();
6730 mSecondWindow->assertNoEvents();
6731}
6732
arthurhung6d4bed92021-03-17 11:59:33 +08006733TEST_F(InputDispatcherDragTests, StylusDragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00006734 startDrag(true, AINPUT_SOURCE_STYLUS);
arthurhung6d4bed92021-03-17 11:59:33 +08006735
6736 // Move on window and keep button pressed.
6737 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6738 injectMotionEvent(mDispatcher,
6739 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
6740 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
6741 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS)
6742 .x(50)
6743 .y(50))
6744 .build()))
6745 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6746 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
6747 mWindow->consumeDragEvent(false, 50, 50);
6748 mSecondWindow->assertNoEvents();
6749
6750 // Move to another window and release button, expect to drop item.
6751 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6752 injectMotionEvent(mDispatcher,
6753 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
6754 .buttonState(0)
6755 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS)
6756 .x(150)
6757 .y(50))
6758 .build()))
6759 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6760 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
6761 mWindow->assertNoEvents();
6762 mSecondWindow->assertNoEvents();
6763 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
6764
6765 // nothing to the window.
6766 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6767 injectMotionEvent(mDispatcher,
6768 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS)
6769 .buttonState(0)
6770 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS)
6771 .x(150)
6772 .y(50))
6773 .build()))
6774 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6775 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
6776 mWindow->assertNoEvents();
6777 mSecondWindow->assertNoEvents();
6778}
6779
Arthur Hung54745652022-04-20 07:17:41 +00006780TEST_F(InputDispatcherDragTests, DragAndDropOnInvalidWindow) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00006781 startDrag();
Arthur Hung6d0571e2021-04-09 20:18:16 +08006782
6783 // Set second window invisible.
6784 mSecondWindow->setVisible(false);
6785 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}});
6786
6787 // Move on window.
6788 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6789 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6790 ADISPLAY_ID_DEFAULT, {50, 50}))
6791 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6792 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
6793 mWindow->consumeDragEvent(false, 50, 50);
6794 mSecondWindow->assertNoEvents();
6795
6796 // Move to another window.
6797 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6798 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6799 ADISPLAY_ID_DEFAULT, {150, 50}))
6800 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6801 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
6802 mWindow->consumeDragEvent(true, 150, 50);
6803 mSecondWindow->assertNoEvents();
6804
6805 // drop to another window.
6806 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6807 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6808 {150, 50}))
6809 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6810 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
6811 mFakePolicy->assertDropTargetEquals(nullptr);
6812 mWindow->assertNoEvents();
6813 mSecondWindow->assertNoEvents();
6814}
6815
Arthur Hung54745652022-04-20 07:17:41 +00006816TEST_F(InputDispatcherDragTests, NoDragAndDropWhenMultiFingers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00006817 // Ensure window could track pointerIds if it didn't support split touch.
6818 mWindow->setPreventSplitting(true);
6819
Arthur Hung54745652022-04-20 07:17:41 +00006820 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6821 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6822 {50, 50}))
6823 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6824 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6825
6826 const MotionEvent secondFingerDownEvent =
6827 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
6828 .displayId(ADISPLAY_ID_DEFAULT)
6829 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
6830 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
6831 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(75).y(50))
6832 .build();
6833 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6834 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
6835 InputEventInjectionSync::WAIT_FOR_RESULT))
6836 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6837 mWindow->consumeMotionPointerDown(1 /* pointerIndex */);
6838
6839 // Should not perform drag and drop when window has multi fingers.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00006840 ASSERT_FALSE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +00006841}
6842
6843TEST_F(InputDispatcherDragTests, DragAndDropWhenSplitTouch) {
6844 // First down on second window.
6845 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6846 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6847 {150, 50}))
6848 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6849
6850 mSecondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6851
6852 // Second down on first window.
6853 const MotionEvent secondFingerDownEvent =
6854 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
6855 .displayId(ADISPLAY_ID_DEFAULT)
6856 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
6857 .pointer(
6858 PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50))
6859 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
6860 .build();
6861 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6862 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
6863 InputEventInjectionSync::WAIT_FOR_RESULT))
6864 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6865 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6866
6867 // Perform drag and drop from first window.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00006868 ASSERT_TRUE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +00006869
6870 // Move on window.
6871 const MotionEvent secondFingerMoveEvent =
6872 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
6873 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
6874 .pointer(
6875 PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50))
6876 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
6877 .build();
6878 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6879 injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT,
6880 InputEventInjectionSync::WAIT_FOR_RESULT));
6881 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
6882 mWindow->consumeDragEvent(false, 50, 50);
6883 mSecondWindow->consumeMotionMove();
6884
6885 // Release the drag pointer should perform drop.
6886 const MotionEvent secondFingerUpEvent =
6887 MotionEventBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
6888 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
6889 .pointer(
6890 PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50))
6891 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
6892 .build();
6893 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6894 injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT,
6895 InputEventInjectionSync::WAIT_FOR_RESULT));
6896 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
6897 mFakePolicy->assertDropTargetEquals(mWindow->getToken());
6898 mWindow->assertNoEvents();
6899 mSecondWindow->consumeMotionMove();
6900}
6901
Arthur Hung3915c1f2022-05-31 07:17:17 +00006902TEST_F(InputDispatcherDragTests, DragAndDropWhenMultiDisplays) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00006903 startDrag();
Arthur Hung3915c1f2022-05-31 07:17:17 +00006904
6905 // Update window of second display.
6906 sp<FakeWindowHandle> windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006907 sp<FakeWindowHandle>::make(mApp, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung3915c1f2022-05-31 07:17:17 +00006908 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
6909
6910 // Let second display has a touch state.
6911 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6912 injectMotionEvent(mDispatcher,
6913 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
6914 AINPUT_SOURCE_TOUCHSCREEN)
6915 .displayId(SECOND_DISPLAY_ID)
6916 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER)
6917 .x(100)
6918 .y(100))
6919 .build()));
6920 windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN,
6921 SECOND_DISPLAY_ID, 0 /* expectedFlag */);
6922 // Update window again.
6923 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
6924
6925 // Move on window.
6926 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6927 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6928 ADISPLAY_ID_DEFAULT, {50, 50}))
6929 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6930 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
6931 mWindow->consumeDragEvent(false, 50, 50);
6932 mSecondWindow->assertNoEvents();
6933
6934 // Move to another window.
6935 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6936 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6937 ADISPLAY_ID_DEFAULT, {150, 50}))
6938 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6939 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
6940 mWindow->consumeDragEvent(true, 150, 50);
6941 mSecondWindow->consumeDragEvent(false, 50, 50);
6942
6943 // drop to another window.
6944 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6945 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6946 {150, 50}))
6947 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6948 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
6949 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
6950 mWindow->assertNoEvents();
6951 mSecondWindow->assertNoEvents();
6952}
6953
Arthur Hungb75c2aa2022-07-15 09:35:36 +00006954TEST_F(InputDispatcherDragTests, MouseDragAndDrop) {
6955 startDrag(true, AINPUT_SOURCE_MOUSE);
6956 // Move on window.
6957 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6958 injectMotionEvent(mDispatcher,
6959 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
6960 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
6961 .pointer(PointerBuilder(MOUSE_POINTER_ID,
6962 AMOTION_EVENT_TOOL_TYPE_MOUSE)
6963 .x(50)
6964 .y(50))
6965 .build()))
6966 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6967 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
6968 mWindow->consumeDragEvent(false, 50, 50);
6969 mSecondWindow->assertNoEvents();
6970
6971 // Move to another window.
6972 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6973 injectMotionEvent(mDispatcher,
6974 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
6975 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
6976 .pointer(PointerBuilder(MOUSE_POINTER_ID,
6977 AMOTION_EVENT_TOOL_TYPE_MOUSE)
6978 .x(150)
6979 .y(50))
6980 .build()))
6981 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6982 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
6983 mWindow->consumeDragEvent(true, 150, 50);
6984 mSecondWindow->consumeDragEvent(false, 50, 50);
6985
6986 // drop to another window.
6987 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6988 injectMotionEvent(mDispatcher,
6989 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
6990 .buttonState(0)
6991 .pointer(PointerBuilder(MOUSE_POINTER_ID,
6992 AMOTION_EVENT_TOOL_TYPE_MOUSE)
6993 .x(150)
6994 .y(50))
6995 .build()))
6996 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6997 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
6998 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
6999 mWindow->assertNoEvents();
7000 mSecondWindow->assertNoEvents();
7001}
7002
Vishnu Nair062a8672021-09-03 16:07:44 -07007003class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {};
7004
7005TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) {
7006 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007007 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
7008 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007009 window->setDropInput(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07007010 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
7011 window->setFocusable(true);
7012 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
7013 setFocusedWindow(window);
7014 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
7015
7016 // With the flag set, window should not get any input
7017 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
7018 mDispatcher->notifyKey(&keyArgs);
7019 window->assertNoEvents();
7020
7021 NotifyMotionArgs motionArgs =
7022 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
7023 ADISPLAY_ID_DEFAULT);
7024 mDispatcher->notifyMotion(&motionArgs);
7025 window->assertNoEvents();
7026
7027 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007028 window->setDropInput(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07007029 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
7030
7031 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
7032 mDispatcher->notifyKey(&keyArgs);
7033 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
7034
7035 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
7036 ADISPLAY_ID_DEFAULT);
7037 mDispatcher->notifyMotion(&motionArgs);
7038 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7039 window->assertNoEvents();
7040}
7041
7042TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) {
7043 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
7044 std::make_shared<FakeApplicationHandle>();
7045 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007046 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
7047 ADISPLAY_ID_DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -07007048 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
7049 obscuringWindow->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007050 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07007051 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007052 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
7053 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007054 window->setDropInputIfObscured(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07007055 window->setOwnerInfo(222, 222);
7056 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
7057 window->setFocusable(true);
7058 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
7059 setFocusedWindow(window);
7060 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
7061
7062 // With the flag set, window should not get any input
7063 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
7064 mDispatcher->notifyKey(&keyArgs);
7065 window->assertNoEvents();
7066
7067 NotifyMotionArgs motionArgs =
7068 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
7069 ADISPLAY_ID_DEFAULT);
7070 mDispatcher->notifyMotion(&motionArgs);
7071 window->assertNoEvents();
7072
7073 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007074 window->setDropInputIfObscured(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07007075 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
7076
7077 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
7078 mDispatcher->notifyKey(&keyArgs);
7079 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
7080
7081 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
7082 ADISPLAY_ID_DEFAULT);
7083 mDispatcher->notifyMotion(&motionArgs);
7084 window->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
7085 window->assertNoEvents();
7086}
7087
7088TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) {
7089 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
7090 std::make_shared<FakeApplicationHandle>();
7091 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007092 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
7093 ADISPLAY_ID_DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -07007094 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
7095 obscuringWindow->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007096 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07007097 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007098 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
7099 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007100 window->setDropInputIfObscured(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07007101 window->setOwnerInfo(222, 222);
7102 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
7103 window->setFocusable(true);
7104 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
7105 setFocusedWindow(window);
7106 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
7107
7108 // With the flag set, window should not get any input
7109 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
7110 mDispatcher->notifyKey(&keyArgs);
7111 window->assertNoEvents();
7112
7113 NotifyMotionArgs motionArgs =
7114 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
7115 ADISPLAY_ID_DEFAULT);
7116 mDispatcher->notifyMotion(&motionArgs);
7117 window->assertNoEvents();
7118
7119 // When the window is no longer obscured because it went on top, it should get input
7120 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, obscuringWindow}}});
7121
7122 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
7123 mDispatcher->notifyKey(&keyArgs);
7124 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
7125
7126 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
7127 ADISPLAY_ID_DEFAULT);
7128 mDispatcher->notifyMotion(&motionArgs);
7129 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7130 window->assertNoEvents();
7131}
7132
Antonio Kantekf16f2832021-09-28 04:39:20 +00007133class InputDispatcherTouchModeChangedTests : public InputDispatcherTest {
7134protected:
7135 std::shared_ptr<FakeApplicationHandle> mApp;
Antonio Kantek15beb512022-06-13 22:35:41 +00007136 std::shared_ptr<FakeApplicationHandle> mSecondaryApp;
Antonio Kantekf16f2832021-09-28 04:39:20 +00007137 sp<FakeWindowHandle> mWindow;
7138 sp<FakeWindowHandle> mSecondWindow;
Antonio Kantek15beb512022-06-13 22:35:41 +00007139 sp<FakeWindowHandle> mThirdWindow;
Antonio Kantekf16f2832021-09-28 04:39:20 +00007140
7141 void SetUp() override {
7142 InputDispatcherTest::SetUp();
7143
7144 mApp = std::make_shared<FakeApplicationHandle>();
Antonio Kantek15beb512022-06-13 22:35:41 +00007145 mSecondaryApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007146 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00007147 mWindow->setFocusable(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00007148 setFocusedWindow(mWindow);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007149 mSecondWindow =
7150 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00007151 mSecondWindow->setFocusable(true);
Antonio Kantek15beb512022-06-13 22:35:41 +00007152 mThirdWindow =
7153 sp<FakeWindowHandle>::make(mSecondaryApp, mDispatcher,
7154 "TestWindow3_SecondaryDisplay", SECOND_DISPLAY_ID);
7155 mThirdWindow->setFocusable(true);
Antonio Kantekf16f2832021-09-28 04:39:20 +00007156
7157 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Antonio Kantek15beb512022-06-13 22:35:41 +00007158 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}},
7159 {SECOND_DISPLAY_ID, {mThirdWindow}}});
7160 mThirdWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
Antonio Kantekf16f2832021-09-28 04:39:20 +00007161 mWindow->consumeFocusEvent(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00007162
Antonio Kantek15beb512022-06-13 22:35:41 +00007163 // Set main display initial touch mode to InputDispatcher::kDefaultInTouchMode.
Prabir Pradhan5735a322022-04-11 17:23:34 +00007164 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, WINDOW_PID,
Antonio Kantek15beb512022-06-13 22:35:41 +00007165 WINDOW_UID, true /* hasPermission */,
7166 ADISPLAY_ID_DEFAULT)) {
Antonio Kantek48710e42022-03-24 14:19:30 -07007167 mWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
7168 mSecondWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +00007169 mThirdWindow->assertNoEvents();
7170 }
7171
7172 // Set secondary display initial touch mode to InputDispatcher::kDefaultInTouchMode.
7173 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, SECONDARY_WINDOW_PID,
7174 SECONDARY_WINDOW_UID, true /* hasPermission */,
7175 SECOND_DISPLAY_ID)) {
7176 mWindow->assertNoEvents();
7177 mSecondWindow->assertNoEvents();
7178 mThirdWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek48710e42022-03-24 14:19:30 -07007179 }
Antonio Kantekf16f2832021-09-28 04:39:20 +00007180 }
7181
Antonio Kantek15beb512022-06-13 22:35:41 +00007182 void changeAndVerifyTouchModeInMainDisplayOnly(bool inTouchMode, int32_t pid, int32_t uid,
7183 bool hasPermission) {
Antonio Kanteka042c022022-07-06 16:51:07 -07007184 ASSERT_TRUE(mDispatcher->setInTouchMode(inTouchMode, pid, uid, hasPermission,
7185 ADISPLAY_ID_DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +00007186 mWindow->consumeTouchModeEvent(inTouchMode);
7187 mSecondWindow->consumeTouchModeEvent(inTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +00007188 mThirdWindow->assertNoEvents();
Antonio Kantekf16f2832021-09-28 04:39:20 +00007189 }
7190};
7191
Antonio Kantek26defcf2022-02-08 01:12:27 +00007192TEST_F(InputDispatcherTouchModeChangedTests, FocusedWindowCanChangeTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -08007193 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek15beb512022-06-13 22:35:41 +00007194 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode,
7195 windowInfo.ownerPid, windowInfo.ownerUid,
7196 false /* hasPermission */);
Antonio Kantekf16f2832021-09-28 04:39:20 +00007197}
7198
Antonio Kantek26defcf2022-02-08 01:12:27 +00007199TEST_F(InputDispatcherTouchModeChangedTests, NonFocusedWindowOwnerCannotChangeTouchMode) {
7200 const WindowInfo& windowInfo = *mWindow->getInfo();
7201 int32_t ownerPid = windowInfo.ownerPid;
7202 int32_t ownerUid = windowInfo.ownerUid;
7203 mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1);
7204 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, ownerPid,
Antonio Kanteka042c022022-07-06 16:51:07 -07007205 ownerUid, false /*hasPermission*/,
7206 ADISPLAY_ID_DEFAULT));
Antonio Kantek26defcf2022-02-08 01:12:27 +00007207 mWindow->assertNoEvents();
7208 mSecondWindow->assertNoEvents();
7209}
7210
7211TEST_F(InputDispatcherTouchModeChangedTests, NonWindowOwnerMayChangeTouchModeOnPermissionGranted) {
7212 const WindowInfo& windowInfo = *mWindow->getInfo();
7213 int32_t ownerPid = windowInfo.ownerPid;
7214 int32_t ownerUid = windowInfo.ownerUid;
7215 mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1);
Antonio Kantek15beb512022-06-13 22:35:41 +00007216 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, ownerPid,
7217 ownerUid, true /*hasPermission*/);
Antonio Kantek26defcf2022-02-08 01:12:27 +00007218}
7219
Antonio Kantekf16f2832021-09-28 04:39:20 +00007220TEST_F(InputDispatcherTouchModeChangedTests, EventIsNotGeneratedIfNotChangingTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -08007221 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek26defcf2022-02-08 01:12:27 +00007222 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode,
7223 windowInfo.ownerPid, windowInfo.ownerUid,
Antonio Kanteka042c022022-07-06 16:51:07 -07007224 true /*hasPermission*/, ADISPLAY_ID_DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +00007225 mWindow->assertNoEvents();
7226 mSecondWindow->assertNoEvents();
7227}
7228
Antonio Kantek15beb512022-06-13 22:35:41 +00007229TEST_F(InputDispatcherTouchModeChangedTests, ChangeTouchOnSecondaryDisplayOnly) {
7230 const WindowInfo& windowInfo = *mThirdWindow->getInfo();
7231 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
7232 windowInfo.ownerPid, windowInfo.ownerUid,
7233 true /*hasPermission*/, SECOND_DISPLAY_ID));
7234 mWindow->assertNoEvents();
7235 mSecondWindow->assertNoEvents();
7236 mThirdWindow->consumeTouchModeEvent(!InputDispatcher::kDefaultInTouchMode);
7237}
7238
Antonio Kantek48710e42022-03-24 14:19:30 -07007239TEST_F(InputDispatcherTouchModeChangedTests, CanChangeTouchModeWhenOwningLastInteractedWindow) {
7240 // Interact with the window first.
7241 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
7242 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
7243 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
7244
7245 // Then remove focus.
7246 mWindow->setFocusable(false);
7247 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
7248
7249 // Assert that caller can switch touch mode by owning one of the last interacted window.
7250 const WindowInfo& windowInfo = *mWindow->getInfo();
7251 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
7252 windowInfo.ownerPid, windowInfo.ownerUid,
Antonio Kanteka042c022022-07-06 16:51:07 -07007253 false /*hasPermission*/, ADISPLAY_ID_DEFAULT));
Antonio Kantek48710e42022-03-24 14:19:30 -07007254}
7255
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007256class InputDispatcherSpyWindowTest : public InputDispatcherTest {
7257public:
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007258 sp<FakeWindowHandle> createSpy() {
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007259 std::shared_ptr<FakeApplicationHandle> application =
7260 std::make_shared<FakeApplicationHandle>();
7261 std::string name = "Fake Spy ";
7262 name += std::to_string(mSpyCount++);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007263 sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher,
7264 name.c_str(), ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007265 spy->setSpy(true);
Prabir Pradhan5c85e052021-12-22 02:27:12 -08007266 spy->setTrustedOverlay(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007267 return spy;
7268 }
7269
7270 sp<FakeWindowHandle> createForeground() {
7271 std::shared_ptr<FakeApplicationHandle> application =
7272 std::make_shared<FakeApplicationHandle>();
7273 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007274 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
7275 ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007276 window->setFocusable(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007277 return window;
7278 }
7279
7280private:
7281 int mSpyCount{0};
7282};
7283
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08007284using InputDispatcherSpyWindowDeathTest = InputDispatcherSpyWindowTest;
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007285/**
Prabir Pradhan5c85e052021-12-22 02:27:12 -08007286 * Adding a spy window that is not a trusted overlay causes Dispatcher to abort.
7287 */
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08007288TEST_F(InputDispatcherSpyWindowDeathTest, UntrustedSpy_AbortsDispatcher) {
7289 ScopedSilentDeath _silentDeath;
7290
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007291 auto spy = createSpy();
Prabir Pradhan5c85e052021-12-22 02:27:12 -08007292 spy->setTrustedOverlay(false);
7293 ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}}),
7294 ".* not a trusted overlay");
7295}
7296
7297/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007298 * Input injection into a display with a spy window but no foreground windows should succeed.
7299 */
7300TEST_F(InputDispatcherSpyWindowTest, NoForegroundWindow) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007301 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007302 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}});
7303
7304 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7305 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
7306 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7307 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7308}
7309
7310/**
7311 * Verify the order in which different input windows receive events. The touched foreground window
7312 * (if there is one) should always receive the event first. When there are multiple spy windows, the
7313 * spy windows will receive the event according to their Z-order, where the top-most spy window will
7314 * receive events before ones belows it.
7315 *
7316 * Here, we set up a scenario with four windows in the following Z order from the top:
7317 * spy1, spy2, window, spy3.
7318 * We then inject an event and verify that the foreground "window" receives it first, followed by
7319 * "spy1" and "spy2". The "spy3" does not receive the event because it is underneath the foreground
7320 * window.
7321 */
7322TEST_F(InputDispatcherSpyWindowTest, ReceivesInputInOrder) {
7323 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007324 auto spy1 = createSpy();
7325 auto spy2 = createSpy();
7326 auto spy3 = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007327 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window, spy3}}});
7328 const std::vector<sp<FakeWindowHandle>> channels{spy1, spy2, window, spy3};
7329 const size_t numChannels = channels.size();
7330
Michael Wright8e9a8562022-02-09 13:44:29 +00007331 base::unique_fd epollFd(epoll_create1(EPOLL_CLOEXEC));
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007332 if (!epollFd.ok()) {
7333 FAIL() << "Failed to create epoll fd";
7334 }
7335
7336 for (size_t i = 0; i < numChannels; i++) {
7337 struct epoll_event event = {.events = EPOLLIN, .data.u64 = i};
7338 if (epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, channels[i]->getChannelFd(), &event) < 0) {
7339 FAIL() << "Failed to add fd to epoll";
7340 }
7341 }
7342
7343 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7344 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
7345 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7346
7347 std::vector<size_t> eventOrder;
7348 std::vector<struct epoll_event> events(numChannels);
7349 for (;;) {
7350 const int nFds = epoll_wait(epollFd.get(), events.data(), static_cast<int>(numChannels),
7351 (100ms).count());
7352 if (nFds < 0) {
7353 FAIL() << "Failed to call epoll_wait";
7354 }
7355 if (nFds == 0) {
7356 break; // epoll_wait timed out
7357 }
7358 for (int i = 0; i < nFds; i++) {
Colin Cross5b799302022-10-18 21:52:41 -07007359 ASSERT_EQ(static_cast<uint32_t>(EPOLLIN), events[i].events);
Siarhei Vishniakou31977182022-09-30 08:51:23 -07007360 eventOrder.push_back(static_cast<size_t>(events[i].data.u64));
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007361 channels[i]->consumeMotionDown();
7362 }
7363 }
7364
7365 // Verify the order in which the events were received.
7366 EXPECT_EQ(3u, eventOrder.size());
7367 EXPECT_EQ(2u, eventOrder[0]); // index 2: window
7368 EXPECT_EQ(0u, eventOrder[1]); // index 0: spy1
7369 EXPECT_EQ(1u, eventOrder[2]); // index 1: spy2
7370}
7371
7372/**
7373 * A spy window using the NOT_TOUCHABLE flag does not receive events.
7374 */
7375TEST_F(InputDispatcherSpyWindowTest, NotTouchable) {
7376 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007377 auto spy = createSpy();
7378 spy->setTouchable(false);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007379 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
7380
7381 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7382 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
7383 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7384 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7385 spy->assertNoEvents();
7386}
7387
7388/**
7389 * A spy window will only receive gestures that originate within its touchable region. Gestures that
7390 * have their ACTION_DOWN outside of the touchable region of the spy window will not be dispatched
7391 * to the window.
7392 */
7393TEST_F(InputDispatcherSpyWindowTest, TouchableRegion) {
7394 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007395 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007396 spy->setTouchableRegion(Region{{0, 0, 20, 20}});
7397 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
7398
7399 // Inject an event outside the spy window's touchable region.
7400 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7401 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
7402 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7403 window->consumeMotionDown();
7404 spy->assertNoEvents();
7405 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7406 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
7407 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7408 window->consumeMotionUp();
7409 spy->assertNoEvents();
7410
7411 // Inject an event inside the spy window's touchable region.
7412 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7413 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7414 {5, 10}))
7415 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7416 window->consumeMotionDown();
7417 spy->consumeMotionDown();
7418}
7419
7420/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007421 * A spy window can listen for touches outside its touchable region using the WATCH_OUTSIDE_TOUCHES
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08007422 * flag, but it will get zero-ed out coordinates if the foreground has a different owner.
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007423 */
7424TEST_F(InputDispatcherSpyWindowTest, WatchOutsideTouches) {
7425 auto window = createForeground();
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08007426 window->setOwnerInfo(12, 34);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007427 auto spy = createSpy();
7428 spy->setWatchOutsideTouch(true);
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08007429 spy->setOwnerInfo(56, 78);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007430 spy->setFrame(Rect{0, 0, 20, 20});
7431 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
7432
7433 // Inject an event outside the spy window's frame and touchable region.
7434 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08007435 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7436 {100, 200}))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007437 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7438 window->consumeMotionDown();
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08007439 spy->consumeMotionOutsideWithZeroedCoords();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007440}
7441
7442/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007443 * Even when a spy window spans over multiple foreground windows, the spy should receive all
7444 * pointers that are down within its bounds.
7445 */
7446TEST_F(InputDispatcherSpyWindowTest, ReceivesMultiplePointers) {
7447 auto windowLeft = createForeground();
7448 windowLeft->setFrame({0, 0, 100, 200});
7449 auto windowRight = createForeground();
7450 windowRight->setFrame({100, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007451 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007452 spy->setFrame({0, 0, 200, 200});
7453 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, windowLeft, windowRight}}});
7454
7455 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7456 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7457 {50, 50}))
7458 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7459 windowLeft->consumeMotionDown();
7460 spy->consumeMotionDown();
7461
7462 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08007463 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007464 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
7465 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
7466 .pointer(
7467 PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50))
7468 .build();
7469 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7470 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
7471 InputEventInjectionSync::WAIT_FOR_RESULT))
7472 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7473 windowRight->consumeMotionDown();
7474 spy->consumeMotionPointerDown(1 /*pointerIndex*/);
7475}
7476
7477/**
7478 * When the first pointer lands outside the spy window and the second pointer lands inside it, the
7479 * the spy should receive the second pointer with ACTION_DOWN.
7480 */
7481TEST_F(InputDispatcherSpyWindowTest, ReceivesSecondPointerAsDown) {
7482 auto window = createForeground();
7483 window->setFrame({0, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007484 auto spyRight = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007485 spyRight->setFrame({100, 0, 200, 200});
7486 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyRight, window}}});
7487
7488 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7489 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7490 {50, 50}))
7491 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7492 window->consumeMotionDown();
7493 spyRight->assertNoEvents();
7494
7495 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08007496 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007497 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
7498 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
7499 .pointer(
7500 PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50))
7501 .build();
7502 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7503 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
7504 InputEventInjectionSync::WAIT_FOR_RESULT))
7505 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7506 window->consumeMotionPointerDown(1 /*pointerIndex*/);
7507 spyRight->consumeMotionDown();
7508}
7509
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007510/**
7511 * The spy window should not be able to affect whether or not touches are split. Only the foreground
7512 * windows should be allowed to control split touch.
7513 */
7514TEST_F(InputDispatcherSpyWindowTest, SplitIfNoForegroundWindowTouched) {
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08007515 // This spy window prevents touch splitting. However, we still expect to split touches
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007516 // because a foreground window has not disabled splitting.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007517 auto spy = createSpy();
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08007518 spy->setPreventSplitting(true);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007519
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007520 auto window = createForeground();
7521 window->setFrame(Rect(0, 0, 100, 100));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007522
7523 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
7524
7525 // First finger down, no window touched.
7526 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7527 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7528 {100, 200}))
7529 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7530 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7531 window->assertNoEvents();
7532
7533 // Second finger down on window, the window should receive touch down.
7534 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08007535 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007536 .displayId(ADISPLAY_ID_DEFAULT)
7537 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
7538 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
7539 .x(100)
7540 .y(200))
7541 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
7542 .build();
7543 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7544 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
7545 InputEventInjectionSync::WAIT_FOR_RESULT))
7546 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7547
7548 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7549 spy->consumeMotionPointerDown(1 /* pointerIndex */);
7550}
7551
7552/**
7553 * A spy window will usually be implemented as an un-focusable window. Verify that these windows
7554 * do not receive key events.
7555 */
7556TEST_F(InputDispatcherSpyWindowTest, UnfocusableSpyDoesNotReceiveKeyEvents) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007557 auto spy = createSpy();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007558 spy->setFocusable(false);
7559
7560 auto window = createForeground();
7561 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
7562 setFocusedWindow(window);
7563 window->consumeFocusEvent(true);
7564
7565 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7566 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
7567 window->consumeKeyDown(ADISPLAY_ID_NONE);
7568
7569 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7570 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
7571 window->consumeKeyUp(ADISPLAY_ID_NONE);
7572
7573 spy->assertNoEvents();
7574}
7575
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00007576using InputDispatcherPilferPointersTest = InputDispatcherSpyWindowTest;
7577
7578/**
7579 * A spy window can pilfer pointers. When this happens, touch gestures used by the spy window that
7580 * are currently sent to any other windows - including other spy windows - will also be cancelled.
7581 */
7582TEST_F(InputDispatcherPilferPointersTest, PilferPointers) {
7583 auto window = createForeground();
7584 auto spy1 = createSpy();
7585 auto spy2 = createSpy();
7586 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window}}});
7587
7588 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7589 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
7590 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7591 window->consumeMotionDown();
7592 spy1->consumeMotionDown();
7593 spy2->consumeMotionDown();
7594
7595 // Pilfer pointers from the second spy window.
7596 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy2->getToken()));
7597 spy2->assertNoEvents();
7598 spy1->consumeMotionCancel();
7599 window->consumeMotionCancel();
7600
7601 // The rest of the gesture should only be sent to the second spy window.
7602 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7603 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
7604 ADISPLAY_ID_DEFAULT))
7605 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7606 spy2->consumeMotionMove();
7607 spy1->assertNoEvents();
7608 window->assertNoEvents();
7609}
7610
7611/**
7612 * A spy window can pilfer pointers for a gesture even after the foreground window has been removed
7613 * in the middle of the gesture.
7614 */
7615TEST_F(InputDispatcherPilferPointersTest, CanPilferAfterWindowIsRemovedMidStream) {
7616 auto window = createForeground();
7617 auto spy = createSpy();
7618 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
7619
7620 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7621 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
7622 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7623 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7624 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7625
7626 window->releaseChannel();
7627
7628 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
7629
7630 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7631 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
7632 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7633 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
7634}
7635
7636/**
7637 * After a spy window pilfers pointers, new pointers that go down in its bounds should be sent to
7638 * the spy, but not to any other windows.
7639 */
7640TEST_F(InputDispatcherPilferPointersTest, ContinuesToReceiveGestureAfterPilfer) {
7641 auto spy = createSpy();
7642 auto window = createForeground();
7643
7644 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
7645
7646 // First finger down on the window and the spy.
7647 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7648 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7649 {100, 200}))
7650 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7651 spy->consumeMotionDown();
7652 window->consumeMotionDown();
7653
7654 // Spy window pilfers the pointers.
7655 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
7656 window->consumeMotionCancel();
7657
7658 // Second finger down on the window and spy, but the window should not receive the pointer down.
7659 const MotionEvent secondFingerDownEvent =
7660 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7661 .displayId(ADISPLAY_ID_DEFAULT)
7662 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
7663 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
7664 .x(100)
7665 .y(200))
7666 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
7667 .build();
7668 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7669 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
7670 InputEventInjectionSync::WAIT_FOR_RESULT))
7671 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7672
7673 spy->consumeMotionPointerDown(1 /*pointerIndex*/);
7674
7675 // Third finger goes down outside all windows, so injection should fail.
7676 const MotionEvent thirdFingerDownEvent =
7677 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7678 .displayId(ADISPLAY_ID_DEFAULT)
7679 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
7680 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
7681 .x(100)
7682 .y(200))
7683 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
7684 .pointer(PointerBuilder(/* id */ 2, AMOTION_EVENT_TOOL_TYPE_FINGER).x(-5).y(-5))
7685 .build();
7686 ASSERT_EQ(InputEventInjectionResult::FAILED,
7687 injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
7688 InputEventInjectionSync::WAIT_FOR_RESULT))
7689 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7690
7691 spy->assertNoEvents();
7692 window->assertNoEvents();
7693}
7694
7695/**
7696 * After a spy window pilfers pointers, only the pointers used by the spy should be canceled
7697 */
7698TEST_F(InputDispatcherPilferPointersTest, PartiallyPilferRequiredPointers) {
7699 auto spy = createSpy();
7700 spy->setFrame(Rect(0, 0, 100, 100));
7701 auto window = createForeground();
7702 window->setFrame(Rect(0, 0, 200, 200));
7703
7704 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
7705
7706 // First finger down on the window only
7707 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7708 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7709 {150, 150}))
7710 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7711 window->consumeMotionDown();
7712
7713 // Second finger down on the spy and window
7714 const MotionEvent secondFingerDownEvent =
7715 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7716 .displayId(ADISPLAY_ID_DEFAULT)
7717 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
7718 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
7719 .x(150)
7720 .y(150))
7721 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(10).y(10))
7722 .build();
7723 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7724 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
7725 InputEventInjectionSync::WAIT_FOR_RESULT))
7726 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7727 spy->consumeMotionDown();
7728 window->consumeMotionPointerDown(1);
7729
7730 // Third finger down on the spy and window
7731 const MotionEvent thirdFingerDownEvent =
7732 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7733 .displayId(ADISPLAY_ID_DEFAULT)
7734 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
7735 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
7736 .x(150)
7737 .y(150))
7738 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(10).y(10))
7739 .pointer(PointerBuilder(/* id */ 2, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
7740 .build();
7741 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7742 injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
7743 InputEventInjectionSync::WAIT_FOR_RESULT))
7744 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7745 spy->consumeMotionPointerDown(1);
7746 window->consumeMotionPointerDown(2);
7747
7748 // Spy window pilfers the pointers.
7749 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
7750 window->consumeMotionPointerUp(/* idx */ 2, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
7751 window->consumeMotionPointerUp(/* idx */ 1, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
7752
7753 spy->assertNoEvents();
7754 window->assertNoEvents();
7755}
7756
7757/**
7758 * After a spy window pilfers pointers, all pilfered pointers that have already been dispatched to
7759 * other windows should be canceled. If this results in the cancellation of all pointers for some
7760 * window, then that window should receive ACTION_CANCEL.
7761 */
7762TEST_F(InputDispatcherPilferPointersTest, PilferAllRequiredPointers) {
7763 auto spy = createSpy();
7764 spy->setFrame(Rect(0, 0, 100, 100));
7765 auto window = createForeground();
7766 window->setFrame(Rect(0, 0, 200, 200));
7767
7768 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
7769
7770 // First finger down on both spy and window
7771 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7772 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7773 {10, 10}))
7774 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7775 window->consumeMotionDown();
7776 spy->consumeMotionDown();
7777
7778 // Second finger down on the spy and window
7779 const MotionEvent secondFingerDownEvent =
7780 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7781 .displayId(ADISPLAY_ID_DEFAULT)
7782 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
7783 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(10).y(10))
7784 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
7785 .build();
7786 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7787 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
7788 InputEventInjectionSync::WAIT_FOR_RESULT))
7789 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7790 spy->consumeMotionPointerDown(1);
7791 window->consumeMotionPointerDown(1);
7792
7793 // Spy window pilfers the pointers.
7794 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
7795 window->consumeMotionCancel();
7796
7797 spy->assertNoEvents();
7798 window->assertNoEvents();
7799}
7800
7801/**
7802 * After a spy window pilfers pointers, new pointers that are not touching the spy window can still
7803 * be sent to other windows
7804 */
7805TEST_F(InputDispatcherPilferPointersTest, CanReceivePointersAfterPilfer) {
7806 auto spy = createSpy();
7807 spy->setFrame(Rect(0, 0, 100, 100));
7808 auto window = createForeground();
7809 window->setFrame(Rect(0, 0, 200, 200));
7810
7811 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
7812
7813 // First finger down on both window and spy
7814 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7815 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7816 {10, 10}))
7817 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7818 window->consumeMotionDown();
7819 spy->consumeMotionDown();
7820
7821 // Spy window pilfers the pointers.
7822 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
7823 window->consumeMotionCancel();
7824
7825 // Second finger down on the window only
7826 const MotionEvent secondFingerDownEvent =
7827 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7828 .displayId(ADISPLAY_ID_DEFAULT)
7829 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
7830 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(10).y(10))
7831 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER)
7832 .x(150)
7833 .y(150))
7834 .build();
7835 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7836 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
7837 InputEventInjectionSync::WAIT_FOR_RESULT))
7838 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7839 window->consumeMotionDown();
7840 window->assertNoEvents();
7841
7842 // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line
7843 spy->consumeMotionMove();
7844 spy->assertNoEvents();
7845}
7846
Prabir Pradhand65552b2021-10-07 11:23:50 -07007847class InputDispatcherStylusInterceptorTest : public InputDispatcherTest {
7848public:
7849 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupStylusOverlayScenario() {
7850 std::shared_ptr<FakeApplicationHandle> overlayApplication =
7851 std::make_shared<FakeApplicationHandle>();
7852 sp<FakeWindowHandle> overlay =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007853 sp<FakeWindowHandle>::make(overlayApplication, mDispatcher,
7854 "Stylus interceptor window", ADISPLAY_ID_DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -07007855 overlay->setFocusable(false);
7856 overlay->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007857 overlay->setTouchable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007858 overlay->setInterceptsStylus(true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07007859 overlay->setTrustedOverlay(true);
7860
7861 std::shared_ptr<FakeApplicationHandle> application =
7862 std::make_shared<FakeApplicationHandle>();
7863 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007864 sp<FakeWindowHandle>::make(application, mDispatcher, "Application window",
7865 ADISPLAY_ID_DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -07007866 window->setFocusable(true);
7867 window->setOwnerInfo(222, 222);
Prabir Pradhand65552b2021-10-07 11:23:50 -07007868
7869 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
7870 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
7871 setFocusedWindow(window);
7872 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
7873 return {std::move(overlay), std::move(window)};
7874 }
7875
7876 void sendFingerEvent(int32_t action) {
7877 NotifyMotionArgs motionArgs =
7878 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
7879 ADISPLAY_ID_DEFAULT, {PointF{20, 20}});
7880 mDispatcher->notifyMotion(&motionArgs);
7881 }
7882
7883 void sendStylusEvent(int32_t action) {
7884 NotifyMotionArgs motionArgs =
7885 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
7886 ADISPLAY_ID_DEFAULT, {PointF{30, 40}});
7887 motionArgs.pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS;
7888 mDispatcher->notifyMotion(&motionArgs);
7889 }
7890};
7891
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08007892using InputDispatcherStylusInterceptorDeathTest = InputDispatcherStylusInterceptorTest;
7893
7894TEST_F(InputDispatcherStylusInterceptorDeathTest, UntrustedOverlay_AbortsDispatcher) {
7895 ScopedSilentDeath _silentDeath;
7896
Prabir Pradhand65552b2021-10-07 11:23:50 -07007897 auto [overlay, window] = setupStylusOverlayScenario();
7898 overlay->setTrustedOverlay(false);
7899 // Configuring an untrusted overlay as a stylus interceptor should cause Dispatcher to abort.
7900 ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}),
7901 ".* not a trusted overlay");
7902}
7903
7904TEST_F(InputDispatcherStylusInterceptorTest, ConsmesOnlyStylusEvents) {
7905 auto [overlay, window] = setupStylusOverlayScenario();
7906 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
7907
7908 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
7909 overlay->consumeMotionDown();
7910 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
7911 overlay->consumeMotionUp();
7912
7913 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
7914 window->consumeMotionDown();
7915 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
7916 window->consumeMotionUp();
7917
7918 overlay->assertNoEvents();
7919 window->assertNoEvents();
7920}
7921
7922TEST_F(InputDispatcherStylusInterceptorTest, SpyWindowStylusInterceptor) {
7923 auto [overlay, window] = setupStylusOverlayScenario();
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007924 overlay->setSpy(true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07007925 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
7926
7927 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
7928 overlay->consumeMotionDown();
7929 window->consumeMotionDown();
7930 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
7931 overlay->consumeMotionUp();
7932 window->consumeMotionUp();
7933
7934 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
7935 window->consumeMotionDown();
7936 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
7937 window->consumeMotionUp();
7938
7939 overlay->assertNoEvents();
7940 window->assertNoEvents();
7941}
7942
Prabir Pradhan6dfbf262022-03-14 15:24:30 +00007943/**
7944 * Set up a scenario to test the behavior used by the stylus handwriting detection feature.
7945 * The scenario is as follows:
7946 * - The stylus interceptor overlay is configured as a spy window.
7947 * - The stylus interceptor spy receives the start of a new stylus gesture.
7948 * - It pilfers pointers and then configures itself to no longer be a spy.
7949 * - The stylus interceptor continues to receive the rest of the gesture.
7950 */
7951TEST_F(InputDispatcherStylusInterceptorTest, StylusHandwritingScenario) {
7952 auto [overlay, window] = setupStylusOverlayScenario();
7953 overlay->setSpy(true);
7954 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
7955
7956 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
7957 overlay->consumeMotionDown();
7958 window->consumeMotionDown();
7959
7960 // The interceptor pilfers the pointers.
7961 EXPECT_EQ(OK, mDispatcher->pilferPointers(overlay->getToken()));
7962 window->consumeMotionCancel();
7963
7964 // The interceptor configures itself so that it is no longer a spy.
7965 overlay->setSpy(false);
7966 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
7967
7968 // It continues to receive the rest of the stylus gesture.
7969 sendStylusEvent(AMOTION_EVENT_ACTION_MOVE);
7970 overlay->consumeMotionMove();
7971 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
7972 overlay->consumeMotionUp();
7973
7974 window->assertNoEvents();
7975}
7976
Prabir Pradhan5735a322022-04-11 17:23:34 +00007977struct User {
7978 int32_t mPid;
7979 int32_t mUid;
7980 uint32_t mPolicyFlags{DEFAULT_POLICY_FLAGS};
7981 std::unique_ptr<InputDispatcher>& mDispatcher;
7982
7983 User(std::unique_ptr<InputDispatcher>& dispatcher, int32_t pid, int32_t uid)
7984 : mPid(pid), mUid(uid), mDispatcher(dispatcher) {}
7985
7986 InputEventInjectionResult injectTargetedMotion(int32_t action) const {
7987 return injectMotionEvent(mDispatcher, action, AINPUT_SOURCE_TOUCHSCREEN,
7988 ADISPLAY_ID_DEFAULT, {100, 200},
7989 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
7990 AMOTION_EVENT_INVALID_CURSOR_POSITION},
7991 INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT,
7992 systemTime(SYSTEM_TIME_MONOTONIC), {mUid}, mPolicyFlags);
7993 }
7994
7995 InputEventInjectionResult injectTargetedKey(int32_t action) const {
7996 return inputdispatcher::injectKey(mDispatcher, action, 0 /* repeatCount*/, ADISPLAY_ID_NONE,
7997 InputEventInjectionSync::WAIT_FOR_RESULT,
7998 INJECT_EVENT_TIMEOUT, false /*allowKeyRepeat*/, {mUid},
7999 mPolicyFlags);
8000 }
8001
8002 sp<FakeWindowHandle> createWindow() const {
8003 std::shared_ptr<FakeApplicationHandle> overlayApplication =
8004 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008005 sp<FakeWindowHandle> window =
8006 sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, "Owned Window",
8007 ADISPLAY_ID_DEFAULT);
Prabir Pradhan5735a322022-04-11 17:23:34 +00008008 window->setOwnerInfo(mPid, mUid);
8009 return window;
8010 }
8011};
8012
8013using InputDispatcherTargetedInjectionTest = InputDispatcherTest;
8014
8015TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedWindow) {
8016 auto owner = User(mDispatcher, 10, 11);
8017 auto window = owner.createWindow();
8018 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
8019
8020 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
8021 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
8022 window->consumeMotionDown();
8023
8024 setFocusedWindow(window);
8025 window->consumeFocusEvent(true);
8026
8027 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
8028 owner.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
8029 window->consumeKeyDown(ADISPLAY_ID_NONE);
8030}
8031
8032TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedWindow) {
8033 auto owner = User(mDispatcher, 10, 11);
8034 auto window = owner.createWindow();
8035 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
8036
8037 auto rando = User(mDispatcher, 20, 21);
8038 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
8039 rando.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
8040
8041 setFocusedWindow(window);
8042 window->consumeFocusEvent(true);
8043
8044 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
8045 rando.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
8046 window->assertNoEvents();
8047}
8048
8049TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedSpyWindow) {
8050 auto owner = User(mDispatcher, 10, 11);
8051 auto window = owner.createWindow();
8052 auto spy = owner.createWindow();
8053 spy->setSpy(true);
8054 spy->setTrustedOverlay(true);
8055 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8056
8057 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
8058 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
8059 spy->consumeMotionDown();
8060 window->consumeMotionDown();
8061}
8062
8063TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedSpyWindow) {
8064 auto owner = User(mDispatcher, 10, 11);
8065 auto window = owner.createWindow();
8066
8067 auto rando = User(mDispatcher, 20, 21);
8068 auto randosSpy = rando.createWindow();
8069 randosSpy->setSpy(true);
8070 randosSpy->setTrustedOverlay(true);
8071 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}});
8072
8073 // The event is targeted at owner's window, so injection should succeed, but the spy should
8074 // not receive the event.
8075 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
8076 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
8077 randosSpy->assertNoEvents();
8078 window->consumeMotionDown();
8079}
8080
8081TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoAnyWindowWhenNotTargeting) {
8082 auto owner = User(mDispatcher, 10, 11);
8083 auto window = owner.createWindow();
8084
8085 auto rando = User(mDispatcher, 20, 21);
8086 auto randosSpy = rando.createWindow();
8087 randosSpy->setSpy(true);
8088 randosSpy->setTrustedOverlay(true);
8089 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}});
8090
8091 // A user that has injection permission can inject into any window.
8092 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
8093 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
8094 ADISPLAY_ID_DEFAULT));
8095 randosSpy->consumeMotionDown();
8096 window->consumeMotionDown();
8097
8098 setFocusedWindow(randosSpy);
8099 randosSpy->consumeFocusEvent(true);
8100
8101 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher));
8102 randosSpy->consumeKeyDown(ADISPLAY_ID_NONE);
8103 window->assertNoEvents();
8104}
8105
8106TEST_F(InputDispatcherTargetedInjectionTest, CanGenerateActionOutsideToOtherUids) {
8107 auto owner = User(mDispatcher, 10, 11);
8108 auto window = owner.createWindow();
8109
8110 auto rando = User(mDispatcher, 20, 21);
8111 auto randosWindow = rando.createWindow();
8112 randosWindow->setFrame(Rect{-10, -10, -5, -5});
8113 randosWindow->setWatchOutsideTouch(true);
8114 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosWindow, window}}});
8115
8116 // We allow generation of ACTION_OUTSIDE events into windows owned by different uids.
8117 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
8118 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
8119 window->consumeMotionDown();
8120 randosWindow->consumeMotionOutside();
8121}
8122
Garfield Tane84e6f92019-08-29 17:28:41 -07008123} // namespace android::inputdispatcher