blob: 984895007be8d4244914373f072c8dcb3f6d0705 [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);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -080065static constexpr int32_t POINTER_1_UP =
66 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
67
Antonio Kantek15beb512022-06-13 22:35:41 +000068// The default pid and uid for windows created on the primary display by the test.
Prabir Pradhan5735a322022-04-11 17:23:34 +000069static constexpr int32_t WINDOW_PID = 999;
70static constexpr int32_t WINDOW_UID = 1001;
71
Antonio Kantek15beb512022-06-13 22:35:41 +000072// The default pid and uid for the windows created on the secondary display by the test.
73static constexpr int32_t SECONDARY_WINDOW_PID = 1010;
74static constexpr int32_t SECONDARY_WINDOW_UID = 1012;
75
Prabir Pradhan5735a322022-04-11 17:23:34 +000076// The default policy flags to use for event injection by tests.
77static constexpr uint32_t DEFAULT_POLICY_FLAGS = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER;
Michael Wrightd02c5b62014-02-10 15:10:22 -080078
Siarhei Vishniakou58cfc602020-12-14 23:21:30 +000079// An arbitrary pid of the gesture monitor window
80static constexpr int32_t MONITOR_PID = 2001;
81
Siarhei Vishniakou289e9242022-02-15 14:50:16 -080082static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 1000ms;
83
chaviwd1c23182019-12-20 18:44:56 -080084struct PointF {
85 float x;
86 float y;
87};
Michael Wrightd02c5b62014-02-10 15:10:22 -080088
Gang Wang342c9272020-01-13 13:15:04 -050089/**
90 * Return a DOWN key event with KEYCODE_A.
91 */
92static KeyEvent getTestKeyEvent() {
93 KeyEvent event;
94
Garfield Tanfbe732e2020-01-24 11:26:14 -080095 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
96 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
97 ARBITRARY_TIME, ARBITRARY_TIME);
Gang Wang342c9272020-01-13 13:15:04 -050098 return event;
99}
100
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000101static void assertMotionAction(int32_t expectedAction, int32_t receivedAction) {
102 ASSERT_EQ(expectedAction, receivedAction)
103 << "expected " << MotionEvent::actionToString(expectedAction) << ", got "
104 << MotionEvent::actionToString(receivedAction);
105}
106
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -0800107MATCHER_P(WithMotionAction, action, "MotionEvent with specified action") {
108 bool matches = action == arg.getAction();
109 if (!matches) {
110 *result_listener << "expected action " << MotionEvent::actionToString(action)
111 << ", but got " << MotionEvent::actionToString(arg.getAction());
112 }
113 if (action == AMOTION_EVENT_ACTION_CANCEL) {
114 if (!matches) {
115 *result_listener << "; ";
116 }
117 *result_listener << "expected FLAG_CANCELED to be set with ACTION_CANCEL, but was not set";
118 matches &= (arg.getFlags() & AMOTION_EVENT_FLAG_CANCELED) != 0;
119 }
120 return matches;
121}
122
123MATCHER_P(WithSource, source, "InputEvent with specified source") {
124 *result_listener << "expected source " << inputEventSourceToString(source) << ", but got "
125 << inputEventSourceToString(arg.getSource());
126 return arg.getSource() == source;
127}
128
Michael Wrightd02c5b62014-02-10 15:10:22 -0800129// --- FakeInputDispatcherPolicy ---
130
131class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface {
132 InputDispatcherConfiguration mConfig;
133
Prabir Pradhanedd96402022-02-15 01:46:16 -0800134 using AnrResult = std::pair<sp<IBinder>, int32_t /*pid*/>;
135
Michael Wrightd02c5b62014-02-10 15:10:22 -0800136protected:
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000137 virtual ~FakeInputDispatcherPolicy() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800138
139public:
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000140 FakeInputDispatcherPolicy() {}
Jackal Guof9696682018-10-05 12:23:23 +0800141
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800142 void assertFilterInputEventWasCalled(const NotifyKeyArgs& args) {
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700143 assertFilterInputEventWasCalledInternal([&args](const InputEvent& event) {
144 ASSERT_EQ(event.getType(), AINPUT_EVENT_TYPE_KEY);
145 EXPECT_EQ(event.getDisplayId(), args.displayId);
146
147 const auto& keyEvent = static_cast<const KeyEvent&>(event);
148 EXPECT_EQ(keyEvent.getEventTime(), args.eventTime);
149 EXPECT_EQ(keyEvent.getAction(), args.action);
150 });
Jackal Guof9696682018-10-05 12:23:23 +0800151 }
152
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700153 void assertFilterInputEventWasCalled(const NotifyMotionArgs& args, vec2 point) {
154 assertFilterInputEventWasCalledInternal([&](const InputEvent& event) {
155 ASSERT_EQ(event.getType(), AINPUT_EVENT_TYPE_MOTION);
156 EXPECT_EQ(event.getDisplayId(), args.displayId);
157
158 const auto& motionEvent = static_cast<const MotionEvent&>(event);
159 EXPECT_EQ(motionEvent.getEventTime(), args.eventTime);
160 EXPECT_EQ(motionEvent.getAction(), args.action);
161 EXPECT_EQ(motionEvent.getX(0), point.x);
162 EXPECT_EQ(motionEvent.getY(0), point.y);
163 EXPECT_EQ(motionEvent.getRawX(0), point.x);
164 EXPECT_EQ(motionEvent.getRawY(0), point.y);
165 });
Jackal Guof9696682018-10-05 12:23:23 +0800166 }
167
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700168 void assertFilterInputEventWasNotCalled() {
169 std::scoped_lock lock(mLock);
170 ASSERT_EQ(nullptr, mFilteredEvent);
171 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800172
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800173 void assertNotifyConfigurationChangedWasCalled(nsecs_t when) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700174 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800175 ASSERT_TRUE(mConfigurationChangedTime)
176 << "Timed out waiting for configuration changed call";
177 ASSERT_EQ(*mConfigurationChangedTime, when);
178 mConfigurationChangedTime = std::nullopt;
179 }
180
181 void assertNotifySwitchWasCalled(const NotifySwitchArgs& args) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700182 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800183 ASSERT_TRUE(mLastNotifySwitch);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800184 // We do not check id because it is not exposed to the policy
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800185 EXPECT_EQ(args.eventTime, mLastNotifySwitch->eventTime);
186 EXPECT_EQ(args.policyFlags, mLastNotifySwitch->policyFlags);
187 EXPECT_EQ(args.switchValues, mLastNotifySwitch->switchValues);
188 EXPECT_EQ(args.switchMask, mLastNotifySwitch->switchMask);
189 mLastNotifySwitch = std::nullopt;
190 }
191
chaviwfd6d3512019-03-25 13:23:49 -0700192 void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700193 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800194 ASSERT_EQ(touchedToken, mOnPointerDownToken);
195 mOnPointerDownToken.clear();
196 }
197
198 void assertOnPointerDownWasNotCalled() {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700199 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800200 ASSERT_TRUE(mOnPointerDownToken == nullptr)
201 << "Expected onPointerDownOutsideFocus to not have been called";
chaviwfd6d3512019-03-25 13:23:49 -0700202 }
203
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700204 // This function must be called soon after the expected ANR timer starts,
205 // because we are also checking how much time has passed.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500206 void assertNotifyNoFocusedWindowAnrWasCalled(
Chris Yea209fde2020-07-22 13:54:51 -0700207 std::chrono::nanoseconds timeout,
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500208 const std::shared_ptr<InputApplicationHandle>& expectedApplication) {
Prabir Pradhanedd96402022-02-15 01:46:16 -0800209 std::unique_lock lock(mLock);
210 android::base::ScopedLockAssertion assumeLocked(mLock);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500211 std::shared_ptr<InputApplicationHandle> application;
Prabir Pradhanedd96402022-02-15 01:46:16 -0800212 ASSERT_NO_FATAL_FAILURE(
213 application = getAnrTokenLockedInterruptible(timeout, mAnrApplications, lock));
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500214 ASSERT_EQ(expectedApplication, application);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700215 }
216
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000217 void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout,
Prabir Pradhanedd96402022-02-15 01:46:16 -0800218 const sp<WindowInfoHandle>& window) {
219 LOG_ALWAYS_FATAL_IF(window == nullptr, "window should not be null");
220 assertNotifyWindowUnresponsiveWasCalled(timeout, window->getToken(),
221 window->getInfo()->ownerPid);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500222 }
223
Prabir Pradhanedd96402022-02-15 01:46:16 -0800224 void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout,
225 const sp<IBinder>& expectedToken,
226 int32_t expectedPid) {
227 std::unique_lock lock(mLock);
228 android::base::ScopedLockAssertion assumeLocked(mLock);
229 AnrResult result;
230 ASSERT_NO_FATAL_FAILURE(result =
231 getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock));
232 const auto& [token, pid] = result;
233 ASSERT_EQ(expectedToken, token);
234 ASSERT_EQ(expectedPid, pid);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500235 }
236
Prabir Pradhanedd96402022-02-15 01:46:16 -0800237 /** Wrap call with ASSERT_NO_FATAL_FAILURE() to ensure the return value is valid. */
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000238 sp<IBinder> getUnresponsiveWindowToken(std::chrono::nanoseconds timeout) {
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500239 std::unique_lock lock(mLock);
240 android::base::ScopedLockAssertion assumeLocked(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800241 AnrResult result = getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock);
242 const auto& [token, _] = result;
243 return token;
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000244 }
245
Prabir Pradhanedd96402022-02-15 01:46:16 -0800246 void assertNotifyWindowResponsiveWasCalled(const sp<IBinder>& expectedToken,
247 int32_t expectedPid) {
248 std::unique_lock lock(mLock);
249 android::base::ScopedLockAssertion assumeLocked(mLock);
250 AnrResult result;
251 ASSERT_NO_FATAL_FAILURE(
252 result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock));
253 const auto& [token, pid] = result;
254 ASSERT_EQ(expectedToken, token);
255 ASSERT_EQ(expectedPid, pid);
256 }
257
258 /** Wrap call with ASSERT_NO_FATAL_FAILURE() to ensure the return value is valid. */
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000259 sp<IBinder> getResponsiveWindowToken() {
260 std::unique_lock lock(mLock);
261 android::base::ScopedLockAssertion assumeLocked(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800262 AnrResult result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock);
263 const auto& [token, _] = result;
264 return token;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700265 }
266
267 void assertNotifyAnrWasNotCalled() {
268 std::scoped_lock lock(mLock);
269 ASSERT_TRUE(mAnrApplications.empty());
Prabir Pradhanedd96402022-02-15 01:46:16 -0800270 ASSERT_TRUE(mAnrWindows.empty());
271 ASSERT_TRUE(mResponsiveWindows.empty())
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500272 << "ANR was not called, but please also consume the 'connection is responsive' "
273 "signal";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700274 }
275
Garfield Tan1c7bc862020-01-28 13:24:04 -0800276 void setKeyRepeatConfiguration(nsecs_t timeout, nsecs_t delay) {
277 mConfig.keyRepeatTimeout = timeout;
278 mConfig.keyRepeatDelay = delay;
279 }
280
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000281 PointerCaptureRequest assertSetPointerCaptureCalled(bool enabled) {
Prabir Pradhan99987712020-11-10 18:43:05 -0800282 std::unique_lock lock(mLock);
283 base::ScopedLockAssertion assumeLocked(mLock);
284
285 if (!mPointerCaptureChangedCondition.wait_for(lock, 100ms,
286 [this, enabled]() REQUIRES(mLock) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000287 return mPointerCaptureRequest->enable ==
Prabir Pradhan99987712020-11-10 18:43:05 -0800288 enabled;
289 })) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000290 ADD_FAILURE() << "Timed out waiting for setPointerCapture(" << enabled
291 << ") to be called.";
292 return {};
Prabir Pradhan99987712020-11-10 18:43:05 -0800293 }
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000294 auto request = *mPointerCaptureRequest;
295 mPointerCaptureRequest.reset();
296 return request;
Prabir Pradhan99987712020-11-10 18:43:05 -0800297 }
298
299 void assertSetPointerCaptureNotCalled() {
300 std::unique_lock lock(mLock);
301 base::ScopedLockAssertion assumeLocked(mLock);
302
303 if (mPointerCaptureChangedCondition.wait_for(lock, 100ms) != std::cv_status::timeout) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000304 FAIL() << "Expected setPointerCapture(request) to not be called, but was called. "
Prabir Pradhan99987712020-11-10 18:43:05 -0800305 "enabled = "
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000306 << std::to_string(mPointerCaptureRequest->enable);
Prabir Pradhan99987712020-11-10 18:43:05 -0800307 }
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000308 mPointerCaptureRequest.reset();
Prabir Pradhan99987712020-11-10 18:43:05 -0800309 }
310
arthurhungf452d0b2021-01-06 00:19:52 +0800311 void assertDropTargetEquals(const sp<IBinder>& targetToken) {
312 std::scoped_lock lock(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800313 ASSERT_TRUE(mNotifyDropWindowWasCalled);
arthurhungf452d0b2021-01-06 00:19:52 +0800314 ASSERT_EQ(targetToken, mDropTargetWindowToken);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800315 mNotifyDropWindowWasCalled = false;
arthurhungf452d0b2021-01-06 00:19:52 +0800316 }
317
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800318 void assertNotifyInputChannelBrokenWasCalled(const sp<IBinder>& token) {
319 std::unique_lock lock(mLock);
320 base::ScopedLockAssertion assumeLocked(mLock);
321 std::optional<sp<IBinder>> receivedToken =
322 getItemFromStorageLockedInterruptible(100ms, mBrokenInputChannels, lock,
323 mNotifyInputChannelBroken);
324 ASSERT_TRUE(receivedToken.has_value());
325 ASSERT_EQ(token, *receivedToken);
326 }
327
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800328 /**
329 * Set policy timeout. A value of zero means next key will not be intercepted.
330 */
331 void setInterceptKeyTimeout(std::chrono::milliseconds timeout) {
332 mInterceptKeyTimeout = timeout;
333 }
334
Michael Wrightd02c5b62014-02-10 15:10:22 -0800335private:
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700336 std::mutex mLock;
337 std::unique_ptr<InputEvent> mFilteredEvent GUARDED_BY(mLock);
338 std::optional<nsecs_t> mConfigurationChangedTime GUARDED_BY(mLock);
339 sp<IBinder> mOnPointerDownToken GUARDED_BY(mLock);
340 std::optional<NotifySwitchArgs> mLastNotifySwitch GUARDED_BY(mLock);
Jackal Guof9696682018-10-05 12:23:23 +0800341
Prabir Pradhan99987712020-11-10 18:43:05 -0800342 std::condition_variable mPointerCaptureChangedCondition;
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000343
344 std::optional<PointerCaptureRequest> mPointerCaptureRequest GUARDED_BY(mLock);
Prabir Pradhan99987712020-11-10 18:43:05 -0800345
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700346 // ANR handling
Chris Yea209fde2020-07-22 13:54:51 -0700347 std::queue<std::shared_ptr<InputApplicationHandle>> mAnrApplications GUARDED_BY(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800348 std::queue<AnrResult> mAnrWindows GUARDED_BY(mLock);
349 std::queue<AnrResult> mResponsiveWindows GUARDED_BY(mLock);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700350 std::condition_variable mNotifyAnr;
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800351 std::queue<sp<IBinder>> mBrokenInputChannels GUARDED_BY(mLock);
352 std::condition_variable mNotifyInputChannelBroken;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700353
arthurhungf452d0b2021-01-06 00:19:52 +0800354 sp<IBinder> mDropTargetWindowToken GUARDED_BY(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800355 bool mNotifyDropWindowWasCalled GUARDED_BY(mLock) = false;
arthurhungf452d0b2021-01-06 00:19:52 +0800356
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800357 std::chrono::milliseconds mInterceptKeyTimeout = 0ms;
358
Prabir Pradhanedd96402022-02-15 01:46:16 -0800359 // All three ANR-related callbacks behave the same way, so we use this generic function to wait
360 // for a specific container to become non-empty. When the container is non-empty, return the
361 // first entry from the container and erase it.
362 template <class T>
363 T getAnrTokenLockedInterruptible(std::chrono::nanoseconds timeout, std::queue<T>& storage,
364 std::unique_lock<std::mutex>& lock) REQUIRES(mLock) {
365 // If there is an ANR, Dispatcher won't be idle because there are still events
366 // in the waitQueue that we need to check on. So we can't wait for dispatcher to be idle
367 // before checking if ANR was called.
368 // Since dispatcher is not guaranteed to call notifyNoFocusedWindowAnr right away, we need
369 // to provide it some time to act. 100ms seems reasonable.
370 std::chrono::duration timeToWait = timeout + 100ms; // provide some slack
371 const std::chrono::time_point start = std::chrono::steady_clock::now();
372 std::optional<T> token =
373 getItemFromStorageLockedInterruptible(timeToWait, storage, lock, mNotifyAnr);
374 if (!token.has_value()) {
375 ADD_FAILURE() << "Did not receive the ANR callback";
376 return {};
377 }
378
379 const std::chrono::duration waited = std::chrono::steady_clock::now() - start;
380 // Ensure that the ANR didn't get raised too early. We can't be too strict here because
381 // the dispatcher started counting before this function was called
382 if (std::chrono::abs(timeout - waited) > 100ms) {
383 ADD_FAILURE() << "ANR was raised too early or too late. Expected "
384 << std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count()
385 << "ms, but waited "
386 << std::chrono::duration_cast<std::chrono::milliseconds>(waited).count()
387 << "ms instead";
388 }
389 return *token;
390 }
391
392 template <class T>
393 std::optional<T> getItemFromStorageLockedInterruptible(std::chrono::nanoseconds timeout,
394 std::queue<T>& storage,
395 std::unique_lock<std::mutex>& lock,
396 std::condition_variable& condition)
397 REQUIRES(mLock) {
398 condition.wait_for(lock, timeout,
399 [&storage]() REQUIRES(mLock) { return !storage.empty(); });
400 if (storage.empty()) {
401 ADD_FAILURE() << "Did not receive the expected callback";
402 return std::nullopt;
403 }
404 T item = storage.front();
405 storage.pop();
406 return std::make_optional(item);
407 }
408
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600409 void notifyConfigurationChanged(nsecs_t when) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700410 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800411 mConfigurationChangedTime = when;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800412 }
413
Prabir Pradhanedd96402022-02-15 01:46:16 -0800414 void notifyWindowUnresponsive(const sp<IBinder>& connectionToken, std::optional<int32_t> pid,
415 const std::string&) override {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700416 std::scoped_lock lock(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800417 ASSERT_TRUE(pid.has_value());
418 mAnrWindows.push({connectionToken, *pid});
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700419 mNotifyAnr.notify_all();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500420 }
421
Prabir Pradhanedd96402022-02-15 01:46:16 -0800422 void notifyWindowResponsive(const sp<IBinder>& connectionToken,
423 std::optional<int32_t> pid) override {
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500424 std::scoped_lock lock(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800425 ASSERT_TRUE(pid.has_value());
426 mResponsiveWindows.push({connectionToken, *pid});
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500427 mNotifyAnr.notify_all();
428 }
429
430 void notifyNoFocusedWindowAnr(
431 const std::shared_ptr<InputApplicationHandle>& applicationHandle) override {
432 std::scoped_lock lock(mLock);
433 mAnrApplications.push(applicationHandle);
434 mNotifyAnr.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800435 }
436
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800437 void notifyInputChannelBroken(const sp<IBinder>& connectionToken) override {
438 std::scoped_lock lock(mLock);
439 mBrokenInputChannels.push(connectionToken);
440 mNotifyInputChannelBroken.notify_all();
441 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800442
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600443 void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {}
Robert Carr740167f2018-10-11 19:03:41 -0700444
Chris Yef59a2f42020-10-16 12:55:26 -0700445 void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType,
446 InputDeviceSensorAccuracy accuracy, nsecs_t timestamp,
447 const std::vector<float>& values) override {}
448
449 void notifySensorAccuracy(int deviceId, InputDeviceSensorType sensorType,
450 InputDeviceSensorAccuracy accuracy) override {}
Bernardo Rufino2e1f6512020-10-08 13:42:07 +0000451
Chris Yefb552902021-02-03 17:18:37 -0800452 void notifyVibratorState(int32_t deviceId, bool isOn) override {}
453
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600454 void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800455 *outConfig = mConfig;
456 }
457
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600458 bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700459 std::scoped_lock lock(mLock);
Jackal Guof9696682018-10-05 12:23:23 +0800460 switch (inputEvent->getType()) {
461 case AINPUT_EVENT_TYPE_KEY: {
462 const KeyEvent* keyEvent = static_cast<const KeyEvent*>(inputEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800463 mFilteredEvent = std::make_unique<KeyEvent>(*keyEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800464 break;
465 }
466
467 case AINPUT_EVENT_TYPE_MOTION: {
468 const MotionEvent* motionEvent = static_cast<const MotionEvent*>(inputEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800469 mFilteredEvent = std::make_unique<MotionEvent>(*motionEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800470 break;
471 }
472 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800473 return true;
474 }
475
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800476 void interceptKeyBeforeQueueing(const KeyEvent* inputEvent, uint32_t&) override {
477 if (inputEvent->getAction() == AKEY_EVENT_ACTION_UP) {
478 // Clear intercept state when we handled the event.
479 mInterceptKeyTimeout = 0ms;
480 }
481 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800482
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600483 void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800484
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600485 nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent*, uint32_t) override {
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800486 nsecs_t delay = std::chrono::nanoseconds(mInterceptKeyTimeout).count();
487 // Clear intercept state so we could dispatch the event in next wake.
488 mInterceptKeyTimeout = 0ms;
489 return delay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800490 }
491
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600492 bool dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent*, uint32_t, KeyEvent*) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800493 return false;
494 }
495
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600496 void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask,
497 uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700498 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800499 /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is
500 * essentially a passthrough for notifySwitch.
501 */
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800502 mLastNotifySwitch = NotifySwitchArgs(1 /*id*/, when, policyFlags, switchValues, switchMask);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800503 }
504
Sean Stoutb4e0a592021-02-23 07:34:53 -0800505 void pokeUserActivity(nsecs_t, int32_t, int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800506
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600507 void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700508 std::scoped_lock lock(mLock);
chaviwfd6d3512019-03-25 13:23:49 -0700509 mOnPointerDownToken = newToken;
510 }
511
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000512 void setPointerCapture(const PointerCaptureRequest& request) override {
Prabir Pradhan99987712020-11-10 18:43:05 -0800513 std::scoped_lock lock(mLock);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000514 mPointerCaptureRequest = {request};
Prabir Pradhan99987712020-11-10 18:43:05 -0800515 mPointerCaptureChangedCondition.notify_all();
516 }
517
arthurhungf452d0b2021-01-06 00:19:52 +0800518 void notifyDropWindow(const sp<IBinder>& token, float x, float y) override {
519 std::scoped_lock lock(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800520 mNotifyDropWindowWasCalled = true;
arthurhungf452d0b2021-01-06 00:19:52 +0800521 mDropTargetWindowToken = token;
522 }
523
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700524 void assertFilterInputEventWasCalledInternal(
525 const std::function<void(const InputEvent&)>& verify) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700526 std::scoped_lock lock(mLock);
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -0800527 ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called.";
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700528 verify(*mFilteredEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800529 mFilteredEvent = nullptr;
Jackal Guof9696682018-10-05 12:23:23 +0800530 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800531};
532
Michael Wrightd02c5b62014-02-10 15:10:22 -0800533// --- InputDispatcherTest ---
534
535class InputDispatcherTest : public testing::Test {
536protected:
537 sp<FakeInputDispatcherPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700538 std::unique_ptr<InputDispatcher> mDispatcher;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800539
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000540 void SetUp() override {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700541 mFakePolicy = sp<FakeInputDispatcherPolicy>::make();
Siarhei Vishniakou289e9242022-02-15 14:50:16 -0800542 mDispatcher = std::make_unique<InputDispatcher>(mFakePolicy, STALE_EVENT_TIMEOUT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800543 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000544 // Start InputDispatcher thread
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700545 ASSERT_EQ(OK, mDispatcher->start());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800546 }
547
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000548 void TearDown() override {
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700549 ASSERT_EQ(OK, mDispatcher->stop());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800550 mFakePolicy.clear();
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700551 mDispatcher.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800552 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700553
554 /**
555 * Used for debugging when writing the test
556 */
557 void dumpDispatcherState() {
558 std::string dump;
559 mDispatcher->dump(dump);
560 std::stringstream ss(dump);
561 std::string to;
562
563 while (std::getline(ss, to, '\n')) {
564 ALOGE("%s", to.c_str());
565 }
566 }
Vishnu Nair958da932020-08-21 17:12:37 -0700567
chaviw3277faf2021-05-19 16:45:23 -0500568 void setFocusedWindow(const sp<WindowInfoHandle>& window,
569 const sp<WindowInfoHandle>& focusedWindow = nullptr) {
Vishnu Nair958da932020-08-21 17:12:37 -0700570 FocusRequest request;
571 request.token = window->getToken();
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +0000572 request.windowName = window->getName();
Vishnu Nair958da932020-08-21 17:12:37 -0700573 if (focusedWindow) {
574 request.focusedToken = focusedWindow->getToken();
575 }
576 request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
577 request.displayId = window->getInfo()->displayId;
578 mDispatcher->setFocusedWindow(request);
579 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800580};
581
Michael Wrightd02c5b62014-02-10 15:10:22 -0800582TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) {
583 KeyEvent event;
584
585 // Rejects undefined key actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800586 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
587 INVALID_HMAC,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600588 /*action*/ -1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME,
589 ARBITRARY_TIME);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800590 ASSERT_EQ(InputEventInjectionResult::FAILED,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000591 mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE,
592 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800593 << "Should reject key events with undefined action.";
594
595 // Rejects ACTION_MULTIPLE since it is not supported despite being defined in the API.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800596 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
597 INVALID_HMAC, AKEY_EVENT_ACTION_MULTIPLE, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600598 ARBITRARY_TIME, ARBITRARY_TIME);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800599 ASSERT_EQ(InputEventInjectionResult::FAILED,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000600 mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE,
601 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800602 << "Should reject key events with ACTION_MULTIPLE.";
603}
604
605TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) {
606 MotionEvent event;
607 PointerProperties pointerProperties[MAX_POINTERS + 1];
608 PointerCoords pointerCoords[MAX_POINTERS + 1];
Siarhei Vishniakou01747382022-01-20 13:23:27 -0800609 for (size_t i = 0; i <= MAX_POINTERS; i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800610 pointerProperties[i].clear();
611 pointerProperties[i].id = i;
612 pointerCoords[i].clear();
613 }
614
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800615 // Some constants commonly used below
616 constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN;
617 constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE;
618 constexpr int32_t metaState = AMETA_NONE;
619 constexpr MotionClassification classification = MotionClassification::NONE;
620
chaviw9eaa22c2020-07-01 16:21:27 -0700621 ui::Transform identityTransform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800622 // Rejects undefined motion actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800623 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
chaviw9eaa22c2020-07-01 16:21:27 -0700624 /*action*/ -1, 0, 0, edgeFlags, metaState, 0, classification,
625 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700626 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
627 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700628 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800629 ASSERT_EQ(InputEventInjectionResult::FAILED,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000630 mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE,
631 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800632 << "Should reject motion events with undefined action.";
633
634 // Rejects pointer down with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800635 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -0800636 POINTER_1_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
637 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
638 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
639 ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500640 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800641 ASSERT_EQ(InputEventInjectionResult::FAILED,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000642 mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE,
643 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800644 << "Should reject motion events with pointer down index too large.";
645
Garfield Tanfbe732e2020-01-24 11:26:14 -0800646 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700647 AMOTION_EVENT_ACTION_POINTER_DOWN |
648 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700649 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
650 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700651 identityTransform, ARBITRARY_TIME, ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500652 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800653 ASSERT_EQ(InputEventInjectionResult::FAILED,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000654 mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE,
655 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800656 << "Should reject motion events with pointer down index too small.";
657
658 // Rejects pointer up with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800659 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -0800660 POINTER_1_UP, 0, 0, edgeFlags, metaState, 0, classification, identityTransform,
661 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
662 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
663 ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500664 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800665 ASSERT_EQ(InputEventInjectionResult::FAILED,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000666 mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE,
667 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800668 << "Should reject motion events with pointer up index too large.";
669
Garfield Tanfbe732e2020-01-24 11:26:14 -0800670 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700671 AMOTION_EVENT_ACTION_POINTER_UP |
672 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700673 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
674 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700675 identityTransform, ARBITRARY_TIME, ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500676 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800677 ASSERT_EQ(InputEventInjectionResult::FAILED,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000678 mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE,
679 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800680 << "Should reject motion events with pointer up index too small.";
681
682 // Rejects motion events with invalid number of pointers.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800683 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
684 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700685 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700686 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
687 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700688 /*pointerCount*/ 0, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800689 ASSERT_EQ(InputEventInjectionResult::FAILED,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000690 mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE,
691 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800692 << "Should reject motion events with 0 pointers.";
693
Garfield Tanfbe732e2020-01-24 11:26:14 -0800694 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
695 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700696 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700697 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
698 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700699 /*pointerCount*/ MAX_POINTERS + 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800700 ASSERT_EQ(InputEventInjectionResult::FAILED,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000701 mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE,
702 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800703 << "Should reject motion events with more than MAX_POINTERS pointers.";
704
705 // Rejects motion events with invalid pointer ids.
706 pointerProperties[0].id = -1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800707 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
708 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700709 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700710 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
711 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700712 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800713 ASSERT_EQ(InputEventInjectionResult::FAILED,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000714 mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE,
715 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800716 << "Should reject motion events with pointer ids less than 0.";
717
718 pointerProperties[0].id = MAX_POINTER_ID + 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800719 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
720 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700721 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700722 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
723 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700724 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800725 ASSERT_EQ(InputEventInjectionResult::FAILED,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000726 mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE,
727 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800728 << "Should reject motion events with pointer ids greater than MAX_POINTER_ID.";
729
730 // Rejects motion events with duplicate pointer ids.
731 pointerProperties[0].id = 1;
732 pointerProperties[1].id = 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800733 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
734 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700735 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700736 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
737 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700738 /*pointerCount*/ 2, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800739 ASSERT_EQ(InputEventInjectionResult::FAILED,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000740 mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE,
741 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800742 << "Should reject motion events with duplicate pointer ids.";
743}
744
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800745/* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */
746
747TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) {
748 constexpr nsecs_t eventTime = 20;
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800749 NotifyConfigurationChangedArgs args(10 /*id*/, eventTime);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800750 mDispatcher->notifyConfigurationChanged(&args);
751 ASSERT_TRUE(mDispatcher->waitForIdle());
752
753 mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime);
754}
755
756TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) {
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800757 NotifySwitchArgs args(10 /*id*/, 20 /*eventTime*/, 0 /*policyFlags*/, 1 /*switchValues*/,
758 2 /*switchMask*/);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800759 mDispatcher->notifySwitch(&args);
760
761 // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener
762 args.policyFlags |= POLICY_FLAG_TRUSTED;
763 mFakePolicy->assertNotifySwitchWasCalled(args);
764}
765
Arthur Hungb92218b2018-08-14 12:00:21 +0800766// --- InputDispatcherTest SetInputWindowTest ---
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700767static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms;
Siarhei Vishniakou1c494c52021-08-11 20:25:01 -0700768// Default input dispatching timeout if there is no focused application or paused window
769// from which to determine an appropriate dispatching timeout.
770static const std::chrono::duration DISPATCHING_TIMEOUT = std::chrono::milliseconds(
771 android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS *
772 android::base::HwTimeoutMultiplier());
Arthur Hungb92218b2018-08-14 12:00:21 +0800773
774class FakeApplicationHandle : public InputApplicationHandle {
775public:
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700776 FakeApplicationHandle() {
777 mInfo.name = "Fake Application";
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700778 mInfo.token = sp<BBinder>::make();
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500779 mInfo.dispatchingTimeoutMillis =
780 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700781 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800782 virtual ~FakeApplicationHandle() {}
783
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000784 virtual bool updateInfo() override { return true; }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700785
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500786 void setDispatchingTimeout(std::chrono::milliseconds timeout) {
787 mInfo.dispatchingTimeoutMillis = timeout.count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700788 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800789};
790
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800791class FakeInputReceiver {
Arthur Hungb92218b2018-08-14 12:00:21 +0800792public:
Garfield Tan15601662020-09-22 15:32:38 -0700793 explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name)
chaviwd1c23182019-12-20 18:44:56 -0800794 : mName(name) {
Garfield Tan15601662020-09-22 15:32:38 -0700795 mConsumer = std::make_unique<InputConsumer>(std::move(clientChannel));
chaviwd1c23182019-12-20 18:44:56 -0800796 }
797
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800798 InputEvent* consume() {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700799 InputEvent* event;
800 std::optional<uint32_t> consumeSeq = receiveEvent(&event);
801 if (!consumeSeq) {
802 return nullptr;
803 }
804 finishEvent(*consumeSeq);
805 return event;
806 }
807
808 /**
809 * Receive an event without acknowledging it.
810 * Return the sequence number that could later be used to send finished signal.
811 */
812 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Arthur Hungb92218b2018-08-14 12:00:21 +0800813 uint32_t consumeSeq;
814 InputEvent* event;
Arthur Hungb92218b2018-08-14 12:00:21 +0800815
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800816 std::chrono::time_point start = std::chrono::steady_clock::now();
817 status_t status = WOULD_BLOCK;
818 while (status == WOULD_BLOCK) {
chaviw81e2bb92019-12-18 15:03:51 -0800819 status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq,
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800820 &event);
821 std::chrono::duration elapsed = std::chrono::steady_clock::now() - start;
822 if (elapsed > 100ms) {
823 break;
824 }
825 }
826
827 if (status == WOULD_BLOCK) {
828 // Just means there's no event available.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700829 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800830 }
831
832 if (status != OK) {
833 ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK.";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700834 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800835 }
836 if (event == nullptr) {
837 ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700838 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800839 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700840 if (outEvent != nullptr) {
841 *outEvent = event;
842 }
843 return consumeSeq;
844 }
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800845
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700846 /**
847 * To be used together with "receiveEvent" to complete the consumption of an event.
848 */
849 void finishEvent(uint32_t consumeSeq) {
850 const status_t status = mConsumer->sendFinishedSignal(consumeSeq, true);
851 ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK.";
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800852 }
853
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000854 void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) {
855 const status_t status = mConsumer->sendTimeline(inputEventId, timeline);
856 ASSERT_EQ(OK, status);
857 }
858
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000859 void consumeEvent(int32_t expectedEventType, int32_t expectedAction,
860 std::optional<int32_t> expectedDisplayId,
861 std::optional<int32_t> expectedFlags) {
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800862 InputEvent* event = consume();
863
864 ASSERT_NE(nullptr, event) << mName.c_str()
865 << ": consumer should have returned non-NULL event.";
Arthur Hungb92218b2018-08-14 12:00:21 +0800866 ASSERT_EQ(expectedEventType, event->getType())
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700867 << mName.c_str() << " expected " << inputEventTypeToString(expectedEventType)
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800868 << " event, got " << inputEventTypeToString(event->getType()) << " event";
Arthur Hungb92218b2018-08-14 12:00:21 +0800869
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000870 if (expectedDisplayId.has_value()) {
871 EXPECT_EQ(expectedDisplayId, event->getDisplayId());
872 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800873
Tiger Huang8664f8c2018-10-11 19:14:35 +0800874 switch (expectedEventType) {
875 case AINPUT_EVENT_TYPE_KEY: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800876 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event);
877 EXPECT_EQ(expectedAction, keyEvent.getAction());
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000878 if (expectedFlags.has_value()) {
879 EXPECT_EQ(expectedFlags.value(), keyEvent.getFlags());
880 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800881 break;
882 }
883 case AINPUT_EVENT_TYPE_MOTION: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800884 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000885 assertMotionAction(expectedAction, motionEvent.getAction());
886
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000887 if (expectedFlags.has_value()) {
888 EXPECT_EQ(expectedFlags.value(), motionEvent.getFlags());
889 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800890 break;
891 }
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100892 case AINPUT_EVENT_TYPE_FOCUS: {
893 FAIL() << "Use 'consumeFocusEvent' for FOCUS events";
894 }
Prabir Pradhan99987712020-11-10 18:43:05 -0800895 case AINPUT_EVENT_TYPE_CAPTURE: {
896 FAIL() << "Use 'consumeCaptureEvent' for CAPTURE events";
897 }
Antonio Kantekf16f2832021-09-28 04:39:20 +0000898 case AINPUT_EVENT_TYPE_TOUCH_MODE: {
899 FAIL() << "Use 'consumeTouchModeEvent' for TOUCH_MODE events";
900 }
arthurhungb89ccb02020-12-30 16:19:01 +0800901 case AINPUT_EVENT_TYPE_DRAG: {
902 FAIL() << "Use 'consumeDragEvent' for DRAG events";
903 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800904 default: {
905 FAIL() << mName.c_str() << ": invalid event type: " << expectedEventType;
906 }
907 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800908 }
909
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100910 void consumeFocusEvent(bool hasFocus, bool inTouchMode) {
911 InputEvent* event = consume();
912 ASSERT_NE(nullptr, event) << mName.c_str()
913 << ": consumer should have returned non-NULL event.";
914 ASSERT_EQ(AINPUT_EVENT_TYPE_FOCUS, event->getType())
915 << "Got " << inputEventTypeToString(event->getType())
916 << " event instead of FOCUS event";
917
918 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
919 << mName.c_str() << ": event displayId should always be NONE.";
920
921 FocusEvent* focusEvent = static_cast<FocusEvent*>(event);
922 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100923 }
924
Prabir Pradhan99987712020-11-10 18:43:05 -0800925 void consumeCaptureEvent(bool hasCapture) {
926 const InputEvent* event = consume();
927 ASSERT_NE(nullptr, event) << mName.c_str()
928 << ": consumer should have returned non-NULL event.";
929 ASSERT_EQ(AINPUT_EVENT_TYPE_CAPTURE, event->getType())
930 << "Got " << inputEventTypeToString(event->getType())
931 << " event instead of CAPTURE event";
932
933 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
934 << mName.c_str() << ": event displayId should always be NONE.";
935
936 const auto& captureEvent = static_cast<const CaptureEvent&>(*event);
937 EXPECT_EQ(hasCapture, captureEvent.getPointerCaptureEnabled());
938 }
939
arthurhungb89ccb02020-12-30 16:19:01 +0800940 void consumeDragEvent(bool isExiting, float x, float y) {
941 const InputEvent* event = consume();
942 ASSERT_NE(nullptr, event) << mName.c_str()
943 << ": consumer should have returned non-NULL event.";
944 ASSERT_EQ(AINPUT_EVENT_TYPE_DRAG, event->getType())
945 << "Got " << inputEventTypeToString(event->getType())
946 << " event instead of DRAG event";
947
948 EXPECT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
949 << mName.c_str() << ": event displayId should always be NONE.";
950
951 const auto& dragEvent = static_cast<const DragEvent&>(*event);
952 EXPECT_EQ(isExiting, dragEvent.isExiting());
953 EXPECT_EQ(x, dragEvent.getX());
954 EXPECT_EQ(y, dragEvent.getY());
955 }
956
Antonio Kantekf16f2832021-09-28 04:39:20 +0000957 void consumeTouchModeEvent(bool inTouchMode) {
958 const InputEvent* event = consume();
959 ASSERT_NE(nullptr, event) << mName.c_str()
960 << ": consumer should have returned non-NULL event.";
961 ASSERT_EQ(AINPUT_EVENT_TYPE_TOUCH_MODE, event->getType())
962 << "Got " << inputEventTypeToString(event->getType())
963 << " event instead of TOUCH_MODE event";
964
965 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
966 << mName.c_str() << ": event displayId should always be NONE.";
967 const auto& touchModeEvent = static_cast<const TouchModeEvent&>(*event);
968 EXPECT_EQ(inTouchMode, touchModeEvent.isInTouchMode());
969 }
970
chaviwd1c23182019-12-20 18:44:56 -0800971 void assertNoEvents() {
972 InputEvent* event = consume();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700973 if (event == nullptr) {
974 return;
975 }
976 if (event->getType() == AINPUT_EVENT_TYPE_KEY) {
977 KeyEvent& keyEvent = static_cast<KeyEvent&>(*event);
978 ADD_FAILURE() << "Received key event "
979 << KeyEvent::actionToString(keyEvent.getAction());
980 } else if (event->getType() == AINPUT_EVENT_TYPE_MOTION) {
981 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
982 ADD_FAILURE() << "Received motion event "
983 << MotionEvent::actionToString(motionEvent.getAction());
984 } else if (event->getType() == AINPUT_EVENT_TYPE_FOCUS) {
985 FocusEvent& focusEvent = static_cast<FocusEvent&>(*event);
986 ADD_FAILURE() << "Received focus event, hasFocus = "
987 << (focusEvent.getHasFocus() ? "true" : "false");
Prabir Pradhan99987712020-11-10 18:43:05 -0800988 } else if (event->getType() == AINPUT_EVENT_TYPE_CAPTURE) {
989 const auto& captureEvent = static_cast<CaptureEvent&>(*event);
990 ADD_FAILURE() << "Received capture event, pointerCaptureEnabled = "
991 << (captureEvent.getPointerCaptureEnabled() ? "true" : "false");
Antonio Kantekf16f2832021-09-28 04:39:20 +0000992 } else if (event->getType() == AINPUT_EVENT_TYPE_TOUCH_MODE) {
993 const auto& touchModeEvent = static_cast<TouchModeEvent&>(*event);
994 ADD_FAILURE() << "Received touch mode event, inTouchMode = "
995 << (touchModeEvent.isInTouchMode() ? "true" : "false");
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700996 }
997 FAIL() << mName.c_str()
998 << ": should not have received any events, so consume() should return NULL";
chaviwd1c23182019-12-20 18:44:56 -0800999 }
1000
1001 sp<IBinder> getToken() { return mConsumer->getChannel()->getConnectionToken(); }
1002
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001003 int getChannelFd() { return mConsumer->getChannel()->getFd().get(); }
1004
chaviwd1c23182019-12-20 18:44:56 -08001005protected:
1006 std::unique_ptr<InputConsumer> mConsumer;
1007 PreallocatedInputEventFactory mEventFactory;
1008
1009 std::string mName;
1010};
1011
chaviw3277faf2021-05-19 16:45:23 -05001012class FakeWindowHandle : public WindowInfoHandle {
chaviwd1c23182019-12-20 18:44:56 -08001013public:
1014 static const int32_t WIDTH = 600;
1015 static const int32_t HEIGHT = 800;
chaviwd1c23182019-12-20 18:44:56 -08001016
Chris Yea209fde2020-07-22 13:54:51 -07001017 FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001018 const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name,
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001019 int32_t displayId, std::optional<sp<IBinder>> token = std::nullopt)
chaviwd1c23182019-12-20 18:44:56 -08001020 : mName(name) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001021 if (token == std::nullopt) {
Garfield Tan15601662020-09-22 15:32:38 -07001022 base::Result<std::unique_ptr<InputChannel>> channel =
1023 dispatcher->createInputChannel(name);
1024 token = (*channel)->getConnectionToken();
1025 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
chaviwd1c23182019-12-20 18:44:56 -08001026 }
1027
1028 inputApplicationHandle->updateInfo();
1029 mInfo.applicationInfo = *inputApplicationHandle->getInfo();
1030
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001031 mInfo.token = *token;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001032 mInfo.id = sId++;
chaviwd1c23182019-12-20 18:44:56 -08001033 mInfo.name = name;
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -05001034 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001035 mInfo.alpha = 1.0;
chaviwd1c23182019-12-20 18:44:56 -08001036 mInfo.frameLeft = 0;
1037 mInfo.frameTop = 0;
1038 mInfo.frameRight = WIDTH;
1039 mInfo.frameBottom = HEIGHT;
chaviw1ff3d1e2020-07-01 15:53:47 -07001040 mInfo.transform.set(0, 0);
chaviwd1c23182019-12-20 18:44:56 -08001041 mInfo.globalScaleFactor = 1.0;
1042 mInfo.touchableRegion.clear();
1043 mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT));
Prabir Pradhan5735a322022-04-11 17:23:34 +00001044 mInfo.ownerPid = WINDOW_PID;
1045 mInfo.ownerUid = WINDOW_UID;
chaviwd1c23182019-12-20 18:44:56 -08001046 mInfo.displayId = displayId;
Prabir Pradhan51e7db02022-02-07 06:02:57 -08001047 mInfo.inputConfig = WindowInfo::InputConfig::DEFAULT;
chaviwd1c23182019-12-20 18:44:56 -08001048 }
1049
Arthur Hungabbb9d82021-09-01 14:52:30 +00001050 sp<FakeWindowHandle> clone(
1051 const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001052 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t displayId) {
Arthur Hungabbb9d82021-09-01 14:52:30 +00001053 sp<FakeWindowHandle> handle =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001054 sp<FakeWindowHandle>::make(inputApplicationHandle, dispatcher,
1055 mInfo.name + "(Mirror)", displayId, mInfo.token);
Arthur Hungabbb9d82021-09-01 14:52:30 +00001056 return handle;
1057 }
1058
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001059 void setTouchable(bool touchable) {
1060 mInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, !touchable);
1061 }
chaviwd1c23182019-12-20 18:44:56 -08001062
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001063 void setFocusable(bool focusable) {
1064 mInfo.setInputConfig(WindowInfo::InputConfig::NOT_FOCUSABLE, !focusable);
1065 }
1066
1067 void setVisible(bool visible) {
1068 mInfo.setInputConfig(WindowInfo::InputConfig::NOT_VISIBLE, !visible);
1069 }
Vishnu Nair958da932020-08-21 17:12:37 -07001070
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001071 void setDispatchingTimeout(std::chrono::nanoseconds timeout) {
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -05001072 mInfo.dispatchingTimeout = timeout;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001073 }
1074
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001075 void setPaused(bool paused) {
1076 mInfo.setInputConfig(WindowInfo::InputConfig::PAUSE_DISPATCHING, paused);
1077 }
1078
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08001079 void setPreventSplitting(bool preventSplitting) {
1080 mInfo.setInputConfig(WindowInfo::InputConfig::PREVENT_SPLITTING, preventSplitting);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001081 }
1082
1083 void setSlippery(bool slippery) {
1084 mInfo.setInputConfig(WindowInfo::InputConfig::SLIPPERY, slippery);
1085 }
1086
1087 void setWatchOutsideTouch(bool watchOutside) {
1088 mInfo.setInputConfig(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH, watchOutside);
1089 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001090
Prabir Pradhan51e7db02022-02-07 06:02:57 -08001091 void setSpy(bool spy) { mInfo.setInputConfig(WindowInfo::InputConfig::SPY, spy); }
1092
1093 void setInterceptsStylus(bool interceptsStylus) {
1094 mInfo.setInputConfig(WindowInfo::InputConfig::INTERCEPTS_STYLUS, interceptsStylus);
1095 }
1096
1097 void setDropInput(bool dropInput) {
1098 mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT, dropInput);
1099 }
1100
1101 void setDropInputIfObscured(bool dropInputIfObscured) {
1102 mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED, dropInputIfObscured);
1103 }
1104
1105 void setNoInputChannel(bool noInputChannel) {
1106 mInfo.setInputConfig(WindowInfo::InputConfig::NO_INPUT_CHANNEL, noInputChannel);
1107 }
1108
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001109 void setAlpha(float alpha) { mInfo.alpha = alpha; }
1110
chaviw3277faf2021-05-19 16:45:23 -05001111 void setTouchOcclusionMode(TouchOcclusionMode mode) { mInfo.touchOcclusionMode = mode; }
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001112
Bernardo Rufino7393d172021-02-26 13:56:11 +00001113 void setApplicationToken(sp<IBinder> token) { mInfo.applicationInfo.token = token; }
1114
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001115 void setFrame(const Rect& frame, const ui::Transform& displayTransform = ui::Transform()) {
chaviwd1c23182019-12-20 18:44:56 -08001116 mInfo.frameLeft = frame.left;
1117 mInfo.frameTop = frame.top;
1118 mInfo.frameRight = frame.right;
1119 mInfo.frameBottom = frame.bottom;
1120 mInfo.touchableRegion.clear();
1121 mInfo.addTouchableRegion(frame);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001122
1123 const Rect logicalDisplayFrame = displayTransform.transform(frame);
1124 ui::Transform translate;
1125 translate.set(-logicalDisplayFrame.left, -logicalDisplayFrame.top);
1126 mInfo.transform = translate * displayTransform;
chaviwd1c23182019-12-20 18:44:56 -08001127 }
1128
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001129 void setTouchableRegion(const Region& region) { mInfo.touchableRegion = region; }
1130
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001131 void setIsWallpaper(bool isWallpaper) {
1132 mInfo.setInputConfig(WindowInfo::InputConfig::IS_WALLPAPER, isWallpaper);
1133 }
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001134
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001135 void setDupTouchToWallpaper(bool hasWallpaper) {
1136 mInfo.setInputConfig(WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER, hasWallpaper);
1137 }
chaviwd1c23182019-12-20 18:44:56 -08001138
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001139 void setTrustedOverlay(bool trustedOverlay) {
1140 mInfo.setInputConfig(WindowInfo::InputConfig::TRUSTED_OVERLAY, trustedOverlay);
1141 }
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001142
chaviw9eaa22c2020-07-01 16:21:27 -07001143 void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) {
1144 mInfo.transform.set(dsdx, dtdx, dtdy, dsdy);
1145 }
1146
1147 void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); }
chaviwaf87b3e2019-10-01 16:59:28 -07001148
yunho.shinf4a80b82020-11-16 21:13:57 +09001149 void setWindowOffset(float offsetX, float offsetY) { mInfo.transform.set(offsetX, offsetY); }
1150
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001151 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
1152 consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId,
1153 expectedFlags);
1154 }
1155
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001156 void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
1157 consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, expectedDisplayId, expectedFlags);
1158 }
1159
Svet Ganov5d3bc372020-01-26 23:11:07 -08001160 void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001161 int32_t expectedFlags = 0) {
Svet Ganov5d3bc372020-01-26 23:11:07 -08001162 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, expectedDisplayId,
1163 expectedFlags);
1164 }
1165
1166 void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001167 int32_t expectedFlags = 0) {
Svet Ganov5d3bc372020-01-26 23:11:07 -08001168 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, expectedDisplayId,
1169 expectedFlags);
1170 }
1171
1172 void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001173 int32_t expectedFlags = 0) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001174 consumeAnyMotionDown(expectedDisplayId, expectedFlags);
1175 }
1176
1177 void consumeAnyMotionDown(std::optional<int32_t> expectedDisplayId = std::nullopt,
1178 std::optional<int32_t> expectedFlags = std::nullopt) {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001179 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId,
1180 expectedFlags);
1181 }
1182
Svet Ganov5d3bc372020-01-26 23:11:07 -08001183 void consumeMotionPointerDown(int32_t pointerIdx,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001184 int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1185 int32_t expectedFlags = 0) {
1186 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
1187 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001188 consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags);
1189 }
1190
1191 void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001192 int32_t expectedFlags = 0) {
1193 int32_t action = AMOTION_EVENT_ACTION_POINTER_UP |
1194 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001195 consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags);
1196 }
1197
1198 void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001199 int32_t expectedFlags = 0) {
Michael Wright3a240c42019-12-10 20:53:41 +00001200 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId,
1201 expectedFlags);
1202 }
1203
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05001204 void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1205 int32_t expectedFlags = 0) {
1206 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, expectedDisplayId,
1207 expectedFlags);
1208 }
1209
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08001210 void consumeMotionOutsideWithZeroedCoords(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1211 int32_t expectedFlags = 0) {
1212 InputEvent* event = consume();
1213 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType());
1214 const MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
1215 EXPECT_EQ(AMOTION_EVENT_ACTION_OUTSIDE, motionEvent.getActionMasked());
1216 EXPECT_EQ(0.f, motionEvent.getRawPointerCoords(0)->getX());
1217 EXPECT_EQ(0.f, motionEvent.getRawPointerCoords(0)->getY());
1218 }
1219
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001220 void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) {
1221 ASSERT_NE(mInputReceiver, nullptr)
1222 << "Cannot consume events from a window with no receiver";
1223 mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode);
1224 }
1225
Prabir Pradhan99987712020-11-10 18:43:05 -08001226 void consumeCaptureEvent(bool hasCapture) {
1227 ASSERT_NE(mInputReceiver, nullptr)
1228 << "Cannot consume events from a window with no receiver";
1229 mInputReceiver->consumeCaptureEvent(hasCapture);
1230 }
1231
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08001232 void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) {
1233 MotionEvent* motionEvent = consumeMotion();
1234 ASSERT_NE(nullptr, motionEvent) << "Did not get a motion event";
1235 ASSERT_THAT(*motionEvent, matcher);
1236 }
1237
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001238 void consumeEvent(int32_t expectedEventType, int32_t expectedAction,
1239 std::optional<int32_t> expectedDisplayId,
1240 std::optional<int32_t> expectedFlags) {
chaviwd1c23182019-12-20 18:44:56 -08001241 ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver";
1242 mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId,
1243 expectedFlags);
1244 }
1245
arthurhungb89ccb02020-12-30 16:19:01 +08001246 void consumeDragEvent(bool isExiting, float x, float y) {
1247 mInputReceiver->consumeDragEvent(isExiting, x, y);
1248 }
1249
Antonio Kantekf16f2832021-09-28 04:39:20 +00001250 void consumeTouchModeEvent(bool inTouchMode) {
1251 ASSERT_NE(mInputReceiver, nullptr)
1252 << "Cannot consume events from a window with no receiver";
1253 mInputReceiver->consumeTouchModeEvent(inTouchMode);
1254 }
1255
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001256 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001257 if (mInputReceiver == nullptr) {
1258 ADD_FAILURE() << "Invalid receive event on window with no receiver";
1259 return std::nullopt;
1260 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001261 return mInputReceiver->receiveEvent(outEvent);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001262 }
1263
1264 void finishEvent(uint32_t sequenceNum) {
1265 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
1266 mInputReceiver->finishEvent(sequenceNum);
1267 }
1268
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00001269 void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) {
1270 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
1271 mInputReceiver->sendTimeline(inputEventId, timeline);
1272 }
1273
chaviwaf87b3e2019-10-01 16:59:28 -07001274 InputEvent* consume() {
1275 if (mInputReceiver == nullptr) {
1276 return nullptr;
1277 }
1278 return mInputReceiver->consume();
1279 }
1280
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00001281 MotionEvent* consumeMotion() {
1282 InputEvent* event = consume();
1283 if (event == nullptr) {
1284 ADD_FAILURE() << "Consume failed : no event";
1285 return nullptr;
1286 }
1287 if (event->getType() != AINPUT_EVENT_TYPE_MOTION) {
1288 ADD_FAILURE() << "Instead of motion event, got "
1289 << inputEventTypeToString(event->getType());
1290 return nullptr;
1291 }
1292 return static_cast<MotionEvent*>(event);
1293 }
1294
Arthur Hungb92218b2018-08-14 12:00:21 +08001295 void assertNoEvents() {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001296 if (mInputReceiver == nullptr &&
Prabir Pradhan51e7db02022-02-07 06:02:57 -08001297 mInfo.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001298 return; // Can't receive events if the window does not have input channel
1299 }
1300 ASSERT_NE(nullptr, mInputReceiver)
1301 << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL";
chaviwd1c23182019-12-20 18:44:56 -08001302 mInputReceiver->assertNoEvents();
Arthur Hungb92218b2018-08-14 12:00:21 +08001303 }
1304
chaviwaf87b3e2019-10-01 16:59:28 -07001305 sp<IBinder> getToken() { return mInfo.token; }
1306
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001307 const std::string& getName() { return mName; }
1308
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001309 void setOwnerInfo(int32_t ownerPid, int32_t ownerUid) {
1310 mInfo.ownerPid = ownerPid;
1311 mInfo.ownerUid = ownerUid;
1312 }
1313
Prabir Pradhanedd96402022-02-15 01:46:16 -08001314 int32_t getPid() const { return mInfo.ownerPid; }
1315
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001316 void destroyReceiver() { mInputReceiver = nullptr; }
1317
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001318 int getChannelFd() { return mInputReceiver->getChannelFd(); }
1319
chaviwd1c23182019-12-20 18:44:56 -08001320private:
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001321 const std::string mName;
chaviwd1c23182019-12-20 18:44:56 -08001322 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001323 static std::atomic<int32_t> sId; // each window gets a unique id, like in surfaceflinger
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001324};
1325
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001326std::atomic<int32_t> FakeWindowHandle::sId{1};
1327
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001328static InputEventInjectionResult injectKey(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001329 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t repeatCount,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001330 int32_t displayId = ADISPLAY_ID_NONE,
1331 InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001332 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Prabir Pradhan5735a322022-04-11 17:23:34 +00001333 bool allowKeyRepeat = true, std::optional<int32_t> targetUid = {},
1334 uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
Arthur Hungb92218b2018-08-14 12:00:21 +08001335 KeyEvent event;
1336 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1337
1338 // Define a valid key down event.
Garfield Tanfbe732e2020-01-24 11:26:14 -08001339 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001340 INVALID_HMAC, action, /* flags */ 0, AKEYCODE_A, KEY_A, AMETA_NONE,
1341 repeatCount, currentTime, currentTime);
Arthur Hungb92218b2018-08-14 12:00:21 +08001342
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001343 if (!allowKeyRepeat) {
1344 policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT;
1345 }
Arthur Hungb92218b2018-08-14 12:00:21 +08001346 // Inject event until dispatch out.
Prabir Pradhan5735a322022-04-11 17:23:34 +00001347 return dispatcher->injectInputEvent(&event, targetUid, syncMode, injectionTimeout, policyFlags);
Arthur Hungb92218b2018-08-14 12:00:21 +08001348}
1349
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001350static InputEventInjectionResult injectKeyDown(const std::unique_ptr<InputDispatcher>& dispatcher,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001351 int32_t displayId = ADISPLAY_ID_NONE) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001352 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /* repeatCount */ 0, displayId);
1353}
1354
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001355// Inject a down event that has key repeat disabled. This allows InputDispatcher to idle without
1356// sending a subsequent key up. When key repeat is enabled, the dispatcher cannot idle because it
1357// has to be woken up to process the repeating key.
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001358static InputEventInjectionResult injectKeyDownNoRepeat(
1359 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t displayId = ADISPLAY_ID_NONE) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001360 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /* repeatCount */ 0, displayId,
1361 InputEventInjectionSync::WAIT_FOR_RESULT, INJECT_EVENT_TIMEOUT,
1362 /* allowKeyRepeat */ false);
1363}
1364
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001365static InputEventInjectionResult injectKeyUp(const std::unique_ptr<InputDispatcher>& dispatcher,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001366 int32_t displayId = ADISPLAY_ID_NONE) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001367 return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /* repeatCount */ 0, displayId);
1368}
1369
Garfield Tandf26e862020-07-01 20:18:19 -07001370class PointerBuilder {
1371public:
1372 PointerBuilder(int32_t id, int32_t toolType) {
1373 mProperties.clear();
1374 mProperties.id = id;
1375 mProperties.toolType = toolType;
1376 mCoords.clear();
1377 }
1378
1379 PointerBuilder& x(float x) { return axis(AMOTION_EVENT_AXIS_X, x); }
1380
1381 PointerBuilder& y(float y) { return axis(AMOTION_EVENT_AXIS_Y, y); }
1382
1383 PointerBuilder& axis(int32_t axis, float value) {
1384 mCoords.setAxisValue(axis, value);
1385 return *this;
1386 }
1387
1388 PointerProperties buildProperties() const { return mProperties; }
1389
1390 PointerCoords buildCoords() const { return mCoords; }
1391
1392private:
1393 PointerProperties mProperties;
1394 PointerCoords mCoords;
1395};
1396
1397class MotionEventBuilder {
1398public:
1399 MotionEventBuilder(int32_t action, int32_t source) {
1400 mAction = action;
1401 mSource = source;
1402 mEventTime = systemTime(SYSTEM_TIME_MONOTONIC);
1403 }
1404
1405 MotionEventBuilder& eventTime(nsecs_t eventTime) {
1406 mEventTime = eventTime;
1407 return *this;
1408 }
1409
1410 MotionEventBuilder& displayId(int32_t displayId) {
1411 mDisplayId = displayId;
1412 return *this;
1413 }
1414
1415 MotionEventBuilder& actionButton(int32_t actionButton) {
1416 mActionButton = actionButton;
1417 return *this;
1418 }
1419
arthurhung6d4bed92021-03-17 11:59:33 +08001420 MotionEventBuilder& buttonState(int32_t buttonState) {
1421 mButtonState = buttonState;
Garfield Tandf26e862020-07-01 20:18:19 -07001422 return *this;
1423 }
1424
1425 MotionEventBuilder& rawXCursorPosition(float rawXCursorPosition) {
1426 mRawXCursorPosition = rawXCursorPosition;
1427 return *this;
1428 }
1429
1430 MotionEventBuilder& rawYCursorPosition(float rawYCursorPosition) {
1431 mRawYCursorPosition = rawYCursorPosition;
1432 return *this;
1433 }
1434
1435 MotionEventBuilder& pointer(PointerBuilder pointer) {
1436 mPointers.push_back(pointer);
1437 return *this;
1438 }
1439
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08001440 MotionEventBuilder& addFlag(uint32_t flags) {
1441 mFlags |= flags;
1442 return *this;
1443 }
1444
Garfield Tandf26e862020-07-01 20:18:19 -07001445 MotionEvent build() {
1446 std::vector<PointerProperties> pointerProperties;
1447 std::vector<PointerCoords> pointerCoords;
1448 for (const PointerBuilder& pointer : mPointers) {
1449 pointerProperties.push_back(pointer.buildProperties());
1450 pointerCoords.push_back(pointer.buildCoords());
1451 }
1452
1453 // Set mouse cursor position for the most common cases to avoid boilerplate.
1454 if (mSource == AINPUT_SOURCE_MOUSE &&
1455 !MotionEvent::isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition) &&
1456 mPointers.size() == 1) {
1457 mRawXCursorPosition = pointerCoords[0].getX();
1458 mRawYCursorPosition = pointerCoords[0].getY();
1459 }
1460
1461 MotionEvent event;
chaviw9eaa22c2020-07-01 16:21:27 -07001462 ui::Transform identityTransform;
Garfield Tandf26e862020-07-01 20:18:19 -07001463 event.initialize(InputEvent::nextId(), DEVICE_ID, mSource, mDisplayId, INVALID_HMAC,
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08001464 mAction, mActionButton, mFlags, /* edgeFlags */ 0, AMETA_NONE,
chaviw9eaa22c2020-07-01 16:21:27 -07001465 mButtonState, MotionClassification::NONE, identityTransform,
1466 /* xPrecision */ 0, /* yPrecision */ 0, mRawXCursorPosition,
Prabir Pradhanb9b18502021-08-26 12:30:32 -07001467 mRawYCursorPosition, identityTransform, mEventTime, mEventTime,
1468 mPointers.size(), pointerProperties.data(), pointerCoords.data());
Garfield Tandf26e862020-07-01 20:18:19 -07001469
1470 return event;
1471 }
1472
1473private:
1474 int32_t mAction;
1475 int32_t mSource;
1476 nsecs_t mEventTime;
1477 int32_t mDisplayId{ADISPLAY_ID_DEFAULT};
1478 int32_t mActionButton{0};
1479 int32_t mButtonState{0};
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08001480 int32_t mFlags{0};
Garfield Tandf26e862020-07-01 20:18:19 -07001481 float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
1482 float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
1483
1484 std::vector<PointerBuilder> mPointers;
1485};
1486
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001487static InputEventInjectionResult injectMotionEvent(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001488 const std::unique_ptr<InputDispatcher>& dispatcher, const MotionEvent& event,
Garfield Tandf26e862020-07-01 20:18:19 -07001489 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Prabir Pradhan5735a322022-04-11 17:23:34 +00001490 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT,
1491 std::optional<int32_t> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
1492 return dispatcher->injectInputEvent(&event, targetUid, injectionMode, injectionTimeout,
1493 policyFlags);
Garfield Tandf26e862020-07-01 20:18:19 -07001494}
1495
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001496static InputEventInjectionResult injectMotionEvent(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001497 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t source,
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001498 int32_t displayId, const PointF& position = {100, 200},
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001499 const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001500 AMOTION_EVENT_INVALID_CURSOR_POSITION},
1501 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001502 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Prabir Pradhan5735a322022-04-11 17:23:34 +00001503 nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC),
1504 std::optional<int32_t> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
Garfield Tandf26e862020-07-01 20:18:19 -07001505 MotionEvent event = MotionEventBuilder(action, source)
1506 .displayId(displayId)
1507 .eventTime(eventTime)
1508 .rawXCursorPosition(cursorPosition.x)
1509 .rawYCursorPosition(cursorPosition.y)
1510 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
1511 .x(position.x)
1512 .y(position.y))
1513 .build();
Arthur Hungb92218b2018-08-14 12:00:21 +08001514
1515 // Inject event until dispatch out.
Prabir Pradhan5735a322022-04-11 17:23:34 +00001516 return injectMotionEvent(dispatcher, event, injectionTimeout, injectionMode, targetUid,
1517 policyFlags);
Arthur Hungb92218b2018-08-14 12:00:21 +08001518}
1519
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001520static InputEventInjectionResult injectMotionDown(
1521 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t source, int32_t displayId,
1522 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001523 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location);
Garfield Tan00f511d2019-06-12 16:55:40 -07001524}
1525
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001526static InputEventInjectionResult injectMotionUp(const std::unique_ptr<InputDispatcher>& dispatcher,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001527 int32_t source, int32_t displayId,
1528 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001529 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location);
Michael Wright3a240c42019-12-10 20:53:41 +00001530}
1531
Jackal Guof9696682018-10-05 12:23:23 +08001532static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) {
1533 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1534 // Define a valid key event.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001535 NotifyKeyArgs args(/* id */ 0, currentTime, 0 /*readTime*/, DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
1536 displayId, POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0, AKEYCODE_A,
1537 KEY_A, AMETA_NONE, currentTime);
Jackal Guof9696682018-10-05 12:23:23 +08001538
1539 return args;
1540}
1541
chaviwd1c23182019-12-20 18:44:56 -08001542static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId,
1543 const std::vector<PointF>& points) {
1544 size_t pointerCount = points.size();
chaviwaf87b3e2019-10-01 16:59:28 -07001545 if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) {
1546 EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer";
1547 }
1548
chaviwd1c23182019-12-20 18:44:56 -08001549 PointerProperties pointerProperties[pointerCount];
1550 PointerCoords pointerCoords[pointerCount];
Jackal Guof9696682018-10-05 12:23:23 +08001551
chaviwd1c23182019-12-20 18:44:56 -08001552 for (size_t i = 0; i < pointerCount; i++) {
1553 pointerProperties[i].clear();
1554 pointerProperties[i].id = i;
1555 pointerProperties[i].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jackal Guof9696682018-10-05 12:23:23 +08001556
chaviwd1c23182019-12-20 18:44:56 -08001557 pointerCoords[i].clear();
1558 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x);
1559 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y);
1560 }
Jackal Guof9696682018-10-05 12:23:23 +08001561
1562 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1563 // Define a valid motion event.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001564 NotifyMotionArgs args(/* id */ 0, currentTime, 0 /*readTime*/, DEVICE_ID, source, displayId,
Garfield Tan00f511d2019-06-12 16:55:40 -07001565 POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0,
1566 AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
chaviwd1c23182019-12-20 18:44:56 -08001567 AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties,
1568 pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0,
Garfield Tan00f511d2019-06-12 16:55:40 -07001569 AMOTION_EVENT_INVALID_CURSOR_POSITION,
1570 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {});
Jackal Guof9696682018-10-05 12:23:23 +08001571
1572 return args;
1573}
1574
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08001575static NotifyMotionArgs generateTouchArgs(int32_t action, const std::vector<PointF>& points) {
1576 return generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, points);
1577}
1578
chaviwd1c23182019-12-20 18:44:56 -08001579static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) {
1580 return generateMotionArgs(action, source, displayId, {PointF{100, 200}});
1581}
1582
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001583static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs(
1584 const PointerCaptureRequest& request) {
1585 return NotifyPointerCaptureChangedArgs(/* id */ 0, systemTime(SYSTEM_TIME_MONOTONIC), request);
Prabir Pradhan99987712020-11-10 18:43:05 -08001586}
1587
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001588/**
1589 * When a window unexpectedly disposes of its input channel, policy should be notified about the
1590 * broken channel.
1591 */
1592TEST_F(InputDispatcherTest, WhenInputChannelBreaks_PolicyIsNotified) {
1593 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1594 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001595 sp<FakeWindowHandle>::make(application, mDispatcher,
1596 "Window that breaks its input channel", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001597
1598 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1599
1600 // Window closes its channel, but the window remains.
1601 window->destroyReceiver();
1602 mFakePolicy->assertNotifyInputChannelBrokenWasCalled(window->getInfo()->token);
1603}
1604
Arthur Hungb92218b2018-08-14 12:00:21 +08001605TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001606 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001607 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1608 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001609
Arthur Hung72d8dc32020-03-28 00:48:39 +00001610 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001611 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1612 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1613 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001614
1615 // Window should receive motion event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001616 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001617}
1618
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001619TEST_F(InputDispatcherTest, WhenDisplayNotSpecified_InjectMotionToDefaultDisplay) {
1620 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001621 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1622 "Fake Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001623
1624 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1625 // Inject a MotionEvent to an unknown display.
1626 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1627 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_NONE))
1628 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1629
1630 // Window should receive motion event.
1631 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1632}
1633
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001634/**
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08001635 * Calling setInputWindows once should not cause any issues.
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001636 * This test serves as a sanity check for the next test, where setInputWindows is
1637 * called twice.
1638 */
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08001639TEST_F(InputDispatcherTest, SetInputWindowOnceWithSingleTouchWindow) {
Chris Yea209fde2020-07-22 13:54:51 -07001640 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001641 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1642 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001643 window->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001644
1645 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001646 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001647 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1648 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001649 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001650
1651 // Window should receive motion event.
1652 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1653}
1654
1655/**
1656 * Calling setInputWindows twice, with the same info, should not cause any issues.
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001657 */
1658TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001659 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001660 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1661 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001662 window->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001663
1664 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1665 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001666 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001667 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1668 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001669 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001670
1671 // Window should receive motion event.
1672 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1673}
1674
Arthur Hungb92218b2018-08-14 12:00:21 +08001675// The foreground window should receive the first touch down event.
1676TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001677 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001678 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001679 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001680 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001681 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001682
Arthur Hung72d8dc32020-03-28 00:48:39 +00001683 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001684 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1685 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1686 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001687
1688 // Top window should receive the touch down event. Second window should not receive anything.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001689 windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001690 windowSecond->assertNoEvents();
1691}
1692
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001693/**
1694 * Two windows: A top window, and a wallpaper behind the window.
1695 * Touch goes to the top window, and then top window disappears. Ensure that wallpaper window
1696 * gets ACTION_CANCEL.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001697 * 1. foregroundWindow <-- dup touch to wallpaper
1698 * 2. wallpaperWindow <-- is wallpaper
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001699 */
1700TEST_F(InputDispatcherTest, WhenForegroundWindowDisappears_WallpaperTouchIsCanceled) {
1701 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1702 sp<FakeWindowHandle> foregroundWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001703 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001704 foregroundWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001705 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001706 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001707 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001708 constexpr int expectedWallpaperFlags =
1709 AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED;
1710
1711 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}});
1712 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1713 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1714 {100, 200}))
1715 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1716
1717 // Both foreground window and its wallpaper should receive the touch down
1718 foregroundWindow->consumeMotionDown();
1719 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1720
1721 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1722 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
1723 ADISPLAY_ID_DEFAULT, {110, 200}))
1724 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1725
1726 foregroundWindow->consumeMotionMove();
1727 wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1728
1729 // Now the foreground window goes away, but the wallpaper stays
1730 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}});
1731 foregroundWindow->consumeMotionCancel();
1732 // Since the "parent" window of the wallpaper is gone, wallpaper should receive cancel, too.
1733 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1734}
1735
1736/**
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001737 * Same test as WhenForegroundWindowDisappears_WallpaperTouchIsCanceled above,
1738 * with the following differences:
1739 * After ACTION_DOWN, Wallpaper window hangs up its channel, which forces the dispatcher to
1740 * clean up the connection.
1741 * This later may crash dispatcher during ACTION_CANCEL synthesis, if the dispatcher is not careful.
1742 * Ensure that there's no crash in the dispatcher.
1743 */
1744TEST_F(InputDispatcherTest, WhenWallpaperDisappears_NoCrash) {
1745 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1746 sp<FakeWindowHandle> foregroundWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001747 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001748 foregroundWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001749 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001750 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001751 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001752 constexpr int expectedWallpaperFlags =
1753 AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED;
1754
1755 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}});
1756 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1757 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1758 {100, 200}))
1759 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1760
1761 // Both foreground window and its wallpaper should receive the touch down
1762 foregroundWindow->consumeMotionDown();
1763 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1764
1765 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1766 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
1767 ADISPLAY_ID_DEFAULT, {110, 200}))
1768 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1769
1770 foregroundWindow->consumeMotionMove();
1771 wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1772
1773 // Wallpaper closes its channel, but the window remains.
1774 wallpaperWindow->destroyReceiver();
1775 mFakePolicy->assertNotifyInputChannelBrokenWasCalled(wallpaperWindow->getInfo()->token);
1776
1777 // Now the foreground window goes away, but the wallpaper stays, even though its channel
1778 // is no longer valid.
1779 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}});
1780 foregroundWindow->consumeMotionCancel();
1781}
1782
1783/**
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001784 * A single window that receives touch (on top), and a wallpaper window underneath it.
1785 * The top window gets a multitouch gesture.
1786 * Ensure that wallpaper gets the same gesture.
1787 */
1788TEST_F(InputDispatcherTest, WallpaperWindow_ReceivesMultiTouch) {
1789 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1790 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001791 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001792 window->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001793
1794 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001795 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001796 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001797 constexpr int expectedWallpaperFlags =
1798 AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED;
1799
1800 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, wallpaperWindow}}});
1801
1802 // Touch down on top window
1803 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1804 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1805 {100, 100}))
1806 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1807
1808 // Both top window and its wallpaper should receive the touch down
1809 window->consumeMotionDown();
1810 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1811
1812 // Second finger down on the top window
1813 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08001814 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001815 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
1816 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
1817 .x(100)
1818 .y(100))
1819 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER)
1820 .x(150)
1821 .y(150))
1822 .build();
1823 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1824 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
1825 InputEventInjectionSync::WAIT_FOR_RESULT))
1826 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1827
1828 window->consumeMotionPointerDown(1 /* pointerIndex */);
1829 wallpaperWindow->consumeMotionPointerDown(1 /* pointerIndex */, ADISPLAY_ID_DEFAULT,
1830 expectedWallpaperFlags);
1831 window->assertNoEvents();
1832 wallpaperWindow->assertNoEvents();
1833}
1834
1835/**
1836 * Two windows: a window on the left and window on the right.
1837 * A third window, wallpaper, is behind both windows, and spans both top windows.
1838 * The first touch down goes to the left window. A second pointer touches down on the right window.
1839 * The touch is split, so both left and right windows should receive ACTION_DOWN.
1840 * The wallpaper will get the full event, so it should receive ACTION_DOWN followed by
1841 * ACTION_POINTER_DOWN(1).
1842 */
1843TEST_F(InputDispatcherTest, TwoWindows_SplitWallpaperTouch) {
1844 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1845 sp<FakeWindowHandle> leftWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001846 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001847 leftWindow->setFrame(Rect(0, 0, 200, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001848 leftWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001849
1850 sp<FakeWindowHandle> rightWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001851 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001852 rightWindow->setFrame(Rect(200, 0, 400, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001853 rightWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001854
1855 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001856 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001857 wallpaperWindow->setFrame(Rect(0, 0, 400, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001858 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001859 constexpr int expectedWallpaperFlags =
1860 AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED;
1861
1862 mDispatcher->setInputWindows(
1863 {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}});
1864
1865 // Touch down on left window
1866 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1867 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1868 {100, 100}))
1869 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1870
1871 // Both foreground window and its wallpaper should receive the touch down
1872 leftWindow->consumeMotionDown();
1873 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1874
1875 // Second finger down on the right window
1876 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08001877 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001878 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
1879 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
1880 .x(100)
1881 .y(100))
1882 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER)
1883 .x(300)
1884 .y(100))
1885 .build();
1886 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1887 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
1888 InputEventInjectionSync::WAIT_FOR_RESULT))
1889 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1890
1891 leftWindow->consumeMotionMove();
1892 // Since the touch is split, right window gets ACTION_DOWN
1893 rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1894 wallpaperWindow->consumeMotionPointerDown(1 /* pointerIndex */, ADISPLAY_ID_DEFAULT,
1895 expectedWallpaperFlags);
1896
1897 // Now, leftWindow, which received the first finger, disappears.
1898 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {rightWindow, wallpaperWindow}}});
1899 leftWindow->consumeMotionCancel();
1900 // Since a "parent" window of the wallpaper is gone, wallpaper should receive cancel, too.
1901 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1902
1903 // The pointer that's still down on the right window moves, and goes to the right window only.
1904 // As far as the dispatcher's concerned though, both pointers are still present.
1905 const MotionEvent secondFingerMoveEvent =
1906 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
1907 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
1908 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
1909 .x(100)
1910 .y(100))
1911 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER)
1912 .x(310)
1913 .y(110))
1914 .build();
1915 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1916 injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT,
1917 InputEventInjectionSync::WAIT_FOR_RESULT));
1918 rightWindow->consumeMotionMove();
1919
1920 leftWindow->assertNoEvents();
1921 rightWindow->assertNoEvents();
1922 wallpaperWindow->assertNoEvents();
1923}
1924
Arthur Hung74c248d2022-11-23 07:09:59 +00001925TEST_F(InputDispatcherTest, WallpaperWindowReceivesMultiTouch) {
1926 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1927 sp<FakeWindowHandle> window =
1928 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
1929 window->setDupTouchToWallpaper(true);
1930
1931 sp<FakeWindowHandle> wallpaperWindow =
1932 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
1933 wallpaperWindow->setIsWallpaper(true);
1934 constexpr int expectedWallpaperFlags =
1935 AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED;
1936 wallpaperWindow->setPreventSplitting(true);
1937
1938 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, wallpaperWindow}}});
1939
1940 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1941 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1942 {50, 50}))
1943 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1944 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1945 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1946
1947 const MotionEvent secondFingerDownEvent =
1948 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
1949 .displayId(ADISPLAY_ID_DEFAULT)
1950 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
1951 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
1952 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(10).y(10))
1953 .build();
1954 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1955 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
1956 InputEventInjectionSync::WAIT_FOR_RESULT))
1957 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1958
1959 window->consumeMotionPointerDown(1);
1960 wallpaperWindow->consumeMotionPointerDown(1, ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1961
1962 const MotionEvent secondFingerUpEvent =
1963 MotionEventBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
1964 .displayId(ADISPLAY_ID_DEFAULT)
1965 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
1966 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
1967 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(10).y(10))
1968 .build();
1969 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1970 injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT,
1971 InputEventInjectionSync::WAIT_FOR_RESULT))
1972 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1973 window->consumeMotionPointerUp(1);
1974 wallpaperWindow->consumeMotionPointerUp(1, ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1975
1976 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1977 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {50, 50}))
1978 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1979 window->consumeMotionUp(ADISPLAY_ID_DEFAULT);
1980 wallpaperWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1981}
1982
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08001983/**
1984 * On the display, have a single window, and also an area where there's no window.
1985 * First pointer touches the "no window" area of the screen. Second pointer touches the window.
1986 * Make sure that the window receives the second pointer, and first pointer is simply ignored.
1987 */
1988TEST_F(InputDispatcherTest, SplitWorksWhenEmptyAreaIsTouched) {
1989 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1990 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001991 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08001992
1993 mDispatcher->setInputWindows({{DISPLAY_ID, {window}}});
1994 NotifyMotionArgs args;
1995
1996 // Touch down on the empty space
1997 mDispatcher->notifyMotion(&(args = generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{-1, -1}})));
1998
1999 mDispatcher->waitForIdle();
2000 window->assertNoEvents();
2001
2002 // Now touch down on the window with another pointer
2003 mDispatcher->notifyMotion(&(args = generateTouchArgs(POINTER_1_DOWN, {{-1, -1}, {10, 10}})));
2004 mDispatcher->waitForIdle();
2005 window->consumeMotionDown();
2006}
2007
2008/**
2009 * Same test as above, but instead of touching the empty space, the first touch goes to
2010 * non-touchable window.
2011 */
2012TEST_F(InputDispatcherTest, SplitWorksWhenNonTouchableWindowIsTouched) {
2013 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2014 sp<FakeWindowHandle> window1 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002015 sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002016 window1->setTouchableRegion(Region{{0, 0, 100, 100}});
2017 window1->setTouchable(false);
2018 sp<FakeWindowHandle> window2 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002019 sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002020 window2->setTouchableRegion(Region{{100, 0, 200, 100}});
2021
2022 mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}});
2023
2024 NotifyMotionArgs args;
2025 // Touch down on the non-touchable window
2026 mDispatcher->notifyMotion(&(args = generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}})));
2027
2028 mDispatcher->waitForIdle();
2029 window1->assertNoEvents();
2030 window2->assertNoEvents();
2031
2032 // Now touch down on the window with another pointer
2033 mDispatcher->notifyMotion(&(args = generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}})));
2034 mDispatcher->waitForIdle();
2035 window2->consumeMotionDown();
2036}
2037
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002038/**
2039 * When splitting touch events the downTime should be adjusted such that the downTime corresponds
2040 * to the event time of the first ACTION_DOWN sent to the particular window.
2041 */
2042TEST_F(InputDispatcherTest, SplitTouchesSendCorrectActionDownTime) {
2043 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2044 sp<FakeWindowHandle> window1 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002045 sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002046 window1->setTouchableRegion(Region{{0, 0, 100, 100}});
2047 sp<FakeWindowHandle> window2 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002048 sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002049 window2->setTouchableRegion(Region{{100, 0, 200, 100}});
2050
2051 mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}});
2052
2053 NotifyMotionArgs args;
2054 // Touch down on the first window
2055 mDispatcher->notifyMotion(&(args = generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}})));
2056
2057 mDispatcher->waitForIdle();
2058 InputEvent* inputEvent1 = window1->consume();
2059 window2->assertNoEvents();
2060 MotionEvent& motionEvent1 = static_cast<MotionEvent&>(*inputEvent1);
2061 nsecs_t downTimeForWindow1 = motionEvent1.getDownTime();
2062 ASSERT_EQ(motionEvent1.getDownTime(), motionEvent1.getEventTime());
2063
2064 // Now touch down on the window with another pointer
2065 mDispatcher->notifyMotion(&(args = generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}})));
2066 mDispatcher->waitForIdle();
2067 InputEvent* inputEvent2 = window2->consume();
2068 MotionEvent& motionEvent2 = static_cast<MotionEvent&>(*inputEvent2);
2069 nsecs_t downTimeForWindow2 = motionEvent2.getDownTime();
2070 ASSERT_NE(downTimeForWindow1, downTimeForWindow2);
2071 ASSERT_EQ(motionEvent2.getDownTime(), motionEvent2.getEventTime());
2072
2073 // Now move the pointer on the second window
2074 mDispatcher->notifyMotion(
2075 &(args = generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{50, 50}, {151, 51}})));
2076 mDispatcher->waitForIdle();
2077 InputEvent* inputEvent3 = window2->consume();
2078 MotionEvent& motionEvent3 = static_cast<MotionEvent&>(*inputEvent3);
2079 ASSERT_EQ(motionEvent3.getDownTime(), downTimeForWindow2);
2080
2081 // Now add new touch down on the second window
2082 mDispatcher->notifyMotion(
2083 &(args = generateTouchArgs(POINTER_2_DOWN, {{50, 50}, {151, 51}, {150, 50}})));
2084 mDispatcher->waitForIdle();
2085 InputEvent* inputEvent4 = window2->consume();
2086 MotionEvent& motionEvent4 = static_cast<MotionEvent&>(*inputEvent4);
2087 ASSERT_EQ(motionEvent4.getDownTime(), downTimeForWindow2);
2088
2089 // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line
2090 window1->consumeMotionMove();
2091 window1->assertNoEvents();
2092
2093 // Now move the pointer on the first window
2094 mDispatcher->notifyMotion(
2095 &(args = generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{51, 51}, {151, 51}})));
2096 mDispatcher->waitForIdle();
2097 InputEvent* inputEvent5 = window1->consume();
2098 MotionEvent& motionEvent5 = static_cast<MotionEvent&>(*inputEvent5);
2099 ASSERT_EQ(motionEvent5.getDownTime(), downTimeForWindow1);
2100
2101 mDispatcher->notifyMotion(&(
2102 args = generateTouchArgs(POINTER_3_DOWN, {{51, 51}, {151, 51}, {150, 50}, {50, 50}})));
2103 mDispatcher->waitForIdle();
2104 InputEvent* inputEvent6 = window1->consume();
2105 MotionEvent& motionEvent6 = static_cast<MotionEvent&>(*inputEvent6);
2106 ASSERT_EQ(motionEvent6.getDownTime(), downTimeForWindow1);
2107}
2108
Garfield Tandf26e862020-07-01 20:18:19 -07002109TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) {
Chris Yea209fde2020-07-22 13:54:51 -07002110 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07002111 sp<FakeWindowHandle> windowLeft =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002112 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07002113 windowLeft->setFrame(Rect(0, 0, 600, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07002114 sp<FakeWindowHandle> windowRight =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002115 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07002116 windowRight->setFrame(Rect(600, 0, 1200, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07002117
2118 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2119
2120 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
2121
2122 // Start cursor position in right window so that we can move the cursor to left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002123 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002124 injectMotionEvent(mDispatcher,
2125 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2126 AINPUT_SOURCE_MOUSE)
2127 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2128 .x(900)
2129 .y(400))
2130 .build()));
2131 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
2132 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
2133 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE,
2134 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
2135
2136 // Move cursor into left window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002137 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002138 injectMotionEvent(mDispatcher,
2139 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2140 AINPUT_SOURCE_MOUSE)
2141 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2142 .x(300)
2143 .y(400))
2144 .build()));
2145 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT,
2146 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
2147 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
2148 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
2149 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE,
2150 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
2151
2152 // Inject a series of mouse events for a mouse click
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_DOWN, AINPUT_SOURCE_MOUSE)
2156 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2157 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2158 .x(300)
2159 .y(400))
2160 .build()));
2161 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2162
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002163 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002164 injectMotionEvent(mDispatcher,
2165 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
2166 AINPUT_SOURCE_MOUSE)
2167 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2168 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2169 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2170 .x(300)
2171 .y(400))
2172 .build()));
2173 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_PRESS,
2174 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
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_RELEASE,
2179 AINPUT_SOURCE_MOUSE)
2180 .buttonState(0)
2181 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2182 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2183 .x(300)
2184 .y(400))
2185 .build()));
2186 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_RELEASE,
2187 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
2188
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002189 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002190 injectMotionEvent(mDispatcher,
2191 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
2192 .buttonState(0)
2193 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2194 .x(300)
2195 .y(400))
2196 .build()));
2197 windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT);
2198
2199 // Move mouse cursor back to right window
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_HOVER_MOVE,
2203 AINPUT_SOURCE_MOUSE)
2204 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2205 .x(900)
2206 .y(400))
2207 .build()));
2208 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT,
2209 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
2210 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
2211 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
2212 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE,
2213 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
2214}
2215
2216// This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected
2217// directly in this test.
2218TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) {
Chris Yea209fde2020-07-22 13:54:51 -07002219 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07002220 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002221 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07002222 window->setFrame(Rect(0, 0, 1200, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07002223
2224 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2225
2226 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2227
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002228 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002229 injectMotionEvent(mDispatcher,
2230 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
2231 AINPUT_SOURCE_MOUSE)
2232 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2233 .x(300)
2234 .y(400))
2235 .build()));
2236 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
2237 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
2238
2239 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002240 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002241 injectMotionEvent(mDispatcher,
2242 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2243 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2244 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2245 .x(300)
2246 .y(400))
2247 .build()));
2248 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2249
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002250 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002251 injectMotionEvent(mDispatcher,
2252 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
2253 AINPUT_SOURCE_MOUSE)
2254 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2255 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2256 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2257 .x(300)
2258 .y(400))
2259 .build()));
2260 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_PRESS,
2261 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
2262
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002263 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002264 injectMotionEvent(mDispatcher,
2265 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
2266 AINPUT_SOURCE_MOUSE)
2267 .buttonState(0)
2268 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2269 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2270 .x(300)
2271 .y(400))
2272 .build()));
2273 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_RELEASE,
2274 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
2275
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002276 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002277 injectMotionEvent(mDispatcher,
2278 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
2279 .buttonState(0)
2280 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2281 .x(300)
2282 .y(400))
2283 .build()));
2284 window->consumeMotionUp(ADISPLAY_ID_DEFAULT);
2285
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002286 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002287 injectMotionEvent(mDispatcher,
2288 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT,
2289 AINPUT_SOURCE_MOUSE)
2290 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2291 .x(300)
2292 .y(400))
2293 .build()));
2294 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT,
2295 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
2296}
2297
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08002298/**
2299 * Inject a mouse hover event followed by a tap from touchscreen.
2300 * In the current implementation, the tap does not cause a HOVER_EXIT event.
2301 */
2302TEST_F(InputDispatcherTest, MouseHoverAndTouchTap) {
2303 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2304 sp<FakeWindowHandle> window =
2305 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2306 window->setFrame(Rect(0, 0, 100, 100));
2307
2308 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2309
2310 // Inject a hover_move from mouse.
2311 NotifyMotionArgs motionArgs =
2312 generateMotionArgs(AMOTION_EVENT_ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE,
2313 ADISPLAY_ID_DEFAULT, {{50, 50}});
2314 motionArgs.xCursorPosition = 50;
2315 motionArgs.yCursorPosition = 50;
2316 mDispatcher->notifyMotion(&motionArgs);
2317 ASSERT_NO_FATAL_FAILURE(
2318 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2319 WithSource(AINPUT_SOURCE_MOUSE))));
2320 ASSERT_NO_FATAL_FAILURE(
2321 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2322 WithSource(AINPUT_SOURCE_MOUSE))));
2323
2324 // Tap on the window
2325 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2326 ADISPLAY_ID_DEFAULT, {{10, 10}});
2327 mDispatcher->notifyMotion(&motionArgs);
2328 ASSERT_NO_FATAL_FAILURE(
2329 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2330 WithSource(AINPUT_SOURCE_TOUCHSCREEN))));
2331
2332 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
2333 ADISPLAY_ID_DEFAULT, {{10, 10}});
2334 mDispatcher->notifyMotion(&motionArgs);
2335 ASSERT_NO_FATAL_FAILURE(
2336 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
2337 WithSource(AINPUT_SOURCE_TOUCHSCREEN))));
2338}
2339
Garfield Tan00f511d2019-06-12 16:55:40 -07002340TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) {
Chris Yea209fde2020-07-22 13:54:51 -07002341 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tan00f511d2019-06-12 16:55:40 -07002342
2343 sp<FakeWindowHandle> windowLeft =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002344 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07002345 windowLeft->setFrame(Rect(0, 0, 600, 800));
Garfield Tan00f511d2019-06-12 16:55:40 -07002346 sp<FakeWindowHandle> windowRight =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002347 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07002348 windowRight->setFrame(Rect(600, 0, 1200, 800));
Garfield Tan00f511d2019-06-12 16:55:40 -07002349
2350 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2351
Arthur Hung72d8dc32020-03-28 00:48:39 +00002352 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
Garfield Tan00f511d2019-06-12 16:55:40 -07002353
2354 // Inject an event with coordinate in the area of right window, with mouse cursor in the area of
2355 // left window. This event should be dispatched to the left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002356 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tan00f511d2019-06-12 16:55:40 -07002357 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07002358 ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400}));
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002359 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07002360 windowRight->assertNoEvents();
2361}
2362
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002363TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) {
Chris Yea209fde2020-07-22 13:54:51 -07002364 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002365 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
2366 "Fake Window", ADISPLAY_ID_DEFAULT);
Vishnu Nair47074b82020-08-14 11:54:47 -07002367 window->setFocusable(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002368
Arthur Hung72d8dc32020-03-28 00:48:39 +00002369 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002370 setFocusedWindow(window);
2371
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002372 window->consumeFocusEvent(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002373
2374 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
2375 mDispatcher->notifyKey(&keyArgs);
2376
2377 // Window should receive key down event.
2378 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2379
2380 // When device reset happens, that key stream should be terminated with FLAG_CANCELED
2381 // on the app side.
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002382 NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002383 mDispatcher->notifyDeviceReset(&args);
2384 window->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
2385 AKEY_EVENT_FLAG_CANCELED);
2386}
2387
2388TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) {
Chris Yea209fde2020-07-22 13:54:51 -07002389 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002390 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
2391 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002392
Arthur Hung72d8dc32020-03-28 00:48:39 +00002393 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002394
2395 NotifyMotionArgs motionArgs =
2396 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2397 ADISPLAY_ID_DEFAULT);
2398 mDispatcher->notifyMotion(&motionArgs);
2399
2400 // Window should receive motion down event.
2401 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2402
2403 // When device reset happens, that motion stream should be terminated with ACTION_CANCEL
2404 // on the app side.
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002405 NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002406 mDispatcher->notifyDeviceReset(&args);
2407 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, ADISPLAY_ID_DEFAULT,
2408 0 /*expectedFlags*/);
2409}
2410
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08002411TEST_F(InputDispatcherTest, InterceptKeyByPolicy) {
2412 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002413 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
2414 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08002415 window->setFocusable(true);
2416
2417 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2418 setFocusedWindow(window);
2419
2420 window->consumeFocusEvent(true);
2421
2422 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
2423 const std::chrono::milliseconds interceptKeyTimeout = 50ms;
2424 const nsecs_t injectTime = keyArgs.eventTime;
2425 mFakePolicy->setInterceptKeyTimeout(interceptKeyTimeout);
2426 mDispatcher->notifyKey(&keyArgs);
2427 // The dispatching time should be always greater than or equal to intercept key timeout.
2428 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2429 ASSERT_TRUE((systemTime(SYSTEM_TIME_MONOTONIC) - injectTime) >=
2430 std::chrono::nanoseconds(interceptKeyTimeout).count());
2431}
2432
2433TEST_F(InputDispatcherTest, InterceptKeyIfKeyUp) {
2434 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002435 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
2436 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08002437 window->setFocusable(true);
2438
2439 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2440 setFocusedWindow(window);
2441
2442 window->consumeFocusEvent(true);
2443
2444 NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
2445 NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
2446 mFakePolicy->setInterceptKeyTimeout(150ms);
2447 mDispatcher->notifyKey(&keyDown);
2448 mDispatcher->notifyKey(&keyUp);
2449
2450 // Window should receive key event immediately when same key up.
2451 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2452 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
2453}
2454
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07002455/**
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00002456 * This test documents the behavior of WATCH_OUTSIDE_TOUCH. The window will get ACTION_OUTSIDE when
2457 * a another pointer causes ACTION_DOWN to be sent to another window for the first time. Only one
2458 * ACTION_OUTSIDE event is sent per gesture.
2459 */
2460TEST_F(InputDispatcherTest, ActionOutsideSentOnlyWhenAWindowIsTouched) {
2461 // There are three windows that do not overlap. `window` wants to WATCH_OUTSIDE_TOUCH.
2462 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002463 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
2464 "First Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00002465 window->setWatchOutsideTouch(true);
2466 window->setFrame(Rect{0, 0, 100, 100});
2467 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002468 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
2469 ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00002470 secondWindow->setFrame(Rect{100, 100, 200, 200});
2471 sp<FakeWindowHandle> thirdWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002472 sp<FakeWindowHandle>::make(application, mDispatcher, "Third Window",
2473 ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00002474 thirdWindow->setFrame(Rect{200, 200, 300, 300});
2475 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, secondWindow, thirdWindow}}});
2476
2477 // First pointer lands outside all windows. `window` does not get ACTION_OUTSIDE.
2478 NotifyMotionArgs motionArgs =
2479 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2480 ADISPLAY_ID_DEFAULT, {PointF{-10, -10}});
2481 mDispatcher->notifyMotion(&motionArgs);
2482 window->assertNoEvents();
2483 secondWindow->assertNoEvents();
2484
2485 // The second pointer lands inside `secondWindow`, which should receive a DOWN event.
2486 // Now, `window` should get ACTION_OUTSIDE.
2487 motionArgs = generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2488 {PointF{-10, -10}, PointF{105, 105}});
2489 mDispatcher->notifyMotion(&motionArgs);
2490 window->consumeMotionOutside();
2491 secondWindow->consumeMotionDown();
2492 thirdWindow->assertNoEvents();
2493
2494 // The third pointer lands inside `thirdWindow`, which should receive a DOWN event. There is
2495 // no ACTION_OUTSIDE sent to `window` because one has already been sent for this gesture.
2496 motionArgs = generateMotionArgs(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2497 {PointF{-10, -10}, PointF{105, 105}, PointF{205, 205}});
2498 mDispatcher->notifyMotion(&motionArgs);
2499 window->assertNoEvents();
2500 secondWindow->consumeMotionMove();
2501 thirdWindow->consumeMotionDown();
2502}
2503
Prabir Pradhan814fe082022-07-22 20:22:18 +00002504TEST_F(InputDispatcherTest, OnWindowInfosChanged_RemoveAllWindowsOnDisplay) {
2505 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002506 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
2507 "Fake Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan814fe082022-07-22 20:22:18 +00002508 window->setFocusable(true);
2509
2510 mDispatcher->onWindowInfosChanged({*window->getInfo()}, {});
2511 setFocusedWindow(window);
2512
2513 window->consumeFocusEvent(true);
2514
2515 NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
2516 NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
2517 mDispatcher->notifyKey(&keyDown);
2518 mDispatcher->notifyKey(&keyUp);
2519
2520 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2521 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
2522
2523 // All windows are removed from the display. Ensure that we can no longer dispatch to it.
2524 mDispatcher->onWindowInfosChanged({}, {});
2525
2526 window->consumeFocusEvent(false);
2527
2528 mDispatcher->notifyKey(&keyDown);
2529 mDispatcher->notifyKey(&keyUp);
2530 window->assertNoEvents();
2531}
2532
Arthur Hung96483742022-11-15 03:30:48 +00002533TEST_F(InputDispatcherTest, NonSplitTouchableWindowReceivesMultiTouch) {
2534 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2535 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
2536 "Fake Window", ADISPLAY_ID_DEFAULT);
2537 // Ensure window is non-split and have some transform.
2538 window->setPreventSplitting(true);
2539 window->setWindowOffset(20, 40);
2540 mDispatcher->onWindowInfosChanged({*window->getInfo()}, {});
2541
2542 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2543 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2544 {50, 50}))
2545 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2546 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2547
2548 const MotionEvent secondFingerDownEvent =
2549 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2550 .displayId(ADISPLAY_ID_DEFAULT)
2551 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
2552 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
2553 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER)
2554 .x(-30)
2555 .y(-50))
2556 .build();
2557 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2558 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
2559 InputEventInjectionSync::WAIT_FOR_RESULT))
2560 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2561
2562 const MotionEvent* event = window->consumeMotion();
2563 EXPECT_EQ(POINTER_1_DOWN, event->getAction());
2564 EXPECT_EQ(70, event->getX(0)); // 50 + 20
2565 EXPECT_EQ(90, event->getY(0)); // 50 + 40
2566 EXPECT_EQ(-10, event->getX(1)); // -30 + 20
2567 EXPECT_EQ(-10, event->getY(1)); // -50 + 40
2568}
2569
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00002570/**
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07002571 * Ensure the correct coordinate spaces are used by InputDispatcher.
2572 *
2573 * InputDispatcher works in the display space, so its coordinate system is relative to the display
2574 * panel. Windows get events in the window space, and get raw coordinates in the logical display
2575 * space.
2576 */
2577class InputDispatcherDisplayProjectionTest : public InputDispatcherTest {
2578public:
2579 void SetUp() override {
2580 InputDispatcherTest::SetUp();
2581 mDisplayInfos.clear();
2582 mWindowInfos.clear();
2583 }
2584
2585 void addDisplayInfo(int displayId, const ui::Transform& transform) {
2586 gui::DisplayInfo info;
2587 info.displayId = displayId;
2588 info.transform = transform;
2589 mDisplayInfos.push_back(std::move(info));
2590 mDispatcher->onWindowInfosChanged(mWindowInfos, mDisplayInfos);
2591 }
2592
2593 void addWindow(const sp<WindowInfoHandle>& windowHandle) {
2594 mWindowInfos.push_back(*windowHandle->getInfo());
2595 mDispatcher->onWindowInfosChanged(mWindowInfos, mDisplayInfos);
2596 }
2597
2598 // Set up a test scenario where the display has a scaled projection and there are two windows
2599 // on the display.
2600 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupScaledDisplayScenario() {
2601 // The display has a projection that has a scale factor of 2 and 4 in the x and y directions
2602 // respectively.
2603 ui::Transform displayTransform;
2604 displayTransform.set(2, 0, 0, 4);
2605 addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform);
2606
2607 std::shared_ptr<FakeApplicationHandle> application =
2608 std::make_shared<FakeApplicationHandle>();
2609
2610 // Add two windows to the display. Their frames are represented in the display space.
2611 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002612 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
2613 ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07002614 firstWindow->setFrame(Rect(0, 0, 100, 200), displayTransform);
2615 addWindow(firstWindow);
2616
2617 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002618 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
2619 ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07002620 secondWindow->setFrame(Rect(100, 200, 200, 400), displayTransform);
2621 addWindow(secondWindow);
2622 return {std::move(firstWindow), std::move(secondWindow)};
2623 }
2624
2625private:
2626 std::vector<gui::DisplayInfo> mDisplayInfos;
2627 std::vector<gui::WindowInfo> mWindowInfos;
2628};
2629
2630TEST_F(InputDispatcherDisplayProjectionTest, HitTestsInDisplaySpace) {
2631 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
2632 // Send down to the first window. The point is represented in the display space. The point is
2633 // selected so that if the hit test was done with the transform applied to it, then it would
2634 // end up in the incorrect window.
2635 NotifyMotionArgs downMotionArgs =
2636 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2637 ADISPLAY_ID_DEFAULT, {PointF{75, 55}});
2638 mDispatcher->notifyMotion(&downMotionArgs);
2639
2640 firstWindow->consumeMotionDown();
2641 secondWindow->assertNoEvents();
2642}
2643
2644// Ensure that when a MotionEvent is injected through the InputDispatcher::injectInputEvent() API,
2645// the event should be treated as being in the logical display space.
2646TEST_F(InputDispatcherDisplayProjectionTest, InjectionInLogicalDisplaySpace) {
2647 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
2648 // Send down to the first window. The point is represented in the logical display space. The
2649 // point is selected so that if the hit test was done in logical display space, then it would
2650 // end up in the incorrect window.
2651 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2652 PointF{75 * 2, 55 * 4});
2653
2654 firstWindow->consumeMotionDown();
2655 secondWindow->assertNoEvents();
2656}
2657
Prabir Pradhandaa2f142021-12-10 09:30:08 +00002658// Ensure that when a MotionEvent that has a custom transform is injected, the post-transformed
2659// event should be treated as being in the logical display space.
2660TEST_F(InputDispatcherDisplayProjectionTest, InjectionWithTransformInLogicalDisplaySpace) {
2661 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
2662
2663 const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0};
2664 ui::Transform injectedEventTransform;
2665 injectedEventTransform.set(matrix);
2666 const vec2 expectedPoint{75, 55}; // The injected point in the logical display space.
2667 const vec2 untransformedPoint = injectedEventTransform.inverse().transform(expectedPoint);
2668
2669 MotionEvent event = MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2670 .displayId(ADISPLAY_ID_DEFAULT)
2671 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
2672 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
2673 .x(untransformedPoint.x)
2674 .y(untransformedPoint.y))
2675 .build();
2676 event.transform(matrix);
2677
2678 injectMotionEvent(mDispatcher, event, INJECT_EVENT_TIMEOUT,
2679 InputEventInjectionSync::WAIT_FOR_RESULT);
2680
2681 firstWindow->consumeMotionDown();
2682 secondWindow->assertNoEvents();
2683}
2684
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07002685TEST_F(InputDispatcherDisplayProjectionTest, WindowGetsEventsInCorrectCoordinateSpace) {
2686 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
2687
2688 // Send down to the second window.
2689 NotifyMotionArgs downMotionArgs =
2690 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2691 ADISPLAY_ID_DEFAULT, {PointF{150, 220}});
2692 mDispatcher->notifyMotion(&downMotionArgs);
2693
2694 firstWindow->assertNoEvents();
2695 const MotionEvent* event = secondWindow->consumeMotion();
2696 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, event->getAction());
2697
2698 // Ensure that the events from the "getRaw" API are in logical display coordinates.
2699 EXPECT_EQ(300, event->getRawX(0));
2700 EXPECT_EQ(880, event->getRawY(0));
2701
2702 // Ensure that the x and y values are in the window's coordinate space.
2703 // The left-top of the second window is at (100, 200) in display space, which is (200, 800) in
2704 // the logical display space. This will be the origin of the window space.
2705 EXPECT_EQ(100, event->getX(0));
2706 EXPECT_EQ(80, event->getY(0));
2707}
2708
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002709using TransferFunction = std::function<bool(const std::unique_ptr<InputDispatcher>& dispatcher,
2710 sp<IBinder>, sp<IBinder>)>;
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00002711
2712class TransferTouchFixture : public InputDispatcherTest,
2713 public ::testing::WithParamInterface<TransferFunction> {};
2714
2715TEST_P(TransferTouchFixture, TransferTouch_OnePointer) {
Chris Yea209fde2020-07-22 13:54:51 -07002716 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08002717
2718 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002719 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002720 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
2721 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002722 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002723 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
2724 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08002725
2726 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00002727 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08002728
2729 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002730 NotifyMotionArgs downMotionArgs =
2731 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2732 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08002733 mDispatcher->notifyMotion(&downMotionArgs);
2734 // Only the first window should get the down event
2735 firstWindow->consumeMotionDown();
2736 secondWindow->assertNoEvents();
2737
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00002738 // Transfer touch to the second window
2739 TransferFunction f = GetParam();
2740 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
2741 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08002742 // The first window gets cancel and the second gets down
2743 firstWindow->consumeMotionCancel();
2744 secondWindow->consumeMotionDown();
2745
2746 // Send up event to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002747 NotifyMotionArgs upMotionArgs =
2748 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
2749 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08002750 mDispatcher->notifyMotion(&upMotionArgs);
2751 // The first window gets no events and the second gets up
2752 firstWindow->assertNoEvents();
2753 secondWindow->consumeMotionUp();
2754}
2755
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07002756/**
2757 * When 'transferTouch' API is invoked, dispatcher needs to find the "best" window to take touch
2758 * from. When we have spy windows, there are several windows to choose from: either spy, or the
2759 * 'real' (non-spy) window. Always prefer the 'real' window because that's what would be most
2760 * natural to the user.
2761 * In this test, we are sending a pointer to both spy window and first window. We then try to
2762 * transfer touch to the second window. The dispatcher should identify the first window as the
2763 * one that should lose the gesture, and therefore the action should be to move the gesture from
2764 * the first window to the second.
2765 * The main goal here is to test the behaviour of 'transferTouch' API, but it's still valid to test
2766 * the other API, as well.
2767 */
2768TEST_P(TransferTouchFixture, TransferTouch_MultipleWindowsWithSpy) {
2769 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2770
2771 // Create a couple of windows + a spy window
2772 sp<FakeWindowHandle> spyWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002773 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07002774 spyWindow->setTrustedOverlay(true);
2775 spyWindow->setSpy(true);
2776 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002777 sp<FakeWindowHandle>::make(application, mDispatcher, "First", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07002778 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002779 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07002780
2781 // Add the windows to the dispatcher
2782 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, firstWindow, secondWindow}}});
2783
2784 // Send down to the first window
2785 NotifyMotionArgs downMotionArgs =
2786 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2787 ADISPLAY_ID_DEFAULT);
2788 mDispatcher->notifyMotion(&downMotionArgs);
2789 // Only the first window and spy should get the down event
2790 spyWindow->consumeMotionDown();
2791 firstWindow->consumeMotionDown();
2792
2793 // Transfer touch to the second window. Non-spy window should be preferred over the spy window
2794 // if f === 'transferTouch'.
2795 TransferFunction f = GetParam();
2796 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
2797 ASSERT_TRUE(success);
2798 // The first window gets cancel and the second gets down
2799 firstWindow->consumeMotionCancel();
2800 secondWindow->consumeMotionDown();
2801
2802 // Send up event to the second window
2803 NotifyMotionArgs upMotionArgs =
2804 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
2805 ADISPLAY_ID_DEFAULT);
2806 mDispatcher->notifyMotion(&upMotionArgs);
2807 // The first window gets no events and the second+spy get up
2808 firstWindow->assertNoEvents();
2809 spyWindow->consumeMotionUp();
2810 secondWindow->consumeMotionUp();
2811}
2812
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00002813TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07002814 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08002815
2816 PointF touchPoint = {10, 10};
2817
2818 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002819 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002820 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
2821 ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08002822 firstWindow->setPreventSplitting(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002823 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002824 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
2825 ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08002826 secondWindow->setPreventSplitting(true);
Svet Ganov5d3bc372020-01-26 23:11:07 -08002827
2828 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00002829 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08002830
2831 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002832 NotifyMotionArgs downMotionArgs =
2833 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2834 ADISPLAY_ID_DEFAULT, {touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08002835 mDispatcher->notifyMotion(&downMotionArgs);
2836 // Only the first window should get the down event
2837 firstWindow->consumeMotionDown();
2838 secondWindow->assertNoEvents();
2839
2840 // Send pointer down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002841 NotifyMotionArgs pointerDownMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002842 generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002843 {touchPoint, touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08002844 mDispatcher->notifyMotion(&pointerDownMotionArgs);
2845 // Only the first window should get the pointer down event
2846 firstWindow->consumeMotionPointerDown(1);
2847 secondWindow->assertNoEvents();
2848
2849 // Transfer touch focus to the second window
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00002850 TransferFunction f = GetParam();
2851 bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
2852 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08002853 // The first window gets cancel and the second gets down and pointer down
2854 firstWindow->consumeMotionCancel();
2855 secondWindow->consumeMotionDown();
2856 secondWindow->consumeMotionPointerDown(1);
2857
2858 // Send pointer up to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002859 NotifyMotionArgs pointerUpMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002860 generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002861 {touchPoint, touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08002862 mDispatcher->notifyMotion(&pointerUpMotionArgs);
2863 // The first window gets nothing and the second gets pointer up
2864 firstWindow->assertNoEvents();
2865 secondWindow->consumeMotionPointerUp(1);
2866
2867 // Send up event to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002868 NotifyMotionArgs upMotionArgs =
2869 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
2870 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08002871 mDispatcher->notifyMotion(&upMotionArgs);
2872 // The first window gets nothing and the second gets up
2873 firstWindow->assertNoEvents();
2874 secondWindow->consumeMotionUp();
2875}
2876
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00002877// For the cases of single pointer touch and two pointers non-split touch, the api's
2878// 'transferTouch' and 'transferTouchFocus' are equivalent in behaviour. They only differ
2879// for the case where there are multiple pointers split across several windows.
2880INSTANTIATE_TEST_SUITE_P(TransferFunctionTests, TransferTouchFixture,
2881 ::testing::Values(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002882 [&](const std::unique_ptr<InputDispatcher>& dispatcher,
2883 sp<IBinder> /*ignored*/, sp<IBinder> destChannelToken) {
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07002884 return dispatcher->transferTouch(destChannelToken,
2885 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00002886 },
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002887 [&](const std::unique_ptr<InputDispatcher>& dispatcher,
2888 sp<IBinder> from, sp<IBinder> to) {
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00002889 return dispatcher->transferTouchFocus(from, to,
2890 false /*isDragAndDrop*/);
2891 }));
2892
Svet Ganov5d3bc372020-01-26 23:11:07 -08002893TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07002894 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08002895
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002896 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002897 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
2898 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08002899 firstWindow->setFrame(Rect(0, 0, 600, 400));
Svet Ganov5d3bc372020-01-26 23:11:07 -08002900
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002901 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002902 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
2903 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08002904 secondWindow->setFrame(Rect(0, 400, 600, 800));
Svet Ganov5d3bc372020-01-26 23:11:07 -08002905
2906 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00002907 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08002908
2909 PointF pointInFirst = {300, 200};
2910 PointF pointInSecond = {300, 600};
2911
2912 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002913 NotifyMotionArgs firstDownMotionArgs =
2914 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2915 ADISPLAY_ID_DEFAULT, {pointInFirst});
Svet Ganov5d3bc372020-01-26 23:11:07 -08002916 mDispatcher->notifyMotion(&firstDownMotionArgs);
2917 // Only the first window should get the down event
2918 firstWindow->consumeMotionDown();
2919 secondWindow->assertNoEvents();
2920
2921 // Send down to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002922 NotifyMotionArgs secondDownMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002923 generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002924 {pointInFirst, pointInSecond});
Svet Ganov5d3bc372020-01-26 23:11:07 -08002925 mDispatcher->notifyMotion(&secondDownMotionArgs);
2926 // The first window gets a move and the second a down
2927 firstWindow->consumeMotionMove();
2928 secondWindow->consumeMotionDown();
2929
2930 // Transfer touch focus to the second window
2931 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
2932 // The first window gets cancel and the new gets pointer down (it already saw down)
2933 firstWindow->consumeMotionCancel();
2934 secondWindow->consumeMotionPointerDown(1);
2935
2936 // Send pointer up to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002937 NotifyMotionArgs pointerUpMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002938 generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002939 {pointInFirst, pointInSecond});
Svet Ganov5d3bc372020-01-26 23:11:07 -08002940 mDispatcher->notifyMotion(&pointerUpMotionArgs);
2941 // The first window gets nothing and the second gets pointer up
2942 firstWindow->assertNoEvents();
2943 secondWindow->consumeMotionPointerUp(1);
2944
2945 // Send up event to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002946 NotifyMotionArgs upMotionArgs =
2947 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
2948 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08002949 mDispatcher->notifyMotion(&upMotionArgs);
2950 // The first window gets nothing and the second gets up
2951 firstWindow->assertNoEvents();
2952 secondWindow->consumeMotionUp();
2953}
2954
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00002955// Same as TransferTouchFocus_TwoPointersSplitTouch, but using 'transferTouch' api.
2956// Unlike 'transferTouchFocus', calling 'transferTouch' when there are two windows receiving
2957// touch is not supported, so the touch should continue on those windows and the transferred-to
2958// window should get nothing.
2959TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) {
2960 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2961
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00002962 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002963 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
2964 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00002965 firstWindow->setFrame(Rect(0, 0, 600, 400));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00002966
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00002967 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002968 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
2969 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00002970 secondWindow->setFrame(Rect(0, 400, 600, 800));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00002971
2972 // Add the windows to the dispatcher
2973 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
2974
2975 PointF pointInFirst = {300, 200};
2976 PointF pointInSecond = {300, 600};
2977
2978 // Send down to the first window
2979 NotifyMotionArgs firstDownMotionArgs =
2980 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2981 ADISPLAY_ID_DEFAULT, {pointInFirst});
2982 mDispatcher->notifyMotion(&firstDownMotionArgs);
2983 // Only the first window should get the down event
2984 firstWindow->consumeMotionDown();
2985 secondWindow->assertNoEvents();
2986
2987 // Send down to the second window
2988 NotifyMotionArgs secondDownMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002989 generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00002990 {pointInFirst, pointInSecond});
2991 mDispatcher->notifyMotion(&secondDownMotionArgs);
2992 // The first window gets a move and the second a down
2993 firstWindow->consumeMotionMove();
2994 secondWindow->consumeMotionDown();
2995
2996 // Transfer touch focus to the second window
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07002997 const bool transferred =
2998 mDispatcher->transferTouch(secondWindow->getToken(), ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00002999 // The 'transferTouch' call should not succeed, because there are 2 touched windows
3000 ASSERT_FALSE(transferred);
3001 firstWindow->assertNoEvents();
3002 secondWindow->assertNoEvents();
3003
3004 // The rest of the dispatch should proceed as normal
3005 // Send pointer up to the second window
3006 NotifyMotionArgs pointerUpMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003007 generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003008 {pointInFirst, pointInSecond});
3009 mDispatcher->notifyMotion(&pointerUpMotionArgs);
3010 // The first window gets MOVE and the second gets pointer up
3011 firstWindow->consumeMotionMove();
3012 secondWindow->consumeMotionUp();
3013
3014 // Send up event to the first window
3015 NotifyMotionArgs upMotionArgs =
3016 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3017 ADISPLAY_ID_DEFAULT);
3018 mDispatcher->notifyMotion(&upMotionArgs);
3019 // The first window gets nothing and the second gets up
3020 firstWindow->consumeMotionUp();
3021 secondWindow->assertNoEvents();
3022}
3023
Arthur Hungabbb9d82021-09-01 14:52:30 +00003024// This case will create two windows and one mirrored window on the default display and mirror
3025// two windows on the second display. It will test if 'transferTouchFocus' works fine if we put
3026// the windows info of second display before default display.
3027TEST_F(InputDispatcherTest, TransferTouchFocus_CloneSurface) {
3028 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3029 sp<FakeWindowHandle> firstWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003030 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00003031 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003032 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003033 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00003034 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003035
3036 sp<FakeWindowHandle> mirrorWindowInPrimary =
3037 firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT);
3038 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003039
3040 sp<FakeWindowHandle> firstWindowInSecondary =
3041 firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
3042 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003043
3044 sp<FakeWindowHandle> secondWindowInSecondary =
3045 secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
3046 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003047
3048 // Update window info, let it find window handle of second display first.
3049 mDispatcher->setInputWindows(
3050 {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}},
3051 {ADISPLAY_ID_DEFAULT,
3052 {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}});
3053
3054 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3055 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3056 {50, 50}))
3057 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3058
3059 // Window should receive motion event.
3060 firstWindowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3061
3062 // Transfer touch focus
3063 ASSERT_TRUE(mDispatcher->transferTouchFocus(firstWindowInPrimary->getToken(),
3064 secondWindowInPrimary->getToken()));
3065 // The first window gets cancel.
3066 firstWindowInPrimary->consumeMotionCancel();
3067 secondWindowInPrimary->consumeMotionDown();
3068
3069 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3070 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
3071 ADISPLAY_ID_DEFAULT, {150, 50}))
3072 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3073 firstWindowInPrimary->assertNoEvents();
3074 secondWindowInPrimary->consumeMotionMove();
3075
3076 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3077 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3078 {150, 50}))
3079 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3080 firstWindowInPrimary->assertNoEvents();
3081 secondWindowInPrimary->consumeMotionUp();
3082}
3083
3084// Same as TransferTouchFocus_CloneSurface, but this touch on the secondary display and use
3085// 'transferTouch' api.
3086TEST_F(InputDispatcherTest, TransferTouch_CloneSurface) {
3087 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3088 sp<FakeWindowHandle> firstWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003089 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00003090 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003091 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003092 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00003093 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003094
3095 sp<FakeWindowHandle> mirrorWindowInPrimary =
3096 firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT);
3097 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003098
3099 sp<FakeWindowHandle> firstWindowInSecondary =
3100 firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
3101 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003102
3103 sp<FakeWindowHandle> secondWindowInSecondary =
3104 secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
3105 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003106
3107 // Update window info, let it find window handle of second display first.
3108 mDispatcher->setInputWindows(
3109 {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}},
3110 {ADISPLAY_ID_DEFAULT,
3111 {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}});
3112
3113 // Touch on second display.
3114 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3115 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {50, 50}))
3116 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3117
3118 // Window should receive motion event.
3119 firstWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID);
3120
3121 // Transfer touch focus
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07003122 ASSERT_TRUE(mDispatcher->transferTouch(secondWindowInSecondary->getToken(), SECOND_DISPLAY_ID));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003123
3124 // The first window gets cancel.
3125 firstWindowInPrimary->consumeMotionCancel(SECOND_DISPLAY_ID);
3126 secondWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID);
3127
3128 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3129 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
3130 SECOND_DISPLAY_ID, {150, 50}))
3131 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3132 firstWindowInPrimary->assertNoEvents();
3133 secondWindowInPrimary->consumeMotionMove(SECOND_DISPLAY_ID);
3134
3135 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3136 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {150, 50}))
3137 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3138 firstWindowInPrimary->assertNoEvents();
3139 secondWindowInPrimary->consumeMotionUp(SECOND_DISPLAY_ID);
3140}
3141
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003142TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07003143 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003144 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3145 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003146
Vishnu Nair47074b82020-08-14 11:54:47 -07003147 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00003148 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003149 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003150
3151 window->consumeFocusEvent(true);
3152
3153 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
3154 mDispatcher->notifyKey(&keyArgs);
3155
3156 // Window should receive key down event.
3157 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3158}
3159
3160TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07003161 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003162 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3163 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003164
Arthur Hung72d8dc32020-03-28 00:48:39 +00003165 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003166
3167 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
3168 mDispatcher->notifyKey(&keyArgs);
3169 mDispatcher->waitForIdle();
3170
3171 window->assertNoEvents();
3172}
3173
3174// If a window is touchable, but does not have focus, it should receive motion events, but not keys
3175TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) {
Chris Yea209fde2020-07-22 13:54:51 -07003176 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003177 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3178 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003179
Arthur Hung72d8dc32020-03-28 00:48:39 +00003180 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003181
3182 // Send key
3183 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
3184 mDispatcher->notifyKey(&keyArgs);
3185 // Send motion
3186 NotifyMotionArgs motionArgs =
3187 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3188 ADISPLAY_ID_DEFAULT);
3189 mDispatcher->notifyMotion(&motionArgs);
3190
3191 // Window should receive only the motion event
3192 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3193 window->assertNoEvents(); // Key event or focus event will not be received
3194}
3195
arthurhungea3f4fc2020-12-21 23:18:53 +08003196TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) {
3197 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3198
arthurhungea3f4fc2020-12-21 23:18:53 +08003199 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003200 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
3201 ADISPLAY_ID_DEFAULT);
arthurhungea3f4fc2020-12-21 23:18:53 +08003202 firstWindow->setFrame(Rect(0, 0, 600, 400));
arthurhungea3f4fc2020-12-21 23:18:53 +08003203
arthurhungea3f4fc2020-12-21 23:18:53 +08003204 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003205 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3206 ADISPLAY_ID_DEFAULT);
arthurhungea3f4fc2020-12-21 23:18:53 +08003207 secondWindow->setFrame(Rect(0, 400, 600, 800));
arthurhungea3f4fc2020-12-21 23:18:53 +08003208
3209 // Add the windows to the dispatcher
3210 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
3211
3212 PointF pointInFirst = {300, 200};
3213 PointF pointInSecond = {300, 600};
3214
3215 // Send down to the first window
3216 NotifyMotionArgs firstDownMotionArgs =
3217 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3218 ADISPLAY_ID_DEFAULT, {pointInFirst});
3219 mDispatcher->notifyMotion(&firstDownMotionArgs);
3220 // Only the first window should get the down event
3221 firstWindow->consumeMotionDown();
3222 secondWindow->assertNoEvents();
3223
3224 // Send down to the second window
3225 NotifyMotionArgs secondDownMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003226 generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
arthurhungea3f4fc2020-12-21 23:18:53 +08003227 {pointInFirst, pointInSecond});
3228 mDispatcher->notifyMotion(&secondDownMotionArgs);
3229 // The first window gets a move and the second a down
3230 firstWindow->consumeMotionMove();
3231 secondWindow->consumeMotionDown();
3232
3233 // Send pointer cancel to the second window
3234 NotifyMotionArgs pointerUpMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003235 generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
arthurhungea3f4fc2020-12-21 23:18:53 +08003236 {pointInFirst, pointInSecond});
3237 pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED;
3238 mDispatcher->notifyMotion(&pointerUpMotionArgs);
3239 // The first window gets move and the second gets cancel.
3240 firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
3241 secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
3242
3243 // Send up event.
3244 NotifyMotionArgs upMotionArgs =
3245 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3246 ADISPLAY_ID_DEFAULT);
3247 mDispatcher->notifyMotion(&upMotionArgs);
3248 // The first window gets up and the second gets nothing.
3249 firstWindow->consumeMotionUp();
3250 secondWindow->assertNoEvents();
3251}
3252
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00003253TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) {
3254 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3255
3256 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003257 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00003258 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3259 std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline;
3260 graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2;
3261 graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3;
3262
3263 window->sendTimeline(1 /*inputEventId*/, graphicsTimeline);
3264 window->assertNoEvents();
3265 mDispatcher->waitForIdle();
3266}
3267
chaviwd1c23182019-12-20 18:44:56 -08003268class FakeMonitorReceiver {
Michael Wright3a240c42019-12-10 20:53:41 +00003269public:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003270 FakeMonitorReceiver(const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003271 int32_t displayId) {
Garfield Tan15601662020-09-22 15:32:38 -07003272 base::Result<std::unique_ptr<InputChannel>> channel =
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08003273 dispatcher->createInputMonitor(displayId, name, MONITOR_PID);
Garfield Tan15601662020-09-22 15:32:38 -07003274 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
Michael Wright3a240c42019-12-10 20:53:41 +00003275 }
3276
chaviwd1c23182019-12-20 18:44:56 -08003277 sp<IBinder> getToken() { return mInputReceiver->getToken(); }
3278
3279 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
3280 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN,
3281 expectedDisplayId, expectedFlags);
3282 }
3283
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003284 std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); }
3285
3286 void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); }
3287
chaviwd1c23182019-12-20 18:44:56 -08003288 void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
3289 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN,
3290 expectedDisplayId, expectedFlags);
3291 }
3292
Siarhei Vishniakouca205502021-07-16 21:31:58 +00003293 void consumeMotionMove(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
3294 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE,
3295 expectedDisplayId, expectedFlags);
3296 }
3297
chaviwd1c23182019-12-20 18:44:56 -08003298 void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
3299 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP,
3300 expectedDisplayId, expectedFlags);
3301 }
3302
Siarhei Vishniakouca205502021-07-16 21:31:58 +00003303 void consumeMotionCancel(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
3304 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL,
3305 expectedDisplayId, expectedFlags);
3306 }
3307
Arthur Hungfbfa5722021-11-16 02:45:54 +00003308 void consumeMotionPointerDown(int32_t pointerIdx) {
3309 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
3310 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
3311 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, ADISPLAY_ID_DEFAULT,
3312 0 /*expectedFlags*/);
3313 }
3314
Evan Rosky84f07f02021-04-16 10:42:42 -07003315 MotionEvent* consumeMotion() {
3316 InputEvent* event = mInputReceiver->consume();
3317 if (!event) {
3318 ADD_FAILURE() << "No event was produced";
3319 return nullptr;
3320 }
3321 if (event->getType() != AINPUT_EVENT_TYPE_MOTION) {
3322 ADD_FAILURE() << "Received event of type " << event->getType() << " instead of motion";
3323 return nullptr;
3324 }
3325 return static_cast<MotionEvent*>(event);
3326 }
3327
chaviwd1c23182019-12-20 18:44:56 -08003328 void assertNoEvents() { mInputReceiver->assertNoEvents(); }
3329
3330private:
3331 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Michael Wright3a240c42019-12-10 20:53:41 +00003332};
3333
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003334using InputDispatcherMonitorTest = InputDispatcherTest;
3335
Siarhei Vishniakouca205502021-07-16 21:31:58 +00003336/**
3337 * Two entities that receive touch: A window, and a global monitor.
3338 * The touch goes to the window, and then the window disappears.
3339 * The monitor does not get cancel right away. But if more events come in, the touch gets canceled
3340 * for the monitor, as well.
3341 * 1. foregroundWindow
3342 * 2. monitor <-- global monitor (doesn't observe z order, receives all events)
3343 */
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003344TEST_F(InputDispatcherMonitorTest, MonitorTouchIsCanceledWhenForegroundWindowDisappears) {
Siarhei Vishniakouca205502021-07-16 21:31:58 +00003345 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3346 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003347 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00003348
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003349 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00003350
3351 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3352 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3353 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3354 {100, 200}))
3355 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3356
3357 // Both the foreground window and the global monitor should receive the touch down
3358 window->consumeMotionDown();
3359 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
3360
3361 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3362 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
3363 ADISPLAY_ID_DEFAULT, {110, 200}))
3364 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3365
3366 window->consumeMotionMove();
3367 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
3368
3369 // Now the foreground window goes away
3370 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
3371 window->consumeMotionCancel();
3372 monitor.assertNoEvents(); // Global monitor does not get a cancel yet
3373
3374 // If more events come in, there will be no more foreground window to send them to. This will
3375 // cause a cancel for the monitor, as well.
3376 ASSERT_EQ(InputEventInjectionResult::FAILED,
3377 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
3378 ADISPLAY_ID_DEFAULT, {120, 200}))
3379 << "Injection should fail because the window was removed";
3380 window->assertNoEvents();
3381 // Global monitor now gets the cancel
3382 monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
3383}
3384
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003385TEST_F(InputDispatcherMonitorTest, ReceivesMotionEvents) {
Chris Yea209fde2020-07-22 13:54:51 -07003386 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003387 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3388 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00003389 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00003390
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003391 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00003392
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003393 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00003394 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003395 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00003396 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08003397 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00003398}
3399
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003400TEST_F(InputDispatcherMonitorTest, MonitorCannotPilferPointers) {
3401 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00003402
Chris Yea209fde2020-07-22 13:54:51 -07003403 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003404 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3405 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00003406 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00003407
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003408 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00003409 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003410 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
chaviwd1c23182019-12-20 18:44:56 -08003411 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003412 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00003413
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003414 // Pilfer pointers from the monitor.
3415 // This should not do anything and the window should continue to receive events.
3416 EXPECT_NE(OK, mDispatcher->pilferPointers(monitor.getToken()));
Michael Wright3a240c42019-12-10 20:53:41 +00003417
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003418 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003419 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
3420 ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003421 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003422
3423 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
3424 window->consumeMotionMove(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00003425}
3426
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003427TEST_F(InputDispatcherMonitorTest, NoWindowTransform) {
Evan Rosky84f07f02021-04-16 10:42:42 -07003428 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003429 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3430 "Fake Window", ADISPLAY_ID_DEFAULT);
Evan Rosky84f07f02021-04-16 10:42:42 -07003431 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3432 window->setWindowOffset(20, 40);
3433 window->setWindowTransform(0, 1, -1, 0);
3434
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003435 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Evan Rosky84f07f02021-04-16 10:42:42 -07003436
3437 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3438 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
3439 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3440 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3441 MotionEvent* event = monitor.consumeMotion();
3442 // Even though window has transform, gesture monitor must not.
3443 ASSERT_EQ(ui::Transform(), event->getTransform());
3444}
3445
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003446TEST_F(InputDispatcherMonitorTest, InjectionFailsWithNoWindow) {
Arthur Hungb3307ee2021-10-14 10:57:37 +00003447 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003448 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Arthur Hungb3307ee2021-10-14 10:57:37 +00003449
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003450 ASSERT_EQ(InputEventInjectionResult::FAILED,
Arthur Hungb3307ee2021-10-14 10:57:37 +00003451 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003452 << "Injection should fail if there is a monitor, but no touchable window";
3453 monitor.assertNoEvents();
Arthur Hungb3307ee2021-10-14 10:57:37 +00003454}
3455
chaviw81e2bb92019-12-18 15:03:51 -08003456TEST_F(InputDispatcherTest, TestMoveEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07003457 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003458 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3459 "Fake Window", ADISPLAY_ID_DEFAULT);
chaviw81e2bb92019-12-18 15:03:51 -08003460
Arthur Hung72d8dc32020-03-28 00:48:39 +00003461 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
chaviw81e2bb92019-12-18 15:03:51 -08003462
3463 NotifyMotionArgs motionArgs =
3464 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3465 ADISPLAY_ID_DEFAULT);
3466
3467 mDispatcher->notifyMotion(&motionArgs);
3468 // Window should receive motion down event.
3469 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3470
3471 motionArgs.action = AMOTION_EVENT_ACTION_MOVE;
Garfield Tanc51d1ba2020-01-28 13:24:04 -08003472 motionArgs.id += 1;
chaviw81e2bb92019-12-18 15:03:51 -08003473 motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
3474 motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
3475 motionArgs.pointerCoords[0].getX() - 10);
3476
3477 mDispatcher->notifyMotion(&motionArgs);
3478 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT,
3479 0 /*expectedFlags*/);
3480}
3481
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003482/**
3483 * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to
3484 * the device default right away. In the test scenario, we check both the default value,
3485 * and the action of enabling / disabling.
3486 */
3487TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) {
Chris Yea209fde2020-07-22 13:54:51 -07003488 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003489 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3490 "Test window", ADISPLAY_ID_DEFAULT);
Antonio Kantekea47acb2021-12-23 12:41:25 -08003491 const WindowInfo& windowInfo = *window->getInfo();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003492
3493 // Set focused application.
3494 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07003495 window->setFocusable(true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003496
3497 SCOPED_TRACE("Check default value of touch mode");
Arthur Hung72d8dc32020-03-28 00:48:39 +00003498 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003499 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003500 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
3501
3502 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07003503 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00003504 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003505 window->consumeFocusEvent(false /*hasFocus*/, true /*inTouchMode*/);
3506
3507 SCOPED_TRACE("Disable touch mode");
Antonio Kantekea47acb2021-12-23 12:41:25 -08003508 mDispatcher->setInTouchMode(false, windowInfo.ownerPid, windowInfo.ownerUid,
Antonio Kanteka042c022022-07-06 16:51:07 -07003509 true /*hasPermission*/, ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00003510 window->consumeTouchModeEvent(false);
Vishnu Nair47074b82020-08-14 11:54:47 -07003511 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00003512 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003513 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003514 window->consumeFocusEvent(true /*hasFocus*/, false /*inTouchMode*/);
3515
3516 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07003517 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00003518 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003519 window->consumeFocusEvent(false /*hasFocus*/, false /*inTouchMode*/);
3520
3521 SCOPED_TRACE("Enable touch mode again");
Antonio Kantekea47acb2021-12-23 12:41:25 -08003522 mDispatcher->setInTouchMode(true, windowInfo.ownerPid, windowInfo.ownerUid,
Antonio Kanteka042c022022-07-06 16:51:07 -07003523 true /*hasPermission*/, ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00003524 window->consumeTouchModeEvent(true);
Vishnu Nair47074b82020-08-14 11:54:47 -07003525 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00003526 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003527 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003528 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
3529
3530 window->assertNoEvents();
3531}
3532
Gang Wange9087892020-01-07 12:17:14 -05003533TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07003534 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003535 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3536 "Test window", ADISPLAY_ID_DEFAULT);
Gang Wange9087892020-01-07 12:17:14 -05003537
3538 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07003539 window->setFocusable(true);
Gang Wange9087892020-01-07 12:17:14 -05003540
Arthur Hung72d8dc32020-03-28 00:48:39 +00003541 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003542 setFocusedWindow(window);
3543
Gang Wange9087892020-01-07 12:17:14 -05003544 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
3545
3546 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
3547 mDispatcher->notifyKey(&keyArgs);
3548
3549 InputEvent* event = window->consume();
3550 ASSERT_NE(event, nullptr);
3551
3552 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
3553 ASSERT_NE(verified, nullptr);
3554 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY);
3555
3556 ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos);
3557 ASSERT_EQ(keyArgs.deviceId, verified->deviceId);
3558 ASSERT_EQ(keyArgs.source, verified->source);
3559 ASSERT_EQ(keyArgs.displayId, verified->displayId);
3560
3561 const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified);
3562
3563 ASSERT_EQ(keyArgs.action, verifiedKey.action);
Gang Wange9087892020-01-07 12:17:14 -05003564 ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08003565 ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos);
Gang Wange9087892020-01-07 12:17:14 -05003566 ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode);
3567 ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode);
3568 ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState);
3569 ASSERT_EQ(0, verifiedKey.repeatCount);
3570}
3571
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08003572TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07003573 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003574 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3575 "Test window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08003576
3577 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3578
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07003579 ui::Transform transform;
3580 transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
3581
3582 gui::DisplayInfo displayInfo;
3583 displayInfo.displayId = ADISPLAY_ID_DEFAULT;
3584 displayInfo.transform = transform;
3585
3586 mDispatcher->onWindowInfosChanged({*window->getInfo()}, {displayInfo});
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08003587
3588 NotifyMotionArgs motionArgs =
3589 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3590 ADISPLAY_ID_DEFAULT);
3591 mDispatcher->notifyMotion(&motionArgs);
3592
3593 InputEvent* event = window->consume();
3594 ASSERT_NE(event, nullptr);
3595
3596 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
3597 ASSERT_NE(verified, nullptr);
3598 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION);
3599
3600 EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos);
3601 EXPECT_EQ(motionArgs.deviceId, verified->deviceId);
3602 EXPECT_EQ(motionArgs.source, verified->source);
3603 EXPECT_EQ(motionArgs.displayId, verified->displayId);
3604
3605 const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified);
3606
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07003607 const vec2 rawXY =
3608 MotionEvent::calculateTransformedXY(motionArgs.source, transform,
3609 motionArgs.pointerCoords[0].getXYValue());
3610 EXPECT_EQ(rawXY.x, verifiedMotion.rawX);
3611 EXPECT_EQ(rawXY.y, verifiedMotion.rawY);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08003612 EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08003613 EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08003614 EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08003615 EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState);
3616 EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState);
3617}
3618
chaviw09c8d2d2020-08-24 15:48:26 -07003619/**
3620 * Ensure that separate calls to sign the same data are generating the same key.
3621 * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance
3622 * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky
3623 * tests.
3624 */
3625TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) {
3626 KeyEvent event = getTestKeyEvent();
3627 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
3628
3629 std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent);
3630 std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent);
3631 ASSERT_EQ(hmac1, hmac2);
3632}
3633
3634/**
3635 * Ensure that changes in VerifiedKeyEvent produce a different hmac.
3636 */
3637TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) {
3638 KeyEvent event = getTestKeyEvent();
3639 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
3640 std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent);
3641
3642 verifiedEvent.deviceId += 1;
3643 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
3644
3645 verifiedEvent.source += 1;
3646 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
3647
3648 verifiedEvent.eventTimeNanos += 1;
3649 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
3650
3651 verifiedEvent.displayId += 1;
3652 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
3653
3654 verifiedEvent.action += 1;
3655 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
3656
3657 verifiedEvent.downTimeNanos += 1;
3658 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
3659
3660 verifiedEvent.flags += 1;
3661 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
3662
3663 verifiedEvent.keyCode += 1;
3664 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
3665
3666 verifiedEvent.scanCode += 1;
3667 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
3668
3669 verifiedEvent.metaState += 1;
3670 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
3671
3672 verifiedEvent.repeatCount += 1;
3673 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
3674}
3675
Vishnu Nair958da932020-08-21 17:12:37 -07003676TEST_F(InputDispatcherTest, SetFocusedWindow) {
3677 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3678 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003679 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07003680 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003681 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07003682 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3683
3684 // Top window is also focusable but is not granted focus.
3685 windowTop->setFocusable(true);
3686 windowSecond->setFocusable(true);
3687 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
3688 setFocusedWindow(windowSecond);
3689
3690 windowSecond->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003691 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
3692 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07003693
3694 // Focused window should receive event.
3695 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
3696 windowTop->assertNoEvents();
3697}
3698
3699TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) {
3700 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3701 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003702 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07003703 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3704
3705 window->setFocusable(true);
3706 // Release channel for window is no longer valid.
3707 window->releaseChannel();
3708 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3709 setFocusedWindow(window);
3710
3711 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003712 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
3713 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07003714
3715 // window channel is invalid, so it should not receive any input event.
3716 window->assertNoEvents();
3717}
3718
3719TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) {
3720 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3721 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003722 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08003723 window->setFocusable(false);
Vishnu Nair958da932020-08-21 17:12:37 -07003724 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3725
Vishnu Nair958da932020-08-21 17:12:37 -07003726 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3727 setFocusedWindow(window);
3728
3729 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003730 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
3731 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07003732
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08003733 // window is not focusable, so it should not receive any input event.
Vishnu Nair958da932020-08-21 17:12:37 -07003734 window->assertNoEvents();
3735}
3736
3737TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) {
3738 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3739 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003740 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07003741 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003742 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07003743 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3744
3745 windowTop->setFocusable(true);
3746 windowSecond->setFocusable(true);
3747 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
3748 setFocusedWindow(windowTop);
3749 windowTop->consumeFocusEvent(true);
3750
3751 setFocusedWindow(windowSecond, windowTop);
3752 windowSecond->consumeFocusEvent(true);
3753 windowTop->consumeFocusEvent(false);
3754
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003755 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
3756 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07003757
3758 // Focused window should receive event.
3759 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
3760}
3761
3762TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestFocusTokenNotFocused) {
3763 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3764 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003765 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07003766 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003767 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07003768 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3769
3770 windowTop->setFocusable(true);
3771 windowSecond->setFocusable(true);
3772 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
3773 setFocusedWindow(windowSecond, windowTop);
3774
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003775 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
3776 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07003777
3778 // Event should be dropped.
3779 windowTop->assertNoEvents();
3780 windowSecond->assertNoEvents();
3781}
3782
3783TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) {
3784 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3785 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003786 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07003787 sp<FakeWindowHandle> previousFocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003788 sp<FakeWindowHandle>::make(application, mDispatcher, "previousFocusedWindow",
3789 ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07003790 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3791
3792 window->setFocusable(true);
3793 previousFocusedWindow->setFocusable(true);
3794 window->setVisible(false);
3795 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}});
3796 setFocusedWindow(previousFocusedWindow);
3797 previousFocusedWindow->consumeFocusEvent(true);
3798
3799 // Requesting focus on invisible window takes focus from currently focused window.
3800 setFocusedWindow(window);
3801 previousFocusedWindow->consumeFocusEvent(false);
3802
3803 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003804 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Vishnu Nair958da932020-08-21 17:12:37 -07003805 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003806 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07003807
3808 // Window does not get focus event or key down.
3809 window->assertNoEvents();
3810
3811 // Window becomes visible.
3812 window->setVisible(true);
3813 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3814
3815 // Window receives focus event.
3816 window->consumeFocusEvent(true);
3817 // Focused window receives key down.
3818 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3819}
3820
Vishnu Nair599f1412021-06-21 10:39:58 -07003821TEST_F(InputDispatcherTest, DisplayRemoved) {
3822 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3823 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003824 sp<FakeWindowHandle>::make(application, mDispatcher, "window", ADISPLAY_ID_DEFAULT);
Vishnu Nair599f1412021-06-21 10:39:58 -07003825 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3826
3827 // window is granted focus.
3828 window->setFocusable(true);
3829 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3830 setFocusedWindow(window);
3831 window->consumeFocusEvent(true);
3832
3833 // When a display is removed window loses focus.
3834 mDispatcher->displayRemoved(ADISPLAY_ID_DEFAULT);
3835 window->consumeFocusEvent(false);
3836}
3837
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003838/**
3839 * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY,
3840 * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top
3841 * of the 'slipperyEnterWindow'.
3842 *
3843 * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such
3844 * a way so that the touched location is no longer covered by the top window.
3845 *
3846 * Next, inject a MOVE event. Because the top window already moved earlier, this event is now
3847 * positioned over the bottom (slipperyEnterWindow) only. And because the top window had
3848 * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive
3849 * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting
3850 * with ACTION_DOWN).
3851 * Thus, the touch has been transferred from the top window into the bottom window, because the top
3852 * window moved itself away from the touched location and had Flag::SLIPPERY.
3853 *
3854 * Even though the top window moved away from the touched location, it is still obscuring the bottom
3855 * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_
3856 * OBSCURED should be set for the MotionEvent that reaches the bottom window.
3857 *
3858 * In this test, we ensure that the event received by the bottom window has
3859 * FLAG_WINDOW_IS_PARTIALLY_OBSCURED.
3860 */
3861TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) {
Prabir Pradhan5735a322022-04-11 17:23:34 +00003862 constexpr int32_t SLIPPERY_PID = WINDOW_PID + 1;
3863 constexpr int32_t SLIPPERY_UID = WINDOW_UID + 1;
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003864
3865 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3866 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3867
3868 sp<FakeWindowHandle> slipperyExitWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003869 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08003870 slipperyExitWindow->setSlippery(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003871 // Make sure this one overlaps the bottom window
3872 slipperyExitWindow->setFrame(Rect(25, 25, 75, 75));
3873 // Change the owner uid/pid of the window so that it is considered to be occluding the bottom
3874 // one. Windows with the same owner are not considered to be occluding each other.
3875 slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID);
3876
3877 sp<FakeWindowHandle> slipperyEnterWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003878 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003879 slipperyExitWindow->setFrame(Rect(0, 0, 100, 100));
3880
3881 mDispatcher->setInputWindows(
3882 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
3883
3884 // Use notifyMotion instead of injecting to avoid dealing with injection permissions
3885 NotifyMotionArgs args = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3886 ADISPLAY_ID_DEFAULT, {{50, 50}});
3887 mDispatcher->notifyMotion(&args);
3888 slipperyExitWindow->consumeMotionDown();
3889 slipperyExitWindow->setFrame(Rect(70, 70, 100, 100));
3890 mDispatcher->setInputWindows(
3891 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
3892
3893 args = generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
3894 ADISPLAY_ID_DEFAULT, {{51, 51}});
3895 mDispatcher->notifyMotion(&args);
3896
3897 slipperyExitWindow->consumeMotionCancel();
3898
3899 slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT,
3900 AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
3901}
3902
Garfield Tan1c7bc862020-01-28 13:24:04 -08003903class InputDispatcherKeyRepeatTest : public InputDispatcherTest {
3904protected:
3905 static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms
3906 static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms
3907
Chris Yea209fde2020-07-22 13:54:51 -07003908 std::shared_ptr<FakeApplicationHandle> mApp;
Garfield Tan1c7bc862020-01-28 13:24:04 -08003909 sp<FakeWindowHandle> mWindow;
3910
3911 virtual void SetUp() override {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003912 mFakePolicy = sp<FakeInputDispatcherPolicy>::make();
Garfield Tan1c7bc862020-01-28 13:24:04 -08003913 mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003914 mDispatcher = std::make_unique<InputDispatcher>(mFakePolicy);
Garfield Tan1c7bc862020-01-28 13:24:04 -08003915 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
3916 ASSERT_EQ(OK, mDispatcher->start());
3917
3918 setUpWindow();
3919 }
3920
3921 void setUpWindow() {
Chris Yea209fde2020-07-22 13:54:51 -07003922 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003923 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Garfield Tan1c7bc862020-01-28 13:24:04 -08003924
Vishnu Nair47074b82020-08-14 11:54:47 -07003925 mWindow->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00003926 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003927 setFocusedWindow(mWindow);
Garfield Tan1c7bc862020-01-28 13:24:04 -08003928 mWindow->consumeFocusEvent(true);
3929 }
3930
Chris Ye2ad95392020-09-01 13:44:44 -07003931 void sendAndConsumeKeyDown(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08003932 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07003933 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08003934 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event
3935 mDispatcher->notifyKey(&keyArgs);
3936
3937 // Window should receive key down event.
3938 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3939 }
3940
3941 void expectKeyRepeatOnce(int32_t repeatCount) {
3942 SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount));
3943 InputEvent* repeatEvent = mWindow->consume();
3944 ASSERT_NE(nullptr, repeatEvent);
3945
3946 uint32_t eventType = repeatEvent->getType();
3947 ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, eventType);
3948
3949 KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent);
3950 uint32_t eventAction = repeatKeyEvent->getAction();
3951 EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction);
3952 EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount());
3953 }
3954
Chris Ye2ad95392020-09-01 13:44:44 -07003955 void sendAndConsumeKeyUp(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08003956 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07003957 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08003958 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event
3959 mDispatcher->notifyKey(&keyArgs);
3960
3961 // Window should receive key down event.
3962 mWindow->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
3963 0 /*expectedFlags*/);
3964 }
3965};
3966
3967TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) {
Chris Ye2ad95392020-09-01 13:44:44 -07003968 sendAndConsumeKeyDown(1 /* deviceId */);
3969 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
3970 expectKeyRepeatOnce(repeatCount);
3971 }
3972}
3973
3974TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) {
3975 sendAndConsumeKeyDown(1 /* deviceId */);
3976 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
3977 expectKeyRepeatOnce(repeatCount);
3978 }
3979 sendAndConsumeKeyDown(2 /* deviceId */);
3980 /* repeatCount will start from 1 for deviceId 2 */
Garfield Tan1c7bc862020-01-28 13:24:04 -08003981 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
3982 expectKeyRepeatOnce(repeatCount);
3983 }
3984}
3985
3986TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) {
Chris Ye2ad95392020-09-01 13:44:44 -07003987 sendAndConsumeKeyDown(1 /* deviceId */);
Garfield Tan1c7bc862020-01-28 13:24:04 -08003988 expectKeyRepeatOnce(1 /*repeatCount*/);
Chris Ye2ad95392020-09-01 13:44:44 -07003989 sendAndConsumeKeyUp(1 /* deviceId */);
3990 mWindow->assertNoEvents();
3991}
3992
3993TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) {
3994 sendAndConsumeKeyDown(1 /* deviceId */);
3995 expectKeyRepeatOnce(1 /*repeatCount*/);
3996 sendAndConsumeKeyDown(2 /* deviceId */);
3997 expectKeyRepeatOnce(1 /*repeatCount*/);
3998 // Stale key up from device 1.
3999 sendAndConsumeKeyUp(1 /* deviceId */);
4000 // Device 2 is still down, keep repeating
4001 expectKeyRepeatOnce(2 /*repeatCount*/);
4002 expectKeyRepeatOnce(3 /*repeatCount*/);
4003 // Device 2 key up
4004 sendAndConsumeKeyUp(2 /* deviceId */);
4005 mWindow->assertNoEvents();
4006}
4007
4008TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) {
4009 sendAndConsumeKeyDown(1 /* deviceId */);
4010 expectKeyRepeatOnce(1 /*repeatCount*/);
4011 sendAndConsumeKeyDown(2 /* deviceId */);
4012 expectKeyRepeatOnce(1 /*repeatCount*/);
4013 // Device 2 which holds the key repeating goes up, expect the repeating to stop.
4014 sendAndConsumeKeyUp(2 /* deviceId */);
4015 // Device 1 still holds key down, but the repeating was already stopped
Garfield Tan1c7bc862020-01-28 13:24:04 -08004016 mWindow->assertNoEvents();
4017}
4018
liushenxiang42232912021-05-21 20:24:09 +08004019TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) {
4020 sendAndConsumeKeyDown(DEVICE_ID);
4021 expectKeyRepeatOnce(1 /*repeatCount*/);
4022 NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID);
4023 mDispatcher->notifyDeviceReset(&args);
4024 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT,
4025 AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS);
4026 mWindow->assertNoEvents();
4027}
4028
Garfield Tan1c7bc862020-01-28 13:24:04 -08004029TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) {
Chris Ye2ad95392020-09-01 13:44:44 -07004030 sendAndConsumeKeyDown(1 /* deviceId */);
Garfield Tan1c7bc862020-01-28 13:24:04 -08004031 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
4032 InputEvent* repeatEvent = mWindow->consume();
4033 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
4034 EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER,
4035 IdGenerator::getSource(repeatEvent->getId()));
4036 }
4037}
4038
4039TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) {
Chris Ye2ad95392020-09-01 13:44:44 -07004040 sendAndConsumeKeyDown(1 /* deviceId */);
Garfield Tan1c7bc862020-01-28 13:24:04 -08004041
4042 std::unordered_set<int32_t> idSet;
4043 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
4044 InputEvent* repeatEvent = mWindow->consume();
4045 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
4046 int32_t id = repeatEvent->getId();
4047 EXPECT_EQ(idSet.end(), idSet.find(id));
4048 idSet.insert(id);
4049 }
4050}
4051
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004052/* Test InputDispatcher for MultiDisplay */
4053class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest {
4054public:
Prabir Pradhan3608aad2019-10-02 17:08:26 -07004055 virtual void SetUp() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004056 InputDispatcherTest::SetUp();
Arthur Hungb92218b2018-08-14 12:00:21 +08004057
Chris Yea209fde2020-07-22 13:54:51 -07004058 application1 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004059 windowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004060 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08004061
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004062 // Set focus window for primary display, but focused display would be second one.
4063 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1);
Vishnu Nair47074b82020-08-14 11:54:47 -07004064 windowInPrimary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004065 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004066 setFocusedWindow(windowInPrimary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004067 windowInPrimary->consumeFocusEvent(true);
Arthur Hungb92218b2018-08-14 12:00:21 +08004068
Chris Yea209fde2020-07-22 13:54:51 -07004069 application2 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004070 windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004071 sp<FakeWindowHandle>::make(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004072 // Set focus to second display window.
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004073 // Set focus display to second one.
4074 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
4075 // Set focus window for second display.
4076 mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2);
Vishnu Nair47074b82020-08-14 11:54:47 -07004077 windowInSecondary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004078 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004079 setFocusedWindow(windowInSecondary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004080 windowInSecondary->consumeFocusEvent(true);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004081 }
4082
Prabir Pradhan3608aad2019-10-02 17:08:26 -07004083 virtual void TearDown() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004084 InputDispatcherTest::TearDown();
4085
Chris Yea209fde2020-07-22 13:54:51 -07004086 application1.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004087 windowInPrimary.clear();
Chris Yea209fde2020-07-22 13:54:51 -07004088 application2.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004089 windowInSecondary.clear();
4090 }
4091
4092protected:
Chris Yea209fde2020-07-22 13:54:51 -07004093 std::shared_ptr<FakeApplicationHandle> application1;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004094 sp<FakeWindowHandle> windowInPrimary;
Chris Yea209fde2020-07-22 13:54:51 -07004095 std::shared_ptr<FakeApplicationHandle> application2;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004096 sp<FakeWindowHandle> windowInSecondary;
4097};
4098
4099TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) {
4100 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004101 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4102 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
4103 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004104 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08004105 windowInSecondary->assertNoEvents();
4106
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004107 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004108 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4109 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
4110 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08004111 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004112 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +08004113}
4114
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004115TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) {
Tiger Huang721e26f2018-07-24 22:26:19 +08004116 // Test inject a key down with display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08004117 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4118 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004119 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004120 windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Tiger Huang721e26f2018-07-24 22:26:19 +08004121 windowInSecondary->assertNoEvents();
4122
4123 // Test inject a key down without display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08004124 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004125 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08004126 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004127 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hungb92218b2018-08-14 12:00:21 +08004128
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08004129 // Remove all windows in secondary display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00004130 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}});
Arthur Hungb92218b2018-08-14 12:00:21 +08004131
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004132 // Old focus should receive a cancel event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004133 windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE,
4134 AKEY_EVENT_FLAG_CANCELED);
Arthur Hungb92218b2018-08-14 12:00:21 +08004135
4136 // Test inject a key down, should timeout because of no target window.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08004137 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004138 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Arthur Hungb92218b2018-08-14 12:00:21 +08004139 windowInPrimary->assertNoEvents();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004140 windowInSecondary->consumeFocusEvent(false);
Arthur Hungb92218b2018-08-14 12:00:21 +08004141 windowInSecondary->assertNoEvents();
4142}
4143
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004144// Test per-display input monitors for motion event.
4145TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) {
chaviwd1c23182019-12-20 18:44:56 -08004146 FakeMonitorReceiver monitorInPrimary =
4147 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
4148 FakeMonitorReceiver monitorInSecondary =
4149 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004150
4151 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004152 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4153 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
4154 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004155 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08004156 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004157 windowInSecondary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08004158 monitorInSecondary.assertNoEvents();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004159
4160 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004161 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4162 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
4163 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004164 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08004165 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004166 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
chaviwd1c23182019-12-20 18:44:56 -08004167 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004168
4169 // Test inject a non-pointer motion event.
4170 // If specific a display, it will dispatch to the focused window of particular display,
4171 // or it will dispatch to the focused window of focused display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004172 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4173 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE))
4174 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004175 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08004176 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004177 windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08004178 monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004179}
4180
4181// Test per-display input monitors for key event.
4182TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) {
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004183 // Input monitor per display.
chaviwd1c23182019-12-20 18:44:56 -08004184 FakeMonitorReceiver monitorInPrimary =
4185 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
4186 FakeMonitorReceiver monitorInSecondary =
4187 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004188
4189 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004190 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4191 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004192 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08004193 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004194 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08004195 monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004196}
4197
Vishnu Nair958da932020-08-21 17:12:37 -07004198TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) {
4199 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004200 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07004201 secondWindowInPrimary->setFocusable(true);
4202 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}});
4203 setFocusedWindow(secondWindowInPrimary);
4204 windowInPrimary->consumeFocusEvent(false);
4205 secondWindowInPrimary->consumeFocusEvent(true);
4206
4207 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004208 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
4209 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004210 windowInPrimary->assertNoEvents();
4211 windowInSecondary->assertNoEvents();
4212 secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4213}
4214
Arthur Hungdfd528e2021-12-08 13:23:04 +00004215TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CancelTouch_MultiDisplay) {
4216 FakeMonitorReceiver monitorInPrimary =
4217 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
4218 FakeMonitorReceiver monitorInSecondary =
4219 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
4220
4221 // Test touch down on primary display.
4222 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4223 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
4224 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4225 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4226 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
4227
4228 // Test touch down on second display.
4229 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4230 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
4231 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4232 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
4233 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
4234
4235 // Trigger cancel touch.
4236 mDispatcher->cancelCurrentTouch();
4237 windowInPrimary->consumeMotionCancel(ADISPLAY_ID_DEFAULT);
4238 monitorInPrimary.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
4239 windowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID);
4240 monitorInSecondary.consumeMotionCancel(SECOND_DISPLAY_ID);
4241
4242 // Test inject a move motion event, no window/monitor should receive the event.
4243 ASSERT_EQ(InputEventInjectionResult::FAILED,
4244 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4245 ADISPLAY_ID_DEFAULT, {110, 200}))
4246 << "Inject motion event should return InputEventInjectionResult::FAILED";
4247 windowInPrimary->assertNoEvents();
4248 monitorInPrimary.assertNoEvents();
4249
4250 ASSERT_EQ(InputEventInjectionResult::FAILED,
4251 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4252 SECOND_DISPLAY_ID, {110, 200}))
4253 << "Inject motion event should return InputEventInjectionResult::FAILED";
4254 windowInSecondary->assertNoEvents();
4255 monitorInSecondary.assertNoEvents();
4256}
4257
Jackal Guof9696682018-10-05 12:23:23 +08004258class InputFilterTest : public InputDispatcherTest {
4259protected:
Prabir Pradhan81420cc2021-09-06 10:28:50 -07004260 void testNotifyMotion(int32_t displayId, bool expectToBeFiltered,
4261 const ui::Transform& transform = ui::Transform()) {
Jackal Guof9696682018-10-05 12:23:23 +08004262 NotifyMotionArgs motionArgs;
4263
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004264 motionArgs =
4265 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Jackal Guof9696682018-10-05 12:23:23 +08004266 mDispatcher->notifyMotion(&motionArgs);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004267 motionArgs =
4268 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Jackal Guof9696682018-10-05 12:23:23 +08004269 mDispatcher->notifyMotion(&motionArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004270 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08004271 if (expectToBeFiltered) {
Prabir Pradhan81420cc2021-09-06 10:28:50 -07004272 const auto xy = transform.transform(motionArgs.pointerCoords->getXYValue());
4273 mFakePolicy->assertFilterInputEventWasCalled(motionArgs, xy);
Jackal Guof9696682018-10-05 12:23:23 +08004274 } else {
4275 mFakePolicy->assertFilterInputEventWasNotCalled();
4276 }
4277 }
4278
4279 void testNotifyKey(bool expectToBeFiltered) {
4280 NotifyKeyArgs keyArgs;
4281
4282 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
4283 mDispatcher->notifyKey(&keyArgs);
4284 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP);
4285 mDispatcher->notifyKey(&keyArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004286 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08004287
4288 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08004289 mFakePolicy->assertFilterInputEventWasCalled(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08004290 } else {
4291 mFakePolicy->assertFilterInputEventWasNotCalled();
4292 }
4293 }
4294};
4295
4296// Test InputFilter for MotionEvent
4297TEST_F(InputFilterTest, MotionEvent_InputFilter) {
4298 // Since the InputFilter is disabled by default, check if touch events aren't filtered.
4299 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
4300 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
4301
4302 // Enable InputFilter
4303 mDispatcher->setInputFilterEnabled(true);
4304 // Test touch on both primary and second display, and check if both events are filtered.
4305 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true);
4306 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true);
4307
4308 // Disable InputFilter
4309 mDispatcher->setInputFilterEnabled(false);
4310 // Test touch on both primary and second display, and check if both events aren't filtered.
4311 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
4312 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
4313}
4314
4315// Test InputFilter for KeyEvent
4316TEST_F(InputFilterTest, KeyEvent_InputFilter) {
4317 // Since the InputFilter is disabled by default, check if key event aren't filtered.
4318 testNotifyKey(/*expectToBeFiltered*/ false);
4319
4320 // Enable InputFilter
4321 mDispatcher->setInputFilterEnabled(true);
4322 // Send a key event, and check if it is filtered.
4323 testNotifyKey(/*expectToBeFiltered*/ true);
4324
4325 // Disable InputFilter
4326 mDispatcher->setInputFilterEnabled(false);
4327 // Send a key event, and check if it isn't filtered.
4328 testNotifyKey(/*expectToBeFiltered*/ false);
4329}
4330
Prabir Pradhan81420cc2021-09-06 10:28:50 -07004331// Ensure that MotionEvents sent to the InputFilter through InputListener are converted to the
4332// logical display coordinate space.
4333TEST_F(InputFilterTest, MotionEvent_UsesLogicalDisplayCoordinates_notifyMotion) {
4334 ui::Transform firstDisplayTransform;
4335 firstDisplayTransform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
4336 ui::Transform secondDisplayTransform;
4337 secondDisplayTransform.set({-6.6, -5.5, -4.4, -3.3, -2.2, -1.1, 0, 0, 1});
4338
4339 std::vector<gui::DisplayInfo> displayInfos(2);
4340 displayInfos[0].displayId = ADISPLAY_ID_DEFAULT;
4341 displayInfos[0].transform = firstDisplayTransform;
4342 displayInfos[1].displayId = SECOND_DISPLAY_ID;
4343 displayInfos[1].transform = secondDisplayTransform;
4344
4345 mDispatcher->onWindowInfosChanged({}, displayInfos);
4346
4347 // Enable InputFilter
4348 mDispatcher->setInputFilterEnabled(true);
4349
4350 // Ensure the correct transforms are used for the displays.
4351 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true, firstDisplayTransform);
4352 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true, secondDisplayTransform);
4353}
4354
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004355class InputFilterInjectionPolicyTest : public InputDispatcherTest {
4356protected:
4357 virtual void SetUp() override {
4358 InputDispatcherTest::SetUp();
4359
4360 /**
4361 * We don't need to enable input filter to test the injected event policy, but we enabled it
4362 * here to make the tests more realistic, since this policy only matters when inputfilter is
4363 * on.
4364 */
4365 mDispatcher->setInputFilterEnabled(true);
4366
4367 std::shared_ptr<InputApplicationHandle> application =
4368 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004369 mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Test Window",
4370 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004371
4372 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
4373 mWindow->setFocusable(true);
4374 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
4375 setFocusedWindow(mWindow);
4376 mWindow->consumeFocusEvent(true);
4377 }
4378
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00004379 void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
4380 int32_t flags) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004381 KeyEvent event;
4382
4383 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
4384 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD,
4385 ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A,
4386 KEY_A, AMETA_NONE, 0 /*repeatCount*/, eventTime, eventTime);
4387 const int32_t additionalPolicyFlags =
4388 POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT;
4389 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Prabir Pradhan5735a322022-04-11 17:23:34 +00004390 mDispatcher->injectInputEvent(&event, {} /*targetUid*/,
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004391 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
4392 policyFlags | additionalPolicyFlags));
4393
4394 InputEvent* received = mWindow->consume();
4395 ASSERT_NE(nullptr, received);
4396 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00004397 ASSERT_EQ(received->getType(), AINPUT_EVENT_TYPE_KEY);
4398 KeyEvent& keyEvent = static_cast<KeyEvent&>(*received);
4399 ASSERT_EQ(flags, keyEvent.getFlags());
4400 }
4401
4402 void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
4403 int32_t flags) {
4404 MotionEvent event;
4405 PointerProperties pointerProperties[1];
4406 PointerCoords pointerCoords[1];
4407 pointerProperties[0].clear();
4408 pointerProperties[0].id = 0;
4409 pointerCoords[0].clear();
4410 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300);
4411 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400);
4412
4413 ui::Transform identityTransform;
4414 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
4415 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN,
4416 DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0,
4417 AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE,
4418 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -07004419 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, eventTime,
Evan Rosky09576692021-07-01 12:22:09 -07004420 eventTime,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00004421 /*pointerCount*/ 1, pointerProperties, pointerCoords);
4422
4423 const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER;
4424 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Prabir Pradhan5735a322022-04-11 17:23:34 +00004425 mDispatcher->injectInputEvent(&event, {} /*targetUid*/,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00004426 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
4427 policyFlags | additionalPolicyFlags));
4428
4429 InputEvent* received = mWindow->consume();
4430 ASSERT_NE(nullptr, received);
4431 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
4432 ASSERT_EQ(received->getType(), AINPUT_EVENT_TYPE_MOTION);
4433 MotionEvent& motionEvent = static_cast<MotionEvent&>(*received);
4434 ASSERT_EQ(flags, motionEvent.getFlags());
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004435 }
4436
4437private:
4438 sp<FakeWindowHandle> mWindow;
4439};
4440
4441TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) {
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00004442 // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input
4443 // filter. Without it, the event will no different from a regularly injected event, and the
4444 // injected device id will be overwritten.
4445 testInjectedKey(POLICY_FLAG_FILTERED, 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/,
4446 0 /*flags*/);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004447}
4448
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00004449TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004450 testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00004451 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/,
4452 AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
4453}
4454
4455TEST_F(InputFilterInjectionPolicyTest,
4456 MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
4457 testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
4458 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/,
4459 AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004460}
4461
4462TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) {
4463 testInjectedKey(0 /*policyFlags*/, 3 /*injectedDeviceId*/,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00004464 VIRTUAL_KEYBOARD_ID /*resolvedDeviceId*/, 0 /*flags*/);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004465}
4466
chaviwfd6d3512019-03-25 13:23:49 -07004467class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest {
Prabir Pradhan3608aad2019-10-02 17:08:26 -07004468 virtual void SetUp() override {
chaviwfd6d3512019-03-25 13:23:49 -07004469 InputDispatcherTest::SetUp();
4470
Chris Yea209fde2020-07-22 13:54:51 -07004471 std::shared_ptr<FakeApplicationHandle> application =
4472 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004473 mUnfocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004474 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07004475 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
chaviwfd6d3512019-03-25 13:23:49 -07004476
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08004477 mFocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004478 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08004479 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
chaviwfd6d3512019-03-25 13:23:49 -07004480
4481 // Set focused application.
4482 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07004483 mFocusedWindow->setFocusable(true);
chaviwfd6d3512019-03-25 13:23:49 -07004484
4485 // Expect one focus window exist in display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00004486 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004487 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004488 mFocusedWindow->consumeFocusEvent(true);
chaviwfd6d3512019-03-25 13:23:49 -07004489 }
4490
Prabir Pradhan3608aad2019-10-02 17:08:26 -07004491 virtual void TearDown() override {
chaviwfd6d3512019-03-25 13:23:49 -07004492 InputDispatcherTest::TearDown();
4493
4494 mUnfocusedWindow.clear();
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08004495 mFocusedWindow.clear();
chaviwfd6d3512019-03-25 13:23:49 -07004496 }
4497
4498protected:
4499 sp<FakeWindowHandle> mUnfocusedWindow;
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08004500 sp<FakeWindowHandle> mFocusedWindow;
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07004501 static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60};
chaviwfd6d3512019-03-25 13:23:49 -07004502};
4503
4504// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
4505// DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received
4506// the onPointerDownOutsideFocus callback.
4507TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004508 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07004509 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4510 {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004511 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07004512 mUnfocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07004513
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004514 ASSERT_TRUE(mDispatcher->waitForIdle());
chaviwfd6d3512019-03-25 13:23:49 -07004515 mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken());
4516}
4517
4518// Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action
4519// DOWN on the window that doesn't have focus. Ensure no window received the
4520// onPointerDownOutsideFocus callback.
4521TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004522 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07004523 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004524 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07004525 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07004526
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004527 ASSERT_TRUE(mDispatcher->waitForIdle());
4528 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07004529}
4530
4531// Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't
4532// have focus. Ensure no window received the onPointerDownOutsideFocus callback.
4533TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08004534 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4535 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004536 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07004537 mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07004538
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004539 ASSERT_TRUE(mDispatcher->waitForIdle());
4540 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07004541}
4542
4543// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
4544// DOWN on the window that already has focus. Ensure no window received the
4545// onPointerDownOutsideFocus callback.
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004546TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004547 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08004548 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07004549 FOCUSED_WINDOW_TOUCH_POINT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004550 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07004551 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07004552
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004553 ASSERT_TRUE(mDispatcher->waitForIdle());
4554 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07004555}
4556
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08004557// Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag
4558// NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback.
4559TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) {
4560 const MotionEvent event =
4561 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
4562 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
4563 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(20).y(20))
4564 .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE)
4565 .build();
4566 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(mDispatcher, event))
4567 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4568 mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
4569
4570 ASSERT_TRUE(mDispatcher->waitForIdle());
4571 mFakePolicy->assertOnPointerDownWasNotCalled();
4572 // Ensure that the unfocused window did not receive any FOCUS events.
4573 mUnfocusedWindow->assertNoEvents();
4574}
4575
chaviwaf87b3e2019-10-01 16:59:28 -07004576// These tests ensures we can send touch events to a single client when there are multiple input
4577// windows that point to the same client token.
4578class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest {
4579 virtual void SetUp() override {
4580 InputDispatcherTest::SetUp();
4581
Chris Yea209fde2020-07-22 13:54:51 -07004582 std::shared_ptr<FakeApplicationHandle> application =
4583 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004584 mWindow1 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 1",
4585 ADISPLAY_ID_DEFAULT);
chaviwaf87b3e2019-10-01 16:59:28 -07004586 mWindow1->setFrame(Rect(0, 0, 100, 100));
4587
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004588 mWindow2 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 2",
4589 ADISPLAY_ID_DEFAULT, mWindow1->getToken());
chaviwaf87b3e2019-10-01 16:59:28 -07004590 mWindow2->setFrame(Rect(100, 100, 200, 200));
4591
Arthur Hung72d8dc32020-03-28 00:48:39 +00004592 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
chaviwaf87b3e2019-10-01 16:59:28 -07004593 }
4594
4595protected:
4596 sp<FakeWindowHandle> mWindow1;
4597 sp<FakeWindowHandle> mWindow2;
4598
4599 // Helper function to convert the point from screen coordinates into the window's space
chaviw3277faf2021-05-19 16:45:23 -05004600 static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) {
chaviw1ff3d1e2020-07-01 15:53:47 -07004601 vec2 vals = windowInfo->transform.transform(point.x, point.y);
4602 return {vals.x, vals.y};
chaviwaf87b3e2019-10-01 16:59:28 -07004603 }
4604
4605 void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction,
4606 const std::vector<PointF>& points) {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004607 const std::string name = window->getName();
chaviwaf87b3e2019-10-01 16:59:28 -07004608 InputEvent* event = window->consume();
4609
4610 ASSERT_NE(nullptr, event) << name.c_str()
4611 << ": consumer should have returned non-NULL event.";
4612
4613 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType())
4614 << name.c_str() << "expected " << inputEventTypeToString(AINPUT_EVENT_TYPE_MOTION)
4615 << " event, got " << inputEventTypeToString(event->getType()) << " event";
4616
4617 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004618 assertMotionAction(expectedAction, motionEvent.getAction());
chaviwaf87b3e2019-10-01 16:59:28 -07004619
4620 for (size_t i = 0; i < points.size(); i++) {
4621 float expectedX = points[i].x;
4622 float expectedY = points[i].y;
4623
4624 EXPECT_EQ(expectedX, motionEvent.getX(i))
4625 << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str()
4626 << ", got " << motionEvent.getX(i);
4627 EXPECT_EQ(expectedY, motionEvent.getY(i))
4628 << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str()
4629 << ", got " << motionEvent.getY(i);
4630 }
4631 }
chaviw9eaa22c2020-07-01 16:21:27 -07004632
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08004633 void touchAndAssertPositions(int32_t action, const std::vector<PointF>& touchedPoints,
chaviw9eaa22c2020-07-01 16:21:27 -07004634 std::vector<PointF> expectedPoints) {
4635 NotifyMotionArgs motionArgs = generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN,
4636 ADISPLAY_ID_DEFAULT, touchedPoints);
4637 mDispatcher->notifyMotion(&motionArgs);
4638
4639 // Always consume from window1 since it's the window that has the InputReceiver
4640 consumeMotionEvent(mWindow1, action, expectedPoints);
4641 }
chaviwaf87b3e2019-10-01 16:59:28 -07004642};
4643
4644TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) {
4645 // Touch Window 1
4646 PointF touchedPoint = {10, 10};
4647 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07004648 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07004649
4650 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07004651 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07004652
4653 // Touch Window 2
4654 touchedPoint = {150, 150};
4655 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07004656 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07004657}
4658
chaviw9eaa22c2020-07-01 16:21:27 -07004659TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) {
4660 // Set scale value for window2
chaviwaf87b3e2019-10-01 16:59:28 -07004661 mWindow2->setWindowScale(0.5f, 0.5f);
4662
4663 // Touch Window 1
4664 PointF touchedPoint = {10, 10};
4665 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07004666 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07004667 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07004668 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07004669
4670 // Touch Window 2
4671 touchedPoint = {150, 150};
4672 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07004673 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
4674 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07004675
chaviw9eaa22c2020-07-01 16:21:27 -07004676 // Update the transform so rotation is set
4677 mWindow2->setWindowTransform(0, -1, 1, 0);
4678 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
4679 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07004680}
4681
chaviw9eaa22c2020-07-01 16:21:27 -07004682TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004683 mWindow2->setWindowScale(0.5f, 0.5f);
4684
4685 // Touch Window 1
4686 std::vector<PointF> touchedPoints = {PointF{10, 10}};
4687 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07004688 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004689
4690 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07004691 touchedPoints.push_back(PointF{150, 150});
4692 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08004693 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004694
chaviw9eaa22c2020-07-01 16:21:27 -07004695 // Release Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08004696 touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07004697 expectedPoints.pop_back();
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004698
chaviw9eaa22c2020-07-01 16:21:27 -07004699 // Update the transform so rotation is set for Window 2
4700 mWindow2->setWindowTransform(0, -1, 1, 0);
4701 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08004702 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004703}
4704
chaviw9eaa22c2020-07-01 16:21:27 -07004705TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004706 mWindow2->setWindowScale(0.5f, 0.5f);
4707
4708 // Touch Window 1
4709 std::vector<PointF> touchedPoints = {PointF{10, 10}};
4710 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07004711 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004712
4713 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07004714 touchedPoints.push_back(PointF{150, 150});
4715 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004716
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08004717 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004718
4719 // Move both windows
4720 touchedPoints = {{20, 20}, {175, 175}};
4721 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
4722 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
4723
chaviw9eaa22c2020-07-01 16:21:27 -07004724 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004725
chaviw9eaa22c2020-07-01 16:21:27 -07004726 // Release Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08004727 touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07004728 expectedPoints.pop_back();
4729
4730 // Touch Window 2
4731 mWindow2->setWindowTransform(0, -1, 1, 0);
4732 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08004733 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07004734
4735 // Move both windows
4736 touchedPoints = {{20, 20}, {175, 175}};
4737 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
4738 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
4739
4740 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004741}
4742
4743TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) {
4744 mWindow1->setWindowScale(0.5f, 0.5f);
4745
4746 // Touch Window 1
4747 std::vector<PointF> touchedPoints = {PointF{10, 10}};
4748 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07004749 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004750
4751 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07004752 touchedPoints.push_back(PointF{150, 150});
4753 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004754
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08004755 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004756
4757 // Move both windows
4758 touchedPoints = {{20, 20}, {175, 175}};
4759 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
4760 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
4761
chaviw9eaa22c2020-07-01 16:21:27 -07004762 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004763}
4764
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004765class InputDispatcherSingleWindowAnr : public InputDispatcherTest {
4766 virtual void SetUp() override {
4767 InputDispatcherTest::SetUp();
4768
Chris Yea209fde2020-07-22 13:54:51 -07004769 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004770 mApplication->setDispatchingTimeout(20ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004771 mWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "TestWindow",
4772 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004773 mWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05004774 mWindow->setDispatchingTimeout(30ms);
Vishnu Nair47074b82020-08-14 11:54:47 -07004775 mWindow->setFocusable(true);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004776
4777 // Set focused application.
4778 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
4779
4780 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004781 setFocusedWindow(mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004782 mWindow->consumeFocusEvent(true);
4783 }
4784
4785 virtual void TearDown() override {
4786 InputDispatcherTest::TearDown();
4787 mWindow.clear();
4788 }
4789
4790protected:
Chris Yea209fde2020-07-22 13:54:51 -07004791 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004792 sp<FakeWindowHandle> mWindow;
4793 static constexpr PointF WINDOW_LOCATION = {20, 20};
4794
4795 void tapOnWindow() {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004796 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004797 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4798 WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004799 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004800 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4801 WINDOW_LOCATION));
4802 }
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004803
4804 sp<FakeWindowHandle> addSpyWindow() {
4805 sp<FakeWindowHandle> spy =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004806 sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004807 spy->setTrustedOverlay(true);
4808 spy->setFocusable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08004809 spy->setSpy(true);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004810 spy->setDispatchingTimeout(30ms);
4811 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, mWindow}}});
4812 return spy;
4813 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004814};
4815
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004816// Send a tap and respond, which should not cause an ANR.
4817TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) {
4818 tapOnWindow();
4819 mWindow->consumeMotionDown();
4820 mWindow->consumeMotionUp();
4821 ASSERT_TRUE(mDispatcher->waitForIdle());
4822 mFakePolicy->assertNotifyAnrWasNotCalled();
4823}
4824
4825// Send a regular key and respond, which should not cause an ANR.
4826TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08004827 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004828 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
4829 ASSERT_TRUE(mDispatcher->waitForIdle());
4830 mFakePolicy->assertNotifyAnrWasNotCalled();
4831}
4832
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05004833TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) {
4834 mWindow->setFocusable(false);
4835 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
4836 mWindow->consumeFocusEvent(false);
4837
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004838 InputEventInjectionResult result =
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05004839 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08004840 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/,
4841 false /* allowKeyRepeat */);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004842 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05004843 // Key will not go to window because we have no focused window.
4844 // The 'no focused window' ANR timer should start instead.
4845
4846 // Now, the focused application goes away.
4847 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr);
4848 // The key should get dropped and there should be no ANR.
4849
4850 ASSERT_TRUE(mDispatcher->waitForIdle());
4851 mFakePolicy->assertNotifyAnrWasNotCalled();
4852}
4853
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004854// Send an event to the app and have the app not respond right away.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004855// When ANR is raised, policy will tell the dispatcher to cancel the events for that window.
4856// So InputDispatcher will enqueue ACTION_CANCEL event as well.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004857TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004858 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004859 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4860 WINDOW_LOCATION));
4861
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004862 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN
4863 ASSERT_TRUE(sequenceNum);
4864 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08004865 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004866
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004867 mWindow->finishEvent(*sequenceNum);
4868 mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL,
4869 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004870 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08004871 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004872}
4873
4874// Send a key to the app and have the app not respond right away.
4875TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) {
4876 // Inject a key, and don't respond - expect that ANR is called.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08004877 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004878 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent();
4879 ASSERT_TRUE(sequenceNum);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004880 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08004881 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004882 ASSERT_TRUE(mDispatcher->waitForIdle());
4883}
4884
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004885// We have a focused application, but no focused window
4886TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) {
Vishnu Nair47074b82020-08-14 11:54:47 -07004887 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004888 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
4889 mWindow->consumeFocusEvent(false);
4890
4891 // taps on the window work as normal
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004892 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004893 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4894 WINDOW_LOCATION));
4895 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
4896 mDispatcher->waitForIdle();
4897 mFakePolicy->assertNotifyAnrWasNotCalled();
4898
4899 // Once a focused event arrives, we get an ANR for this application
4900 // We specify the injection timeout to be smaller than the application timeout, to ensure that
4901 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004902 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004903 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08004904 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, false /* allowKeyRepeat */);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004905 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004906 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Vishnu Naire4df8752022-09-08 09:17:55 -07004907 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004908 ASSERT_TRUE(mDispatcher->waitForIdle());
4909}
4910
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08004911/**
4912 * Make sure the stale key is dropped before causing an ANR. So even if there's no focused window,
4913 * there will not be an ANR.
4914 */
4915TEST_F(InputDispatcherSingleWindowAnr, StaleKeyEventDoesNotAnr) {
4916 mWindow->setFocusable(false);
4917 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
4918 mWindow->consumeFocusEvent(false);
4919
4920 KeyEvent event;
4921 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC) -
4922 std::chrono::nanoseconds(STALE_EVENT_TIMEOUT).count();
4923
4924 // Define a valid key down event that is stale (too old).
4925 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
4926 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, /* flags */ 0, AKEYCODE_A, KEY_A,
4927 AMETA_NONE, 1 /*repeatCount*/, eventTime, eventTime);
4928
4929 const int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER;
4930
4931 InputEventInjectionResult result =
Prabir Pradhan5735a322022-04-11 17:23:34 +00004932 mDispatcher->injectInputEvent(&event, {} /* targetUid */,
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08004933 InputEventInjectionSync::WAIT_FOR_RESULT,
4934 INJECT_EVENT_TIMEOUT, policyFlags);
4935 ASSERT_EQ(InputEventInjectionResult::FAILED, result)
4936 << "Injection should fail because the event is stale";
4937
4938 ASSERT_TRUE(mDispatcher->waitForIdle());
4939 mFakePolicy->assertNotifyAnrWasNotCalled();
4940 mWindow->assertNoEvents();
4941}
4942
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004943// We have a focused application, but no focused window
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004944// Make sure that we don't notify policy twice about the same ANR.
4945TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) {
Vishnu Nair47074b82020-08-14 11:54:47 -07004946 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004947 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
4948 mWindow->consumeFocusEvent(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004949
4950 // Once a focused event arrives, we get an ANR for this application
4951 // We specify the injection timeout to be smaller than the application timeout, to ensure that
4952 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004953 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004954 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08004955 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, false /* allowKeyRepeat */);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004956 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Vishnu Naire4df8752022-09-08 09:17:55 -07004957 const std::chrono::duration appTimeout =
4958 mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
4959 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004960
Vishnu Naire4df8752022-09-08 09:17:55 -07004961 std::this_thread::sleep_for(appTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004962 // ANR should not be raised again. It is up to policy to do that if it desires.
4963 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004964
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004965 // If we now get a focused window, the ANR should stop, but the policy handles that via
4966 // 'notifyFocusChanged' callback. This is implemented in the policy so we can't test it here.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004967 ASSERT_TRUE(mDispatcher->waitForIdle());
4968}
4969
4970// We have a focused application, but no focused window
4971TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) {
Vishnu Nair47074b82020-08-14 11:54:47 -07004972 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004973 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
4974 mWindow->consumeFocusEvent(false);
4975
4976 // Once a focused event arrives, we get an ANR for this application
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004977 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004978 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004979 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
4980 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004981
Vishnu Naire4df8752022-09-08 09:17:55 -07004982 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
4983 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004984
4985 // Future focused events get dropped right away
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004986 ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004987 ASSERT_TRUE(mDispatcher->waitForIdle());
4988 mWindow->assertNoEvents();
4989}
4990
4991/**
4992 * Ensure that the implementation is valid. Since we are using multiset to keep track of the
4993 * ANR timeouts, we are allowing entries with identical timestamps in the same connection.
4994 * If we process 1 of the events, but ANR on the second event with the same timestamp,
4995 * the ANR mechanism should still work.
4996 *
4997 * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the
4998 * DOWN event, while not responding on the second one.
4999 */
5000TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) {
5001 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
5002 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5003 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
5004 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
5005 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005006 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005007
5008 // Now send ACTION_UP, with identical timestamp
5009 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
5010 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
5011 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
5012 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005013 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005014
5015 // We have now sent down and up. Let's consume first event and then ANR on the second.
5016 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5017 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005018 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005019}
5020
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005021// A spy window can receive an ANR
5022TEST_F(InputDispatcherSingleWindowAnr, SpyWindowAnr) {
5023 sp<FakeWindowHandle> spy = addSpyWindow();
5024
5025 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5026 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5027 WINDOW_LOCATION));
5028 mWindow->consumeMotionDown();
5029
5030 std::optional<uint32_t> sequenceNum = spy->receiveEvent(); // ACTION_DOWN
5031 ASSERT_TRUE(sequenceNum);
5032 const std::chrono::duration timeout = spy->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005033 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, spy);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005034
5035 spy->finishEvent(*sequenceNum);
5036 spy->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, ADISPLAY_ID_DEFAULT,
5037 0 /*flags*/);
5038 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08005039 mFakePolicy->assertNotifyWindowResponsiveWasCalled(spy->getToken(), mWindow->getPid());
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005040}
5041
5042// If an app is not responding to a key event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005043// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005044TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnKey) {
5045 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005046
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005047 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5048 injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005049 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005050 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005051
5052 // Stuck on the ACTION_UP
5053 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005054 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005055
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005056 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005057 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005058 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5059 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005060
5061 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion
5062 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08005063 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005064 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005065 spy->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005066}
5067
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005068// If an app is not responding to a motion event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005069// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005070TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnMotion) {
5071 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005072
5073 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005074 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5075 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005076
5077 mWindow->consumeMotionDown();
5078 // Stuck on the ACTION_UP
5079 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005080 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005081
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005082 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005083 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005084 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5085 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005086
5087 mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion
5088 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08005089 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005090 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005091 spy->assertNoEvents();
5092}
5093
5094TEST_F(InputDispatcherSingleWindowAnr, UnresponsiveMonitorAnr) {
5095 mDispatcher->setMonitorDispatchingTimeoutForTest(30ms);
5096
5097 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
5098
5099 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5100 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5101 WINDOW_LOCATION));
5102
5103 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5104 const std::optional<uint32_t> consumeSeq = monitor.receiveEvent();
5105 ASSERT_TRUE(consumeSeq);
5106
Prabir Pradhanedd96402022-02-15 01:46:16 -08005107 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(30ms, monitor.getToken(), MONITOR_PID);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005108
5109 monitor.finishEvent(*consumeSeq);
5110 monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
5111
5112 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08005113 mFakePolicy->assertNotifyWindowResponsiveWasCalled(monitor.getToken(), MONITOR_PID);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005114}
5115
5116// If a window is unresponsive, then you get anr. if the window later catches up and starts to
5117// process events, you don't get an anr. When the window later becomes unresponsive again, you
5118// get an ANR again.
5119// 1. tap -> block on ACTION_UP -> receive ANR
5120// 2. consume all pending events (= queue becomes healthy again)
5121// 3. tap again -> block on ACTION_UP again -> receive ANR second time
5122TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) {
5123 tapOnWindow();
5124
5125 mWindow->consumeMotionDown();
5126 // Block on ACTION_UP
5127 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005128 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005129 mWindow->consumeMotionUp(); // Now the connection should be healthy again
5130 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08005131 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005132 mWindow->assertNoEvents();
5133
5134 tapOnWindow();
5135 mWindow->consumeMotionDown();
Prabir Pradhanedd96402022-02-15 01:46:16 -08005136 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005137 mWindow->consumeMotionUp();
5138
5139 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08005140 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005141 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005142 mWindow->assertNoEvents();
5143}
5144
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005145// If a connection remains unresponsive for a while, make sure policy is only notified once about
5146// it.
5147TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005148 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005149 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5150 WINDOW_LOCATION));
5151
5152 const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005153 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005154 std::this_thread::sleep_for(windowTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005155 // 'notifyConnectionUnresponsive' should only be called once per connection
5156 mFakePolicy->assertNotifyAnrWasNotCalled();
5157 // When the ANR happened, dispatcher should abort the current event stream via ACTION_CANCEL
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005158 mWindow->consumeMotionDown();
5159 mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL,
5160 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
5161 mWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005162 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08005163 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005164 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005165}
5166
5167/**
5168 * If a window is processing a motion event, and then a key event comes in, the key event should
5169 * not to to the focused window until the motion is processed.
5170 *
5171 * Warning!!!
5172 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
5173 * and the injection timeout that we specify when injecting the key.
5174 * We must have the injection timeout (10ms) be smaller than
5175 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
5176 *
5177 * If that value changes, this test should also change.
5178 */
5179TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) {
5180 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
5181 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
5182
5183 tapOnWindow();
5184 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
5185 ASSERT_TRUE(downSequenceNum);
5186 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
5187 ASSERT_TRUE(upSequenceNum);
5188 // Don't finish the events yet, and send a key
5189 // Injection will "succeed" because we will eventually give up and send the key to the focused
5190 // window even if motions are still being processed. But because the injection timeout is short,
5191 // we will receive INJECTION_TIMED_OUT as the result.
5192
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005193 InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005194 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005195 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
5196 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005197 // Key will not be sent to the window, yet, because the window is still processing events
5198 // and the key remains pending, waiting for the touch events to be processed
5199 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
5200 ASSERT_FALSE(keySequenceNum);
5201
5202 std::this_thread::sleep_for(500ms);
5203 // if we wait long enough though, dispatcher will give up, and still send the key
5204 // to the focused window, even though we have not yet finished the motion event
5205 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5206 mWindow->finishEvent(*downSequenceNum);
5207 mWindow->finishEvent(*upSequenceNum);
5208}
5209
5210/**
5211 * If a window is processing a motion event, and then a key event comes in, the key event should
5212 * not go to the focused window until the motion is processed.
5213 * If then a new motion comes in, then the pending key event should be going to the currently
5214 * focused window right away.
5215 */
5216TEST_F(InputDispatcherSingleWindowAnr,
5217 PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) {
5218 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
5219 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
5220
5221 tapOnWindow();
5222 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
5223 ASSERT_TRUE(downSequenceNum);
5224 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
5225 ASSERT_TRUE(upSequenceNum);
5226 // Don't finish the events yet, and send a key
5227 // Injection is async, so it will succeed
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005228 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005229 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005230 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005231 // At this point, key is still pending, and should not be sent to the application yet.
5232 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
5233 ASSERT_FALSE(keySequenceNum);
5234
5235 // Now tap down again. It should cause the pending key to go to the focused window right away.
5236 tapOnWindow();
5237 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd
5238 // the other events yet. We can finish events in any order.
5239 mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN
5240 mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP
5241 mWindow->consumeMotionDown();
5242 mWindow->consumeMotionUp();
5243 mWindow->assertNoEvents();
5244}
5245
5246class InputDispatcherMultiWindowAnr : public InputDispatcherTest {
5247 virtual void SetUp() override {
5248 InputDispatcherTest::SetUp();
5249
Chris Yea209fde2020-07-22 13:54:51 -07005250 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005251 mApplication->setDispatchingTimeout(10ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005252 mUnfocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Unfocused",
5253 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005254 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005255 // Adding FLAG_WATCH_OUTSIDE_TOUCH to receive ACTION_OUTSIDE when another window is tapped
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08005256 mUnfocusedWindow->setWatchOutsideTouch(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005257
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005258 mFocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Focused",
5259 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05005260 mFocusedWindow->setDispatchingTimeout(30ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005261 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005262
5263 // Set focused application.
5264 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
Vishnu Nair47074b82020-08-14 11:54:47 -07005265 mFocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005266
5267 // Expect one focus window exist in display.
5268 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005269 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005270 mFocusedWindow->consumeFocusEvent(true);
5271 }
5272
5273 virtual void TearDown() override {
5274 InputDispatcherTest::TearDown();
5275
5276 mUnfocusedWindow.clear();
5277 mFocusedWindow.clear();
5278 }
5279
5280protected:
Chris Yea209fde2020-07-22 13:54:51 -07005281 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005282 sp<FakeWindowHandle> mUnfocusedWindow;
5283 sp<FakeWindowHandle> mFocusedWindow;
5284 static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20};
5285 static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75};
5286 static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40};
5287
5288 void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); }
5289
5290 void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); }
5291
5292private:
5293 void tap(const PointF& location) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005294 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005295 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5296 location));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005297 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005298 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5299 location));
5300 }
5301};
5302
5303// If we have 2 windows that are both unresponsive, the one with the shortest timeout
5304// should be ANR'd first.
5305TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005306 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005307 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5308 FOCUSED_WINDOW_LOCATION))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005309 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005310 mFocusedWindow->consumeMotionDown();
5311 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
5312 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
5313 // We consumed all events, so no ANR
5314 ASSERT_TRUE(mDispatcher->waitForIdle());
5315 mFakePolicy->assertNotifyAnrWasNotCalled();
5316
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005317 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005318 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5319 FOCUSED_WINDOW_LOCATION));
5320 std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent();
5321 ASSERT_TRUE(unfocusedSequenceNum);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005322
5323 const std::chrono::duration timeout =
5324 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005325 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005326 // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event
5327 // sequence to make it consistent
5328 mFocusedWindow->consumeMotionCancel();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005329 mUnfocusedWindow->finishEvent(*unfocusedSequenceNum);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005330 mFocusedWindow->consumeMotionDown();
5331 // This cancel is generated because the connection was unresponsive
5332 mFocusedWindow->consumeMotionCancel();
5333 mFocusedWindow->assertNoEvents();
5334 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005335 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08005336 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
5337 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005338 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005339}
5340
5341// If we have 2 windows with identical timeouts that are both unresponsive,
5342// it doesn't matter which order they should have ANR.
5343// But we should receive ANR for both.
5344TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) {
5345 // Set the timeout for unfocused window to match the focused window
5346 mUnfocusedWindow->setDispatchingTimeout(10ms);
5347 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
5348
5349 tapOnFocusedWindow();
5350 // we should have ACTION_DOWN/ACTION_UP on focused window and ACTION_OUTSIDE on unfocused window
Prabir Pradhanedd96402022-02-15 01:46:16 -08005351 sp<IBinder> anrConnectionToken1, anrConnectionToken2;
5352 ASSERT_NO_FATAL_FAILURE(anrConnectionToken1 = mFakePolicy->getUnresponsiveWindowToken(10ms));
5353 ASSERT_NO_FATAL_FAILURE(anrConnectionToken2 = mFakePolicy->getUnresponsiveWindowToken(0ms));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005354
5355 // We don't know which window will ANR first. But both of them should happen eventually.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005356 ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 ||
5357 mFocusedWindow->getToken() == anrConnectionToken2);
5358 ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 ||
5359 mUnfocusedWindow->getToken() == anrConnectionToken2);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005360
5361 ASSERT_TRUE(mDispatcher->waitForIdle());
5362 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005363
5364 mFocusedWindow->consumeMotionDown();
5365 mFocusedWindow->consumeMotionUp();
5366 mUnfocusedWindow->consumeMotionOutside();
5367
Prabir Pradhanedd96402022-02-15 01:46:16 -08005368 sp<IBinder> responsiveToken1, responsiveToken2;
5369 ASSERT_NO_FATAL_FAILURE(responsiveToken1 = mFakePolicy->getResponsiveWindowToken());
5370 ASSERT_NO_FATAL_FAILURE(responsiveToken2 = mFakePolicy->getResponsiveWindowToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005371
5372 // Both applications should be marked as responsive, in any order
5373 ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 ||
5374 mFocusedWindow->getToken() == responsiveToken2);
5375 ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 ||
5376 mUnfocusedWindow->getToken() == responsiveToken2);
5377 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005378}
5379
5380// If a window is already not responding, the second tap on the same window should be ignored.
5381// We should also log an error to account for the dropped event (not tested here).
5382// At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events.
5383TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) {
5384 tapOnFocusedWindow();
5385 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
5386 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
5387 // Receive the events, but don't respond
5388 std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN
5389 ASSERT_TRUE(downEventSequenceNum);
5390 std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP
5391 ASSERT_TRUE(upEventSequenceNum);
5392 const std::chrono::duration timeout =
5393 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005394 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005395
5396 // Tap once again
5397 // We cannot use "tapOnFocusedWindow" because it asserts the injection result to be success
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005398 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005399 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5400 FOCUSED_WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005401 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005402 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5403 FOCUSED_WINDOW_LOCATION));
5404 // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a
5405 // valid touch target
5406 mUnfocusedWindow->assertNoEvents();
5407
5408 // Consume the first tap
5409 mFocusedWindow->finishEvent(*downEventSequenceNum);
5410 mFocusedWindow->finishEvent(*upEventSequenceNum);
5411 ASSERT_TRUE(mDispatcher->waitForIdle());
5412 // The second tap did not go to the focused window
5413 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005414 // Since all events are finished, connection should be deemed healthy again
Prabir Pradhanedd96402022-02-15 01:46:16 -08005415 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
5416 mFocusedWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005417 mFakePolicy->assertNotifyAnrWasNotCalled();
5418}
5419
5420// If you tap outside of all windows, there will not be ANR
5421TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005422 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005423 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5424 LOCATION_OUTSIDE_ALL_WINDOWS));
5425 ASSERT_TRUE(mDispatcher->waitForIdle());
5426 mFakePolicy->assertNotifyAnrWasNotCalled();
5427}
5428
5429// Since the focused window is paused, tapping on it should not produce any events
5430TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) {
5431 mFocusedWindow->setPaused(true);
5432 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
5433
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005434 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005435 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5436 FOCUSED_WINDOW_LOCATION));
5437
5438 std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT));
5439 ASSERT_TRUE(mDispatcher->waitForIdle());
5440 // Should not ANR because the window is paused, and touches shouldn't go to it
5441 mFakePolicy->assertNotifyAnrWasNotCalled();
5442
5443 mFocusedWindow->assertNoEvents();
5444 mUnfocusedWindow->assertNoEvents();
5445}
5446
5447/**
5448 * If a window is processing a motion event, and then a key event comes in, the key event should
5449 * not to to the focused window until the motion is processed.
5450 * If a different window becomes focused at this time, the key should go to that window instead.
5451 *
5452 * Warning!!!
5453 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
5454 * and the injection timeout that we specify when injecting the key.
5455 * We must have the injection timeout (10ms) be smaller than
5456 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
5457 *
5458 * If that value changes, this test should also change.
5459 */
5460TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) {
5461 // Set a long ANR timeout to prevent it from triggering
5462 mFocusedWindow->setDispatchingTimeout(2s);
5463 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
5464
5465 tapOnUnfocusedWindow();
5466 std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent();
5467 ASSERT_TRUE(downSequenceNum);
5468 std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent();
5469 ASSERT_TRUE(upSequenceNum);
5470 // Don't finish the events yet, and send a key
5471 // Injection will succeed because we will eventually give up and send the key to the focused
5472 // window even if motions are still being processed.
5473
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005474 InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005475 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005476 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/);
5477 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005478 // Key will not be sent to the window, yet, because the window is still processing events
5479 // and the key remains pending, waiting for the touch events to be processed
5480 std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent();
5481 ASSERT_FALSE(keySequenceNum);
5482
5483 // Switch the focus to the "unfocused" window that we tapped. Expect the key to go there
Vishnu Nair47074b82020-08-14 11:54:47 -07005484 mFocusedWindow->setFocusable(false);
5485 mUnfocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005486 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005487 setFocusedWindow(mUnfocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005488
5489 // Focus events should precede the key events
5490 mUnfocusedWindow->consumeFocusEvent(true);
5491 mFocusedWindow->consumeFocusEvent(false);
5492
5493 // Finish the tap events, which should unblock dispatcher
5494 mUnfocusedWindow->finishEvent(*downSequenceNum);
5495 mUnfocusedWindow->finishEvent(*upSequenceNum);
5496
5497 // Now that all queues are cleared and no backlog in the connections, the key event
5498 // can finally go to the newly focused "mUnfocusedWindow".
5499 mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5500 mFocusedWindow->assertNoEvents();
5501 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005502 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005503}
5504
5505// When the touch stream is split across 2 windows, and one of them does not respond,
5506// then ANR should be raised and the touch should be canceled for the unresponsive window.
5507// The other window should not be affected by that.
5508TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) {
5509 // Touch Window 1
5510 NotifyMotionArgs motionArgs =
5511 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5512 ADISPLAY_ID_DEFAULT, {FOCUSED_WINDOW_LOCATION});
5513 mDispatcher->notifyMotion(&motionArgs);
5514 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
5515 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
5516
5517 // Touch Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08005518 motionArgs = generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5519 {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005520 mDispatcher->notifyMotion(&motionArgs);
5521
5522 const std::chrono::duration timeout =
5523 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005524 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005525
5526 mUnfocusedWindow->consumeMotionDown();
5527 mFocusedWindow->consumeMotionDown();
5528 // Focused window may or may not receive ACTION_MOVE
5529 // But it should definitely receive ACTION_CANCEL due to the ANR
5530 InputEvent* event;
5531 std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event);
5532 ASSERT_TRUE(moveOrCancelSequenceNum);
5533 mFocusedWindow->finishEvent(*moveOrCancelSequenceNum);
5534 ASSERT_NE(nullptr, event);
5535 ASSERT_EQ(event->getType(), AINPUT_EVENT_TYPE_MOTION);
5536 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
5537 if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) {
5538 mFocusedWindow->consumeMotionCancel();
5539 } else {
5540 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction());
5541 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005542 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08005543 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
5544 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005545
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005546 mUnfocusedWindow->assertNoEvents();
5547 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005548 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005549}
5550
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05005551/**
5552 * If we have no focused window, and a key comes in, we start the ANR timer.
5553 * The focused application should add a focused window before the timer runs out to prevent ANR.
5554 *
5555 * If the user touches another application during this time, the key should be dropped.
5556 * Next, if a new focused window comes in, without toggling the focused application,
5557 * then no ANR should occur.
5558 *
5559 * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication',
5560 * but in some cases the policy may not update the focused application.
5561 */
5562TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) {
5563 std::shared_ptr<FakeApplicationHandle> focusedApplication =
5564 std::make_shared<FakeApplicationHandle>();
5565 focusedApplication->setDispatchingTimeout(60ms);
5566 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication);
5567 // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused.
5568 mFocusedWindow->setFocusable(false);
5569
5570 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
5571 mFocusedWindow->consumeFocusEvent(false);
5572
5573 // Send a key. The ANR timer should start because there is no focused window.
5574 // 'focusedApplication' will get blamed if this timer completes.
5575 // Key will not be sent anywhere because we have no focused window. It will remain pending.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005576 InputEventInjectionResult result =
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05005577 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005578 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/,
5579 false /* allowKeyRepeat */);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005580 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05005581
5582 // Wait until dispatcher starts the "no focused window" timer. If we don't wait here,
5583 // then the injected touches won't cause the focused event to get dropped.
5584 // The dispatcher only checks for whether the queue should be pruned upon queueing.
5585 // If we inject the touch right away and the ANR timer hasn't started, the touch event would
5586 // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'.
5587 // For this test, it means that the key would get delivered to the window once it becomes
5588 // focused.
5589 std::this_thread::sleep_for(10ms);
5590
5591 // Touch unfocused window. This should force the pending key to get dropped.
5592 NotifyMotionArgs motionArgs =
5593 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5594 ADISPLAY_ID_DEFAULT, {UNFOCUSED_WINDOW_LOCATION});
5595 mDispatcher->notifyMotion(&motionArgs);
5596
5597 // We do not consume the motion right away, because that would require dispatcher to first
5598 // process (== drop) the key event, and by that time, ANR will be raised.
5599 // Set the focused window first.
5600 mFocusedWindow->setFocusable(true);
5601 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
5602 setFocusedWindow(mFocusedWindow);
5603 mFocusedWindow->consumeFocusEvent(true);
5604 // We do not call "setFocusedApplication" here, even though the newly focused window belongs
5605 // to another application. This could be a bug / behaviour in the policy.
5606
5607 mUnfocusedWindow->consumeMotionDown();
5608
5609 ASSERT_TRUE(mDispatcher->waitForIdle());
5610 // Should not ANR because we actually have a focused window. It was just added too slowly.
5611 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled());
5612}
5613
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05005614// These tests ensure we cannot send touch events to a window that's positioned behind a window
5615// that has feature NO_INPUT_CHANNEL.
5616// Layout:
5617// Top (closest to user)
5618// mNoInputWindow (above all windows)
5619// mBottomWindow
5620// Bottom (furthest from user)
5621class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest {
5622 virtual void SetUp() override {
5623 InputDispatcherTest::SetUp();
5624
5625 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005626 mNoInputWindow =
5627 sp<FakeWindowHandle>::make(mApplication, mDispatcher,
5628 "Window without input channel", ADISPLAY_ID_DEFAULT,
5629 std::make_optional<sp<IBinder>>(nullptr) /*token*/);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08005630 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05005631 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
5632 // It's perfectly valid for this window to not have an associated input channel
5633
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005634 mBottomWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Bottom window",
5635 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05005636 mBottomWindow->setFrame(Rect(0, 0, 100, 100));
5637
5638 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
5639 }
5640
5641protected:
5642 std::shared_ptr<FakeApplicationHandle> mApplication;
5643 sp<FakeWindowHandle> mNoInputWindow;
5644 sp<FakeWindowHandle> mBottomWindow;
5645};
5646
5647TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) {
5648 PointF touchedPoint = {10, 10};
5649
5650 NotifyMotionArgs motionArgs =
5651 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5652 ADISPLAY_ID_DEFAULT, {touchedPoint});
5653 mDispatcher->notifyMotion(&motionArgs);
5654
5655 mNoInputWindow->assertNoEvents();
5656 // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have
5657 // an input channel, it is not marked as FLAG_NOT_TOUCHABLE,
5658 // and therefore should prevent mBottomWindow from receiving touches
5659 mBottomWindow->assertNoEvents();
5660}
5661
5662/**
5663 * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel,
5664 * ensure that this window does not receive any touches, and blocks touches to windows underneath.
5665 */
5666TEST_F(InputDispatcherMultiWindowOcclusionTests,
5667 NoInputChannelFeature_DropsTouchesWithValidChannel) {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005668 mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher,
5669 "Window with input channel and NO_INPUT_CHANNEL",
5670 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05005671
Prabir Pradhan51e7db02022-02-07 06:02:57 -08005672 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05005673 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
5674 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
5675
5676 PointF touchedPoint = {10, 10};
5677
5678 NotifyMotionArgs motionArgs =
5679 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5680 ADISPLAY_ID_DEFAULT, {touchedPoint});
5681 mDispatcher->notifyMotion(&motionArgs);
5682
5683 mNoInputWindow->assertNoEvents();
5684 mBottomWindow->assertNoEvents();
5685}
5686
Vishnu Nair958da932020-08-21 17:12:37 -07005687class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest {
5688protected:
5689 std::shared_ptr<FakeApplicationHandle> mApp;
5690 sp<FakeWindowHandle> mWindow;
5691 sp<FakeWindowHandle> mMirror;
5692
5693 virtual void SetUp() override {
5694 InputDispatcherTest::SetUp();
5695 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005696 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
5697 mMirror = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindowMirror",
5698 ADISPLAY_ID_DEFAULT, mWindow->getToken());
Vishnu Nair958da932020-08-21 17:12:37 -07005699 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
5700 mWindow->setFocusable(true);
5701 mMirror->setFocusable(true);
5702 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
5703 }
5704};
5705
5706TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) {
5707 // Request focus on a mirrored window
5708 setFocusedWindow(mMirror);
5709
5710 // window gets focused
5711 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005712 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5713 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005714 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
5715}
5716
5717// A focused & mirrored window remains focused only if the window and its mirror are both
5718// focusable.
5719TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) {
5720 setFocusedWindow(mMirror);
5721
5722 // window gets focused
5723 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005724 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5725 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005726 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005727 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
5728 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005729 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
5730
5731 mMirror->setFocusable(false);
5732 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
5733
5734 // window loses focus since one of the windows associated with the token in not focusable
5735 mWindow->consumeFocusEvent(false);
5736
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005737 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
5738 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07005739 mWindow->assertNoEvents();
5740}
5741
5742// A focused & mirrored window remains focused until the window and its mirror both become
5743// invisible.
5744TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) {
5745 setFocusedWindow(mMirror);
5746
5747 // window gets focused
5748 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005749 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5750 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005751 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005752 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
5753 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005754 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
5755
5756 mMirror->setVisible(false);
5757 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
5758
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005759 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5760 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005761 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005762 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
5763 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005764 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
5765
5766 mWindow->setVisible(false);
5767 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
5768
5769 // window loses focus only after all windows associated with the token become invisible.
5770 mWindow->consumeFocusEvent(false);
5771
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005772 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
5773 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07005774 mWindow->assertNoEvents();
5775}
5776
5777// A focused & mirrored window remains focused until both windows are removed.
5778TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) {
5779 setFocusedWindow(mMirror);
5780
5781 // window gets focused
5782 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005783 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5784 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005785 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005786 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
5787 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005788 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
5789
5790 // single window is removed but the window token remains focused
5791 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}});
5792
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005793 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5794 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005795 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005796 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
5797 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005798 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
5799
5800 // Both windows are removed
5801 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
5802 mWindow->consumeFocusEvent(false);
5803
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005804 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
5805 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07005806 mWindow->assertNoEvents();
5807}
5808
5809// Focus request can be pending until one window becomes visible.
5810TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) {
5811 // Request focus on an invisible mirror.
5812 mWindow->setVisible(false);
5813 mMirror->setVisible(false);
5814 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
5815 setFocusedWindow(mMirror);
5816
5817 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005818 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Vishnu Nair958da932020-08-21 17:12:37 -07005819 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005820 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07005821
5822 mMirror->setVisible(true);
5823 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
5824
5825 // window gets focused
5826 mWindow->consumeFocusEvent(true);
5827 // window gets the pending key event
5828 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5829}
Prabir Pradhan99987712020-11-10 18:43:05 -08005830
5831class InputDispatcherPointerCaptureTests : public InputDispatcherTest {
5832protected:
5833 std::shared_ptr<FakeApplicationHandle> mApp;
5834 sp<FakeWindowHandle> mWindow;
5835 sp<FakeWindowHandle> mSecondWindow;
5836
5837 void SetUp() override {
5838 InputDispatcherTest::SetUp();
5839 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005840 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -08005841 mWindow->setFocusable(true);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005842 mSecondWindow =
5843 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -08005844 mSecondWindow->setFocusable(true);
5845
5846 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
5847 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}});
5848
5849 setFocusedWindow(mWindow);
5850 mWindow->consumeFocusEvent(true);
5851 }
5852
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00005853 void notifyPointerCaptureChanged(const PointerCaptureRequest& request) {
5854 const NotifyPointerCaptureChangedArgs args = generatePointerCaptureChangedArgs(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08005855 mDispatcher->notifyPointerCaptureChanged(&args);
5856 }
5857
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00005858 PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window,
5859 bool enabled) {
Prabir Pradhan99987712020-11-10 18:43:05 -08005860 mDispatcher->requestPointerCapture(window->getToken(), enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00005861 auto request = mFakePolicy->assertSetPointerCaptureCalled(enabled);
5862 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08005863 window->consumeCaptureEvent(enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00005864 return request;
Prabir Pradhan99987712020-11-10 18:43:05 -08005865 }
5866};
5867
5868TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) {
5869 // Ensure that capture cannot be obtained for unfocused windows.
5870 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
5871 mFakePolicy->assertSetPointerCaptureNotCalled();
5872 mSecondWindow->assertNoEvents();
5873
5874 // Ensure that capture can be enabled from the focus window.
5875 requestAndVerifyPointerCapture(mWindow, true);
5876
5877 // Ensure that capture cannot be disabled from a window that does not have capture.
5878 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false);
5879 mFakePolicy->assertSetPointerCaptureNotCalled();
5880
5881 // Ensure that capture can be disabled from the window with capture.
5882 requestAndVerifyPointerCapture(mWindow, false);
5883}
5884
5885TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00005886 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08005887
5888 setFocusedWindow(mSecondWindow);
5889
5890 // Ensure that the capture disabled event was sent first.
5891 mWindow->consumeCaptureEvent(false);
5892 mWindow->consumeFocusEvent(false);
5893 mSecondWindow->consumeFocusEvent(true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00005894 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08005895
5896 // Ensure that additional state changes from InputReader are not sent to the window.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00005897 notifyPointerCaptureChanged({});
5898 notifyPointerCaptureChanged(request);
5899 notifyPointerCaptureChanged({});
Prabir Pradhan99987712020-11-10 18:43:05 -08005900 mWindow->assertNoEvents();
5901 mSecondWindow->assertNoEvents();
5902 mFakePolicy->assertSetPointerCaptureNotCalled();
5903}
5904
5905TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00005906 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08005907
5908 // InputReader unexpectedly disables and enables pointer capture.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00005909 notifyPointerCaptureChanged({});
5910 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08005911
5912 // Ensure that Pointer Capture is disabled.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00005913 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08005914 mWindow->consumeCaptureEvent(false);
5915 mWindow->assertNoEvents();
5916}
5917
Prabir Pradhan167e6d92021-02-04 16:18:17 -08005918TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) {
5919 requestAndVerifyPointerCapture(mWindow, true);
5920
5921 // The first window loses focus.
5922 setFocusedWindow(mSecondWindow);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00005923 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08005924 mWindow->consumeCaptureEvent(false);
5925
5926 // Request Pointer Capture from the second window before the notification from InputReader
5927 // arrives.
5928 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00005929 auto request = mFakePolicy->assertSetPointerCaptureCalled(true);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08005930
5931 // InputReader notifies Pointer Capture was disabled (because of the focus change).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00005932 notifyPointerCaptureChanged({});
Prabir Pradhan167e6d92021-02-04 16:18:17 -08005933
5934 // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00005935 notifyPointerCaptureChanged(request);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08005936
5937 mSecondWindow->consumeFocusEvent(true);
5938 mSecondWindow->consumeCaptureEvent(true);
5939}
5940
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00005941TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) {
5942 // App repeatedly enables and disables capture.
5943 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
5944 auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
5945 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
5946 mFakePolicy->assertSetPointerCaptureCalled(false);
5947 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
5948 auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
5949
5950 // InputReader notifies that PointerCapture has been enabled for the first request. Since the
5951 // first request is now stale, this should do nothing.
5952 notifyPointerCaptureChanged(firstRequest);
5953 mWindow->assertNoEvents();
5954
5955 // InputReader notifies that the second request was enabled.
5956 notifyPointerCaptureChanged(secondRequest);
5957 mWindow->consumeCaptureEvent(true);
5958}
5959
Prabir Pradhan7092e262022-05-03 16:51:09 +00005960TEST_F(InputDispatcherPointerCaptureTests, RapidToggleRequests) {
5961 requestAndVerifyPointerCapture(mWindow, true);
5962
5963 // App toggles pointer capture off and on.
5964 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
5965 mFakePolicy->assertSetPointerCaptureCalled(false);
5966
5967 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
5968 auto enableRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
5969
5970 // InputReader notifies that the latest "enable" request was processed, while skipping over the
5971 // preceding "disable" request.
5972 notifyPointerCaptureChanged(enableRequest);
5973
5974 // Since pointer capture was never disabled during the rapid toggle, the window does not receive
5975 // any notifications.
5976 mWindow->assertNoEvents();
5977}
5978
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00005979class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest {
5980protected:
5981 constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8;
Bernardo Rufino7393d172021-02-26 13:56:11 +00005982
5983 constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9;
5984 static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY);
5985
5986 constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7;
5987 static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
5988
5989 // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold
5990 constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5;
5991 static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
5992 static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) <
5993 MAXIMUM_OBSCURING_OPACITY);
5994
Bernardo Rufino6d52e542021-02-15 18:38:10 +00005995 static const int32_t TOUCHED_APP_UID = 10001;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00005996 static const int32_t APP_B_UID = 10002;
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00005997 static const int32_t APP_C_UID = 10003;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00005998
5999 sp<FakeWindowHandle> mTouchWindow;
6000
6001 virtual void SetUp() override {
6002 InputDispatcherTest::SetUp();
Bernardo Rufino6d52e542021-02-15 18:38:10 +00006003 mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched");
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006004 mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY);
6005 }
6006
6007 virtual void TearDown() override {
6008 InputDispatcherTest::TearDown();
6009 mTouchWindow.clear();
6010 }
6011
chaviw3277faf2021-05-19 16:45:23 -05006012 sp<FakeWindowHandle> getOccludingWindow(int32_t uid, std::string name, TouchOcclusionMode mode,
6013 float alpha = 1.0f) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006014 sp<FakeWindowHandle> window = getWindow(uid, name);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08006015 window->setTouchable(false);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006016 window->setTouchOcclusionMode(mode);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006017 window->setAlpha(alpha);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006018 return window;
6019 }
6020
6021 sp<FakeWindowHandle> getWindow(int32_t uid, std::string name) {
6022 std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>();
6023 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006024 sp<FakeWindowHandle>::make(app, mDispatcher, name, ADISPLAY_ID_DEFAULT);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006025 // Generate an arbitrary PID based on the UID
6026 window->setOwnerInfo(1777 + (uid % 10000), uid);
6027 return window;
6028 }
6029
6030 void touch(const std::vector<PointF>& points = {PointF{100, 200}}) {
6031 NotifyMotionArgs args =
6032 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6033 ADISPLAY_ID_DEFAULT, points);
6034 mDispatcher->notifyMotion(&args);
6035 }
6036};
6037
6038TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006039 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006040 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006041 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006042
6043 touch();
6044
6045 mTouchWindow->assertNoEvents();
6046}
6047
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00006048TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufino7393d172021-02-26 13:56:11 +00006049 WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) {
6050 const sp<FakeWindowHandle>& w =
6051 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f);
6052 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6053
6054 touch();
6055
6056 mTouchWindow->assertNoEvents();
6057}
6058
6059TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00006060 WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) {
6061 const sp<FakeWindowHandle>& w =
6062 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
6063 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6064
6065 touch();
6066
6067 w->assertNoEvents();
6068}
6069
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006070TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006071 const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW);
6072 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006073
6074 touch();
6075
6076 mTouchWindow->consumeAnyMotionDown();
6077}
6078
6079TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006080 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006081 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006082 w->setFrame(Rect(0, 0, 50, 50));
6083 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006084
6085 touch({PointF{100, 100}});
6086
6087 mTouchWindow->consumeAnyMotionDown();
6088}
6089
6090TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006091 const sp<FakeWindowHandle>& w =
Bernardo Rufino6d52e542021-02-15 18:38:10 +00006092 getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006093 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6094
6095 touch();
6096
6097 mTouchWindow->consumeAnyMotionDown();
6098}
6099
6100TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) {
6101 const sp<FakeWindowHandle>& w =
6102 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
6103 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006104
6105 touch();
6106
6107 mTouchWindow->consumeAnyMotionDown();
6108}
6109
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00006110TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) {
6111 const sp<FakeWindowHandle>& w =
6112 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
6113 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6114
6115 touch();
6116
6117 w->assertNoEvents();
6118}
6119
6120/**
6121 * This is important to make sure apps can't indirectly learn the position of touches (outside vs
6122 * inside) while letting them pass-through. Note that even though touch passes through the occluding
6123 * window, the occluding window will still receive ACTION_OUTSIDE event.
6124 */
6125TEST_F(InputDispatcherUntrustedTouchesTest,
6126 WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) {
6127 const sp<FakeWindowHandle>& w =
6128 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08006129 w->setWatchOutsideTouch(true);
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00006130 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6131
6132 touch();
6133
6134 w->consumeMotionOutside();
6135}
6136
6137TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) {
6138 const sp<FakeWindowHandle>& w =
6139 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08006140 w->setWatchOutsideTouch(true);
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00006141 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6142
6143 touch();
6144
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08006145 w->consumeMotionOutsideWithZeroedCoords();
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00006146}
6147
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006148TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006149 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006150 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
6151 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006152 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6153
6154 touch();
6155
6156 mTouchWindow->consumeAnyMotionDown();
6157}
6158
6159TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) {
6160 const sp<FakeWindowHandle>& w =
6161 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
6162 MAXIMUM_OBSCURING_OPACITY);
6163 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006164
6165 touch();
6166
6167 mTouchWindow->consumeAnyMotionDown();
6168}
6169
6170TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006171 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006172 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
6173 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006174 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6175
6176 touch();
6177
6178 mTouchWindow->assertNoEvents();
6179}
6180
6181TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) {
6182 // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91
6183 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006184 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
6185 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006186 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006187 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
6188 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006189 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
6190
6191 touch();
6192
6193 mTouchWindow->assertNoEvents();
6194}
6195
6196TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) {
6197 // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75
6198 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006199 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
6200 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006201 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006202 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
6203 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006204 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
6205
6206 touch();
6207
6208 mTouchWindow->consumeAnyMotionDown();
6209}
6210
6211TEST_F(InputDispatcherUntrustedTouchesTest,
6212 WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) {
6213 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006214 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
6215 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006216 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006217 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
6218 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006219 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
6220
6221 touch();
6222
6223 mTouchWindow->consumeAnyMotionDown();
6224}
6225
6226TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) {
6227 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006228 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
6229 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006230 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006231 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
6232 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006233 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006234
6235 touch();
6236
6237 mTouchWindow->assertNoEvents();
6238}
6239
Bernardo Rufino6d52e542021-02-15 18:38:10 +00006240TEST_F(InputDispatcherUntrustedTouchesTest,
6241 WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) {
6242 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006243 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
6244 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00006245 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006246 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
6247 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00006248 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
6249
6250 touch();
6251
6252 mTouchWindow->assertNoEvents();
6253}
6254
6255TEST_F(InputDispatcherUntrustedTouchesTest,
6256 WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) {
6257 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006258 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
6259 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00006260 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006261 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
6262 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00006263 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
6264
6265 touch();
6266
6267 mTouchWindow->consumeAnyMotionDown();
6268}
6269
6270TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) {
6271 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006272 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
6273 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00006274 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6275
6276 touch();
6277
6278 mTouchWindow->consumeAnyMotionDown();
6279}
6280
6281TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) {
6282 const sp<FakeWindowHandle>& w =
6283 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED);
6284 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6285
6286 touch();
6287
6288 mTouchWindow->consumeAnyMotionDown();
6289}
6290
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00006291TEST_F(InputDispatcherUntrustedTouchesTest,
6292 OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) {
6293 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
6294 const sp<FakeWindowHandle>& w =
6295 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f);
6296 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6297
6298 touch();
6299
6300 mTouchWindow->assertNoEvents();
6301}
6302
6303TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) {
6304 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
6305 const sp<FakeWindowHandle>& w =
6306 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f);
6307 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6308
6309 touch();
6310
6311 mTouchWindow->consumeAnyMotionDown();
6312}
6313
6314TEST_F(InputDispatcherUntrustedTouchesTest,
6315 OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) {
6316 mDispatcher->setMaximumObscuringOpacityForTouch(1.0f);
6317 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006318 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
6319 OPACITY_ABOVE_THRESHOLD);
6320 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6321
6322 touch();
6323
6324 mTouchWindow->consumeAnyMotionDown();
6325}
6326
6327TEST_F(InputDispatcherUntrustedTouchesTest,
6328 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) {
6329 const sp<FakeWindowHandle>& w1 =
6330 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
6331 OPACITY_BELOW_THRESHOLD);
6332 const sp<FakeWindowHandle>& w2 =
6333 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
6334 OPACITY_BELOW_THRESHOLD);
6335 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
6336
6337 touch();
6338
6339 mTouchWindow->assertNoEvents();
6340}
6341
6342/**
6343 * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the
6344 * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold
6345 * (which alone would result in allowing touches) does not affect the blocking behavior.
6346 */
6347TEST_F(InputDispatcherUntrustedTouchesTest,
6348 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) {
6349 const sp<FakeWindowHandle>& wB =
6350 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
6351 OPACITY_BELOW_THRESHOLD);
6352 const sp<FakeWindowHandle>& wC =
6353 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
6354 OPACITY_BELOW_THRESHOLD);
6355 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
6356
6357 touch();
6358
6359 mTouchWindow->assertNoEvents();
6360}
6361
6362/**
6363 * This test is testing that a window from a different UID but with same application token doesn't
6364 * block the touch. Apps can share the application token for close UI collaboration for example.
6365 */
6366TEST_F(InputDispatcherUntrustedTouchesTest,
6367 WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) {
6368 const sp<FakeWindowHandle>& w =
6369 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
6370 w->setApplicationToken(mTouchWindow->getApplicationToken());
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00006371 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6372
6373 touch();
6374
6375 mTouchWindow->consumeAnyMotionDown();
6376}
6377
arthurhungb89ccb02020-12-30 16:19:01 +08006378class InputDispatcherDragTests : public InputDispatcherTest {
6379protected:
6380 std::shared_ptr<FakeApplicationHandle> mApp;
6381 sp<FakeWindowHandle> mWindow;
6382 sp<FakeWindowHandle> mSecondWindow;
6383 sp<FakeWindowHandle> mDragWindow;
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00006384 sp<FakeWindowHandle> mSpyWindow;
Arthur Hungb75c2aa2022-07-15 09:35:36 +00006385 // Mouse would force no-split, set the id as non-zero to verify if drag state could track it.
6386 static constexpr int32_t MOUSE_POINTER_ID = 1;
arthurhungb89ccb02020-12-30 16:19:01 +08006387
6388 void SetUp() override {
6389 InputDispatcherTest::SetUp();
6390 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006391 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +08006392 mWindow->setFrame(Rect(0, 0, 100, 100));
arthurhungb89ccb02020-12-30 16:19:01 +08006393
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006394 mSecondWindow =
6395 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +08006396 mSecondWindow->setFrame(Rect(100, 0, 200, 100));
arthurhungb89ccb02020-12-30 16:19:01 +08006397
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006398 mSpyWindow =
6399 sp<FakeWindowHandle>::make(mApp, mDispatcher, "SpyWindow", ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00006400 mSpyWindow->setSpy(true);
6401 mSpyWindow->setTrustedOverlay(true);
6402 mSpyWindow->setFrame(Rect(0, 0, 200, 100));
6403
arthurhungb89ccb02020-12-30 16:19:01 +08006404 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00006405 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mSpyWindow, mWindow, mSecondWindow}}});
arthurhungb89ccb02020-12-30 16:19:01 +08006406 }
6407
Arthur Hungb75c2aa2022-07-15 09:35:36 +00006408 void injectDown(int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
6409 switch (fromSource) {
6410 case AINPUT_SOURCE_TOUCHSCREEN:
6411 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6412 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
6413 ADISPLAY_ID_DEFAULT, {50, 50}))
6414 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6415 break;
6416 case AINPUT_SOURCE_STYLUS:
6417 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6418 injectMotionEvent(
6419 mDispatcher,
6420 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
6421 AINPUT_SOURCE_STYLUS)
6422 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
6423 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS)
6424 .x(50)
6425 .y(50))
6426 .build()));
6427 break;
6428 case AINPUT_SOURCE_MOUSE:
6429 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6430 injectMotionEvent(
6431 mDispatcher,
6432 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
6433 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
6434 .pointer(PointerBuilder(MOUSE_POINTER_ID,
6435 AMOTION_EVENT_TOOL_TYPE_MOUSE)
6436 .x(50)
6437 .y(50))
6438 .build()));
6439 break;
6440 default:
6441 FAIL() << "Source " << fromSource << " doesn't support drag and drop";
6442 }
arthurhungb89ccb02020-12-30 16:19:01 +08006443
6444 // Window should receive motion event.
6445 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00006446 // Spy window should also receive motion event
6447 mSpyWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung54745652022-04-20 07:17:41 +00006448 }
6449
6450 // Start performing drag, we will create a drag window and transfer touch to it.
6451 // @param sendDown : if true, send a motion down on first window before perform drag and drop.
6452 // Returns true on success.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00006453 bool startDrag(bool sendDown = true, int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
Arthur Hung54745652022-04-20 07:17:41 +00006454 if (sendDown) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00006455 injectDown(fromSource);
Arthur Hung54745652022-04-20 07:17:41 +00006456 }
arthurhungb89ccb02020-12-30 16:19:01 +08006457
6458 // The drag window covers the entire display
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006459 mDragWindow =
6460 sp<FakeWindowHandle>::make(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00006461 mDragWindow->setTouchableRegion(Region{{0, 0, 0, 0}});
arthurhungb89ccb02020-12-30 16:19:01 +08006462 mDispatcher->setInputWindows(
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00006463 {{ADISPLAY_ID_DEFAULT, {mDragWindow, mSpyWindow, mWindow, mSecondWindow}}});
arthurhungb89ccb02020-12-30 16:19:01 +08006464
6465 // Transfer touch focus to the drag window
Arthur Hung54745652022-04-20 07:17:41 +00006466 bool transferred =
6467 mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(),
6468 true /* isDragDrop */);
6469 if (transferred) {
6470 mWindow->consumeMotionCancel();
6471 mDragWindow->consumeMotionDown();
6472 }
6473 return transferred;
arthurhungb89ccb02020-12-30 16:19:01 +08006474 }
6475};
6476
6477TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00006478 startDrag();
arthurhungb89ccb02020-12-30 16:19:01 +08006479
6480 // Move on window.
6481 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6482 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6483 ADISPLAY_ID_DEFAULT, {50, 50}))
6484 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6485 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
6486 mWindow->consumeDragEvent(false, 50, 50);
6487 mSecondWindow->assertNoEvents();
6488
6489 // Move to another window.
6490 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6491 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6492 ADISPLAY_ID_DEFAULT, {150, 50}))
6493 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6494 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
6495 mWindow->consumeDragEvent(true, 150, 50);
6496 mSecondWindow->consumeDragEvent(false, 50, 50);
6497
6498 // Move back to original window.
6499 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6500 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6501 ADISPLAY_ID_DEFAULT, {50, 50}))
6502 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6503 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
6504 mWindow->consumeDragEvent(false, 50, 50);
6505 mSecondWindow->consumeDragEvent(true, -50, 50);
6506
6507 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6508 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {50, 50}))
6509 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6510 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
6511 mWindow->assertNoEvents();
6512 mSecondWindow->assertNoEvents();
6513}
6514
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00006515TEST_F(InputDispatcherDragTests, DragEnterAndPointerDownPilfersPointers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00006516 startDrag();
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00006517
6518 // No cancel event after drag start
6519 mSpyWindow->assertNoEvents();
6520
6521 const MotionEvent secondFingerDownEvent =
6522 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
6523 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
6524 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
6525 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(60).y(60))
6526 .build();
6527 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6528 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
6529 InputEventInjectionSync::WAIT_FOR_RESULT))
6530 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6531
6532 // Receives cancel for first pointer after next pointer down
6533 mSpyWindow->consumeMotionCancel();
6534 mSpyWindow->consumeMotionDown();
6535
6536 mSpyWindow->assertNoEvents();
6537}
6538
arthurhungf452d0b2021-01-06 00:19:52 +08006539TEST_F(InputDispatcherDragTests, DragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00006540 startDrag();
arthurhungf452d0b2021-01-06 00:19:52 +08006541
6542 // Move on window.
6543 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6544 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6545 ADISPLAY_ID_DEFAULT, {50, 50}))
6546 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6547 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
6548 mWindow->consumeDragEvent(false, 50, 50);
6549 mSecondWindow->assertNoEvents();
6550
6551 // Move to another window.
6552 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6553 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6554 ADISPLAY_ID_DEFAULT, {150, 50}))
6555 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6556 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
6557 mWindow->consumeDragEvent(true, 150, 50);
6558 mSecondWindow->consumeDragEvent(false, 50, 50);
6559
6560 // drop to another window.
6561 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6562 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6563 {150, 50}))
6564 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6565 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
6566 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
6567 mWindow->assertNoEvents();
6568 mSecondWindow->assertNoEvents();
6569}
6570
arthurhung6d4bed92021-03-17 11:59:33 +08006571TEST_F(InputDispatcherDragTests, StylusDragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00006572 startDrag(true, AINPUT_SOURCE_STYLUS);
arthurhung6d4bed92021-03-17 11:59:33 +08006573
6574 // Move on window and keep button pressed.
6575 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6576 injectMotionEvent(mDispatcher,
6577 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
6578 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
6579 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS)
6580 .x(50)
6581 .y(50))
6582 .build()))
6583 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6584 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
6585 mWindow->consumeDragEvent(false, 50, 50);
6586 mSecondWindow->assertNoEvents();
6587
6588 // Move to another window and release button, expect to drop item.
6589 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6590 injectMotionEvent(mDispatcher,
6591 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
6592 .buttonState(0)
6593 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS)
6594 .x(150)
6595 .y(50))
6596 .build()))
6597 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6598 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
6599 mWindow->assertNoEvents();
6600 mSecondWindow->assertNoEvents();
6601 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
6602
6603 // nothing to the window.
6604 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6605 injectMotionEvent(mDispatcher,
6606 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS)
6607 .buttonState(0)
6608 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS)
6609 .x(150)
6610 .y(50))
6611 .build()))
6612 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6613 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
6614 mWindow->assertNoEvents();
6615 mSecondWindow->assertNoEvents();
6616}
6617
Arthur Hung54745652022-04-20 07:17:41 +00006618TEST_F(InputDispatcherDragTests, DragAndDropOnInvalidWindow) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00006619 startDrag();
Arthur Hung6d0571e2021-04-09 20:18:16 +08006620
6621 // Set second window invisible.
6622 mSecondWindow->setVisible(false);
6623 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}});
6624
6625 // Move on window.
6626 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6627 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6628 ADISPLAY_ID_DEFAULT, {50, 50}))
6629 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6630 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
6631 mWindow->consumeDragEvent(false, 50, 50);
6632 mSecondWindow->assertNoEvents();
6633
6634 // Move to another window.
6635 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6636 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6637 ADISPLAY_ID_DEFAULT, {150, 50}))
6638 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6639 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
6640 mWindow->consumeDragEvent(true, 150, 50);
6641 mSecondWindow->assertNoEvents();
6642
6643 // drop to another window.
6644 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6645 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6646 {150, 50}))
6647 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6648 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
6649 mFakePolicy->assertDropTargetEquals(nullptr);
6650 mWindow->assertNoEvents();
6651 mSecondWindow->assertNoEvents();
6652}
6653
Arthur Hung54745652022-04-20 07:17:41 +00006654TEST_F(InputDispatcherDragTests, NoDragAndDropWhenMultiFingers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00006655 // Ensure window could track pointerIds if it didn't support split touch.
6656 mWindow->setPreventSplitting(true);
6657
Arthur Hung54745652022-04-20 07:17:41 +00006658 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6659 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6660 {50, 50}))
6661 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6662 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6663
6664 const MotionEvent secondFingerDownEvent =
6665 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
6666 .displayId(ADISPLAY_ID_DEFAULT)
6667 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
6668 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
6669 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(75).y(50))
6670 .build();
6671 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6672 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
6673 InputEventInjectionSync::WAIT_FOR_RESULT))
6674 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6675 mWindow->consumeMotionPointerDown(1 /* pointerIndex */);
6676
6677 // Should not perform drag and drop when window has multi fingers.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00006678 ASSERT_FALSE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +00006679}
6680
6681TEST_F(InputDispatcherDragTests, DragAndDropWhenSplitTouch) {
6682 // First down on second window.
6683 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6684 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6685 {150, 50}))
6686 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6687
6688 mSecondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6689
6690 // Second down on first window.
6691 const MotionEvent secondFingerDownEvent =
6692 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
6693 .displayId(ADISPLAY_ID_DEFAULT)
6694 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
6695 .pointer(
6696 PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50))
6697 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
6698 .build();
6699 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6700 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
6701 InputEventInjectionSync::WAIT_FOR_RESULT))
6702 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6703 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6704
6705 // Perform drag and drop from first window.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00006706 ASSERT_TRUE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +00006707
6708 // Move on window.
6709 const MotionEvent secondFingerMoveEvent =
6710 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
6711 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
6712 .pointer(
6713 PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50))
6714 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
6715 .build();
6716 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6717 injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT,
6718 InputEventInjectionSync::WAIT_FOR_RESULT));
6719 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
6720 mWindow->consumeDragEvent(false, 50, 50);
6721 mSecondWindow->consumeMotionMove();
6722
6723 // Release the drag pointer should perform drop.
6724 const MotionEvent secondFingerUpEvent =
6725 MotionEventBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
6726 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
6727 .pointer(
6728 PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50))
6729 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
6730 .build();
6731 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6732 injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT,
6733 InputEventInjectionSync::WAIT_FOR_RESULT));
6734 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
6735 mFakePolicy->assertDropTargetEquals(mWindow->getToken());
6736 mWindow->assertNoEvents();
6737 mSecondWindow->consumeMotionMove();
6738}
6739
Arthur Hung3915c1f2022-05-31 07:17:17 +00006740TEST_F(InputDispatcherDragTests, DragAndDropWhenMultiDisplays) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00006741 startDrag();
Arthur Hung3915c1f2022-05-31 07:17:17 +00006742
6743 // Update window of second display.
6744 sp<FakeWindowHandle> windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006745 sp<FakeWindowHandle>::make(mApp, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung3915c1f2022-05-31 07:17:17 +00006746 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
6747
6748 // Let second display has a touch state.
6749 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6750 injectMotionEvent(mDispatcher,
6751 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
6752 AINPUT_SOURCE_TOUCHSCREEN)
6753 .displayId(SECOND_DISPLAY_ID)
6754 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER)
6755 .x(100)
6756 .y(100))
6757 .build()));
6758 windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN,
6759 SECOND_DISPLAY_ID, 0 /* expectedFlag */);
6760 // Update window again.
6761 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
6762
6763 // Move on window.
6764 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6765 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6766 ADISPLAY_ID_DEFAULT, {50, 50}))
6767 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6768 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
6769 mWindow->consumeDragEvent(false, 50, 50);
6770 mSecondWindow->assertNoEvents();
6771
6772 // Move to another window.
6773 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6774 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6775 ADISPLAY_ID_DEFAULT, {150, 50}))
6776 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6777 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
6778 mWindow->consumeDragEvent(true, 150, 50);
6779 mSecondWindow->consumeDragEvent(false, 50, 50);
6780
6781 // drop to another window.
6782 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6783 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6784 {150, 50}))
6785 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6786 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
6787 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
6788 mWindow->assertNoEvents();
6789 mSecondWindow->assertNoEvents();
6790}
6791
Arthur Hungb75c2aa2022-07-15 09:35:36 +00006792TEST_F(InputDispatcherDragTests, MouseDragAndDrop) {
6793 startDrag(true, AINPUT_SOURCE_MOUSE);
6794 // Move on window.
6795 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6796 injectMotionEvent(mDispatcher,
6797 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
6798 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
6799 .pointer(PointerBuilder(MOUSE_POINTER_ID,
6800 AMOTION_EVENT_TOOL_TYPE_MOUSE)
6801 .x(50)
6802 .y(50))
6803 .build()))
6804 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6805 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
6806 mWindow->consumeDragEvent(false, 50, 50);
6807 mSecondWindow->assertNoEvents();
6808
6809 // Move to another window.
6810 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6811 injectMotionEvent(mDispatcher,
6812 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
6813 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
6814 .pointer(PointerBuilder(MOUSE_POINTER_ID,
6815 AMOTION_EVENT_TOOL_TYPE_MOUSE)
6816 .x(150)
6817 .y(50))
6818 .build()))
6819 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6820 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
6821 mWindow->consumeDragEvent(true, 150, 50);
6822 mSecondWindow->consumeDragEvent(false, 50, 50);
6823
6824 // drop to another window.
6825 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6826 injectMotionEvent(mDispatcher,
6827 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
6828 .buttonState(0)
6829 .pointer(PointerBuilder(MOUSE_POINTER_ID,
6830 AMOTION_EVENT_TOOL_TYPE_MOUSE)
6831 .x(150)
6832 .y(50))
6833 .build()))
6834 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6835 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
6836 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
6837 mWindow->assertNoEvents();
6838 mSecondWindow->assertNoEvents();
6839}
6840
Vishnu Nair062a8672021-09-03 16:07:44 -07006841class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {};
6842
6843TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) {
6844 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006845 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
6846 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08006847 window->setDropInput(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07006848 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
6849 window->setFocusable(true);
6850 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
6851 setFocusedWindow(window);
6852 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
6853
6854 // With the flag set, window should not get any input
6855 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
6856 mDispatcher->notifyKey(&keyArgs);
6857 window->assertNoEvents();
6858
6859 NotifyMotionArgs motionArgs =
6860 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6861 ADISPLAY_ID_DEFAULT);
6862 mDispatcher->notifyMotion(&motionArgs);
6863 window->assertNoEvents();
6864
6865 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -08006866 window->setDropInput(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07006867 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
6868
6869 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
6870 mDispatcher->notifyKey(&keyArgs);
6871 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
6872
6873 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6874 ADISPLAY_ID_DEFAULT);
6875 mDispatcher->notifyMotion(&motionArgs);
6876 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6877 window->assertNoEvents();
6878}
6879
6880TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) {
6881 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
6882 std::make_shared<FakeApplicationHandle>();
6883 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006884 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
6885 ADISPLAY_ID_DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -07006886 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
6887 obscuringWindow->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08006888 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07006889 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006890 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
6891 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08006892 window->setDropInputIfObscured(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07006893 window->setOwnerInfo(222, 222);
6894 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
6895 window->setFocusable(true);
6896 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
6897 setFocusedWindow(window);
6898 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
6899
6900 // With the flag set, window should not get any input
6901 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
6902 mDispatcher->notifyKey(&keyArgs);
6903 window->assertNoEvents();
6904
6905 NotifyMotionArgs motionArgs =
6906 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6907 ADISPLAY_ID_DEFAULT);
6908 mDispatcher->notifyMotion(&motionArgs);
6909 window->assertNoEvents();
6910
6911 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -08006912 window->setDropInputIfObscured(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07006913 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
6914
6915 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
6916 mDispatcher->notifyKey(&keyArgs);
6917 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
6918
6919 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6920 ADISPLAY_ID_DEFAULT);
6921 mDispatcher->notifyMotion(&motionArgs);
6922 window->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
6923 window->assertNoEvents();
6924}
6925
6926TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) {
6927 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
6928 std::make_shared<FakeApplicationHandle>();
6929 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006930 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
6931 ADISPLAY_ID_DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -07006932 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
6933 obscuringWindow->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08006934 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07006935 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006936 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
6937 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08006938 window->setDropInputIfObscured(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07006939 window->setOwnerInfo(222, 222);
6940 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
6941 window->setFocusable(true);
6942 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
6943 setFocusedWindow(window);
6944 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
6945
6946 // With the flag set, window should not get any input
6947 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
6948 mDispatcher->notifyKey(&keyArgs);
6949 window->assertNoEvents();
6950
6951 NotifyMotionArgs motionArgs =
6952 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6953 ADISPLAY_ID_DEFAULT);
6954 mDispatcher->notifyMotion(&motionArgs);
6955 window->assertNoEvents();
6956
6957 // When the window is no longer obscured because it went on top, it should get input
6958 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, obscuringWindow}}});
6959
6960 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
6961 mDispatcher->notifyKey(&keyArgs);
6962 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
6963
6964 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6965 ADISPLAY_ID_DEFAULT);
6966 mDispatcher->notifyMotion(&motionArgs);
6967 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6968 window->assertNoEvents();
6969}
6970
Antonio Kantekf16f2832021-09-28 04:39:20 +00006971class InputDispatcherTouchModeChangedTests : public InputDispatcherTest {
6972protected:
6973 std::shared_ptr<FakeApplicationHandle> mApp;
Antonio Kantek15beb512022-06-13 22:35:41 +00006974 std::shared_ptr<FakeApplicationHandle> mSecondaryApp;
Antonio Kantekf16f2832021-09-28 04:39:20 +00006975 sp<FakeWindowHandle> mWindow;
6976 sp<FakeWindowHandle> mSecondWindow;
Antonio Kantek15beb512022-06-13 22:35:41 +00006977 sp<FakeWindowHandle> mThirdWindow;
Antonio Kantekf16f2832021-09-28 04:39:20 +00006978
6979 void SetUp() override {
6980 InputDispatcherTest::SetUp();
6981
6982 mApp = std::make_shared<FakeApplicationHandle>();
Antonio Kantek15beb512022-06-13 22:35:41 +00006983 mSecondaryApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006984 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00006985 mWindow->setFocusable(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00006986 setFocusedWindow(mWindow);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006987 mSecondWindow =
6988 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00006989 mSecondWindow->setFocusable(true);
Antonio Kantek15beb512022-06-13 22:35:41 +00006990 mThirdWindow =
6991 sp<FakeWindowHandle>::make(mSecondaryApp, mDispatcher,
6992 "TestWindow3_SecondaryDisplay", SECOND_DISPLAY_ID);
6993 mThirdWindow->setFocusable(true);
Antonio Kantekf16f2832021-09-28 04:39:20 +00006994
6995 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Antonio Kantek15beb512022-06-13 22:35:41 +00006996 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}},
6997 {SECOND_DISPLAY_ID, {mThirdWindow}}});
6998 mThirdWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
Antonio Kantekf16f2832021-09-28 04:39:20 +00006999 mWindow->consumeFocusEvent(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00007000
Antonio Kantek15beb512022-06-13 22:35:41 +00007001 // Set main display initial touch mode to InputDispatcher::kDefaultInTouchMode.
Prabir Pradhan5735a322022-04-11 17:23:34 +00007002 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, WINDOW_PID,
Antonio Kantek15beb512022-06-13 22:35:41 +00007003 WINDOW_UID, true /* hasPermission */,
7004 ADISPLAY_ID_DEFAULT)) {
Antonio Kantek48710e42022-03-24 14:19:30 -07007005 mWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
7006 mSecondWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +00007007 mThirdWindow->assertNoEvents();
7008 }
7009
7010 // Set secondary display initial touch mode to InputDispatcher::kDefaultInTouchMode.
7011 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, SECONDARY_WINDOW_PID,
7012 SECONDARY_WINDOW_UID, true /* hasPermission */,
7013 SECOND_DISPLAY_ID)) {
7014 mWindow->assertNoEvents();
7015 mSecondWindow->assertNoEvents();
7016 mThirdWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek48710e42022-03-24 14:19:30 -07007017 }
Antonio Kantekf16f2832021-09-28 04:39:20 +00007018 }
7019
Antonio Kantek15beb512022-06-13 22:35:41 +00007020 void changeAndVerifyTouchModeInMainDisplayOnly(bool inTouchMode, int32_t pid, int32_t uid,
7021 bool hasPermission) {
Antonio Kanteka042c022022-07-06 16:51:07 -07007022 ASSERT_TRUE(mDispatcher->setInTouchMode(inTouchMode, pid, uid, hasPermission,
7023 ADISPLAY_ID_DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +00007024 mWindow->consumeTouchModeEvent(inTouchMode);
7025 mSecondWindow->consumeTouchModeEvent(inTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +00007026 mThirdWindow->assertNoEvents();
Antonio Kantekf16f2832021-09-28 04:39:20 +00007027 }
7028};
7029
Antonio Kantek26defcf2022-02-08 01:12:27 +00007030TEST_F(InputDispatcherTouchModeChangedTests, FocusedWindowCanChangeTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -08007031 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek15beb512022-06-13 22:35:41 +00007032 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode,
7033 windowInfo.ownerPid, windowInfo.ownerUid,
7034 false /* hasPermission */);
Antonio Kantekf16f2832021-09-28 04:39:20 +00007035}
7036
Antonio Kantek26defcf2022-02-08 01:12:27 +00007037TEST_F(InputDispatcherTouchModeChangedTests, NonFocusedWindowOwnerCannotChangeTouchMode) {
7038 const WindowInfo& windowInfo = *mWindow->getInfo();
7039 int32_t ownerPid = windowInfo.ownerPid;
7040 int32_t ownerUid = windowInfo.ownerUid;
7041 mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1);
7042 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, ownerPid,
Antonio Kanteka042c022022-07-06 16:51:07 -07007043 ownerUid, false /*hasPermission*/,
7044 ADISPLAY_ID_DEFAULT));
Antonio Kantek26defcf2022-02-08 01:12:27 +00007045 mWindow->assertNoEvents();
7046 mSecondWindow->assertNoEvents();
7047}
7048
7049TEST_F(InputDispatcherTouchModeChangedTests, NonWindowOwnerMayChangeTouchModeOnPermissionGranted) {
7050 const WindowInfo& windowInfo = *mWindow->getInfo();
7051 int32_t ownerPid = windowInfo.ownerPid;
7052 int32_t ownerUid = windowInfo.ownerUid;
7053 mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1);
Antonio Kantek15beb512022-06-13 22:35:41 +00007054 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, ownerPid,
7055 ownerUid, true /*hasPermission*/);
Antonio Kantek26defcf2022-02-08 01:12:27 +00007056}
7057
Antonio Kantekf16f2832021-09-28 04:39:20 +00007058TEST_F(InputDispatcherTouchModeChangedTests, EventIsNotGeneratedIfNotChangingTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -08007059 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek26defcf2022-02-08 01:12:27 +00007060 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode,
7061 windowInfo.ownerPid, windowInfo.ownerUid,
Antonio Kanteka042c022022-07-06 16:51:07 -07007062 true /*hasPermission*/, ADISPLAY_ID_DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +00007063 mWindow->assertNoEvents();
7064 mSecondWindow->assertNoEvents();
7065}
7066
Antonio Kantek15beb512022-06-13 22:35:41 +00007067TEST_F(InputDispatcherTouchModeChangedTests, ChangeTouchOnSecondaryDisplayOnly) {
7068 const WindowInfo& windowInfo = *mThirdWindow->getInfo();
7069 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
7070 windowInfo.ownerPid, windowInfo.ownerUid,
7071 true /*hasPermission*/, SECOND_DISPLAY_ID));
7072 mWindow->assertNoEvents();
7073 mSecondWindow->assertNoEvents();
7074 mThirdWindow->consumeTouchModeEvent(!InputDispatcher::kDefaultInTouchMode);
7075}
7076
Antonio Kantek48710e42022-03-24 14:19:30 -07007077TEST_F(InputDispatcherTouchModeChangedTests, CanChangeTouchModeWhenOwningLastInteractedWindow) {
7078 // Interact with the window first.
7079 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
7080 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
7081 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
7082
7083 // Then remove focus.
7084 mWindow->setFocusable(false);
7085 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
7086
7087 // Assert that caller can switch touch mode by owning one of the last interacted window.
7088 const WindowInfo& windowInfo = *mWindow->getInfo();
7089 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
7090 windowInfo.ownerPid, windowInfo.ownerUid,
Antonio Kanteka042c022022-07-06 16:51:07 -07007091 false /*hasPermission*/, ADISPLAY_ID_DEFAULT));
Antonio Kantek48710e42022-03-24 14:19:30 -07007092}
7093
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007094class InputDispatcherSpyWindowTest : public InputDispatcherTest {
7095public:
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007096 sp<FakeWindowHandle> createSpy() {
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007097 std::shared_ptr<FakeApplicationHandle> application =
7098 std::make_shared<FakeApplicationHandle>();
7099 std::string name = "Fake Spy ";
7100 name += std::to_string(mSpyCount++);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007101 sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher,
7102 name.c_str(), ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007103 spy->setSpy(true);
Prabir Pradhan5c85e052021-12-22 02:27:12 -08007104 spy->setTrustedOverlay(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007105 return spy;
7106 }
7107
7108 sp<FakeWindowHandle> createForeground() {
7109 std::shared_ptr<FakeApplicationHandle> application =
7110 std::make_shared<FakeApplicationHandle>();
7111 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007112 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
7113 ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007114 window->setFocusable(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007115 return window;
7116 }
7117
7118private:
7119 int mSpyCount{0};
7120};
7121
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08007122using InputDispatcherSpyWindowDeathTest = InputDispatcherSpyWindowTest;
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007123/**
Prabir Pradhan5c85e052021-12-22 02:27:12 -08007124 * Adding a spy window that is not a trusted overlay causes Dispatcher to abort.
7125 */
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08007126TEST_F(InputDispatcherSpyWindowDeathTest, UntrustedSpy_AbortsDispatcher) {
7127 ScopedSilentDeath _silentDeath;
7128
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007129 auto spy = createSpy();
Prabir Pradhan5c85e052021-12-22 02:27:12 -08007130 spy->setTrustedOverlay(false);
7131 ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}}),
7132 ".* not a trusted overlay");
7133}
7134
7135/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007136 * Input injection into a display with a spy window but no foreground windows should succeed.
7137 */
7138TEST_F(InputDispatcherSpyWindowTest, NoForegroundWindow) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007139 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007140 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}});
7141
7142 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7143 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
7144 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7145 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7146}
7147
7148/**
7149 * Verify the order in which different input windows receive events. The touched foreground window
7150 * (if there is one) should always receive the event first. When there are multiple spy windows, the
7151 * spy windows will receive the event according to their Z-order, where the top-most spy window will
7152 * receive events before ones belows it.
7153 *
7154 * Here, we set up a scenario with four windows in the following Z order from the top:
7155 * spy1, spy2, window, spy3.
7156 * We then inject an event and verify that the foreground "window" receives it first, followed by
7157 * "spy1" and "spy2". The "spy3" does not receive the event because it is underneath the foreground
7158 * window.
7159 */
7160TEST_F(InputDispatcherSpyWindowTest, ReceivesInputInOrder) {
7161 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007162 auto spy1 = createSpy();
7163 auto spy2 = createSpy();
7164 auto spy3 = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007165 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window, spy3}}});
7166 const std::vector<sp<FakeWindowHandle>> channels{spy1, spy2, window, spy3};
7167 const size_t numChannels = channels.size();
7168
Michael Wright8e9a8562022-02-09 13:44:29 +00007169 base::unique_fd epollFd(epoll_create1(EPOLL_CLOEXEC));
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007170 if (!epollFd.ok()) {
7171 FAIL() << "Failed to create epoll fd";
7172 }
7173
7174 for (size_t i = 0; i < numChannels; i++) {
7175 struct epoll_event event = {.events = EPOLLIN, .data.u64 = i};
7176 if (epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, channels[i]->getChannelFd(), &event) < 0) {
7177 FAIL() << "Failed to add fd to epoll";
7178 }
7179 }
7180
7181 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7182 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
7183 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7184
7185 std::vector<size_t> eventOrder;
7186 std::vector<struct epoll_event> events(numChannels);
7187 for (;;) {
7188 const int nFds = epoll_wait(epollFd.get(), events.data(), static_cast<int>(numChannels),
7189 (100ms).count());
7190 if (nFds < 0) {
7191 FAIL() << "Failed to call epoll_wait";
7192 }
7193 if (nFds == 0) {
7194 break; // epoll_wait timed out
7195 }
7196 for (int i = 0; i < nFds; i++) {
Colin Cross5b799302022-10-18 21:52:41 -07007197 ASSERT_EQ(static_cast<uint32_t>(EPOLLIN), events[i].events);
Siarhei Vishniakou31977182022-09-30 08:51:23 -07007198 eventOrder.push_back(static_cast<size_t>(events[i].data.u64));
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007199 channels[i]->consumeMotionDown();
7200 }
7201 }
7202
7203 // Verify the order in which the events were received.
7204 EXPECT_EQ(3u, eventOrder.size());
7205 EXPECT_EQ(2u, eventOrder[0]); // index 2: window
7206 EXPECT_EQ(0u, eventOrder[1]); // index 0: spy1
7207 EXPECT_EQ(1u, eventOrder[2]); // index 1: spy2
7208}
7209
7210/**
7211 * A spy window using the NOT_TOUCHABLE flag does not receive events.
7212 */
7213TEST_F(InputDispatcherSpyWindowTest, NotTouchable) {
7214 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007215 auto spy = createSpy();
7216 spy->setTouchable(false);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007217 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
7218
7219 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7220 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
7221 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7222 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7223 spy->assertNoEvents();
7224}
7225
7226/**
7227 * A spy window will only receive gestures that originate within its touchable region. Gestures that
7228 * have their ACTION_DOWN outside of the touchable region of the spy window will not be dispatched
7229 * to the window.
7230 */
7231TEST_F(InputDispatcherSpyWindowTest, TouchableRegion) {
7232 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007233 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007234 spy->setTouchableRegion(Region{{0, 0, 20, 20}});
7235 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
7236
7237 // Inject an event outside the spy window's touchable region.
7238 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7239 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
7240 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7241 window->consumeMotionDown();
7242 spy->assertNoEvents();
7243 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7244 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
7245 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7246 window->consumeMotionUp();
7247 spy->assertNoEvents();
7248
7249 // Inject an event inside the spy window's touchable region.
7250 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7251 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7252 {5, 10}))
7253 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7254 window->consumeMotionDown();
7255 spy->consumeMotionDown();
7256}
7257
7258/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007259 * A spy window can listen for touches outside its touchable region using the WATCH_OUTSIDE_TOUCHES
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08007260 * flag, but it will get zero-ed out coordinates if the foreground has a different owner.
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007261 */
7262TEST_F(InputDispatcherSpyWindowTest, WatchOutsideTouches) {
7263 auto window = createForeground();
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08007264 window->setOwnerInfo(12, 34);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007265 auto spy = createSpy();
7266 spy->setWatchOutsideTouch(true);
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08007267 spy->setOwnerInfo(56, 78);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007268 spy->setFrame(Rect{0, 0, 20, 20});
7269 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
7270
7271 // Inject an event outside the spy window's frame and touchable region.
7272 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08007273 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7274 {100, 200}))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007275 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7276 window->consumeMotionDown();
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08007277 spy->consumeMotionOutsideWithZeroedCoords();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007278}
7279
7280/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007281 * Even when a spy window spans over multiple foreground windows, the spy should receive all
7282 * pointers that are down within its bounds.
7283 */
7284TEST_F(InputDispatcherSpyWindowTest, ReceivesMultiplePointers) {
7285 auto windowLeft = createForeground();
7286 windowLeft->setFrame({0, 0, 100, 200});
7287 auto windowRight = createForeground();
7288 windowRight->setFrame({100, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007289 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007290 spy->setFrame({0, 0, 200, 200});
7291 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, windowLeft, windowRight}}});
7292
7293 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7294 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7295 {50, 50}))
7296 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7297 windowLeft->consumeMotionDown();
7298 spy->consumeMotionDown();
7299
7300 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08007301 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007302 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
7303 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
7304 .pointer(
7305 PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50))
7306 .build();
7307 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7308 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
7309 InputEventInjectionSync::WAIT_FOR_RESULT))
7310 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7311 windowRight->consumeMotionDown();
7312 spy->consumeMotionPointerDown(1 /*pointerIndex*/);
7313}
7314
7315/**
7316 * When the first pointer lands outside the spy window and the second pointer lands inside it, the
7317 * the spy should receive the second pointer with ACTION_DOWN.
7318 */
7319TEST_F(InputDispatcherSpyWindowTest, ReceivesSecondPointerAsDown) {
7320 auto window = createForeground();
7321 window->setFrame({0, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007322 auto spyRight = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007323 spyRight->setFrame({100, 0, 200, 200});
7324 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyRight, window}}});
7325
7326 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7327 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7328 {50, 50}))
7329 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7330 window->consumeMotionDown();
7331 spyRight->assertNoEvents();
7332
7333 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08007334 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007335 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
7336 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
7337 .pointer(
7338 PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50))
7339 .build();
7340 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7341 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
7342 InputEventInjectionSync::WAIT_FOR_RESULT))
7343 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7344 window->consumeMotionPointerDown(1 /*pointerIndex*/);
7345 spyRight->consumeMotionDown();
7346}
7347
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007348/**
7349 * The spy window should not be able to affect whether or not touches are split. Only the foreground
7350 * windows should be allowed to control split touch.
7351 */
7352TEST_F(InputDispatcherSpyWindowTest, SplitIfNoForegroundWindowTouched) {
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08007353 // This spy window prevents touch splitting. However, we still expect to split touches
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007354 // because a foreground window has not disabled splitting.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007355 auto spy = createSpy();
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08007356 spy->setPreventSplitting(true);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007357
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007358 auto window = createForeground();
7359 window->setFrame(Rect(0, 0, 100, 100));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007360
7361 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
7362
7363 // First finger down, no window touched.
7364 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7365 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7366 {100, 200}))
7367 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7368 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7369 window->assertNoEvents();
7370
7371 // Second finger down on window, the window should receive touch down.
7372 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08007373 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007374 .displayId(ADISPLAY_ID_DEFAULT)
7375 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
7376 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
7377 .x(100)
7378 .y(200))
7379 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
7380 .build();
7381 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7382 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
7383 InputEventInjectionSync::WAIT_FOR_RESULT))
7384 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7385
7386 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7387 spy->consumeMotionPointerDown(1 /* pointerIndex */);
7388}
7389
7390/**
7391 * A spy window will usually be implemented as an un-focusable window. Verify that these windows
7392 * do not receive key events.
7393 */
7394TEST_F(InputDispatcherSpyWindowTest, UnfocusableSpyDoesNotReceiveKeyEvents) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007395 auto spy = createSpy();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007396 spy->setFocusable(false);
7397
7398 auto window = createForeground();
7399 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
7400 setFocusedWindow(window);
7401 window->consumeFocusEvent(true);
7402
7403 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7404 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
7405 window->consumeKeyDown(ADISPLAY_ID_NONE);
7406
7407 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7408 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
7409 window->consumeKeyUp(ADISPLAY_ID_NONE);
7410
7411 spy->assertNoEvents();
7412}
7413
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00007414using InputDispatcherPilferPointersTest = InputDispatcherSpyWindowTest;
7415
7416/**
7417 * A spy window can pilfer pointers. When this happens, touch gestures used by the spy window that
7418 * are currently sent to any other windows - including other spy windows - will also be cancelled.
7419 */
7420TEST_F(InputDispatcherPilferPointersTest, PilferPointers) {
7421 auto window = createForeground();
7422 auto spy1 = createSpy();
7423 auto spy2 = createSpy();
7424 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window}}});
7425
7426 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7427 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
7428 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7429 window->consumeMotionDown();
7430 spy1->consumeMotionDown();
7431 spy2->consumeMotionDown();
7432
7433 // Pilfer pointers from the second spy window.
7434 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy2->getToken()));
7435 spy2->assertNoEvents();
7436 spy1->consumeMotionCancel();
7437 window->consumeMotionCancel();
7438
7439 // The rest of the gesture should only be sent to the second spy window.
7440 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7441 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
7442 ADISPLAY_ID_DEFAULT))
7443 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7444 spy2->consumeMotionMove();
7445 spy1->assertNoEvents();
7446 window->assertNoEvents();
7447}
7448
7449/**
7450 * A spy window can pilfer pointers for a gesture even after the foreground window has been removed
7451 * in the middle of the gesture.
7452 */
7453TEST_F(InputDispatcherPilferPointersTest, CanPilferAfterWindowIsRemovedMidStream) {
7454 auto window = createForeground();
7455 auto spy = createSpy();
7456 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
7457
7458 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7459 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
7460 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7461 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7462 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7463
7464 window->releaseChannel();
7465
7466 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
7467
7468 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7469 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
7470 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7471 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
7472}
7473
7474/**
7475 * After a spy window pilfers pointers, new pointers that go down in its bounds should be sent to
7476 * the spy, but not to any other windows.
7477 */
7478TEST_F(InputDispatcherPilferPointersTest, ContinuesToReceiveGestureAfterPilfer) {
7479 auto spy = createSpy();
7480 auto window = createForeground();
7481
7482 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
7483
7484 // First finger down on the window and the spy.
7485 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7486 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7487 {100, 200}))
7488 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7489 spy->consumeMotionDown();
7490 window->consumeMotionDown();
7491
7492 // Spy window pilfers the pointers.
7493 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
7494 window->consumeMotionCancel();
7495
7496 // Second finger down on the window and spy, but the window should not receive the pointer down.
7497 const MotionEvent secondFingerDownEvent =
7498 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7499 .displayId(ADISPLAY_ID_DEFAULT)
7500 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
7501 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
7502 .x(100)
7503 .y(200))
7504 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
7505 .build();
7506 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7507 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
7508 InputEventInjectionSync::WAIT_FOR_RESULT))
7509 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7510
7511 spy->consumeMotionPointerDown(1 /*pointerIndex*/);
7512
7513 // Third finger goes down outside all windows, so injection should fail.
7514 const MotionEvent thirdFingerDownEvent =
7515 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7516 .displayId(ADISPLAY_ID_DEFAULT)
7517 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
7518 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
7519 .x(100)
7520 .y(200))
7521 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
7522 .pointer(PointerBuilder(/* id */ 2, AMOTION_EVENT_TOOL_TYPE_FINGER).x(-5).y(-5))
7523 .build();
7524 ASSERT_EQ(InputEventInjectionResult::FAILED,
7525 injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
7526 InputEventInjectionSync::WAIT_FOR_RESULT))
7527 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7528
7529 spy->assertNoEvents();
7530 window->assertNoEvents();
7531}
7532
7533/**
7534 * After a spy window pilfers pointers, only the pointers used by the spy should be canceled
7535 */
7536TEST_F(InputDispatcherPilferPointersTest, PartiallyPilferRequiredPointers) {
7537 auto spy = createSpy();
7538 spy->setFrame(Rect(0, 0, 100, 100));
7539 auto window = createForeground();
7540 window->setFrame(Rect(0, 0, 200, 200));
7541
7542 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
7543
7544 // First finger down on the window only
7545 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7546 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7547 {150, 150}))
7548 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7549 window->consumeMotionDown();
7550
7551 // Second finger down on the spy and window
7552 const MotionEvent secondFingerDownEvent =
7553 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7554 .displayId(ADISPLAY_ID_DEFAULT)
7555 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
7556 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
7557 .x(150)
7558 .y(150))
7559 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(10).y(10))
7560 .build();
7561 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7562 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
7563 InputEventInjectionSync::WAIT_FOR_RESULT))
7564 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7565 spy->consumeMotionDown();
7566 window->consumeMotionPointerDown(1);
7567
7568 // Third finger down on the spy and window
7569 const MotionEvent thirdFingerDownEvent =
7570 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7571 .displayId(ADISPLAY_ID_DEFAULT)
7572 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
7573 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
7574 .x(150)
7575 .y(150))
7576 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(10).y(10))
7577 .pointer(PointerBuilder(/* id */ 2, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
7578 .build();
7579 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7580 injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
7581 InputEventInjectionSync::WAIT_FOR_RESULT))
7582 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7583 spy->consumeMotionPointerDown(1);
7584 window->consumeMotionPointerDown(2);
7585
7586 // Spy window pilfers the pointers.
7587 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
7588 window->consumeMotionPointerUp(/* idx */ 2, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
7589 window->consumeMotionPointerUp(/* idx */ 1, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
7590
7591 spy->assertNoEvents();
7592 window->assertNoEvents();
7593}
7594
7595/**
7596 * After a spy window pilfers pointers, all pilfered pointers that have already been dispatched to
7597 * other windows should be canceled. If this results in the cancellation of all pointers for some
7598 * window, then that window should receive ACTION_CANCEL.
7599 */
7600TEST_F(InputDispatcherPilferPointersTest, PilferAllRequiredPointers) {
7601 auto spy = createSpy();
7602 spy->setFrame(Rect(0, 0, 100, 100));
7603 auto window = createForeground();
7604 window->setFrame(Rect(0, 0, 200, 200));
7605
7606 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
7607
7608 // First finger down on both spy and window
7609 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7610 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7611 {10, 10}))
7612 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7613 window->consumeMotionDown();
7614 spy->consumeMotionDown();
7615
7616 // Second finger down on the spy and window
7617 const MotionEvent secondFingerDownEvent =
7618 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7619 .displayId(ADISPLAY_ID_DEFAULT)
7620 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
7621 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(10).y(10))
7622 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
7623 .build();
7624 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7625 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
7626 InputEventInjectionSync::WAIT_FOR_RESULT))
7627 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7628 spy->consumeMotionPointerDown(1);
7629 window->consumeMotionPointerDown(1);
7630
7631 // Spy window pilfers the pointers.
7632 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
7633 window->consumeMotionCancel();
7634
7635 spy->assertNoEvents();
7636 window->assertNoEvents();
7637}
7638
7639/**
7640 * After a spy window pilfers pointers, new pointers that are not touching the spy window can still
7641 * be sent to other windows
7642 */
7643TEST_F(InputDispatcherPilferPointersTest, CanReceivePointersAfterPilfer) {
7644 auto spy = createSpy();
7645 spy->setFrame(Rect(0, 0, 100, 100));
7646 auto window = createForeground();
7647 window->setFrame(Rect(0, 0, 200, 200));
7648
7649 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
7650
7651 // First finger down on both window and spy
7652 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7653 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7654 {10, 10}))
7655 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7656 window->consumeMotionDown();
7657 spy->consumeMotionDown();
7658
7659 // Spy window pilfers the pointers.
7660 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
7661 window->consumeMotionCancel();
7662
7663 // Second finger down on the window only
7664 const MotionEvent secondFingerDownEvent =
7665 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7666 .displayId(ADISPLAY_ID_DEFAULT)
7667 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
7668 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(10).y(10))
7669 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER)
7670 .x(150)
7671 .y(150))
7672 .build();
7673 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7674 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
7675 InputEventInjectionSync::WAIT_FOR_RESULT))
7676 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7677 window->consumeMotionDown();
7678 window->assertNoEvents();
7679
7680 // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line
7681 spy->consumeMotionMove();
7682 spy->assertNoEvents();
7683}
7684
Prabir Pradhand65552b2021-10-07 11:23:50 -07007685class InputDispatcherStylusInterceptorTest : public InputDispatcherTest {
7686public:
7687 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupStylusOverlayScenario() {
7688 std::shared_ptr<FakeApplicationHandle> overlayApplication =
7689 std::make_shared<FakeApplicationHandle>();
7690 sp<FakeWindowHandle> overlay =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007691 sp<FakeWindowHandle>::make(overlayApplication, mDispatcher,
7692 "Stylus interceptor window", ADISPLAY_ID_DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -07007693 overlay->setFocusable(false);
7694 overlay->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007695 overlay->setTouchable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007696 overlay->setInterceptsStylus(true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07007697 overlay->setTrustedOverlay(true);
7698
7699 std::shared_ptr<FakeApplicationHandle> application =
7700 std::make_shared<FakeApplicationHandle>();
7701 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007702 sp<FakeWindowHandle>::make(application, mDispatcher, "Application window",
7703 ADISPLAY_ID_DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -07007704 window->setFocusable(true);
7705 window->setOwnerInfo(222, 222);
Prabir Pradhand65552b2021-10-07 11:23:50 -07007706
7707 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
7708 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
7709 setFocusedWindow(window);
7710 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
7711 return {std::move(overlay), std::move(window)};
7712 }
7713
7714 void sendFingerEvent(int32_t action) {
7715 NotifyMotionArgs motionArgs =
7716 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
7717 ADISPLAY_ID_DEFAULT, {PointF{20, 20}});
7718 mDispatcher->notifyMotion(&motionArgs);
7719 }
7720
7721 void sendStylusEvent(int32_t action) {
7722 NotifyMotionArgs motionArgs =
7723 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
7724 ADISPLAY_ID_DEFAULT, {PointF{30, 40}});
7725 motionArgs.pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS;
7726 mDispatcher->notifyMotion(&motionArgs);
7727 }
7728};
7729
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08007730using InputDispatcherStylusInterceptorDeathTest = InputDispatcherStylusInterceptorTest;
7731
7732TEST_F(InputDispatcherStylusInterceptorDeathTest, UntrustedOverlay_AbortsDispatcher) {
7733 ScopedSilentDeath _silentDeath;
7734
Prabir Pradhand65552b2021-10-07 11:23:50 -07007735 auto [overlay, window] = setupStylusOverlayScenario();
7736 overlay->setTrustedOverlay(false);
7737 // Configuring an untrusted overlay as a stylus interceptor should cause Dispatcher to abort.
7738 ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}),
7739 ".* not a trusted overlay");
7740}
7741
7742TEST_F(InputDispatcherStylusInterceptorTest, ConsmesOnlyStylusEvents) {
7743 auto [overlay, window] = setupStylusOverlayScenario();
7744 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
7745
7746 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
7747 overlay->consumeMotionDown();
7748 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
7749 overlay->consumeMotionUp();
7750
7751 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
7752 window->consumeMotionDown();
7753 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
7754 window->consumeMotionUp();
7755
7756 overlay->assertNoEvents();
7757 window->assertNoEvents();
7758}
7759
7760TEST_F(InputDispatcherStylusInterceptorTest, SpyWindowStylusInterceptor) {
7761 auto [overlay, window] = setupStylusOverlayScenario();
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007762 overlay->setSpy(true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07007763 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
7764
7765 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
7766 overlay->consumeMotionDown();
7767 window->consumeMotionDown();
7768 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
7769 overlay->consumeMotionUp();
7770 window->consumeMotionUp();
7771
7772 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
7773 window->consumeMotionDown();
7774 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
7775 window->consumeMotionUp();
7776
7777 overlay->assertNoEvents();
7778 window->assertNoEvents();
7779}
7780
Prabir Pradhan6dfbf262022-03-14 15:24:30 +00007781/**
7782 * Set up a scenario to test the behavior used by the stylus handwriting detection feature.
7783 * The scenario is as follows:
7784 * - The stylus interceptor overlay is configured as a spy window.
7785 * - The stylus interceptor spy receives the start of a new stylus gesture.
7786 * - It pilfers pointers and then configures itself to no longer be a spy.
7787 * - The stylus interceptor continues to receive the rest of the gesture.
7788 */
7789TEST_F(InputDispatcherStylusInterceptorTest, StylusHandwritingScenario) {
7790 auto [overlay, window] = setupStylusOverlayScenario();
7791 overlay->setSpy(true);
7792 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
7793
7794 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
7795 overlay->consumeMotionDown();
7796 window->consumeMotionDown();
7797
7798 // The interceptor pilfers the pointers.
7799 EXPECT_EQ(OK, mDispatcher->pilferPointers(overlay->getToken()));
7800 window->consumeMotionCancel();
7801
7802 // The interceptor configures itself so that it is no longer a spy.
7803 overlay->setSpy(false);
7804 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
7805
7806 // It continues to receive the rest of the stylus gesture.
7807 sendStylusEvent(AMOTION_EVENT_ACTION_MOVE);
7808 overlay->consumeMotionMove();
7809 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
7810 overlay->consumeMotionUp();
7811
7812 window->assertNoEvents();
7813}
7814
Prabir Pradhan5735a322022-04-11 17:23:34 +00007815struct User {
7816 int32_t mPid;
7817 int32_t mUid;
7818 uint32_t mPolicyFlags{DEFAULT_POLICY_FLAGS};
7819 std::unique_ptr<InputDispatcher>& mDispatcher;
7820
7821 User(std::unique_ptr<InputDispatcher>& dispatcher, int32_t pid, int32_t uid)
7822 : mPid(pid), mUid(uid), mDispatcher(dispatcher) {}
7823
7824 InputEventInjectionResult injectTargetedMotion(int32_t action) const {
7825 return injectMotionEvent(mDispatcher, action, AINPUT_SOURCE_TOUCHSCREEN,
7826 ADISPLAY_ID_DEFAULT, {100, 200},
7827 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
7828 AMOTION_EVENT_INVALID_CURSOR_POSITION},
7829 INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT,
7830 systemTime(SYSTEM_TIME_MONOTONIC), {mUid}, mPolicyFlags);
7831 }
7832
7833 InputEventInjectionResult injectTargetedKey(int32_t action) const {
7834 return inputdispatcher::injectKey(mDispatcher, action, 0 /* repeatCount*/, ADISPLAY_ID_NONE,
7835 InputEventInjectionSync::WAIT_FOR_RESULT,
7836 INJECT_EVENT_TIMEOUT, false /*allowKeyRepeat*/, {mUid},
7837 mPolicyFlags);
7838 }
7839
7840 sp<FakeWindowHandle> createWindow() const {
7841 std::shared_ptr<FakeApplicationHandle> overlayApplication =
7842 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007843 sp<FakeWindowHandle> window =
7844 sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, "Owned Window",
7845 ADISPLAY_ID_DEFAULT);
Prabir Pradhan5735a322022-04-11 17:23:34 +00007846 window->setOwnerInfo(mPid, mUid);
7847 return window;
7848 }
7849};
7850
7851using InputDispatcherTargetedInjectionTest = InputDispatcherTest;
7852
7853TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedWindow) {
7854 auto owner = User(mDispatcher, 10, 11);
7855 auto window = owner.createWindow();
7856 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
7857
7858 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
7859 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
7860 window->consumeMotionDown();
7861
7862 setFocusedWindow(window);
7863 window->consumeFocusEvent(true);
7864
7865 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
7866 owner.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
7867 window->consumeKeyDown(ADISPLAY_ID_NONE);
7868}
7869
7870TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedWindow) {
7871 auto owner = User(mDispatcher, 10, 11);
7872 auto window = owner.createWindow();
7873 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
7874
7875 auto rando = User(mDispatcher, 20, 21);
7876 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
7877 rando.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
7878
7879 setFocusedWindow(window);
7880 window->consumeFocusEvent(true);
7881
7882 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
7883 rando.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
7884 window->assertNoEvents();
7885}
7886
7887TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedSpyWindow) {
7888 auto owner = User(mDispatcher, 10, 11);
7889 auto window = owner.createWindow();
7890 auto spy = owner.createWindow();
7891 spy->setSpy(true);
7892 spy->setTrustedOverlay(true);
7893 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
7894
7895 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
7896 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
7897 spy->consumeMotionDown();
7898 window->consumeMotionDown();
7899}
7900
7901TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedSpyWindow) {
7902 auto owner = User(mDispatcher, 10, 11);
7903 auto window = owner.createWindow();
7904
7905 auto rando = User(mDispatcher, 20, 21);
7906 auto randosSpy = rando.createWindow();
7907 randosSpy->setSpy(true);
7908 randosSpy->setTrustedOverlay(true);
7909 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}});
7910
7911 // The event is targeted at owner's window, so injection should succeed, but the spy should
7912 // not receive the event.
7913 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
7914 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
7915 randosSpy->assertNoEvents();
7916 window->consumeMotionDown();
7917}
7918
7919TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoAnyWindowWhenNotTargeting) {
7920 auto owner = User(mDispatcher, 10, 11);
7921 auto window = owner.createWindow();
7922
7923 auto rando = User(mDispatcher, 20, 21);
7924 auto randosSpy = rando.createWindow();
7925 randosSpy->setSpy(true);
7926 randosSpy->setTrustedOverlay(true);
7927 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}});
7928
7929 // A user that has injection permission can inject into any window.
7930 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
7931 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
7932 ADISPLAY_ID_DEFAULT));
7933 randosSpy->consumeMotionDown();
7934 window->consumeMotionDown();
7935
7936 setFocusedWindow(randosSpy);
7937 randosSpy->consumeFocusEvent(true);
7938
7939 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher));
7940 randosSpy->consumeKeyDown(ADISPLAY_ID_NONE);
7941 window->assertNoEvents();
7942}
7943
7944TEST_F(InputDispatcherTargetedInjectionTest, CanGenerateActionOutsideToOtherUids) {
7945 auto owner = User(mDispatcher, 10, 11);
7946 auto window = owner.createWindow();
7947
7948 auto rando = User(mDispatcher, 20, 21);
7949 auto randosWindow = rando.createWindow();
7950 randosWindow->setFrame(Rect{-10, -10, -5, -5});
7951 randosWindow->setWatchOutsideTouch(true);
7952 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosWindow, window}}});
7953
7954 // We allow generation of ACTION_OUTSIDE events into windows owned by different uids.
7955 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
7956 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
7957 window->consumeMotionDown();
7958 randosWindow->consumeMotionOutside();
7959}
7960
Garfield Tane84e6f92019-08-29 17:28:41 -07007961} // namespace android::inputdispatcher