blob: a5b5bb8cf87634057cccc0840f1932c7795cc627 [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();
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08001234 ASSERT_NE(nullptr, motionEvent) << "Did not get a motion event, but expected " << matcher;
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08001235 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()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002131 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2132 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE));
Garfield Tandf26e862020-07-01 20:18:19 -07002133
2134 // Move cursor into left window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002135 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002136 injectMotionEvent(mDispatcher,
2137 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2138 AINPUT_SOURCE_MOUSE)
2139 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2140 .x(300)
2141 .y(400))
2142 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002143 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2144 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2145 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE));
Garfield Tandf26e862020-07-01 20:18:19 -07002146
2147 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002148 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002149 injectMotionEvent(mDispatcher,
2150 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2151 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2152 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2153 .x(300)
2154 .y(400))
2155 .build()));
2156 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2157
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002158 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002159 injectMotionEvent(mDispatcher,
2160 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
2161 AINPUT_SOURCE_MOUSE)
2162 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2163 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2164 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2165 .x(300)
2166 .y(400))
2167 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002168 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
Garfield Tandf26e862020-07-01 20:18:19 -07002169
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002170 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002171 injectMotionEvent(mDispatcher,
2172 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
2173 AINPUT_SOURCE_MOUSE)
2174 .buttonState(0)
2175 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2176 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2177 .x(300)
2178 .y(400))
2179 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002180 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE));
Garfield Tandf26e862020-07-01 20:18:19 -07002181
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002182 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002183 injectMotionEvent(mDispatcher,
2184 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
2185 .buttonState(0)
2186 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2187 .x(300)
2188 .y(400))
2189 .build()));
2190 windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT);
2191
2192 // Move mouse cursor back to right window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002193 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002194 injectMotionEvent(mDispatcher,
2195 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2196 AINPUT_SOURCE_MOUSE)
2197 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2198 .x(900)
2199 .y(400))
2200 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002201 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2202 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2203 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE));
2204
2205 // No more events
2206 windowLeft->assertNoEvents();
2207 windowRight->assertNoEvents();
2208}
2209
2210TEST_F(InputDispatcherTest, HoverWithSpyWindows) {
2211 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2212
2213 sp<FakeWindowHandle> spyWindow =
2214 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
2215 spyWindow->setFrame(Rect(0, 0, 600, 800));
2216 spyWindow->setTrustedOverlay(true);
2217 spyWindow->setSpy(true);
2218 sp<FakeWindowHandle> window =
2219 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2220 window->setFrame(Rect(0, 0, 600, 800));
2221
2222 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2223 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
2224
2225 // Send mouse cursor to the window
2226 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2227 injectMotionEvent(mDispatcher,
2228 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
2229 AINPUT_SOURCE_MOUSE)
2230 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2231 .x(100)
2232 .y(100))
2233 .build()));
2234
2235 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2236 WithSource(AINPUT_SOURCE_MOUSE)));
2237 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2238 WithSource(AINPUT_SOURCE_MOUSE)));
2239
2240 window->assertNoEvents();
2241 spyWindow->assertNoEvents();
Garfield Tandf26e862020-07-01 20:18:19 -07002242}
2243
2244// This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected
2245// directly in this test.
2246TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) {
Chris Yea209fde2020-07-22 13:54:51 -07002247 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07002248 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002249 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07002250 window->setFrame(Rect(0, 0, 1200, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07002251
2252 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2253
2254 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2255
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002256 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002257 injectMotionEvent(mDispatcher,
2258 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
2259 AINPUT_SOURCE_MOUSE)
2260 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2261 .x(300)
2262 .y(400))
2263 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002264 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07002265
2266 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002267 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002268 injectMotionEvent(mDispatcher,
2269 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2270 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2271 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2272 .x(300)
2273 .y(400))
2274 .build()));
2275 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2276
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002277 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002278 injectMotionEvent(mDispatcher,
2279 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
2280 AINPUT_SOURCE_MOUSE)
2281 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2282 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2283 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2284 .x(300)
2285 .y(400))
2286 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002287 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
Garfield Tandf26e862020-07-01 20:18:19 -07002288
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002289 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002290 injectMotionEvent(mDispatcher,
2291 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
2292 AINPUT_SOURCE_MOUSE)
2293 .buttonState(0)
2294 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2295 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2296 .x(300)
2297 .y(400))
2298 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002299 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE));
Garfield Tandf26e862020-07-01 20:18:19 -07002300
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002301 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002302 injectMotionEvent(mDispatcher,
2303 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
2304 .buttonState(0)
2305 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2306 .x(300)
2307 .y(400))
2308 .build()));
2309 window->consumeMotionUp(ADISPLAY_ID_DEFAULT);
2310
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002311 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002312 injectMotionEvent(mDispatcher,
2313 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT,
2314 AINPUT_SOURCE_MOUSE)
2315 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2316 .x(300)
2317 .y(400))
2318 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002319 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
Garfield Tandf26e862020-07-01 20:18:19 -07002320}
2321
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08002322/**
2323 * Inject a mouse hover event followed by a tap from touchscreen.
2324 * In the current implementation, the tap does not cause a HOVER_EXIT event.
2325 */
2326TEST_F(InputDispatcherTest, MouseHoverAndTouchTap) {
2327 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2328 sp<FakeWindowHandle> window =
2329 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2330 window->setFrame(Rect(0, 0, 100, 100));
2331
2332 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2333
2334 // Inject a hover_move from mouse.
2335 NotifyMotionArgs motionArgs =
2336 generateMotionArgs(AMOTION_EVENT_ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE,
2337 ADISPLAY_ID_DEFAULT, {{50, 50}});
2338 motionArgs.xCursorPosition = 50;
2339 motionArgs.yCursorPosition = 50;
2340 mDispatcher->notifyMotion(&motionArgs);
2341 ASSERT_NO_FATAL_FAILURE(
2342 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2343 WithSource(AINPUT_SOURCE_MOUSE))));
2344 ASSERT_NO_FATAL_FAILURE(
2345 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2346 WithSource(AINPUT_SOURCE_MOUSE))));
2347
2348 // Tap on the window
2349 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2350 ADISPLAY_ID_DEFAULT, {{10, 10}});
2351 mDispatcher->notifyMotion(&motionArgs);
2352 ASSERT_NO_FATAL_FAILURE(
2353 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2354 WithSource(AINPUT_SOURCE_TOUCHSCREEN))));
2355
2356 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
2357 ADISPLAY_ID_DEFAULT, {{10, 10}});
2358 mDispatcher->notifyMotion(&motionArgs);
2359 ASSERT_NO_FATAL_FAILURE(
2360 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
2361 WithSource(AINPUT_SOURCE_TOUCHSCREEN))));
2362}
2363
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02002364TEST_F(InputDispatcherTest, HoverEnterMoveRemoveWindowsInSecondDisplay) {
2365 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2366 sp<FakeWindowHandle> windowDefaultDisplay =
2367 sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay",
2368 ADISPLAY_ID_DEFAULT);
2369 windowDefaultDisplay->setFrame(Rect(0, 0, 600, 800));
2370 sp<FakeWindowHandle> windowSecondDisplay =
2371 sp<FakeWindowHandle>::make(application, mDispatcher, "SecondDisplay",
2372 SECOND_DISPLAY_ID);
2373 windowSecondDisplay->setFrame(Rect(0, 0, 600, 800));
2374
2375 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}},
2376 {SECOND_DISPLAY_ID, {windowSecondDisplay}}});
2377
2378 // Set cursor position in window in default display and check that hover enter and move
2379 // events are generated.
2380 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2381 injectMotionEvent(mDispatcher,
2382 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2383 AINPUT_SOURCE_MOUSE)
2384 .displayId(ADISPLAY_ID_DEFAULT)
2385 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2386 .x(300)
2387 .y(600))
2388 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002389 windowDefaultDisplay->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2390 windowDefaultDisplay->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE));
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02002391
2392 // Remove all windows in secondary display and check that no event happens on window in
2393 // primary display.
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002394 mDispatcher->setInputWindows(
2395 {{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}}, {SECOND_DISPLAY_ID, {}}});
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02002396 windowDefaultDisplay->assertNoEvents();
2397
2398 // Move cursor position in window in default display and check that only hover move
2399 // event is generated and not hover enter event.
2400 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}},
2401 {SECOND_DISPLAY_ID, {windowSecondDisplay}}});
2402 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2403 injectMotionEvent(mDispatcher,
2404 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2405 AINPUT_SOURCE_MOUSE)
2406 .displayId(ADISPLAY_ID_DEFAULT)
2407 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2408 .x(400)
2409 .y(700))
2410 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002411 windowDefaultDisplay->consumeMotionEvent(
2412 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2413 WithSource(AINPUT_SOURCE_MOUSE)));
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02002414 windowDefaultDisplay->assertNoEvents();
2415}
2416
Garfield Tan00f511d2019-06-12 16:55:40 -07002417TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) {
Chris Yea209fde2020-07-22 13:54:51 -07002418 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tan00f511d2019-06-12 16:55:40 -07002419
2420 sp<FakeWindowHandle> windowLeft =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002421 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07002422 windowLeft->setFrame(Rect(0, 0, 600, 800));
Garfield Tan00f511d2019-06-12 16:55:40 -07002423 sp<FakeWindowHandle> windowRight =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002424 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07002425 windowRight->setFrame(Rect(600, 0, 1200, 800));
Garfield Tan00f511d2019-06-12 16:55:40 -07002426
2427 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2428
Arthur Hung72d8dc32020-03-28 00:48:39 +00002429 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
Garfield Tan00f511d2019-06-12 16:55:40 -07002430
2431 // Inject an event with coordinate in the area of right window, with mouse cursor in the area of
2432 // left window. This event should be dispatched to the left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002433 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tan00f511d2019-06-12 16:55:40 -07002434 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07002435 ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400}));
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002436 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07002437 windowRight->assertNoEvents();
2438}
2439
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002440TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) {
Chris Yea209fde2020-07-22 13:54:51 -07002441 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002442 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
2443 "Fake Window", ADISPLAY_ID_DEFAULT);
Vishnu Nair47074b82020-08-14 11:54:47 -07002444 window->setFocusable(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002445
Arthur Hung72d8dc32020-03-28 00:48:39 +00002446 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002447 setFocusedWindow(window);
2448
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002449 window->consumeFocusEvent(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002450
2451 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
2452 mDispatcher->notifyKey(&keyArgs);
2453
2454 // Window should receive key down event.
2455 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2456
2457 // When device reset happens, that key stream should be terminated with FLAG_CANCELED
2458 // on the app side.
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002459 NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002460 mDispatcher->notifyDeviceReset(&args);
2461 window->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
2462 AKEY_EVENT_FLAG_CANCELED);
2463}
2464
2465TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) {
Chris Yea209fde2020-07-22 13:54:51 -07002466 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002467 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
2468 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002469
Arthur Hung72d8dc32020-03-28 00:48:39 +00002470 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002471
2472 NotifyMotionArgs motionArgs =
2473 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2474 ADISPLAY_ID_DEFAULT);
2475 mDispatcher->notifyMotion(&motionArgs);
2476
2477 // Window should receive motion down event.
2478 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2479
2480 // When device reset happens, that motion stream should be terminated with ACTION_CANCEL
2481 // on the app side.
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002482 NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002483 mDispatcher->notifyDeviceReset(&args);
2484 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, ADISPLAY_ID_DEFAULT,
2485 0 /*expectedFlags*/);
2486}
2487
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08002488TEST_F(InputDispatcherTest, InterceptKeyByPolicy) {
2489 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002490 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
2491 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08002492 window->setFocusable(true);
2493
2494 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2495 setFocusedWindow(window);
2496
2497 window->consumeFocusEvent(true);
2498
2499 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
2500 const std::chrono::milliseconds interceptKeyTimeout = 50ms;
2501 const nsecs_t injectTime = keyArgs.eventTime;
2502 mFakePolicy->setInterceptKeyTimeout(interceptKeyTimeout);
2503 mDispatcher->notifyKey(&keyArgs);
2504 // The dispatching time should be always greater than or equal to intercept key timeout.
2505 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2506 ASSERT_TRUE((systemTime(SYSTEM_TIME_MONOTONIC) - injectTime) >=
2507 std::chrono::nanoseconds(interceptKeyTimeout).count());
2508}
2509
2510TEST_F(InputDispatcherTest, InterceptKeyIfKeyUp) {
2511 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002512 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
2513 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08002514 window->setFocusable(true);
2515
2516 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2517 setFocusedWindow(window);
2518
2519 window->consumeFocusEvent(true);
2520
2521 NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
2522 NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
2523 mFakePolicy->setInterceptKeyTimeout(150ms);
2524 mDispatcher->notifyKey(&keyDown);
2525 mDispatcher->notifyKey(&keyUp);
2526
2527 // Window should receive key event immediately when same key up.
2528 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2529 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
2530}
2531
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07002532/**
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00002533 * This test documents the behavior of WATCH_OUTSIDE_TOUCH. The window will get ACTION_OUTSIDE when
2534 * a another pointer causes ACTION_DOWN to be sent to another window for the first time. Only one
2535 * ACTION_OUTSIDE event is sent per gesture.
2536 */
2537TEST_F(InputDispatcherTest, ActionOutsideSentOnlyWhenAWindowIsTouched) {
2538 // There are three windows that do not overlap. `window` wants to WATCH_OUTSIDE_TOUCH.
2539 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002540 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
2541 "First Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00002542 window->setWatchOutsideTouch(true);
2543 window->setFrame(Rect{0, 0, 100, 100});
2544 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002545 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
2546 ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00002547 secondWindow->setFrame(Rect{100, 100, 200, 200});
2548 sp<FakeWindowHandle> thirdWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002549 sp<FakeWindowHandle>::make(application, mDispatcher, "Third Window",
2550 ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00002551 thirdWindow->setFrame(Rect{200, 200, 300, 300});
2552 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, secondWindow, thirdWindow}}});
2553
2554 // First pointer lands outside all windows. `window` does not get ACTION_OUTSIDE.
2555 NotifyMotionArgs motionArgs =
2556 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2557 ADISPLAY_ID_DEFAULT, {PointF{-10, -10}});
2558 mDispatcher->notifyMotion(&motionArgs);
2559 window->assertNoEvents();
2560 secondWindow->assertNoEvents();
2561
2562 // The second pointer lands inside `secondWindow`, which should receive a DOWN event.
2563 // Now, `window` should get ACTION_OUTSIDE.
2564 motionArgs = generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2565 {PointF{-10, -10}, PointF{105, 105}});
2566 mDispatcher->notifyMotion(&motionArgs);
2567 window->consumeMotionOutside();
2568 secondWindow->consumeMotionDown();
2569 thirdWindow->assertNoEvents();
2570
2571 // The third pointer lands inside `thirdWindow`, which should receive a DOWN event. There is
2572 // no ACTION_OUTSIDE sent to `window` because one has already been sent for this gesture.
2573 motionArgs = generateMotionArgs(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2574 {PointF{-10, -10}, PointF{105, 105}, PointF{205, 205}});
2575 mDispatcher->notifyMotion(&motionArgs);
2576 window->assertNoEvents();
2577 secondWindow->consumeMotionMove();
2578 thirdWindow->consumeMotionDown();
2579}
2580
Prabir Pradhan814fe082022-07-22 20:22:18 +00002581TEST_F(InputDispatcherTest, OnWindowInfosChanged_RemoveAllWindowsOnDisplay) {
2582 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002583 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
2584 "Fake Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan814fe082022-07-22 20:22:18 +00002585 window->setFocusable(true);
2586
2587 mDispatcher->onWindowInfosChanged({*window->getInfo()}, {});
2588 setFocusedWindow(window);
2589
2590 window->consumeFocusEvent(true);
2591
2592 NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
2593 NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
2594 mDispatcher->notifyKey(&keyDown);
2595 mDispatcher->notifyKey(&keyUp);
2596
2597 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2598 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
2599
2600 // All windows are removed from the display. Ensure that we can no longer dispatch to it.
2601 mDispatcher->onWindowInfosChanged({}, {});
2602
2603 window->consumeFocusEvent(false);
2604
2605 mDispatcher->notifyKey(&keyDown);
2606 mDispatcher->notifyKey(&keyUp);
2607 window->assertNoEvents();
2608}
2609
Arthur Hung96483742022-11-15 03:30:48 +00002610TEST_F(InputDispatcherTest, NonSplitTouchableWindowReceivesMultiTouch) {
2611 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2612 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
2613 "Fake Window", ADISPLAY_ID_DEFAULT);
2614 // Ensure window is non-split and have some transform.
2615 window->setPreventSplitting(true);
2616 window->setWindowOffset(20, 40);
2617 mDispatcher->onWindowInfosChanged({*window->getInfo()}, {});
2618
2619 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2620 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2621 {50, 50}))
2622 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2623 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2624
2625 const MotionEvent secondFingerDownEvent =
2626 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2627 .displayId(ADISPLAY_ID_DEFAULT)
2628 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
2629 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
2630 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER)
2631 .x(-30)
2632 .y(-50))
2633 .build();
2634 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2635 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
2636 InputEventInjectionSync::WAIT_FOR_RESULT))
2637 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2638
2639 const MotionEvent* event = window->consumeMotion();
2640 EXPECT_EQ(POINTER_1_DOWN, event->getAction());
2641 EXPECT_EQ(70, event->getX(0)); // 50 + 20
2642 EXPECT_EQ(90, event->getY(0)); // 50 + 40
2643 EXPECT_EQ(-10, event->getX(1)); // -30 + 20
2644 EXPECT_EQ(-10, event->getY(1)); // -50 + 40
2645}
2646
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00002647/**
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07002648 * Ensure the correct coordinate spaces are used by InputDispatcher.
2649 *
2650 * InputDispatcher works in the display space, so its coordinate system is relative to the display
2651 * panel. Windows get events in the window space, and get raw coordinates in the logical display
2652 * space.
2653 */
2654class InputDispatcherDisplayProjectionTest : public InputDispatcherTest {
2655public:
2656 void SetUp() override {
2657 InputDispatcherTest::SetUp();
2658 mDisplayInfos.clear();
2659 mWindowInfos.clear();
2660 }
2661
2662 void addDisplayInfo(int displayId, const ui::Transform& transform) {
2663 gui::DisplayInfo info;
2664 info.displayId = displayId;
2665 info.transform = transform;
2666 mDisplayInfos.push_back(std::move(info));
2667 mDispatcher->onWindowInfosChanged(mWindowInfos, mDisplayInfos);
2668 }
2669
2670 void addWindow(const sp<WindowInfoHandle>& windowHandle) {
2671 mWindowInfos.push_back(*windowHandle->getInfo());
2672 mDispatcher->onWindowInfosChanged(mWindowInfos, mDisplayInfos);
2673 }
2674
2675 // Set up a test scenario where the display has a scaled projection and there are two windows
2676 // on the display.
2677 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupScaledDisplayScenario() {
2678 // The display has a projection that has a scale factor of 2 and 4 in the x and y directions
2679 // respectively.
2680 ui::Transform displayTransform;
2681 displayTransform.set(2, 0, 0, 4);
2682 addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform);
2683
2684 std::shared_ptr<FakeApplicationHandle> application =
2685 std::make_shared<FakeApplicationHandle>();
2686
2687 // Add two windows to the display. Their frames are represented in the display space.
2688 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002689 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
2690 ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07002691 firstWindow->setFrame(Rect(0, 0, 100, 200), displayTransform);
2692 addWindow(firstWindow);
2693
2694 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002695 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
2696 ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07002697 secondWindow->setFrame(Rect(100, 200, 200, 400), displayTransform);
2698 addWindow(secondWindow);
2699 return {std::move(firstWindow), std::move(secondWindow)};
2700 }
2701
2702private:
2703 std::vector<gui::DisplayInfo> mDisplayInfos;
2704 std::vector<gui::WindowInfo> mWindowInfos;
2705};
2706
2707TEST_F(InputDispatcherDisplayProjectionTest, HitTestsInDisplaySpace) {
2708 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
2709 // Send down to the first window. The point is represented in the display space. The point is
2710 // selected so that if the hit test was done with the transform applied to it, then it would
2711 // end up in the incorrect window.
2712 NotifyMotionArgs downMotionArgs =
2713 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2714 ADISPLAY_ID_DEFAULT, {PointF{75, 55}});
2715 mDispatcher->notifyMotion(&downMotionArgs);
2716
2717 firstWindow->consumeMotionDown();
2718 secondWindow->assertNoEvents();
2719}
2720
2721// Ensure that when a MotionEvent is injected through the InputDispatcher::injectInputEvent() API,
2722// the event should be treated as being in the logical display space.
2723TEST_F(InputDispatcherDisplayProjectionTest, InjectionInLogicalDisplaySpace) {
2724 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
2725 // Send down to the first window. The point is represented in the logical display space. The
2726 // point is selected so that if the hit test was done in logical display space, then it would
2727 // end up in the incorrect window.
2728 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2729 PointF{75 * 2, 55 * 4});
2730
2731 firstWindow->consumeMotionDown();
2732 secondWindow->assertNoEvents();
2733}
2734
Prabir Pradhandaa2f142021-12-10 09:30:08 +00002735// Ensure that when a MotionEvent that has a custom transform is injected, the post-transformed
2736// event should be treated as being in the logical display space.
2737TEST_F(InputDispatcherDisplayProjectionTest, InjectionWithTransformInLogicalDisplaySpace) {
2738 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
2739
2740 const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0};
2741 ui::Transform injectedEventTransform;
2742 injectedEventTransform.set(matrix);
2743 const vec2 expectedPoint{75, 55}; // The injected point in the logical display space.
2744 const vec2 untransformedPoint = injectedEventTransform.inverse().transform(expectedPoint);
2745
2746 MotionEvent event = MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2747 .displayId(ADISPLAY_ID_DEFAULT)
2748 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
2749 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
2750 .x(untransformedPoint.x)
2751 .y(untransformedPoint.y))
2752 .build();
2753 event.transform(matrix);
2754
2755 injectMotionEvent(mDispatcher, event, INJECT_EVENT_TIMEOUT,
2756 InputEventInjectionSync::WAIT_FOR_RESULT);
2757
2758 firstWindow->consumeMotionDown();
2759 secondWindow->assertNoEvents();
2760}
2761
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07002762TEST_F(InputDispatcherDisplayProjectionTest, WindowGetsEventsInCorrectCoordinateSpace) {
2763 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
2764
2765 // Send down to the second window.
2766 NotifyMotionArgs downMotionArgs =
2767 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2768 ADISPLAY_ID_DEFAULT, {PointF{150, 220}});
2769 mDispatcher->notifyMotion(&downMotionArgs);
2770
2771 firstWindow->assertNoEvents();
2772 const MotionEvent* event = secondWindow->consumeMotion();
2773 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, event->getAction());
2774
2775 // Ensure that the events from the "getRaw" API are in logical display coordinates.
2776 EXPECT_EQ(300, event->getRawX(0));
2777 EXPECT_EQ(880, event->getRawY(0));
2778
2779 // Ensure that the x and y values are in the window's coordinate space.
2780 // The left-top of the second window is at (100, 200) in display space, which is (200, 800) in
2781 // the logical display space. This will be the origin of the window space.
2782 EXPECT_EQ(100, event->getX(0));
2783 EXPECT_EQ(80, event->getY(0));
2784}
2785
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002786using TransferFunction = std::function<bool(const std::unique_ptr<InputDispatcher>& dispatcher,
2787 sp<IBinder>, sp<IBinder>)>;
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00002788
2789class TransferTouchFixture : public InputDispatcherTest,
2790 public ::testing::WithParamInterface<TransferFunction> {};
2791
2792TEST_P(TransferTouchFixture, TransferTouch_OnePointer) {
Chris Yea209fde2020-07-22 13:54:51 -07002793 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08002794
2795 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002796 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002797 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
2798 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002799 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002800 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
2801 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08002802
2803 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00002804 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08002805
2806 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002807 NotifyMotionArgs downMotionArgs =
2808 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2809 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08002810 mDispatcher->notifyMotion(&downMotionArgs);
2811 // Only the first window should get the down event
2812 firstWindow->consumeMotionDown();
2813 secondWindow->assertNoEvents();
2814
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00002815 // Transfer touch to the second window
2816 TransferFunction f = GetParam();
2817 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
2818 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08002819 // The first window gets cancel and the second gets down
2820 firstWindow->consumeMotionCancel();
2821 secondWindow->consumeMotionDown();
2822
2823 // Send up event to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002824 NotifyMotionArgs upMotionArgs =
2825 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
2826 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08002827 mDispatcher->notifyMotion(&upMotionArgs);
2828 // The first window gets no events and the second gets up
2829 firstWindow->assertNoEvents();
2830 secondWindow->consumeMotionUp();
2831}
2832
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07002833/**
2834 * When 'transferTouch' API is invoked, dispatcher needs to find the "best" window to take touch
2835 * from. When we have spy windows, there are several windows to choose from: either spy, or the
2836 * 'real' (non-spy) window. Always prefer the 'real' window because that's what would be most
2837 * natural to the user.
2838 * In this test, we are sending a pointer to both spy window and first window. We then try to
2839 * transfer touch to the second window. The dispatcher should identify the first window as the
2840 * one that should lose the gesture, and therefore the action should be to move the gesture from
2841 * the first window to the second.
2842 * The main goal here is to test the behaviour of 'transferTouch' API, but it's still valid to test
2843 * the other API, as well.
2844 */
2845TEST_P(TransferTouchFixture, TransferTouch_MultipleWindowsWithSpy) {
2846 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2847
2848 // Create a couple of windows + a spy window
2849 sp<FakeWindowHandle> spyWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002850 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07002851 spyWindow->setTrustedOverlay(true);
2852 spyWindow->setSpy(true);
2853 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002854 sp<FakeWindowHandle>::make(application, mDispatcher, "First", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07002855 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002856 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07002857
2858 // Add the windows to the dispatcher
2859 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, firstWindow, secondWindow}}});
2860
2861 // Send down to the first window
2862 NotifyMotionArgs downMotionArgs =
2863 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2864 ADISPLAY_ID_DEFAULT);
2865 mDispatcher->notifyMotion(&downMotionArgs);
2866 // Only the first window and spy should get the down event
2867 spyWindow->consumeMotionDown();
2868 firstWindow->consumeMotionDown();
2869
2870 // Transfer touch to the second window. Non-spy window should be preferred over the spy window
2871 // if f === 'transferTouch'.
2872 TransferFunction f = GetParam();
2873 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
2874 ASSERT_TRUE(success);
2875 // The first window gets cancel and the second gets down
2876 firstWindow->consumeMotionCancel();
2877 secondWindow->consumeMotionDown();
2878
2879 // Send up event to the second window
2880 NotifyMotionArgs upMotionArgs =
2881 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
2882 ADISPLAY_ID_DEFAULT);
2883 mDispatcher->notifyMotion(&upMotionArgs);
2884 // The first window gets no events and the second+spy get up
2885 firstWindow->assertNoEvents();
2886 spyWindow->consumeMotionUp();
2887 secondWindow->consumeMotionUp();
2888}
2889
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00002890TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07002891 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08002892
2893 PointF touchPoint = {10, 10};
2894
2895 // Create a couple of windows
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);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08002899 firstWindow->setPreventSplitting(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002900 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002901 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
2902 ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08002903 secondWindow->setPreventSplitting(true);
Svet Ganov5d3bc372020-01-26 23:11:07 -08002904
2905 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00002906 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08002907
2908 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002909 NotifyMotionArgs downMotionArgs =
2910 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2911 ADISPLAY_ID_DEFAULT, {touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08002912 mDispatcher->notifyMotion(&downMotionArgs);
2913 // Only the first window should get the down event
2914 firstWindow->consumeMotionDown();
2915 secondWindow->assertNoEvents();
2916
2917 // Send pointer down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002918 NotifyMotionArgs pointerDownMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002919 generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002920 {touchPoint, touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08002921 mDispatcher->notifyMotion(&pointerDownMotionArgs);
2922 // Only the first window should get the pointer down event
2923 firstWindow->consumeMotionPointerDown(1);
2924 secondWindow->assertNoEvents();
2925
2926 // Transfer touch focus to the second window
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00002927 TransferFunction f = GetParam();
2928 bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
2929 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08002930 // The first window gets cancel and the second gets down and pointer down
2931 firstWindow->consumeMotionCancel();
2932 secondWindow->consumeMotionDown();
2933 secondWindow->consumeMotionPointerDown(1);
2934
2935 // Send pointer up to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002936 NotifyMotionArgs pointerUpMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002937 generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002938 {touchPoint, touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08002939 mDispatcher->notifyMotion(&pointerUpMotionArgs);
2940 // The first window gets nothing and the second gets pointer up
2941 firstWindow->assertNoEvents();
2942 secondWindow->consumeMotionPointerUp(1);
2943
2944 // Send up event to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002945 NotifyMotionArgs upMotionArgs =
2946 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
2947 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08002948 mDispatcher->notifyMotion(&upMotionArgs);
2949 // The first window gets nothing and the second gets up
2950 firstWindow->assertNoEvents();
2951 secondWindow->consumeMotionUp();
2952}
2953
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00002954// For the cases of single pointer touch and two pointers non-split touch, the api's
2955// 'transferTouch' and 'transferTouchFocus' are equivalent in behaviour. They only differ
2956// for the case where there are multiple pointers split across several windows.
2957INSTANTIATE_TEST_SUITE_P(TransferFunctionTests, TransferTouchFixture,
2958 ::testing::Values(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002959 [&](const std::unique_ptr<InputDispatcher>& dispatcher,
2960 sp<IBinder> /*ignored*/, sp<IBinder> destChannelToken) {
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07002961 return dispatcher->transferTouch(destChannelToken,
2962 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00002963 },
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002964 [&](const std::unique_ptr<InputDispatcher>& dispatcher,
2965 sp<IBinder> from, sp<IBinder> to) {
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00002966 return dispatcher->transferTouchFocus(from, to,
2967 false /*isDragAndDrop*/);
2968 }));
2969
Svet Ganov5d3bc372020-01-26 23:11:07 -08002970TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07002971 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08002972
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002973 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002974 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
2975 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08002976 firstWindow->setFrame(Rect(0, 0, 600, 400));
Svet Ganov5d3bc372020-01-26 23:11:07 -08002977
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002978 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002979 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
2980 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08002981 secondWindow->setFrame(Rect(0, 400, 600, 800));
Svet Ganov5d3bc372020-01-26 23:11:07 -08002982
2983 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00002984 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08002985
2986 PointF pointInFirst = {300, 200};
2987 PointF pointInSecond = {300, 600};
2988
2989 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002990 NotifyMotionArgs firstDownMotionArgs =
2991 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2992 ADISPLAY_ID_DEFAULT, {pointInFirst});
Svet Ganov5d3bc372020-01-26 23:11:07 -08002993 mDispatcher->notifyMotion(&firstDownMotionArgs);
2994 // Only the first window should get the down event
2995 firstWindow->consumeMotionDown();
2996 secondWindow->assertNoEvents();
2997
2998 // Send down to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002999 NotifyMotionArgs secondDownMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003000 generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003001 {pointInFirst, pointInSecond});
Svet Ganov5d3bc372020-01-26 23:11:07 -08003002 mDispatcher->notifyMotion(&secondDownMotionArgs);
3003 // The first window gets a move and the second a down
3004 firstWindow->consumeMotionMove();
3005 secondWindow->consumeMotionDown();
3006
3007 // Transfer touch focus to the second window
3008 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
3009 // The first window gets cancel and the new gets pointer down (it already saw down)
3010 firstWindow->consumeMotionCancel();
3011 secondWindow->consumeMotionPointerDown(1);
3012
3013 // Send pointer up to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003014 NotifyMotionArgs pointerUpMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003015 generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003016 {pointInFirst, pointInSecond});
Svet Ganov5d3bc372020-01-26 23:11:07 -08003017 mDispatcher->notifyMotion(&pointerUpMotionArgs);
3018 // The first window gets nothing and the second gets pointer up
3019 firstWindow->assertNoEvents();
3020 secondWindow->consumeMotionPointerUp(1);
3021
3022 // Send up event to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003023 NotifyMotionArgs upMotionArgs =
3024 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3025 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08003026 mDispatcher->notifyMotion(&upMotionArgs);
3027 // The first window gets nothing and the second gets up
3028 firstWindow->assertNoEvents();
3029 secondWindow->consumeMotionUp();
3030}
3031
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003032// Same as TransferTouchFocus_TwoPointersSplitTouch, but using 'transferTouch' api.
3033// Unlike 'transferTouchFocus', calling 'transferTouch' when there are two windows receiving
3034// touch is not supported, so the touch should continue on those windows and the transferred-to
3035// window should get nothing.
3036TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) {
3037 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3038
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003039 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003040 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
3041 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003042 firstWindow->setFrame(Rect(0, 0, 600, 400));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003043
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003044 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003045 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3046 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003047 secondWindow->setFrame(Rect(0, 400, 600, 800));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003048
3049 // Add the windows to the dispatcher
3050 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
3051
3052 PointF pointInFirst = {300, 200};
3053 PointF pointInSecond = {300, 600};
3054
3055 // Send down to the first window
3056 NotifyMotionArgs firstDownMotionArgs =
3057 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3058 ADISPLAY_ID_DEFAULT, {pointInFirst});
3059 mDispatcher->notifyMotion(&firstDownMotionArgs);
3060 // Only the first window should get the down event
3061 firstWindow->consumeMotionDown();
3062 secondWindow->assertNoEvents();
3063
3064 // Send down to the second window
3065 NotifyMotionArgs secondDownMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003066 generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003067 {pointInFirst, pointInSecond});
3068 mDispatcher->notifyMotion(&secondDownMotionArgs);
3069 // The first window gets a move and the second a down
3070 firstWindow->consumeMotionMove();
3071 secondWindow->consumeMotionDown();
3072
3073 // Transfer touch focus to the second window
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07003074 const bool transferred =
3075 mDispatcher->transferTouch(secondWindow->getToken(), ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003076 // The 'transferTouch' call should not succeed, because there are 2 touched windows
3077 ASSERT_FALSE(transferred);
3078 firstWindow->assertNoEvents();
3079 secondWindow->assertNoEvents();
3080
3081 // The rest of the dispatch should proceed as normal
3082 // Send pointer up to the second window
3083 NotifyMotionArgs pointerUpMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003084 generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003085 {pointInFirst, pointInSecond});
3086 mDispatcher->notifyMotion(&pointerUpMotionArgs);
3087 // The first window gets MOVE and the second gets pointer up
3088 firstWindow->consumeMotionMove();
3089 secondWindow->consumeMotionUp();
3090
3091 // Send up event to the first window
3092 NotifyMotionArgs upMotionArgs =
3093 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3094 ADISPLAY_ID_DEFAULT);
3095 mDispatcher->notifyMotion(&upMotionArgs);
3096 // The first window gets nothing and the second gets up
3097 firstWindow->consumeMotionUp();
3098 secondWindow->assertNoEvents();
3099}
3100
Arthur Hungabbb9d82021-09-01 14:52:30 +00003101// This case will create two windows and one mirrored window on the default display and mirror
3102// two windows on the second display. It will test if 'transferTouchFocus' works fine if we put
3103// the windows info of second display before default display.
3104TEST_F(InputDispatcherTest, TransferTouchFocus_CloneSurface) {
3105 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3106 sp<FakeWindowHandle> firstWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003107 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00003108 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003109 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003110 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00003111 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003112
3113 sp<FakeWindowHandle> mirrorWindowInPrimary =
3114 firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT);
3115 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003116
3117 sp<FakeWindowHandle> firstWindowInSecondary =
3118 firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
3119 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003120
3121 sp<FakeWindowHandle> secondWindowInSecondary =
3122 secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
3123 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003124
3125 // Update window info, let it find window handle of second display first.
3126 mDispatcher->setInputWindows(
3127 {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}},
3128 {ADISPLAY_ID_DEFAULT,
3129 {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}});
3130
3131 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3132 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3133 {50, 50}))
3134 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3135
3136 // Window should receive motion event.
3137 firstWindowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3138
3139 // Transfer touch focus
3140 ASSERT_TRUE(mDispatcher->transferTouchFocus(firstWindowInPrimary->getToken(),
3141 secondWindowInPrimary->getToken()));
3142 // The first window gets cancel.
3143 firstWindowInPrimary->consumeMotionCancel();
3144 secondWindowInPrimary->consumeMotionDown();
3145
3146 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3147 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
3148 ADISPLAY_ID_DEFAULT, {150, 50}))
3149 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3150 firstWindowInPrimary->assertNoEvents();
3151 secondWindowInPrimary->consumeMotionMove();
3152
3153 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3154 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3155 {150, 50}))
3156 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3157 firstWindowInPrimary->assertNoEvents();
3158 secondWindowInPrimary->consumeMotionUp();
3159}
3160
3161// Same as TransferTouchFocus_CloneSurface, but this touch on the secondary display and use
3162// 'transferTouch' api.
3163TEST_F(InputDispatcherTest, TransferTouch_CloneSurface) {
3164 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3165 sp<FakeWindowHandle> firstWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003166 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00003167 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003168 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003169 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00003170 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003171
3172 sp<FakeWindowHandle> mirrorWindowInPrimary =
3173 firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT);
3174 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003175
3176 sp<FakeWindowHandle> firstWindowInSecondary =
3177 firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
3178 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003179
3180 sp<FakeWindowHandle> secondWindowInSecondary =
3181 secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
3182 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003183
3184 // Update window info, let it find window handle of second display first.
3185 mDispatcher->setInputWindows(
3186 {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}},
3187 {ADISPLAY_ID_DEFAULT,
3188 {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}});
3189
3190 // Touch on second display.
3191 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3192 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {50, 50}))
3193 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3194
3195 // Window should receive motion event.
3196 firstWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID);
3197
3198 // Transfer touch focus
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07003199 ASSERT_TRUE(mDispatcher->transferTouch(secondWindowInSecondary->getToken(), SECOND_DISPLAY_ID));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003200
3201 // The first window gets cancel.
3202 firstWindowInPrimary->consumeMotionCancel(SECOND_DISPLAY_ID);
3203 secondWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID);
3204
3205 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3206 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
3207 SECOND_DISPLAY_ID, {150, 50}))
3208 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3209 firstWindowInPrimary->assertNoEvents();
3210 secondWindowInPrimary->consumeMotionMove(SECOND_DISPLAY_ID);
3211
3212 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3213 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {150, 50}))
3214 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3215 firstWindowInPrimary->assertNoEvents();
3216 secondWindowInPrimary->consumeMotionUp(SECOND_DISPLAY_ID);
3217}
3218
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003219TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07003220 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003221 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3222 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003223
Vishnu Nair47074b82020-08-14 11:54:47 -07003224 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00003225 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003226 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003227
3228 window->consumeFocusEvent(true);
3229
3230 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
3231 mDispatcher->notifyKey(&keyArgs);
3232
3233 // Window should receive key down event.
3234 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3235}
3236
3237TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07003238 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003239 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3240 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003241
Arthur Hung72d8dc32020-03-28 00:48:39 +00003242 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003243
3244 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
3245 mDispatcher->notifyKey(&keyArgs);
3246 mDispatcher->waitForIdle();
3247
3248 window->assertNoEvents();
3249}
3250
3251// If a window is touchable, but does not have focus, it should receive motion events, but not keys
3252TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) {
Chris Yea209fde2020-07-22 13:54:51 -07003253 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003254 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3255 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003256
Arthur Hung72d8dc32020-03-28 00:48:39 +00003257 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003258
3259 // Send key
3260 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
3261 mDispatcher->notifyKey(&keyArgs);
3262 // Send motion
3263 NotifyMotionArgs motionArgs =
3264 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3265 ADISPLAY_ID_DEFAULT);
3266 mDispatcher->notifyMotion(&motionArgs);
3267
3268 // Window should receive only the motion event
3269 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3270 window->assertNoEvents(); // Key event or focus event will not be received
3271}
3272
arthurhungea3f4fc2020-12-21 23:18:53 +08003273TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) {
3274 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3275
arthurhungea3f4fc2020-12-21 23:18:53 +08003276 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003277 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
3278 ADISPLAY_ID_DEFAULT);
arthurhungea3f4fc2020-12-21 23:18:53 +08003279 firstWindow->setFrame(Rect(0, 0, 600, 400));
arthurhungea3f4fc2020-12-21 23:18:53 +08003280
arthurhungea3f4fc2020-12-21 23:18:53 +08003281 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003282 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3283 ADISPLAY_ID_DEFAULT);
arthurhungea3f4fc2020-12-21 23:18:53 +08003284 secondWindow->setFrame(Rect(0, 400, 600, 800));
arthurhungea3f4fc2020-12-21 23:18:53 +08003285
3286 // Add the windows to the dispatcher
3287 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
3288
3289 PointF pointInFirst = {300, 200};
3290 PointF pointInSecond = {300, 600};
3291
3292 // Send down to the first window
3293 NotifyMotionArgs firstDownMotionArgs =
3294 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3295 ADISPLAY_ID_DEFAULT, {pointInFirst});
3296 mDispatcher->notifyMotion(&firstDownMotionArgs);
3297 // Only the first window should get the down event
3298 firstWindow->consumeMotionDown();
3299 secondWindow->assertNoEvents();
3300
3301 // Send down to the second window
3302 NotifyMotionArgs secondDownMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003303 generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
arthurhungea3f4fc2020-12-21 23:18:53 +08003304 {pointInFirst, pointInSecond});
3305 mDispatcher->notifyMotion(&secondDownMotionArgs);
3306 // The first window gets a move and the second a down
3307 firstWindow->consumeMotionMove();
3308 secondWindow->consumeMotionDown();
3309
3310 // Send pointer cancel to the second window
3311 NotifyMotionArgs pointerUpMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003312 generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
arthurhungea3f4fc2020-12-21 23:18:53 +08003313 {pointInFirst, pointInSecond});
3314 pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED;
3315 mDispatcher->notifyMotion(&pointerUpMotionArgs);
3316 // The first window gets move and the second gets cancel.
3317 firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
3318 secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
3319
3320 // Send up event.
3321 NotifyMotionArgs upMotionArgs =
3322 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3323 ADISPLAY_ID_DEFAULT);
3324 mDispatcher->notifyMotion(&upMotionArgs);
3325 // The first window gets up and the second gets nothing.
3326 firstWindow->consumeMotionUp();
3327 secondWindow->assertNoEvents();
3328}
3329
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00003330TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) {
3331 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3332
3333 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003334 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00003335 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3336 std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline;
3337 graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2;
3338 graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3;
3339
3340 window->sendTimeline(1 /*inputEventId*/, graphicsTimeline);
3341 window->assertNoEvents();
3342 mDispatcher->waitForIdle();
3343}
3344
chaviwd1c23182019-12-20 18:44:56 -08003345class FakeMonitorReceiver {
Michael Wright3a240c42019-12-10 20:53:41 +00003346public:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003347 FakeMonitorReceiver(const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003348 int32_t displayId) {
Garfield Tan15601662020-09-22 15:32:38 -07003349 base::Result<std::unique_ptr<InputChannel>> channel =
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08003350 dispatcher->createInputMonitor(displayId, name, MONITOR_PID);
Garfield Tan15601662020-09-22 15:32:38 -07003351 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
Michael Wright3a240c42019-12-10 20:53:41 +00003352 }
3353
chaviwd1c23182019-12-20 18:44:56 -08003354 sp<IBinder> getToken() { return mInputReceiver->getToken(); }
3355
3356 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
3357 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN,
3358 expectedDisplayId, expectedFlags);
3359 }
3360
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003361 std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); }
3362
3363 void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); }
3364
chaviwd1c23182019-12-20 18:44:56 -08003365 void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
3366 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN,
3367 expectedDisplayId, expectedFlags);
3368 }
3369
Siarhei Vishniakouca205502021-07-16 21:31:58 +00003370 void consumeMotionMove(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
3371 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE,
3372 expectedDisplayId, expectedFlags);
3373 }
3374
chaviwd1c23182019-12-20 18:44:56 -08003375 void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
3376 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP,
3377 expectedDisplayId, expectedFlags);
3378 }
3379
Siarhei Vishniakouca205502021-07-16 21:31:58 +00003380 void consumeMotionCancel(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
3381 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL,
3382 expectedDisplayId, expectedFlags);
3383 }
3384
Arthur Hungfbfa5722021-11-16 02:45:54 +00003385 void consumeMotionPointerDown(int32_t pointerIdx) {
3386 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
3387 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
3388 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, ADISPLAY_ID_DEFAULT,
3389 0 /*expectedFlags*/);
3390 }
3391
Evan Rosky84f07f02021-04-16 10:42:42 -07003392 MotionEvent* consumeMotion() {
3393 InputEvent* event = mInputReceiver->consume();
3394 if (!event) {
3395 ADD_FAILURE() << "No event was produced";
3396 return nullptr;
3397 }
3398 if (event->getType() != AINPUT_EVENT_TYPE_MOTION) {
3399 ADD_FAILURE() << "Received event of type " << event->getType() << " instead of motion";
3400 return nullptr;
3401 }
3402 return static_cast<MotionEvent*>(event);
3403 }
3404
chaviwd1c23182019-12-20 18:44:56 -08003405 void assertNoEvents() { mInputReceiver->assertNoEvents(); }
3406
3407private:
3408 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Michael Wright3a240c42019-12-10 20:53:41 +00003409};
3410
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003411using InputDispatcherMonitorTest = InputDispatcherTest;
3412
Siarhei Vishniakouca205502021-07-16 21:31:58 +00003413/**
3414 * Two entities that receive touch: A window, and a global monitor.
3415 * The touch goes to the window, and then the window disappears.
3416 * The monitor does not get cancel right away. But if more events come in, the touch gets canceled
3417 * for the monitor, as well.
3418 * 1. foregroundWindow
3419 * 2. monitor <-- global monitor (doesn't observe z order, receives all events)
3420 */
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003421TEST_F(InputDispatcherMonitorTest, MonitorTouchIsCanceledWhenForegroundWindowDisappears) {
Siarhei Vishniakouca205502021-07-16 21:31:58 +00003422 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3423 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003424 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00003425
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003426 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00003427
3428 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3429 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3430 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3431 {100, 200}))
3432 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3433
3434 // Both the foreground window and the global monitor should receive the touch down
3435 window->consumeMotionDown();
3436 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
3437
3438 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3439 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
3440 ADISPLAY_ID_DEFAULT, {110, 200}))
3441 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3442
3443 window->consumeMotionMove();
3444 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
3445
3446 // Now the foreground window goes away
3447 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
3448 window->consumeMotionCancel();
3449 monitor.assertNoEvents(); // Global monitor does not get a cancel yet
3450
3451 // If more events come in, there will be no more foreground window to send them to. This will
3452 // cause a cancel for the monitor, as well.
3453 ASSERT_EQ(InputEventInjectionResult::FAILED,
3454 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
3455 ADISPLAY_ID_DEFAULT, {120, 200}))
3456 << "Injection should fail because the window was removed";
3457 window->assertNoEvents();
3458 // Global monitor now gets the cancel
3459 monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
3460}
3461
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003462TEST_F(InputDispatcherMonitorTest, ReceivesMotionEvents) {
Chris Yea209fde2020-07-22 13:54:51 -07003463 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003464 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3465 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00003466 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00003467
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003468 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00003469
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003470 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00003471 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003472 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00003473 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08003474 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00003475}
3476
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003477TEST_F(InputDispatcherMonitorTest, MonitorCannotPilferPointers) {
3478 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00003479
Chris Yea209fde2020-07-22 13:54:51 -07003480 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003481 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3482 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00003483 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00003484
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003485 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00003486 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003487 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
chaviwd1c23182019-12-20 18:44:56 -08003488 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003489 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00003490
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003491 // Pilfer pointers from the monitor.
3492 // This should not do anything and the window should continue to receive events.
3493 EXPECT_NE(OK, mDispatcher->pilferPointers(monitor.getToken()));
Michael Wright3a240c42019-12-10 20:53:41 +00003494
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003495 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003496 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
3497 ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003498 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003499
3500 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
3501 window->consumeMotionMove(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00003502}
3503
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003504TEST_F(InputDispatcherMonitorTest, NoWindowTransform) {
Evan Rosky84f07f02021-04-16 10:42:42 -07003505 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003506 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3507 "Fake Window", ADISPLAY_ID_DEFAULT);
Evan Rosky84f07f02021-04-16 10:42:42 -07003508 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3509 window->setWindowOffset(20, 40);
3510 window->setWindowTransform(0, 1, -1, 0);
3511
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003512 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Evan Rosky84f07f02021-04-16 10:42:42 -07003513
3514 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3515 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
3516 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3517 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3518 MotionEvent* event = monitor.consumeMotion();
3519 // Even though window has transform, gesture monitor must not.
3520 ASSERT_EQ(ui::Transform(), event->getTransform());
3521}
3522
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003523TEST_F(InputDispatcherMonitorTest, InjectionFailsWithNoWindow) {
Arthur Hungb3307ee2021-10-14 10:57:37 +00003524 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003525 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Arthur Hungb3307ee2021-10-14 10:57:37 +00003526
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003527 ASSERT_EQ(InputEventInjectionResult::FAILED,
Arthur Hungb3307ee2021-10-14 10:57:37 +00003528 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003529 << "Injection should fail if there is a monitor, but no touchable window";
3530 monitor.assertNoEvents();
Arthur Hungb3307ee2021-10-14 10:57:37 +00003531}
3532
chaviw81e2bb92019-12-18 15:03:51 -08003533TEST_F(InputDispatcherTest, TestMoveEvent) {
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 "Fake Window", ADISPLAY_ID_DEFAULT);
chaviw81e2bb92019-12-18 15:03:51 -08003537
Arthur Hung72d8dc32020-03-28 00:48:39 +00003538 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
chaviw81e2bb92019-12-18 15:03:51 -08003539
3540 NotifyMotionArgs motionArgs =
3541 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3542 ADISPLAY_ID_DEFAULT);
3543
3544 mDispatcher->notifyMotion(&motionArgs);
3545 // Window should receive motion down event.
3546 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3547
3548 motionArgs.action = AMOTION_EVENT_ACTION_MOVE;
Garfield Tanc51d1ba2020-01-28 13:24:04 -08003549 motionArgs.id += 1;
chaviw81e2bb92019-12-18 15:03:51 -08003550 motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
3551 motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
3552 motionArgs.pointerCoords[0].getX() - 10);
3553
3554 mDispatcher->notifyMotion(&motionArgs);
3555 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT,
3556 0 /*expectedFlags*/);
3557}
3558
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003559/**
3560 * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to
3561 * the device default right away. In the test scenario, we check both the default value,
3562 * and the action of enabling / disabling.
3563 */
3564TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) {
Chris Yea209fde2020-07-22 13:54:51 -07003565 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003566 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3567 "Test window", ADISPLAY_ID_DEFAULT);
Antonio Kantekea47acb2021-12-23 12:41:25 -08003568 const WindowInfo& windowInfo = *window->getInfo();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003569
3570 // Set focused application.
3571 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07003572 window->setFocusable(true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003573
3574 SCOPED_TRACE("Check default value of touch mode");
Arthur Hung72d8dc32020-03-28 00:48:39 +00003575 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003576 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003577 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
3578
3579 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07003580 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00003581 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003582 window->consumeFocusEvent(false /*hasFocus*/, true /*inTouchMode*/);
3583
3584 SCOPED_TRACE("Disable touch mode");
Antonio Kantekea47acb2021-12-23 12:41:25 -08003585 mDispatcher->setInTouchMode(false, windowInfo.ownerPid, windowInfo.ownerUid,
Antonio Kanteka042c022022-07-06 16:51:07 -07003586 true /*hasPermission*/, ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00003587 window->consumeTouchModeEvent(false);
Vishnu Nair47074b82020-08-14 11:54:47 -07003588 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00003589 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003590 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003591 window->consumeFocusEvent(true /*hasFocus*/, false /*inTouchMode*/);
3592
3593 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07003594 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00003595 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003596 window->consumeFocusEvent(false /*hasFocus*/, false /*inTouchMode*/);
3597
3598 SCOPED_TRACE("Enable touch mode again");
Antonio Kantekea47acb2021-12-23 12:41:25 -08003599 mDispatcher->setInTouchMode(true, windowInfo.ownerPid, windowInfo.ownerUid,
Antonio Kanteka042c022022-07-06 16:51:07 -07003600 true /*hasPermission*/, ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00003601 window->consumeTouchModeEvent(true);
Vishnu Nair47074b82020-08-14 11:54:47 -07003602 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00003603 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003604 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003605 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
3606
3607 window->assertNoEvents();
3608}
3609
Gang Wange9087892020-01-07 12:17:14 -05003610TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07003611 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003612 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3613 "Test window", ADISPLAY_ID_DEFAULT);
Gang Wange9087892020-01-07 12:17:14 -05003614
3615 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07003616 window->setFocusable(true);
Gang Wange9087892020-01-07 12:17:14 -05003617
Arthur Hung72d8dc32020-03-28 00:48:39 +00003618 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003619 setFocusedWindow(window);
3620
Gang Wange9087892020-01-07 12:17:14 -05003621 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
3622
3623 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
3624 mDispatcher->notifyKey(&keyArgs);
3625
3626 InputEvent* event = window->consume();
3627 ASSERT_NE(event, nullptr);
3628
3629 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
3630 ASSERT_NE(verified, nullptr);
3631 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY);
3632
3633 ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos);
3634 ASSERT_EQ(keyArgs.deviceId, verified->deviceId);
3635 ASSERT_EQ(keyArgs.source, verified->source);
3636 ASSERT_EQ(keyArgs.displayId, verified->displayId);
3637
3638 const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified);
3639
3640 ASSERT_EQ(keyArgs.action, verifiedKey.action);
Gang Wange9087892020-01-07 12:17:14 -05003641 ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08003642 ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos);
Gang Wange9087892020-01-07 12:17:14 -05003643 ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode);
3644 ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode);
3645 ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState);
3646 ASSERT_EQ(0, verifiedKey.repeatCount);
3647}
3648
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08003649TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07003650 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003651 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3652 "Test window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08003653
3654 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3655
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07003656 ui::Transform transform;
3657 transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
3658
3659 gui::DisplayInfo displayInfo;
3660 displayInfo.displayId = ADISPLAY_ID_DEFAULT;
3661 displayInfo.transform = transform;
3662
3663 mDispatcher->onWindowInfosChanged({*window->getInfo()}, {displayInfo});
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08003664
3665 NotifyMotionArgs motionArgs =
3666 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3667 ADISPLAY_ID_DEFAULT);
3668 mDispatcher->notifyMotion(&motionArgs);
3669
3670 InputEvent* event = window->consume();
3671 ASSERT_NE(event, nullptr);
3672
3673 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
3674 ASSERT_NE(verified, nullptr);
3675 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION);
3676
3677 EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos);
3678 EXPECT_EQ(motionArgs.deviceId, verified->deviceId);
3679 EXPECT_EQ(motionArgs.source, verified->source);
3680 EXPECT_EQ(motionArgs.displayId, verified->displayId);
3681
3682 const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified);
3683
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07003684 const vec2 rawXY =
3685 MotionEvent::calculateTransformedXY(motionArgs.source, transform,
3686 motionArgs.pointerCoords[0].getXYValue());
3687 EXPECT_EQ(rawXY.x, verifiedMotion.rawX);
3688 EXPECT_EQ(rawXY.y, verifiedMotion.rawY);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08003689 EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08003690 EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08003691 EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08003692 EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState);
3693 EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState);
3694}
3695
chaviw09c8d2d2020-08-24 15:48:26 -07003696/**
3697 * Ensure that separate calls to sign the same data are generating the same key.
3698 * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance
3699 * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky
3700 * tests.
3701 */
3702TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) {
3703 KeyEvent event = getTestKeyEvent();
3704 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
3705
3706 std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent);
3707 std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent);
3708 ASSERT_EQ(hmac1, hmac2);
3709}
3710
3711/**
3712 * Ensure that changes in VerifiedKeyEvent produce a different hmac.
3713 */
3714TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) {
3715 KeyEvent event = getTestKeyEvent();
3716 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
3717 std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent);
3718
3719 verifiedEvent.deviceId += 1;
3720 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
3721
3722 verifiedEvent.source += 1;
3723 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
3724
3725 verifiedEvent.eventTimeNanos += 1;
3726 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
3727
3728 verifiedEvent.displayId += 1;
3729 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
3730
3731 verifiedEvent.action += 1;
3732 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
3733
3734 verifiedEvent.downTimeNanos += 1;
3735 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
3736
3737 verifiedEvent.flags += 1;
3738 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
3739
3740 verifiedEvent.keyCode += 1;
3741 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
3742
3743 verifiedEvent.scanCode += 1;
3744 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
3745
3746 verifiedEvent.metaState += 1;
3747 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
3748
3749 verifiedEvent.repeatCount += 1;
3750 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
3751}
3752
Vishnu Nair958da932020-08-21 17:12:37 -07003753TEST_F(InputDispatcherTest, SetFocusedWindow) {
3754 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3755 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003756 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07003757 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003758 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07003759 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3760
3761 // Top window is also focusable but is not granted focus.
3762 windowTop->setFocusable(true);
3763 windowSecond->setFocusable(true);
3764 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
3765 setFocusedWindow(windowSecond);
3766
3767 windowSecond->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003768 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
3769 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07003770
3771 // Focused window should receive event.
3772 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
3773 windowTop->assertNoEvents();
3774}
3775
3776TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) {
3777 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3778 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003779 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07003780 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3781
3782 window->setFocusable(true);
3783 // Release channel for window is no longer valid.
3784 window->releaseChannel();
3785 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3786 setFocusedWindow(window);
3787
3788 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003789 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
3790 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07003791
3792 // window channel is invalid, so it should not receive any input event.
3793 window->assertNoEvents();
3794}
3795
3796TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) {
3797 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3798 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003799 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08003800 window->setFocusable(false);
Vishnu Nair958da932020-08-21 17:12:37 -07003801 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3802
Vishnu Nair958da932020-08-21 17:12:37 -07003803 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3804 setFocusedWindow(window);
3805
3806 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003807 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
3808 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07003809
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08003810 // window is not focusable, so it should not receive any input event.
Vishnu Nair958da932020-08-21 17:12:37 -07003811 window->assertNoEvents();
3812}
3813
3814TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) {
3815 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3816 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003817 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07003818 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003819 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07003820 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3821
3822 windowTop->setFocusable(true);
3823 windowSecond->setFocusable(true);
3824 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
3825 setFocusedWindow(windowTop);
3826 windowTop->consumeFocusEvent(true);
3827
3828 setFocusedWindow(windowSecond, windowTop);
3829 windowSecond->consumeFocusEvent(true);
3830 windowTop->consumeFocusEvent(false);
3831
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003832 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
3833 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07003834
3835 // Focused window should receive event.
3836 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
3837}
3838
3839TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestFocusTokenNotFocused) {
3840 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3841 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003842 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07003843 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003844 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07003845 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3846
3847 windowTop->setFocusable(true);
3848 windowSecond->setFocusable(true);
3849 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
3850 setFocusedWindow(windowSecond, windowTop);
3851
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003852 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
3853 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07003854
3855 // Event should be dropped.
3856 windowTop->assertNoEvents();
3857 windowSecond->assertNoEvents();
3858}
3859
3860TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) {
3861 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3862 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003863 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07003864 sp<FakeWindowHandle> previousFocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003865 sp<FakeWindowHandle>::make(application, mDispatcher, "previousFocusedWindow",
3866 ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07003867 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3868
3869 window->setFocusable(true);
3870 previousFocusedWindow->setFocusable(true);
3871 window->setVisible(false);
3872 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}});
3873 setFocusedWindow(previousFocusedWindow);
3874 previousFocusedWindow->consumeFocusEvent(true);
3875
3876 // Requesting focus on invisible window takes focus from currently focused window.
3877 setFocusedWindow(window);
3878 previousFocusedWindow->consumeFocusEvent(false);
3879
3880 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003881 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Vishnu Nair958da932020-08-21 17:12:37 -07003882 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003883 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07003884
3885 // Window does not get focus event or key down.
3886 window->assertNoEvents();
3887
3888 // Window becomes visible.
3889 window->setVisible(true);
3890 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3891
3892 // Window receives focus event.
3893 window->consumeFocusEvent(true);
3894 // Focused window receives key down.
3895 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3896}
3897
Vishnu Nair599f1412021-06-21 10:39:58 -07003898TEST_F(InputDispatcherTest, DisplayRemoved) {
3899 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3900 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003901 sp<FakeWindowHandle>::make(application, mDispatcher, "window", ADISPLAY_ID_DEFAULT);
Vishnu Nair599f1412021-06-21 10:39:58 -07003902 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3903
3904 // window is granted focus.
3905 window->setFocusable(true);
3906 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3907 setFocusedWindow(window);
3908 window->consumeFocusEvent(true);
3909
3910 // When a display is removed window loses focus.
3911 mDispatcher->displayRemoved(ADISPLAY_ID_DEFAULT);
3912 window->consumeFocusEvent(false);
3913}
3914
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003915/**
3916 * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY,
3917 * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top
3918 * of the 'slipperyEnterWindow'.
3919 *
3920 * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such
3921 * a way so that the touched location is no longer covered by the top window.
3922 *
3923 * Next, inject a MOVE event. Because the top window already moved earlier, this event is now
3924 * positioned over the bottom (slipperyEnterWindow) only. And because the top window had
3925 * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive
3926 * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting
3927 * with ACTION_DOWN).
3928 * Thus, the touch has been transferred from the top window into the bottom window, because the top
3929 * window moved itself away from the touched location and had Flag::SLIPPERY.
3930 *
3931 * Even though the top window moved away from the touched location, it is still obscuring the bottom
3932 * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_
3933 * OBSCURED should be set for the MotionEvent that reaches the bottom window.
3934 *
3935 * In this test, we ensure that the event received by the bottom window has
3936 * FLAG_WINDOW_IS_PARTIALLY_OBSCURED.
3937 */
3938TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) {
Prabir Pradhan5735a322022-04-11 17:23:34 +00003939 constexpr int32_t SLIPPERY_PID = WINDOW_PID + 1;
3940 constexpr int32_t SLIPPERY_UID = WINDOW_UID + 1;
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003941
3942 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3943 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3944
3945 sp<FakeWindowHandle> slipperyExitWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003946 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08003947 slipperyExitWindow->setSlippery(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003948 // Make sure this one overlaps the bottom window
3949 slipperyExitWindow->setFrame(Rect(25, 25, 75, 75));
3950 // Change the owner uid/pid of the window so that it is considered to be occluding the bottom
3951 // one. Windows with the same owner are not considered to be occluding each other.
3952 slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID);
3953
3954 sp<FakeWindowHandle> slipperyEnterWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003955 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003956 slipperyExitWindow->setFrame(Rect(0, 0, 100, 100));
3957
3958 mDispatcher->setInputWindows(
3959 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
3960
3961 // Use notifyMotion instead of injecting to avoid dealing with injection permissions
3962 NotifyMotionArgs args = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3963 ADISPLAY_ID_DEFAULT, {{50, 50}});
3964 mDispatcher->notifyMotion(&args);
3965 slipperyExitWindow->consumeMotionDown();
3966 slipperyExitWindow->setFrame(Rect(70, 70, 100, 100));
3967 mDispatcher->setInputWindows(
3968 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
3969
3970 args = generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
3971 ADISPLAY_ID_DEFAULT, {{51, 51}});
3972 mDispatcher->notifyMotion(&args);
3973
3974 slipperyExitWindow->consumeMotionCancel();
3975
3976 slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT,
3977 AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
3978}
3979
Garfield Tan1c7bc862020-01-28 13:24:04 -08003980class InputDispatcherKeyRepeatTest : public InputDispatcherTest {
3981protected:
3982 static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms
3983 static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms
3984
Chris Yea209fde2020-07-22 13:54:51 -07003985 std::shared_ptr<FakeApplicationHandle> mApp;
Garfield Tan1c7bc862020-01-28 13:24:04 -08003986 sp<FakeWindowHandle> mWindow;
3987
3988 virtual void SetUp() override {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003989 mFakePolicy = sp<FakeInputDispatcherPolicy>::make();
Garfield Tan1c7bc862020-01-28 13:24:04 -08003990 mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003991 mDispatcher = std::make_unique<InputDispatcher>(mFakePolicy);
Garfield Tan1c7bc862020-01-28 13:24:04 -08003992 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
3993 ASSERT_EQ(OK, mDispatcher->start());
3994
3995 setUpWindow();
3996 }
3997
3998 void setUpWindow() {
Chris Yea209fde2020-07-22 13:54:51 -07003999 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004000 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Garfield Tan1c7bc862020-01-28 13:24:04 -08004001
Vishnu Nair47074b82020-08-14 11:54:47 -07004002 mWindow->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004003 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004004 setFocusedWindow(mWindow);
Garfield Tan1c7bc862020-01-28 13:24:04 -08004005 mWindow->consumeFocusEvent(true);
4006 }
4007
Chris Ye2ad95392020-09-01 13:44:44 -07004008 void sendAndConsumeKeyDown(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08004009 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07004010 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08004011 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event
4012 mDispatcher->notifyKey(&keyArgs);
4013
4014 // Window should receive key down event.
4015 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4016 }
4017
4018 void expectKeyRepeatOnce(int32_t repeatCount) {
4019 SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount));
4020 InputEvent* repeatEvent = mWindow->consume();
4021 ASSERT_NE(nullptr, repeatEvent);
4022
4023 uint32_t eventType = repeatEvent->getType();
4024 ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, eventType);
4025
4026 KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent);
4027 uint32_t eventAction = repeatKeyEvent->getAction();
4028 EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction);
4029 EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount());
4030 }
4031
Chris Ye2ad95392020-09-01 13:44:44 -07004032 void sendAndConsumeKeyUp(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08004033 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07004034 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08004035 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event
4036 mDispatcher->notifyKey(&keyArgs);
4037
4038 // Window should receive key down event.
4039 mWindow->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
4040 0 /*expectedFlags*/);
4041 }
4042};
4043
4044TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) {
Chris Ye2ad95392020-09-01 13:44:44 -07004045 sendAndConsumeKeyDown(1 /* deviceId */);
4046 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
4047 expectKeyRepeatOnce(repeatCount);
4048 }
4049}
4050
4051TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) {
4052 sendAndConsumeKeyDown(1 /* deviceId */);
4053 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
4054 expectKeyRepeatOnce(repeatCount);
4055 }
4056 sendAndConsumeKeyDown(2 /* deviceId */);
4057 /* repeatCount will start from 1 for deviceId 2 */
Garfield Tan1c7bc862020-01-28 13:24:04 -08004058 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
4059 expectKeyRepeatOnce(repeatCount);
4060 }
4061}
4062
4063TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) {
Chris Ye2ad95392020-09-01 13:44:44 -07004064 sendAndConsumeKeyDown(1 /* deviceId */);
Garfield Tan1c7bc862020-01-28 13:24:04 -08004065 expectKeyRepeatOnce(1 /*repeatCount*/);
Chris Ye2ad95392020-09-01 13:44:44 -07004066 sendAndConsumeKeyUp(1 /* deviceId */);
4067 mWindow->assertNoEvents();
4068}
4069
4070TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) {
4071 sendAndConsumeKeyDown(1 /* deviceId */);
4072 expectKeyRepeatOnce(1 /*repeatCount*/);
4073 sendAndConsumeKeyDown(2 /* deviceId */);
4074 expectKeyRepeatOnce(1 /*repeatCount*/);
4075 // Stale key up from device 1.
4076 sendAndConsumeKeyUp(1 /* deviceId */);
4077 // Device 2 is still down, keep repeating
4078 expectKeyRepeatOnce(2 /*repeatCount*/);
4079 expectKeyRepeatOnce(3 /*repeatCount*/);
4080 // Device 2 key up
4081 sendAndConsumeKeyUp(2 /* deviceId */);
4082 mWindow->assertNoEvents();
4083}
4084
4085TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) {
4086 sendAndConsumeKeyDown(1 /* deviceId */);
4087 expectKeyRepeatOnce(1 /*repeatCount*/);
4088 sendAndConsumeKeyDown(2 /* deviceId */);
4089 expectKeyRepeatOnce(1 /*repeatCount*/);
4090 // Device 2 which holds the key repeating goes up, expect the repeating to stop.
4091 sendAndConsumeKeyUp(2 /* deviceId */);
4092 // Device 1 still holds key down, but the repeating was already stopped
Garfield Tan1c7bc862020-01-28 13:24:04 -08004093 mWindow->assertNoEvents();
4094}
4095
liushenxiang42232912021-05-21 20:24:09 +08004096TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) {
4097 sendAndConsumeKeyDown(DEVICE_ID);
4098 expectKeyRepeatOnce(1 /*repeatCount*/);
4099 NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID);
4100 mDispatcher->notifyDeviceReset(&args);
4101 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT,
4102 AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS);
4103 mWindow->assertNoEvents();
4104}
4105
Garfield Tan1c7bc862020-01-28 13:24:04 -08004106TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) {
Chris Ye2ad95392020-09-01 13:44:44 -07004107 sendAndConsumeKeyDown(1 /* deviceId */);
Garfield Tan1c7bc862020-01-28 13:24:04 -08004108 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
4109 InputEvent* repeatEvent = mWindow->consume();
4110 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
4111 EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER,
4112 IdGenerator::getSource(repeatEvent->getId()));
4113 }
4114}
4115
4116TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) {
Chris Ye2ad95392020-09-01 13:44:44 -07004117 sendAndConsumeKeyDown(1 /* deviceId */);
Garfield Tan1c7bc862020-01-28 13:24:04 -08004118
4119 std::unordered_set<int32_t> idSet;
4120 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
4121 InputEvent* repeatEvent = mWindow->consume();
4122 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
4123 int32_t id = repeatEvent->getId();
4124 EXPECT_EQ(idSet.end(), idSet.find(id));
4125 idSet.insert(id);
4126 }
4127}
4128
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004129/* Test InputDispatcher for MultiDisplay */
4130class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest {
4131public:
Prabir Pradhan3608aad2019-10-02 17:08:26 -07004132 virtual void SetUp() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004133 InputDispatcherTest::SetUp();
Arthur Hungb92218b2018-08-14 12:00:21 +08004134
Chris Yea209fde2020-07-22 13:54:51 -07004135 application1 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004136 windowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004137 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08004138
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004139 // Set focus window for primary display, but focused display would be second one.
4140 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1);
Vishnu Nair47074b82020-08-14 11:54:47 -07004141 windowInPrimary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004142 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004143 setFocusedWindow(windowInPrimary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004144 windowInPrimary->consumeFocusEvent(true);
Arthur Hungb92218b2018-08-14 12:00:21 +08004145
Chris Yea209fde2020-07-22 13:54:51 -07004146 application2 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004147 windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004148 sp<FakeWindowHandle>::make(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004149 // Set focus to second display window.
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004150 // Set focus display to second one.
4151 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
4152 // Set focus window for second display.
4153 mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2);
Vishnu Nair47074b82020-08-14 11:54:47 -07004154 windowInSecondary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004155 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004156 setFocusedWindow(windowInSecondary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004157 windowInSecondary->consumeFocusEvent(true);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004158 }
4159
Prabir Pradhan3608aad2019-10-02 17:08:26 -07004160 virtual void TearDown() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004161 InputDispatcherTest::TearDown();
4162
Chris Yea209fde2020-07-22 13:54:51 -07004163 application1.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004164 windowInPrimary.clear();
Chris Yea209fde2020-07-22 13:54:51 -07004165 application2.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004166 windowInSecondary.clear();
4167 }
4168
4169protected:
Chris Yea209fde2020-07-22 13:54:51 -07004170 std::shared_ptr<FakeApplicationHandle> application1;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004171 sp<FakeWindowHandle> windowInPrimary;
Chris Yea209fde2020-07-22 13:54:51 -07004172 std::shared_ptr<FakeApplicationHandle> application2;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004173 sp<FakeWindowHandle> windowInSecondary;
4174};
4175
4176TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) {
4177 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004178 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4179 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
4180 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004181 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08004182 windowInSecondary->assertNoEvents();
4183
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004184 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004185 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4186 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
4187 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08004188 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004189 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +08004190}
4191
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004192TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) {
Tiger Huang721e26f2018-07-24 22:26:19 +08004193 // Test inject a key down with display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08004194 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4195 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004196 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004197 windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Tiger Huang721e26f2018-07-24 22:26:19 +08004198 windowInSecondary->assertNoEvents();
4199
4200 // Test inject a key down without display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08004201 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004202 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08004203 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004204 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hungb92218b2018-08-14 12:00:21 +08004205
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08004206 // Remove all windows in secondary display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00004207 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}});
Arthur Hungb92218b2018-08-14 12:00:21 +08004208
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004209 // Old focus should receive a cancel event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004210 windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE,
4211 AKEY_EVENT_FLAG_CANCELED);
Arthur Hungb92218b2018-08-14 12:00:21 +08004212
4213 // Test inject a key down, should timeout because of no target window.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08004214 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004215 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Arthur Hungb92218b2018-08-14 12:00:21 +08004216 windowInPrimary->assertNoEvents();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004217 windowInSecondary->consumeFocusEvent(false);
Arthur Hungb92218b2018-08-14 12:00:21 +08004218 windowInSecondary->assertNoEvents();
4219}
4220
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004221// Test per-display input monitors for motion event.
4222TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) {
chaviwd1c23182019-12-20 18:44:56 -08004223 FakeMonitorReceiver monitorInPrimary =
4224 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
4225 FakeMonitorReceiver monitorInSecondary =
4226 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004227
4228 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004229 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4230 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
4231 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004232 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08004233 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004234 windowInSecondary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08004235 monitorInSecondary.assertNoEvents();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004236
4237 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004238 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4239 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
4240 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004241 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08004242 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004243 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
chaviwd1c23182019-12-20 18:44:56 -08004244 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004245
4246 // Test inject a non-pointer motion event.
4247 // If specific a display, it will dispatch to the focused window of particular display,
4248 // or it will dispatch to the focused window of focused display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004249 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4250 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE))
4251 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004252 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08004253 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004254 windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08004255 monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004256}
4257
4258// Test per-display input monitors for key event.
4259TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) {
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004260 // Input monitor per display.
chaviwd1c23182019-12-20 18:44:56 -08004261 FakeMonitorReceiver monitorInPrimary =
4262 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
4263 FakeMonitorReceiver monitorInSecondary =
4264 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004265
4266 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004267 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4268 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004269 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08004270 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004271 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08004272 monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004273}
4274
Vishnu Nair958da932020-08-21 17:12:37 -07004275TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) {
4276 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004277 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07004278 secondWindowInPrimary->setFocusable(true);
4279 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}});
4280 setFocusedWindow(secondWindowInPrimary);
4281 windowInPrimary->consumeFocusEvent(false);
4282 secondWindowInPrimary->consumeFocusEvent(true);
4283
4284 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004285 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
4286 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004287 windowInPrimary->assertNoEvents();
4288 windowInSecondary->assertNoEvents();
4289 secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4290}
4291
Arthur Hungdfd528e2021-12-08 13:23:04 +00004292TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CancelTouch_MultiDisplay) {
4293 FakeMonitorReceiver monitorInPrimary =
4294 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
4295 FakeMonitorReceiver monitorInSecondary =
4296 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
4297
4298 // Test touch down on primary display.
4299 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4300 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
4301 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4302 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4303 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
4304
4305 // Test touch down on second display.
4306 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4307 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
4308 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4309 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
4310 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
4311
4312 // Trigger cancel touch.
4313 mDispatcher->cancelCurrentTouch();
4314 windowInPrimary->consumeMotionCancel(ADISPLAY_ID_DEFAULT);
4315 monitorInPrimary.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
4316 windowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID);
4317 monitorInSecondary.consumeMotionCancel(SECOND_DISPLAY_ID);
4318
4319 // Test inject a move motion event, no window/monitor should receive the event.
4320 ASSERT_EQ(InputEventInjectionResult::FAILED,
4321 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4322 ADISPLAY_ID_DEFAULT, {110, 200}))
4323 << "Inject motion event should return InputEventInjectionResult::FAILED";
4324 windowInPrimary->assertNoEvents();
4325 monitorInPrimary.assertNoEvents();
4326
4327 ASSERT_EQ(InputEventInjectionResult::FAILED,
4328 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4329 SECOND_DISPLAY_ID, {110, 200}))
4330 << "Inject motion event should return InputEventInjectionResult::FAILED";
4331 windowInSecondary->assertNoEvents();
4332 monitorInSecondary.assertNoEvents();
4333}
4334
Jackal Guof9696682018-10-05 12:23:23 +08004335class InputFilterTest : public InputDispatcherTest {
4336protected:
Prabir Pradhan81420cc2021-09-06 10:28:50 -07004337 void testNotifyMotion(int32_t displayId, bool expectToBeFiltered,
4338 const ui::Transform& transform = ui::Transform()) {
Jackal Guof9696682018-10-05 12:23:23 +08004339 NotifyMotionArgs motionArgs;
4340
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004341 motionArgs =
4342 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Jackal Guof9696682018-10-05 12:23:23 +08004343 mDispatcher->notifyMotion(&motionArgs);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004344 motionArgs =
4345 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Jackal Guof9696682018-10-05 12:23:23 +08004346 mDispatcher->notifyMotion(&motionArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004347 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08004348 if (expectToBeFiltered) {
Prabir Pradhan81420cc2021-09-06 10:28:50 -07004349 const auto xy = transform.transform(motionArgs.pointerCoords->getXYValue());
4350 mFakePolicy->assertFilterInputEventWasCalled(motionArgs, xy);
Jackal Guof9696682018-10-05 12:23:23 +08004351 } else {
4352 mFakePolicy->assertFilterInputEventWasNotCalled();
4353 }
4354 }
4355
4356 void testNotifyKey(bool expectToBeFiltered) {
4357 NotifyKeyArgs keyArgs;
4358
4359 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
4360 mDispatcher->notifyKey(&keyArgs);
4361 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP);
4362 mDispatcher->notifyKey(&keyArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004363 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08004364
4365 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08004366 mFakePolicy->assertFilterInputEventWasCalled(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08004367 } else {
4368 mFakePolicy->assertFilterInputEventWasNotCalled();
4369 }
4370 }
4371};
4372
4373// Test InputFilter for MotionEvent
4374TEST_F(InputFilterTest, MotionEvent_InputFilter) {
4375 // Since the InputFilter is disabled by default, check if touch events aren't filtered.
4376 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
4377 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
4378
4379 // Enable InputFilter
4380 mDispatcher->setInputFilterEnabled(true);
4381 // Test touch on both primary and second display, and check if both events are filtered.
4382 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true);
4383 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true);
4384
4385 // Disable InputFilter
4386 mDispatcher->setInputFilterEnabled(false);
4387 // Test touch on both primary and second display, and check if both events aren't filtered.
4388 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
4389 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
4390}
4391
4392// Test InputFilter for KeyEvent
4393TEST_F(InputFilterTest, KeyEvent_InputFilter) {
4394 // Since the InputFilter is disabled by default, check if key event aren't filtered.
4395 testNotifyKey(/*expectToBeFiltered*/ false);
4396
4397 // Enable InputFilter
4398 mDispatcher->setInputFilterEnabled(true);
4399 // Send a key event, and check if it is filtered.
4400 testNotifyKey(/*expectToBeFiltered*/ true);
4401
4402 // Disable InputFilter
4403 mDispatcher->setInputFilterEnabled(false);
4404 // Send a key event, and check if it isn't filtered.
4405 testNotifyKey(/*expectToBeFiltered*/ false);
4406}
4407
Prabir Pradhan81420cc2021-09-06 10:28:50 -07004408// Ensure that MotionEvents sent to the InputFilter through InputListener are converted to the
4409// logical display coordinate space.
4410TEST_F(InputFilterTest, MotionEvent_UsesLogicalDisplayCoordinates_notifyMotion) {
4411 ui::Transform firstDisplayTransform;
4412 firstDisplayTransform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
4413 ui::Transform secondDisplayTransform;
4414 secondDisplayTransform.set({-6.6, -5.5, -4.4, -3.3, -2.2, -1.1, 0, 0, 1});
4415
4416 std::vector<gui::DisplayInfo> displayInfos(2);
4417 displayInfos[0].displayId = ADISPLAY_ID_DEFAULT;
4418 displayInfos[0].transform = firstDisplayTransform;
4419 displayInfos[1].displayId = SECOND_DISPLAY_ID;
4420 displayInfos[1].transform = secondDisplayTransform;
4421
4422 mDispatcher->onWindowInfosChanged({}, displayInfos);
4423
4424 // Enable InputFilter
4425 mDispatcher->setInputFilterEnabled(true);
4426
4427 // Ensure the correct transforms are used for the displays.
4428 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true, firstDisplayTransform);
4429 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true, secondDisplayTransform);
4430}
4431
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004432class InputFilterInjectionPolicyTest : public InputDispatcherTest {
4433protected:
4434 virtual void SetUp() override {
4435 InputDispatcherTest::SetUp();
4436
4437 /**
4438 * We don't need to enable input filter to test the injected event policy, but we enabled it
4439 * here to make the tests more realistic, since this policy only matters when inputfilter is
4440 * on.
4441 */
4442 mDispatcher->setInputFilterEnabled(true);
4443
4444 std::shared_ptr<InputApplicationHandle> application =
4445 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004446 mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Test Window",
4447 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004448
4449 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
4450 mWindow->setFocusable(true);
4451 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
4452 setFocusedWindow(mWindow);
4453 mWindow->consumeFocusEvent(true);
4454 }
4455
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00004456 void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
4457 int32_t flags) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004458 KeyEvent event;
4459
4460 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
4461 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD,
4462 ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A,
4463 KEY_A, AMETA_NONE, 0 /*repeatCount*/, eventTime, eventTime);
4464 const int32_t additionalPolicyFlags =
4465 POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT;
4466 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Prabir Pradhan5735a322022-04-11 17:23:34 +00004467 mDispatcher->injectInputEvent(&event, {} /*targetUid*/,
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004468 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
4469 policyFlags | additionalPolicyFlags));
4470
4471 InputEvent* received = mWindow->consume();
4472 ASSERT_NE(nullptr, received);
4473 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00004474 ASSERT_EQ(received->getType(), AINPUT_EVENT_TYPE_KEY);
4475 KeyEvent& keyEvent = static_cast<KeyEvent&>(*received);
4476 ASSERT_EQ(flags, keyEvent.getFlags());
4477 }
4478
4479 void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
4480 int32_t flags) {
4481 MotionEvent event;
4482 PointerProperties pointerProperties[1];
4483 PointerCoords pointerCoords[1];
4484 pointerProperties[0].clear();
4485 pointerProperties[0].id = 0;
4486 pointerCoords[0].clear();
4487 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300);
4488 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400);
4489
4490 ui::Transform identityTransform;
4491 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
4492 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN,
4493 DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0,
4494 AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE,
4495 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -07004496 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, eventTime,
Evan Rosky09576692021-07-01 12:22:09 -07004497 eventTime,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00004498 /*pointerCount*/ 1, pointerProperties, pointerCoords);
4499
4500 const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER;
4501 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Prabir Pradhan5735a322022-04-11 17:23:34 +00004502 mDispatcher->injectInputEvent(&event, {} /*targetUid*/,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00004503 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
4504 policyFlags | additionalPolicyFlags));
4505
4506 InputEvent* received = mWindow->consume();
4507 ASSERT_NE(nullptr, received);
4508 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
4509 ASSERT_EQ(received->getType(), AINPUT_EVENT_TYPE_MOTION);
4510 MotionEvent& motionEvent = static_cast<MotionEvent&>(*received);
4511 ASSERT_EQ(flags, motionEvent.getFlags());
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004512 }
4513
4514private:
4515 sp<FakeWindowHandle> mWindow;
4516};
4517
4518TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) {
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00004519 // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input
4520 // filter. Without it, the event will no different from a regularly injected event, and the
4521 // injected device id will be overwritten.
4522 testInjectedKey(POLICY_FLAG_FILTERED, 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/,
4523 0 /*flags*/);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004524}
4525
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00004526TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004527 testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00004528 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/,
4529 AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
4530}
4531
4532TEST_F(InputFilterInjectionPolicyTest,
4533 MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
4534 testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
4535 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/,
4536 AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004537}
4538
4539TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) {
4540 testInjectedKey(0 /*policyFlags*/, 3 /*injectedDeviceId*/,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00004541 VIRTUAL_KEYBOARD_ID /*resolvedDeviceId*/, 0 /*flags*/);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004542}
4543
chaviwfd6d3512019-03-25 13:23:49 -07004544class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest {
Prabir Pradhan3608aad2019-10-02 17:08:26 -07004545 virtual void SetUp() override {
chaviwfd6d3512019-03-25 13:23:49 -07004546 InputDispatcherTest::SetUp();
4547
Chris Yea209fde2020-07-22 13:54:51 -07004548 std::shared_ptr<FakeApplicationHandle> application =
4549 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004550 mUnfocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004551 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07004552 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
chaviwfd6d3512019-03-25 13:23:49 -07004553
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08004554 mFocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004555 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08004556 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
chaviwfd6d3512019-03-25 13:23:49 -07004557
4558 // Set focused application.
4559 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07004560 mFocusedWindow->setFocusable(true);
chaviwfd6d3512019-03-25 13:23:49 -07004561
4562 // Expect one focus window exist in display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00004563 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004564 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004565 mFocusedWindow->consumeFocusEvent(true);
chaviwfd6d3512019-03-25 13:23:49 -07004566 }
4567
Prabir Pradhan3608aad2019-10-02 17:08:26 -07004568 virtual void TearDown() override {
chaviwfd6d3512019-03-25 13:23:49 -07004569 InputDispatcherTest::TearDown();
4570
4571 mUnfocusedWindow.clear();
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08004572 mFocusedWindow.clear();
chaviwfd6d3512019-03-25 13:23:49 -07004573 }
4574
4575protected:
4576 sp<FakeWindowHandle> mUnfocusedWindow;
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08004577 sp<FakeWindowHandle> mFocusedWindow;
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07004578 static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60};
chaviwfd6d3512019-03-25 13:23:49 -07004579};
4580
4581// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
4582// DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received
4583// the onPointerDownOutsideFocus callback.
4584TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004585 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07004586 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4587 {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004588 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07004589 mUnfocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07004590
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004591 ASSERT_TRUE(mDispatcher->waitForIdle());
chaviwfd6d3512019-03-25 13:23:49 -07004592 mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken());
4593}
4594
4595// Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action
4596// DOWN on the window that doesn't have focus. Ensure no window received the
4597// onPointerDownOutsideFocus callback.
4598TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004599 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07004600 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004601 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07004602 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07004603
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004604 ASSERT_TRUE(mDispatcher->waitForIdle());
4605 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07004606}
4607
4608// Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't
4609// have focus. Ensure no window received the onPointerDownOutsideFocus callback.
4610TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08004611 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4612 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004613 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07004614 mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07004615
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004616 ASSERT_TRUE(mDispatcher->waitForIdle());
4617 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07004618}
4619
4620// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
4621// DOWN on the window that already has focus. Ensure no window received the
4622// onPointerDownOutsideFocus callback.
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004623TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004624 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08004625 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07004626 FOCUSED_WINDOW_TOUCH_POINT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004627 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07004628 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07004629
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004630 ASSERT_TRUE(mDispatcher->waitForIdle());
4631 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07004632}
4633
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08004634// Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag
4635// NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback.
4636TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) {
4637 const MotionEvent event =
4638 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
4639 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
4640 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(20).y(20))
4641 .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE)
4642 .build();
4643 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(mDispatcher, event))
4644 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4645 mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
4646
4647 ASSERT_TRUE(mDispatcher->waitForIdle());
4648 mFakePolicy->assertOnPointerDownWasNotCalled();
4649 // Ensure that the unfocused window did not receive any FOCUS events.
4650 mUnfocusedWindow->assertNoEvents();
4651}
4652
chaviwaf87b3e2019-10-01 16:59:28 -07004653// These tests ensures we can send touch events to a single client when there are multiple input
4654// windows that point to the same client token.
4655class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest {
4656 virtual void SetUp() override {
4657 InputDispatcherTest::SetUp();
4658
Chris Yea209fde2020-07-22 13:54:51 -07004659 std::shared_ptr<FakeApplicationHandle> application =
4660 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004661 mWindow1 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 1",
4662 ADISPLAY_ID_DEFAULT);
chaviwaf87b3e2019-10-01 16:59:28 -07004663 mWindow1->setFrame(Rect(0, 0, 100, 100));
4664
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004665 mWindow2 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 2",
4666 ADISPLAY_ID_DEFAULT, mWindow1->getToken());
chaviwaf87b3e2019-10-01 16:59:28 -07004667 mWindow2->setFrame(Rect(100, 100, 200, 200));
4668
Arthur Hung72d8dc32020-03-28 00:48:39 +00004669 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
chaviwaf87b3e2019-10-01 16:59:28 -07004670 }
4671
4672protected:
4673 sp<FakeWindowHandle> mWindow1;
4674 sp<FakeWindowHandle> mWindow2;
4675
4676 // Helper function to convert the point from screen coordinates into the window's space
chaviw3277faf2021-05-19 16:45:23 -05004677 static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) {
chaviw1ff3d1e2020-07-01 15:53:47 -07004678 vec2 vals = windowInfo->transform.transform(point.x, point.y);
4679 return {vals.x, vals.y};
chaviwaf87b3e2019-10-01 16:59:28 -07004680 }
4681
4682 void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction,
4683 const std::vector<PointF>& points) {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004684 const std::string name = window->getName();
chaviwaf87b3e2019-10-01 16:59:28 -07004685 InputEvent* event = window->consume();
4686
4687 ASSERT_NE(nullptr, event) << name.c_str()
4688 << ": consumer should have returned non-NULL event.";
4689
4690 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType())
4691 << name.c_str() << "expected " << inputEventTypeToString(AINPUT_EVENT_TYPE_MOTION)
4692 << " event, got " << inputEventTypeToString(event->getType()) << " event";
4693
4694 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004695 assertMotionAction(expectedAction, motionEvent.getAction());
chaviwaf87b3e2019-10-01 16:59:28 -07004696
4697 for (size_t i = 0; i < points.size(); i++) {
4698 float expectedX = points[i].x;
4699 float expectedY = points[i].y;
4700
4701 EXPECT_EQ(expectedX, motionEvent.getX(i))
4702 << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str()
4703 << ", got " << motionEvent.getX(i);
4704 EXPECT_EQ(expectedY, motionEvent.getY(i))
4705 << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str()
4706 << ", got " << motionEvent.getY(i);
4707 }
4708 }
chaviw9eaa22c2020-07-01 16:21:27 -07004709
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08004710 void touchAndAssertPositions(int32_t action, const std::vector<PointF>& touchedPoints,
chaviw9eaa22c2020-07-01 16:21:27 -07004711 std::vector<PointF> expectedPoints) {
4712 NotifyMotionArgs motionArgs = generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN,
4713 ADISPLAY_ID_DEFAULT, touchedPoints);
4714 mDispatcher->notifyMotion(&motionArgs);
4715
4716 // Always consume from window1 since it's the window that has the InputReceiver
4717 consumeMotionEvent(mWindow1, action, expectedPoints);
4718 }
chaviwaf87b3e2019-10-01 16:59:28 -07004719};
4720
4721TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) {
4722 // Touch Window 1
4723 PointF touchedPoint = {10, 10};
4724 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07004725 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07004726
4727 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07004728 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07004729
4730 // Touch Window 2
4731 touchedPoint = {150, 150};
4732 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07004733 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07004734}
4735
chaviw9eaa22c2020-07-01 16:21:27 -07004736TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) {
4737 // Set scale value for window2
chaviwaf87b3e2019-10-01 16:59:28 -07004738 mWindow2->setWindowScale(0.5f, 0.5f);
4739
4740 // Touch Window 1
4741 PointF touchedPoint = {10, 10};
4742 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07004743 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07004744 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07004745 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07004746
4747 // Touch Window 2
4748 touchedPoint = {150, 150};
4749 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07004750 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
4751 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07004752
chaviw9eaa22c2020-07-01 16:21:27 -07004753 // Update the transform so rotation is set
4754 mWindow2->setWindowTransform(0, -1, 1, 0);
4755 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
4756 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07004757}
4758
chaviw9eaa22c2020-07-01 16:21:27 -07004759TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004760 mWindow2->setWindowScale(0.5f, 0.5f);
4761
4762 // Touch Window 1
4763 std::vector<PointF> touchedPoints = {PointF{10, 10}};
4764 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07004765 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004766
4767 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07004768 touchedPoints.push_back(PointF{150, 150});
4769 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08004770 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004771
chaviw9eaa22c2020-07-01 16:21:27 -07004772 // Release Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08004773 touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07004774 expectedPoints.pop_back();
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004775
chaviw9eaa22c2020-07-01 16:21:27 -07004776 // Update the transform so rotation is set for Window 2
4777 mWindow2->setWindowTransform(0, -1, 1, 0);
4778 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08004779 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004780}
4781
chaviw9eaa22c2020-07-01 16:21:27 -07004782TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004783 mWindow2->setWindowScale(0.5f, 0.5f);
4784
4785 // Touch Window 1
4786 std::vector<PointF> touchedPoints = {PointF{10, 10}};
4787 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07004788 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004789
4790 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07004791 touchedPoints.push_back(PointF{150, 150});
4792 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004793
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08004794 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004795
4796 // Move both windows
4797 touchedPoints = {{20, 20}, {175, 175}};
4798 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
4799 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
4800
chaviw9eaa22c2020-07-01 16:21:27 -07004801 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004802
chaviw9eaa22c2020-07-01 16:21:27 -07004803 // Release Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08004804 touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07004805 expectedPoints.pop_back();
4806
4807 // Touch Window 2
4808 mWindow2->setWindowTransform(0, -1, 1, 0);
4809 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08004810 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07004811
4812 // Move both windows
4813 touchedPoints = {{20, 20}, {175, 175}};
4814 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
4815 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
4816
4817 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004818}
4819
4820TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) {
4821 mWindow1->setWindowScale(0.5f, 0.5f);
4822
4823 // Touch Window 1
4824 std::vector<PointF> touchedPoints = {PointF{10, 10}};
4825 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07004826 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004827
4828 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07004829 touchedPoints.push_back(PointF{150, 150});
4830 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004831
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08004832 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004833
4834 // Move both windows
4835 touchedPoints = {{20, 20}, {175, 175}};
4836 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
4837 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
4838
chaviw9eaa22c2020-07-01 16:21:27 -07004839 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004840}
4841
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004842class InputDispatcherSingleWindowAnr : public InputDispatcherTest {
4843 virtual void SetUp() override {
4844 InputDispatcherTest::SetUp();
4845
Chris Yea209fde2020-07-22 13:54:51 -07004846 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004847 mApplication->setDispatchingTimeout(20ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004848 mWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "TestWindow",
4849 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004850 mWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05004851 mWindow->setDispatchingTimeout(30ms);
Vishnu Nair47074b82020-08-14 11:54:47 -07004852 mWindow->setFocusable(true);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004853
4854 // Set focused application.
4855 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
4856
4857 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004858 setFocusedWindow(mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004859 mWindow->consumeFocusEvent(true);
4860 }
4861
4862 virtual void TearDown() override {
4863 InputDispatcherTest::TearDown();
4864 mWindow.clear();
4865 }
4866
4867protected:
Chris Yea209fde2020-07-22 13:54:51 -07004868 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004869 sp<FakeWindowHandle> mWindow;
4870 static constexpr PointF WINDOW_LOCATION = {20, 20};
4871
4872 void tapOnWindow() {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004873 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004874 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4875 WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004876 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004877 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4878 WINDOW_LOCATION));
4879 }
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004880
4881 sp<FakeWindowHandle> addSpyWindow() {
4882 sp<FakeWindowHandle> spy =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004883 sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004884 spy->setTrustedOverlay(true);
4885 spy->setFocusable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08004886 spy->setSpy(true);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004887 spy->setDispatchingTimeout(30ms);
4888 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, mWindow}}});
4889 return spy;
4890 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004891};
4892
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004893// Send a tap and respond, which should not cause an ANR.
4894TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) {
4895 tapOnWindow();
4896 mWindow->consumeMotionDown();
4897 mWindow->consumeMotionUp();
4898 ASSERT_TRUE(mDispatcher->waitForIdle());
4899 mFakePolicy->assertNotifyAnrWasNotCalled();
4900}
4901
4902// Send a regular key and respond, which should not cause an ANR.
4903TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08004904 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004905 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
4906 ASSERT_TRUE(mDispatcher->waitForIdle());
4907 mFakePolicy->assertNotifyAnrWasNotCalled();
4908}
4909
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05004910TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) {
4911 mWindow->setFocusable(false);
4912 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
4913 mWindow->consumeFocusEvent(false);
4914
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004915 InputEventInjectionResult result =
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05004916 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08004917 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/,
4918 false /* allowKeyRepeat */);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004919 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05004920 // Key will not go to window because we have no focused window.
4921 // The 'no focused window' ANR timer should start instead.
4922
4923 // Now, the focused application goes away.
4924 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr);
4925 // The key should get dropped and there should be no ANR.
4926
4927 ASSERT_TRUE(mDispatcher->waitForIdle());
4928 mFakePolicy->assertNotifyAnrWasNotCalled();
4929}
4930
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004931// Send an event to the app and have the app not respond right away.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004932// When ANR is raised, policy will tell the dispatcher to cancel the events for that window.
4933// So InputDispatcher will enqueue ACTION_CANCEL event as well.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004934TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004935 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004936 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4937 WINDOW_LOCATION));
4938
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004939 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN
4940 ASSERT_TRUE(sequenceNum);
4941 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08004942 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004943
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004944 mWindow->finishEvent(*sequenceNum);
4945 mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL,
4946 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004947 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08004948 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004949}
4950
4951// Send a key to the app and have the app not respond right away.
4952TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) {
4953 // Inject a key, and don't respond - expect that ANR is called.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08004954 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004955 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent();
4956 ASSERT_TRUE(sequenceNum);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004957 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08004958 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004959 ASSERT_TRUE(mDispatcher->waitForIdle());
4960}
4961
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004962// We have a focused application, but no focused window
4963TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) {
Vishnu Nair47074b82020-08-14 11:54:47 -07004964 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004965 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
4966 mWindow->consumeFocusEvent(false);
4967
4968 // taps on the window work as normal
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004969 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004970 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4971 WINDOW_LOCATION));
4972 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
4973 mDispatcher->waitForIdle();
4974 mFakePolicy->assertNotifyAnrWasNotCalled();
4975
4976 // Once a focused event arrives, we get an ANR for this application
4977 // We specify the injection timeout to be smaller than the application timeout, to ensure that
4978 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004979 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004980 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08004981 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, false /* allowKeyRepeat */);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004982 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004983 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Vishnu Naire4df8752022-09-08 09:17:55 -07004984 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004985 ASSERT_TRUE(mDispatcher->waitForIdle());
4986}
4987
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08004988/**
4989 * Make sure the stale key is dropped before causing an ANR. So even if there's no focused window,
4990 * there will not be an ANR.
4991 */
4992TEST_F(InputDispatcherSingleWindowAnr, StaleKeyEventDoesNotAnr) {
4993 mWindow->setFocusable(false);
4994 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
4995 mWindow->consumeFocusEvent(false);
4996
4997 KeyEvent event;
4998 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC) -
4999 std::chrono::nanoseconds(STALE_EVENT_TIMEOUT).count();
5000
5001 // Define a valid key down event that is stale (too old).
5002 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
5003 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, /* flags */ 0, AKEYCODE_A, KEY_A,
5004 AMETA_NONE, 1 /*repeatCount*/, eventTime, eventTime);
5005
5006 const int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER;
5007
5008 InputEventInjectionResult result =
Prabir Pradhan5735a322022-04-11 17:23:34 +00005009 mDispatcher->injectInputEvent(&event, {} /* targetUid */,
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08005010 InputEventInjectionSync::WAIT_FOR_RESULT,
5011 INJECT_EVENT_TIMEOUT, policyFlags);
5012 ASSERT_EQ(InputEventInjectionResult::FAILED, result)
5013 << "Injection should fail because the event is stale";
5014
5015 ASSERT_TRUE(mDispatcher->waitForIdle());
5016 mFakePolicy->assertNotifyAnrWasNotCalled();
5017 mWindow->assertNoEvents();
5018}
5019
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005020// We have a focused application, but no focused window
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005021// Make sure that we don't notify policy twice about the same ANR.
5022TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) {
Vishnu Nair47074b82020-08-14 11:54:47 -07005023 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005024 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
5025 mWindow->consumeFocusEvent(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005026
5027 // Once a focused event arrives, we get an ANR for this application
5028 // We specify the injection timeout to be smaller than the application timeout, to ensure that
5029 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005030 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005031 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005032 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, false /* allowKeyRepeat */);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005033 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Vishnu Naire4df8752022-09-08 09:17:55 -07005034 const std::chrono::duration appTimeout =
5035 mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
5036 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005037
Vishnu Naire4df8752022-09-08 09:17:55 -07005038 std::this_thread::sleep_for(appTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005039 // ANR should not be raised again. It is up to policy to do that if it desires.
5040 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005041
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005042 // If we now get a focused window, the ANR should stop, but the policy handles that via
5043 // 'notifyFocusChanged' callback. This is implemented in the policy so we can't test it here.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005044 ASSERT_TRUE(mDispatcher->waitForIdle());
5045}
5046
5047// We have a focused application, but no focused window
5048TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) {
Vishnu Nair47074b82020-08-14 11:54:47 -07005049 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005050 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
5051 mWindow->consumeFocusEvent(false);
5052
5053 // Once a focused event arrives, we get an ANR for this application
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005054 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005055 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005056 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
5057 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005058
Vishnu Naire4df8752022-09-08 09:17:55 -07005059 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
5060 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005061
5062 // Future focused events get dropped right away
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005063 ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005064 ASSERT_TRUE(mDispatcher->waitForIdle());
5065 mWindow->assertNoEvents();
5066}
5067
5068/**
5069 * Ensure that the implementation is valid. Since we are using multiset to keep track of the
5070 * ANR timeouts, we are allowing entries with identical timestamps in the same connection.
5071 * If we process 1 of the events, but ANR on the second event with the same timestamp,
5072 * the ANR mechanism should still work.
5073 *
5074 * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the
5075 * DOWN event, while not responding on the second one.
5076 */
5077TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) {
5078 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
5079 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5080 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
5081 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
5082 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005083 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005084
5085 // Now send ACTION_UP, with identical timestamp
5086 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
5087 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
5088 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
5089 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005090 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005091
5092 // We have now sent down and up. Let's consume first event and then ANR on the second.
5093 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5094 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005095 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005096}
5097
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005098// A spy window can receive an ANR
5099TEST_F(InputDispatcherSingleWindowAnr, SpyWindowAnr) {
5100 sp<FakeWindowHandle> spy = addSpyWindow();
5101
5102 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5103 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5104 WINDOW_LOCATION));
5105 mWindow->consumeMotionDown();
5106
5107 std::optional<uint32_t> sequenceNum = spy->receiveEvent(); // ACTION_DOWN
5108 ASSERT_TRUE(sequenceNum);
5109 const std::chrono::duration timeout = spy->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005110 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, spy);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005111
5112 spy->finishEvent(*sequenceNum);
5113 spy->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, ADISPLAY_ID_DEFAULT,
5114 0 /*flags*/);
5115 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08005116 mFakePolicy->assertNotifyWindowResponsiveWasCalled(spy->getToken(), mWindow->getPid());
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005117}
5118
5119// If an app is not responding to a key event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005120// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005121TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnKey) {
5122 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005123
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005124 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5125 injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005126 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005127 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005128
5129 // Stuck on the ACTION_UP
5130 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005131 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005132
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005133 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005134 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005135 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5136 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005137
5138 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion
5139 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08005140 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005141 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005142 spy->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005143}
5144
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005145// If an app is not responding to a motion event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005146// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005147TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnMotion) {
5148 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005149
5150 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005151 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5152 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005153
5154 mWindow->consumeMotionDown();
5155 // Stuck on the ACTION_UP
5156 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005157 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005158
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005159 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005160 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005161 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5162 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005163
5164 mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion
5165 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08005166 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005167 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005168 spy->assertNoEvents();
5169}
5170
5171TEST_F(InputDispatcherSingleWindowAnr, UnresponsiveMonitorAnr) {
5172 mDispatcher->setMonitorDispatchingTimeoutForTest(30ms);
5173
5174 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
5175
5176 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5177 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5178 WINDOW_LOCATION));
5179
5180 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5181 const std::optional<uint32_t> consumeSeq = monitor.receiveEvent();
5182 ASSERT_TRUE(consumeSeq);
5183
Prabir Pradhanedd96402022-02-15 01:46:16 -08005184 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(30ms, monitor.getToken(), MONITOR_PID);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005185
5186 monitor.finishEvent(*consumeSeq);
5187 monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
5188
5189 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08005190 mFakePolicy->assertNotifyWindowResponsiveWasCalled(monitor.getToken(), MONITOR_PID);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005191}
5192
5193// If a window is unresponsive, then you get anr. if the window later catches up and starts to
5194// process events, you don't get an anr. When the window later becomes unresponsive again, you
5195// get an ANR again.
5196// 1. tap -> block on ACTION_UP -> receive ANR
5197// 2. consume all pending events (= queue becomes healthy again)
5198// 3. tap again -> block on ACTION_UP again -> receive ANR second time
5199TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) {
5200 tapOnWindow();
5201
5202 mWindow->consumeMotionDown();
5203 // Block on ACTION_UP
5204 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005205 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005206 mWindow->consumeMotionUp(); // Now the connection should be healthy again
5207 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08005208 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005209 mWindow->assertNoEvents();
5210
5211 tapOnWindow();
5212 mWindow->consumeMotionDown();
Prabir Pradhanedd96402022-02-15 01:46:16 -08005213 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005214 mWindow->consumeMotionUp();
5215
5216 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08005217 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005218 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005219 mWindow->assertNoEvents();
5220}
5221
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005222// If a connection remains unresponsive for a while, make sure policy is only notified once about
5223// it.
5224TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005225 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005226 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5227 WINDOW_LOCATION));
5228
5229 const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005230 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005231 std::this_thread::sleep_for(windowTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005232 // 'notifyConnectionUnresponsive' should only be called once per connection
5233 mFakePolicy->assertNotifyAnrWasNotCalled();
5234 // When the ANR happened, dispatcher should abort the current event stream via ACTION_CANCEL
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005235 mWindow->consumeMotionDown();
5236 mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL,
5237 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
5238 mWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005239 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08005240 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005241 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005242}
5243
5244/**
5245 * If a window is processing a motion event, and then a key event comes in, the key event should
5246 * not to to the focused window until the motion is processed.
5247 *
5248 * Warning!!!
5249 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
5250 * and the injection timeout that we specify when injecting the key.
5251 * We must have the injection timeout (10ms) be smaller than
5252 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
5253 *
5254 * If that value changes, this test should also change.
5255 */
5256TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) {
5257 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
5258 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
5259
5260 tapOnWindow();
5261 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
5262 ASSERT_TRUE(downSequenceNum);
5263 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
5264 ASSERT_TRUE(upSequenceNum);
5265 // Don't finish the events yet, and send a key
5266 // Injection will "succeed" because we will eventually give up and send the key to the focused
5267 // window even if motions are still being processed. But because the injection timeout is short,
5268 // we will receive INJECTION_TIMED_OUT as the result.
5269
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005270 InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005271 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005272 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
5273 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005274 // Key will not be sent to the window, yet, because the window is still processing events
5275 // and the key remains pending, waiting for the touch events to be processed
5276 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
5277 ASSERT_FALSE(keySequenceNum);
5278
5279 std::this_thread::sleep_for(500ms);
5280 // if we wait long enough though, dispatcher will give up, and still send the key
5281 // to the focused window, even though we have not yet finished the motion event
5282 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5283 mWindow->finishEvent(*downSequenceNum);
5284 mWindow->finishEvent(*upSequenceNum);
5285}
5286
5287/**
5288 * If a window is processing a motion event, and then a key event comes in, the key event should
5289 * not go to the focused window until the motion is processed.
5290 * If then a new motion comes in, then the pending key event should be going to the currently
5291 * focused window right away.
5292 */
5293TEST_F(InputDispatcherSingleWindowAnr,
5294 PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) {
5295 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
5296 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
5297
5298 tapOnWindow();
5299 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
5300 ASSERT_TRUE(downSequenceNum);
5301 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
5302 ASSERT_TRUE(upSequenceNum);
5303 // Don't finish the events yet, and send a key
5304 // Injection is async, so it will succeed
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005305 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005306 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005307 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005308 // At this point, key is still pending, and should not be sent to the application yet.
5309 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
5310 ASSERT_FALSE(keySequenceNum);
5311
5312 // Now tap down again. It should cause the pending key to go to the focused window right away.
5313 tapOnWindow();
5314 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd
5315 // the other events yet. We can finish events in any order.
5316 mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN
5317 mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP
5318 mWindow->consumeMotionDown();
5319 mWindow->consumeMotionUp();
5320 mWindow->assertNoEvents();
5321}
5322
5323class InputDispatcherMultiWindowAnr : public InputDispatcherTest {
5324 virtual void SetUp() override {
5325 InputDispatcherTest::SetUp();
5326
Chris Yea209fde2020-07-22 13:54:51 -07005327 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005328 mApplication->setDispatchingTimeout(10ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005329 mUnfocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Unfocused",
5330 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005331 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005332 // Adding FLAG_WATCH_OUTSIDE_TOUCH to receive ACTION_OUTSIDE when another window is tapped
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08005333 mUnfocusedWindow->setWatchOutsideTouch(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005334
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005335 mFocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Focused",
5336 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05005337 mFocusedWindow->setDispatchingTimeout(30ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005338 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005339
5340 // Set focused application.
5341 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
Vishnu Nair47074b82020-08-14 11:54:47 -07005342 mFocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005343
5344 // Expect one focus window exist in display.
5345 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005346 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005347 mFocusedWindow->consumeFocusEvent(true);
5348 }
5349
5350 virtual void TearDown() override {
5351 InputDispatcherTest::TearDown();
5352
5353 mUnfocusedWindow.clear();
5354 mFocusedWindow.clear();
5355 }
5356
5357protected:
Chris Yea209fde2020-07-22 13:54:51 -07005358 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005359 sp<FakeWindowHandle> mUnfocusedWindow;
5360 sp<FakeWindowHandle> mFocusedWindow;
5361 static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20};
5362 static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75};
5363 static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40};
5364
5365 void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); }
5366
5367 void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); }
5368
5369private:
5370 void tap(const PointF& location) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005371 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005372 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5373 location));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005374 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005375 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5376 location));
5377 }
5378};
5379
5380// If we have 2 windows that are both unresponsive, the one with the shortest timeout
5381// should be ANR'd first.
5382TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005383 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005384 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5385 FOCUSED_WINDOW_LOCATION))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005386 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005387 mFocusedWindow->consumeMotionDown();
5388 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
5389 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
5390 // We consumed all events, so no ANR
5391 ASSERT_TRUE(mDispatcher->waitForIdle());
5392 mFakePolicy->assertNotifyAnrWasNotCalled();
5393
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005394 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005395 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5396 FOCUSED_WINDOW_LOCATION));
5397 std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent();
5398 ASSERT_TRUE(unfocusedSequenceNum);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005399
5400 const std::chrono::duration timeout =
5401 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005402 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005403 // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event
5404 // sequence to make it consistent
5405 mFocusedWindow->consumeMotionCancel();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005406 mUnfocusedWindow->finishEvent(*unfocusedSequenceNum);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005407 mFocusedWindow->consumeMotionDown();
5408 // This cancel is generated because the connection was unresponsive
5409 mFocusedWindow->consumeMotionCancel();
5410 mFocusedWindow->assertNoEvents();
5411 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005412 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08005413 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
5414 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005415 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005416}
5417
5418// If we have 2 windows with identical timeouts that are both unresponsive,
5419// it doesn't matter which order they should have ANR.
5420// But we should receive ANR for both.
5421TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) {
5422 // Set the timeout for unfocused window to match the focused window
5423 mUnfocusedWindow->setDispatchingTimeout(10ms);
5424 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
5425
5426 tapOnFocusedWindow();
5427 // we should have ACTION_DOWN/ACTION_UP on focused window and ACTION_OUTSIDE on unfocused window
Prabir Pradhanedd96402022-02-15 01:46:16 -08005428 sp<IBinder> anrConnectionToken1, anrConnectionToken2;
5429 ASSERT_NO_FATAL_FAILURE(anrConnectionToken1 = mFakePolicy->getUnresponsiveWindowToken(10ms));
5430 ASSERT_NO_FATAL_FAILURE(anrConnectionToken2 = mFakePolicy->getUnresponsiveWindowToken(0ms));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005431
5432 // We don't know which window will ANR first. But both of them should happen eventually.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005433 ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 ||
5434 mFocusedWindow->getToken() == anrConnectionToken2);
5435 ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 ||
5436 mUnfocusedWindow->getToken() == anrConnectionToken2);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005437
5438 ASSERT_TRUE(mDispatcher->waitForIdle());
5439 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005440
5441 mFocusedWindow->consumeMotionDown();
5442 mFocusedWindow->consumeMotionUp();
5443 mUnfocusedWindow->consumeMotionOutside();
5444
Prabir Pradhanedd96402022-02-15 01:46:16 -08005445 sp<IBinder> responsiveToken1, responsiveToken2;
5446 ASSERT_NO_FATAL_FAILURE(responsiveToken1 = mFakePolicy->getResponsiveWindowToken());
5447 ASSERT_NO_FATAL_FAILURE(responsiveToken2 = mFakePolicy->getResponsiveWindowToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005448
5449 // Both applications should be marked as responsive, in any order
5450 ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 ||
5451 mFocusedWindow->getToken() == responsiveToken2);
5452 ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 ||
5453 mUnfocusedWindow->getToken() == responsiveToken2);
5454 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005455}
5456
5457// If a window is already not responding, the second tap on the same window should be ignored.
5458// We should also log an error to account for the dropped event (not tested here).
5459// At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events.
5460TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) {
5461 tapOnFocusedWindow();
5462 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
5463 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
5464 // Receive the events, but don't respond
5465 std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN
5466 ASSERT_TRUE(downEventSequenceNum);
5467 std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP
5468 ASSERT_TRUE(upEventSequenceNum);
5469 const std::chrono::duration timeout =
5470 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005471 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005472
5473 // Tap once again
5474 // We cannot use "tapOnFocusedWindow" because it asserts the injection result to be success
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005475 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005476 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5477 FOCUSED_WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005478 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005479 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5480 FOCUSED_WINDOW_LOCATION));
5481 // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a
5482 // valid touch target
5483 mUnfocusedWindow->assertNoEvents();
5484
5485 // Consume the first tap
5486 mFocusedWindow->finishEvent(*downEventSequenceNum);
5487 mFocusedWindow->finishEvent(*upEventSequenceNum);
5488 ASSERT_TRUE(mDispatcher->waitForIdle());
5489 // The second tap did not go to the focused window
5490 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005491 // Since all events are finished, connection should be deemed healthy again
Prabir Pradhanedd96402022-02-15 01:46:16 -08005492 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
5493 mFocusedWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005494 mFakePolicy->assertNotifyAnrWasNotCalled();
5495}
5496
5497// If you tap outside of all windows, there will not be ANR
5498TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005499 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005500 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5501 LOCATION_OUTSIDE_ALL_WINDOWS));
5502 ASSERT_TRUE(mDispatcher->waitForIdle());
5503 mFakePolicy->assertNotifyAnrWasNotCalled();
5504}
5505
5506// Since the focused window is paused, tapping on it should not produce any events
5507TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) {
5508 mFocusedWindow->setPaused(true);
5509 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
5510
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005511 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005512 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5513 FOCUSED_WINDOW_LOCATION));
5514
5515 std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT));
5516 ASSERT_TRUE(mDispatcher->waitForIdle());
5517 // Should not ANR because the window is paused, and touches shouldn't go to it
5518 mFakePolicy->assertNotifyAnrWasNotCalled();
5519
5520 mFocusedWindow->assertNoEvents();
5521 mUnfocusedWindow->assertNoEvents();
5522}
5523
5524/**
5525 * If a window is processing a motion event, and then a key event comes in, the key event should
5526 * not to to the focused window until the motion is processed.
5527 * If a different window becomes focused at this time, the key should go to that window instead.
5528 *
5529 * Warning!!!
5530 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
5531 * and the injection timeout that we specify when injecting the key.
5532 * We must have the injection timeout (10ms) be smaller than
5533 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
5534 *
5535 * If that value changes, this test should also change.
5536 */
5537TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) {
5538 // Set a long ANR timeout to prevent it from triggering
5539 mFocusedWindow->setDispatchingTimeout(2s);
5540 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
5541
5542 tapOnUnfocusedWindow();
5543 std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent();
5544 ASSERT_TRUE(downSequenceNum);
5545 std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent();
5546 ASSERT_TRUE(upSequenceNum);
5547 // Don't finish the events yet, and send a key
5548 // Injection will succeed because we will eventually give up and send the key to the focused
5549 // window even if motions are still being processed.
5550
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005551 InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005552 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005553 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/);
5554 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005555 // Key will not be sent to the window, yet, because the window is still processing events
5556 // and the key remains pending, waiting for the touch events to be processed
5557 std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent();
5558 ASSERT_FALSE(keySequenceNum);
5559
5560 // Switch the focus to the "unfocused" window that we tapped. Expect the key to go there
Vishnu Nair47074b82020-08-14 11:54:47 -07005561 mFocusedWindow->setFocusable(false);
5562 mUnfocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005563 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005564 setFocusedWindow(mUnfocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005565
5566 // Focus events should precede the key events
5567 mUnfocusedWindow->consumeFocusEvent(true);
5568 mFocusedWindow->consumeFocusEvent(false);
5569
5570 // Finish the tap events, which should unblock dispatcher
5571 mUnfocusedWindow->finishEvent(*downSequenceNum);
5572 mUnfocusedWindow->finishEvent(*upSequenceNum);
5573
5574 // Now that all queues are cleared and no backlog in the connections, the key event
5575 // can finally go to the newly focused "mUnfocusedWindow".
5576 mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5577 mFocusedWindow->assertNoEvents();
5578 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005579 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005580}
5581
5582// When the touch stream is split across 2 windows, and one of them does not respond,
5583// then ANR should be raised and the touch should be canceled for the unresponsive window.
5584// The other window should not be affected by that.
5585TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) {
5586 // Touch Window 1
5587 NotifyMotionArgs motionArgs =
5588 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5589 ADISPLAY_ID_DEFAULT, {FOCUSED_WINDOW_LOCATION});
5590 mDispatcher->notifyMotion(&motionArgs);
5591 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
5592 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
5593
5594 // Touch Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08005595 motionArgs = generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5596 {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005597 mDispatcher->notifyMotion(&motionArgs);
5598
5599 const std::chrono::duration timeout =
5600 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005601 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005602
5603 mUnfocusedWindow->consumeMotionDown();
5604 mFocusedWindow->consumeMotionDown();
5605 // Focused window may or may not receive ACTION_MOVE
5606 // But it should definitely receive ACTION_CANCEL due to the ANR
5607 InputEvent* event;
5608 std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event);
5609 ASSERT_TRUE(moveOrCancelSequenceNum);
5610 mFocusedWindow->finishEvent(*moveOrCancelSequenceNum);
5611 ASSERT_NE(nullptr, event);
5612 ASSERT_EQ(event->getType(), AINPUT_EVENT_TYPE_MOTION);
5613 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
5614 if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) {
5615 mFocusedWindow->consumeMotionCancel();
5616 } else {
5617 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction());
5618 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005619 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08005620 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
5621 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005622
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005623 mUnfocusedWindow->assertNoEvents();
5624 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005625 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005626}
5627
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05005628/**
5629 * If we have no focused window, and a key comes in, we start the ANR timer.
5630 * The focused application should add a focused window before the timer runs out to prevent ANR.
5631 *
5632 * If the user touches another application during this time, the key should be dropped.
5633 * Next, if a new focused window comes in, without toggling the focused application,
5634 * then no ANR should occur.
5635 *
5636 * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication',
5637 * but in some cases the policy may not update the focused application.
5638 */
5639TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) {
5640 std::shared_ptr<FakeApplicationHandle> focusedApplication =
5641 std::make_shared<FakeApplicationHandle>();
5642 focusedApplication->setDispatchingTimeout(60ms);
5643 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication);
5644 // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused.
5645 mFocusedWindow->setFocusable(false);
5646
5647 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
5648 mFocusedWindow->consumeFocusEvent(false);
5649
5650 // Send a key. The ANR timer should start because there is no focused window.
5651 // 'focusedApplication' will get blamed if this timer completes.
5652 // Key will not be sent anywhere because we have no focused window. It will remain pending.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005653 InputEventInjectionResult result =
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05005654 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005655 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/,
5656 false /* allowKeyRepeat */);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005657 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05005658
5659 // Wait until dispatcher starts the "no focused window" timer. If we don't wait here,
5660 // then the injected touches won't cause the focused event to get dropped.
5661 // The dispatcher only checks for whether the queue should be pruned upon queueing.
5662 // If we inject the touch right away and the ANR timer hasn't started, the touch event would
5663 // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'.
5664 // For this test, it means that the key would get delivered to the window once it becomes
5665 // focused.
5666 std::this_thread::sleep_for(10ms);
5667
5668 // Touch unfocused window. This should force the pending key to get dropped.
5669 NotifyMotionArgs motionArgs =
5670 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5671 ADISPLAY_ID_DEFAULT, {UNFOCUSED_WINDOW_LOCATION});
5672 mDispatcher->notifyMotion(&motionArgs);
5673
5674 // We do not consume the motion right away, because that would require dispatcher to first
5675 // process (== drop) the key event, and by that time, ANR will be raised.
5676 // Set the focused window first.
5677 mFocusedWindow->setFocusable(true);
5678 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
5679 setFocusedWindow(mFocusedWindow);
5680 mFocusedWindow->consumeFocusEvent(true);
5681 // We do not call "setFocusedApplication" here, even though the newly focused window belongs
5682 // to another application. This could be a bug / behaviour in the policy.
5683
5684 mUnfocusedWindow->consumeMotionDown();
5685
5686 ASSERT_TRUE(mDispatcher->waitForIdle());
5687 // Should not ANR because we actually have a focused window. It was just added too slowly.
5688 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled());
5689}
5690
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05005691// These tests ensure we cannot send touch events to a window that's positioned behind a window
5692// that has feature NO_INPUT_CHANNEL.
5693// Layout:
5694// Top (closest to user)
5695// mNoInputWindow (above all windows)
5696// mBottomWindow
5697// Bottom (furthest from user)
5698class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest {
5699 virtual void SetUp() override {
5700 InputDispatcherTest::SetUp();
5701
5702 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005703 mNoInputWindow =
5704 sp<FakeWindowHandle>::make(mApplication, mDispatcher,
5705 "Window without input channel", ADISPLAY_ID_DEFAULT,
5706 std::make_optional<sp<IBinder>>(nullptr) /*token*/);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08005707 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05005708 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
5709 // It's perfectly valid for this window to not have an associated input channel
5710
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005711 mBottomWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Bottom window",
5712 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05005713 mBottomWindow->setFrame(Rect(0, 0, 100, 100));
5714
5715 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
5716 }
5717
5718protected:
5719 std::shared_ptr<FakeApplicationHandle> mApplication;
5720 sp<FakeWindowHandle> mNoInputWindow;
5721 sp<FakeWindowHandle> mBottomWindow;
5722};
5723
5724TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) {
5725 PointF touchedPoint = {10, 10};
5726
5727 NotifyMotionArgs motionArgs =
5728 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5729 ADISPLAY_ID_DEFAULT, {touchedPoint});
5730 mDispatcher->notifyMotion(&motionArgs);
5731
5732 mNoInputWindow->assertNoEvents();
5733 // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have
5734 // an input channel, it is not marked as FLAG_NOT_TOUCHABLE,
5735 // and therefore should prevent mBottomWindow from receiving touches
5736 mBottomWindow->assertNoEvents();
5737}
5738
5739/**
5740 * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel,
5741 * ensure that this window does not receive any touches, and blocks touches to windows underneath.
5742 */
5743TEST_F(InputDispatcherMultiWindowOcclusionTests,
5744 NoInputChannelFeature_DropsTouchesWithValidChannel) {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005745 mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher,
5746 "Window with input channel and NO_INPUT_CHANNEL",
5747 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05005748
Prabir Pradhan51e7db02022-02-07 06:02:57 -08005749 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05005750 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
5751 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
5752
5753 PointF touchedPoint = {10, 10};
5754
5755 NotifyMotionArgs motionArgs =
5756 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5757 ADISPLAY_ID_DEFAULT, {touchedPoint});
5758 mDispatcher->notifyMotion(&motionArgs);
5759
5760 mNoInputWindow->assertNoEvents();
5761 mBottomWindow->assertNoEvents();
5762}
5763
Vishnu Nair958da932020-08-21 17:12:37 -07005764class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest {
5765protected:
5766 std::shared_ptr<FakeApplicationHandle> mApp;
5767 sp<FakeWindowHandle> mWindow;
5768 sp<FakeWindowHandle> mMirror;
5769
5770 virtual void SetUp() override {
5771 InputDispatcherTest::SetUp();
5772 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005773 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
5774 mMirror = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindowMirror",
5775 ADISPLAY_ID_DEFAULT, mWindow->getToken());
Vishnu Nair958da932020-08-21 17:12:37 -07005776 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
5777 mWindow->setFocusable(true);
5778 mMirror->setFocusable(true);
5779 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
5780 }
5781};
5782
5783TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) {
5784 // Request focus on a mirrored window
5785 setFocusedWindow(mMirror);
5786
5787 // window gets focused
5788 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005789 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5790 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005791 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
5792}
5793
5794// A focused & mirrored window remains focused only if the window and its mirror are both
5795// focusable.
5796TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) {
5797 setFocusedWindow(mMirror);
5798
5799 // window gets focused
5800 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005801 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5802 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005803 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005804 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
5805 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005806 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
5807
5808 mMirror->setFocusable(false);
5809 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
5810
5811 // window loses focus since one of the windows associated with the token in not focusable
5812 mWindow->consumeFocusEvent(false);
5813
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005814 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
5815 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07005816 mWindow->assertNoEvents();
5817}
5818
5819// A focused & mirrored window remains focused until the window and its mirror both become
5820// invisible.
5821TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) {
5822 setFocusedWindow(mMirror);
5823
5824 // window gets focused
5825 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005826 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5827 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005828 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005829 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
5830 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005831 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
5832
5833 mMirror->setVisible(false);
5834 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
5835
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005836 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5837 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005838 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005839 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
5840 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005841 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
5842
5843 mWindow->setVisible(false);
5844 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
5845
5846 // window loses focus only after all windows associated with the token become invisible.
5847 mWindow->consumeFocusEvent(false);
5848
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005849 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
5850 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07005851 mWindow->assertNoEvents();
5852}
5853
5854// A focused & mirrored window remains focused until both windows are removed.
5855TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) {
5856 setFocusedWindow(mMirror);
5857
5858 // window gets focused
5859 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005860 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5861 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005862 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005863 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
5864 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005865 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
5866
5867 // single window is removed but the window token remains focused
5868 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}});
5869
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005870 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5871 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005872 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005873 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
5874 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005875 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
5876
5877 // Both windows are removed
5878 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
5879 mWindow->consumeFocusEvent(false);
5880
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005881 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
5882 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07005883 mWindow->assertNoEvents();
5884}
5885
5886// Focus request can be pending until one window becomes visible.
5887TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) {
5888 // Request focus on an invisible mirror.
5889 mWindow->setVisible(false);
5890 mMirror->setVisible(false);
5891 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
5892 setFocusedWindow(mMirror);
5893
5894 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005895 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Vishnu Nair958da932020-08-21 17:12:37 -07005896 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005897 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07005898
5899 mMirror->setVisible(true);
5900 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
5901
5902 // window gets focused
5903 mWindow->consumeFocusEvent(true);
5904 // window gets the pending key event
5905 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5906}
Prabir Pradhan99987712020-11-10 18:43:05 -08005907
5908class InputDispatcherPointerCaptureTests : public InputDispatcherTest {
5909protected:
5910 std::shared_ptr<FakeApplicationHandle> mApp;
5911 sp<FakeWindowHandle> mWindow;
5912 sp<FakeWindowHandle> mSecondWindow;
5913
5914 void SetUp() override {
5915 InputDispatcherTest::SetUp();
5916 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005917 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -08005918 mWindow->setFocusable(true);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005919 mSecondWindow =
5920 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -08005921 mSecondWindow->setFocusable(true);
5922
5923 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
5924 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}});
5925
5926 setFocusedWindow(mWindow);
5927 mWindow->consumeFocusEvent(true);
5928 }
5929
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00005930 void notifyPointerCaptureChanged(const PointerCaptureRequest& request) {
5931 const NotifyPointerCaptureChangedArgs args = generatePointerCaptureChangedArgs(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08005932 mDispatcher->notifyPointerCaptureChanged(&args);
5933 }
5934
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00005935 PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window,
5936 bool enabled) {
Prabir Pradhan99987712020-11-10 18:43:05 -08005937 mDispatcher->requestPointerCapture(window->getToken(), enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00005938 auto request = mFakePolicy->assertSetPointerCaptureCalled(enabled);
5939 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08005940 window->consumeCaptureEvent(enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00005941 return request;
Prabir Pradhan99987712020-11-10 18:43:05 -08005942 }
5943};
5944
5945TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) {
5946 // Ensure that capture cannot be obtained for unfocused windows.
5947 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
5948 mFakePolicy->assertSetPointerCaptureNotCalled();
5949 mSecondWindow->assertNoEvents();
5950
5951 // Ensure that capture can be enabled from the focus window.
5952 requestAndVerifyPointerCapture(mWindow, true);
5953
5954 // Ensure that capture cannot be disabled from a window that does not have capture.
5955 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false);
5956 mFakePolicy->assertSetPointerCaptureNotCalled();
5957
5958 // Ensure that capture can be disabled from the window with capture.
5959 requestAndVerifyPointerCapture(mWindow, false);
5960}
5961
5962TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00005963 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08005964
5965 setFocusedWindow(mSecondWindow);
5966
5967 // Ensure that the capture disabled event was sent first.
5968 mWindow->consumeCaptureEvent(false);
5969 mWindow->consumeFocusEvent(false);
5970 mSecondWindow->consumeFocusEvent(true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00005971 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08005972
5973 // Ensure that additional state changes from InputReader are not sent to the window.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00005974 notifyPointerCaptureChanged({});
5975 notifyPointerCaptureChanged(request);
5976 notifyPointerCaptureChanged({});
Prabir Pradhan99987712020-11-10 18:43:05 -08005977 mWindow->assertNoEvents();
5978 mSecondWindow->assertNoEvents();
5979 mFakePolicy->assertSetPointerCaptureNotCalled();
5980}
5981
5982TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00005983 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08005984
5985 // InputReader unexpectedly disables and enables pointer capture.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00005986 notifyPointerCaptureChanged({});
5987 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08005988
5989 // Ensure that Pointer Capture is disabled.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00005990 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08005991 mWindow->consumeCaptureEvent(false);
5992 mWindow->assertNoEvents();
5993}
5994
Prabir Pradhan167e6d92021-02-04 16:18:17 -08005995TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) {
5996 requestAndVerifyPointerCapture(mWindow, true);
5997
5998 // The first window loses focus.
5999 setFocusedWindow(mSecondWindow);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006000 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08006001 mWindow->consumeCaptureEvent(false);
6002
6003 // Request Pointer Capture from the second window before the notification from InputReader
6004 // arrives.
6005 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006006 auto request = mFakePolicy->assertSetPointerCaptureCalled(true);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08006007
6008 // InputReader notifies Pointer Capture was disabled (because of the focus change).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006009 notifyPointerCaptureChanged({});
Prabir Pradhan167e6d92021-02-04 16:18:17 -08006010
6011 // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006012 notifyPointerCaptureChanged(request);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08006013
6014 mSecondWindow->consumeFocusEvent(true);
6015 mSecondWindow->consumeCaptureEvent(true);
6016}
6017
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006018TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) {
6019 // App repeatedly enables and disables capture.
6020 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
6021 auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
6022 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
6023 mFakePolicy->assertSetPointerCaptureCalled(false);
6024 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
6025 auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
6026
6027 // InputReader notifies that PointerCapture has been enabled for the first request. Since the
6028 // first request is now stale, this should do nothing.
6029 notifyPointerCaptureChanged(firstRequest);
6030 mWindow->assertNoEvents();
6031
6032 // InputReader notifies that the second request was enabled.
6033 notifyPointerCaptureChanged(secondRequest);
6034 mWindow->consumeCaptureEvent(true);
6035}
6036
Prabir Pradhan7092e262022-05-03 16:51:09 +00006037TEST_F(InputDispatcherPointerCaptureTests, RapidToggleRequests) {
6038 requestAndVerifyPointerCapture(mWindow, true);
6039
6040 // App toggles pointer capture off and on.
6041 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
6042 mFakePolicy->assertSetPointerCaptureCalled(false);
6043
6044 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
6045 auto enableRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
6046
6047 // InputReader notifies that the latest "enable" request was processed, while skipping over the
6048 // preceding "disable" request.
6049 notifyPointerCaptureChanged(enableRequest);
6050
6051 // Since pointer capture was never disabled during the rapid toggle, the window does not receive
6052 // any notifications.
6053 mWindow->assertNoEvents();
6054}
6055
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006056class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest {
6057protected:
6058 constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8;
Bernardo Rufino7393d172021-02-26 13:56:11 +00006059
6060 constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9;
6061 static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY);
6062
6063 constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7;
6064 static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
6065
6066 // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold
6067 constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5;
6068 static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
6069 static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) <
6070 MAXIMUM_OBSCURING_OPACITY);
6071
Bernardo Rufino6d52e542021-02-15 18:38:10 +00006072 static const int32_t TOUCHED_APP_UID = 10001;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006073 static const int32_t APP_B_UID = 10002;
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006074 static const int32_t APP_C_UID = 10003;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006075
6076 sp<FakeWindowHandle> mTouchWindow;
6077
6078 virtual void SetUp() override {
6079 InputDispatcherTest::SetUp();
Bernardo Rufino6d52e542021-02-15 18:38:10 +00006080 mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched");
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006081 mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY);
6082 }
6083
6084 virtual void TearDown() override {
6085 InputDispatcherTest::TearDown();
6086 mTouchWindow.clear();
6087 }
6088
chaviw3277faf2021-05-19 16:45:23 -05006089 sp<FakeWindowHandle> getOccludingWindow(int32_t uid, std::string name, TouchOcclusionMode mode,
6090 float alpha = 1.0f) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006091 sp<FakeWindowHandle> window = getWindow(uid, name);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08006092 window->setTouchable(false);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006093 window->setTouchOcclusionMode(mode);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006094 window->setAlpha(alpha);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006095 return window;
6096 }
6097
6098 sp<FakeWindowHandle> getWindow(int32_t uid, std::string name) {
6099 std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>();
6100 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006101 sp<FakeWindowHandle>::make(app, mDispatcher, name, ADISPLAY_ID_DEFAULT);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006102 // Generate an arbitrary PID based on the UID
6103 window->setOwnerInfo(1777 + (uid % 10000), uid);
6104 return window;
6105 }
6106
6107 void touch(const std::vector<PointF>& points = {PointF{100, 200}}) {
6108 NotifyMotionArgs args =
6109 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6110 ADISPLAY_ID_DEFAULT, points);
6111 mDispatcher->notifyMotion(&args);
6112 }
6113};
6114
6115TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006116 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006117 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006118 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006119
6120 touch();
6121
6122 mTouchWindow->assertNoEvents();
6123}
6124
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00006125TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufino7393d172021-02-26 13:56:11 +00006126 WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) {
6127 const sp<FakeWindowHandle>& w =
6128 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f);
6129 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6130
6131 touch();
6132
6133 mTouchWindow->assertNoEvents();
6134}
6135
6136TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00006137 WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) {
6138 const sp<FakeWindowHandle>& w =
6139 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
6140 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6141
6142 touch();
6143
6144 w->assertNoEvents();
6145}
6146
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006147TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006148 const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW);
6149 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006150
6151 touch();
6152
6153 mTouchWindow->consumeAnyMotionDown();
6154}
6155
6156TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006157 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006158 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006159 w->setFrame(Rect(0, 0, 50, 50));
6160 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006161
6162 touch({PointF{100, 100}});
6163
6164 mTouchWindow->consumeAnyMotionDown();
6165}
6166
6167TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006168 const sp<FakeWindowHandle>& w =
Bernardo Rufino6d52e542021-02-15 18:38:10 +00006169 getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006170 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6171
6172 touch();
6173
6174 mTouchWindow->consumeAnyMotionDown();
6175}
6176
6177TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) {
6178 const sp<FakeWindowHandle>& w =
6179 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
6180 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006181
6182 touch();
6183
6184 mTouchWindow->consumeAnyMotionDown();
6185}
6186
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00006187TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) {
6188 const sp<FakeWindowHandle>& w =
6189 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
6190 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6191
6192 touch();
6193
6194 w->assertNoEvents();
6195}
6196
6197/**
6198 * This is important to make sure apps can't indirectly learn the position of touches (outside vs
6199 * inside) while letting them pass-through. Note that even though touch passes through the occluding
6200 * window, the occluding window will still receive ACTION_OUTSIDE event.
6201 */
6202TEST_F(InputDispatcherUntrustedTouchesTest,
6203 WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) {
6204 const sp<FakeWindowHandle>& w =
6205 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08006206 w->setWatchOutsideTouch(true);
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00006207 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6208
6209 touch();
6210
6211 w->consumeMotionOutside();
6212}
6213
6214TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) {
6215 const sp<FakeWindowHandle>& w =
6216 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08006217 w->setWatchOutsideTouch(true);
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00006218 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6219
6220 touch();
6221
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08006222 w->consumeMotionOutsideWithZeroedCoords();
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00006223}
6224
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006225TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006226 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006227 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
6228 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006229 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6230
6231 touch();
6232
6233 mTouchWindow->consumeAnyMotionDown();
6234}
6235
6236TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) {
6237 const sp<FakeWindowHandle>& w =
6238 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
6239 MAXIMUM_OBSCURING_OPACITY);
6240 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006241
6242 touch();
6243
6244 mTouchWindow->consumeAnyMotionDown();
6245}
6246
6247TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006248 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006249 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
6250 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006251 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6252
6253 touch();
6254
6255 mTouchWindow->assertNoEvents();
6256}
6257
6258TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) {
6259 // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91
6260 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006261 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
6262 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006263 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006264 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
6265 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006266 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
6267
6268 touch();
6269
6270 mTouchWindow->assertNoEvents();
6271}
6272
6273TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) {
6274 // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75
6275 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006276 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
6277 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006278 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006279 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
6280 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006281 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
6282
6283 touch();
6284
6285 mTouchWindow->consumeAnyMotionDown();
6286}
6287
6288TEST_F(InputDispatcherUntrustedTouchesTest,
6289 WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) {
6290 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006291 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
6292 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006293 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006294 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
6295 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006296 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
6297
6298 touch();
6299
6300 mTouchWindow->consumeAnyMotionDown();
6301}
6302
6303TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) {
6304 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006305 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
6306 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006307 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006308 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
6309 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006310 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006311
6312 touch();
6313
6314 mTouchWindow->assertNoEvents();
6315}
6316
Bernardo Rufino6d52e542021-02-15 18:38:10 +00006317TEST_F(InputDispatcherUntrustedTouchesTest,
6318 WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) {
6319 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006320 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
6321 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00006322 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006323 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
6324 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00006325 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
6326
6327 touch();
6328
6329 mTouchWindow->assertNoEvents();
6330}
6331
6332TEST_F(InputDispatcherUntrustedTouchesTest,
6333 WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) {
6334 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006335 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
6336 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00006337 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006338 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
6339 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00006340 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
6341
6342 touch();
6343
6344 mTouchWindow->consumeAnyMotionDown();
6345}
6346
6347TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) {
6348 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006349 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
6350 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00006351 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6352
6353 touch();
6354
6355 mTouchWindow->consumeAnyMotionDown();
6356}
6357
6358TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) {
6359 const sp<FakeWindowHandle>& w =
6360 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED);
6361 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6362
6363 touch();
6364
6365 mTouchWindow->consumeAnyMotionDown();
6366}
6367
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00006368TEST_F(InputDispatcherUntrustedTouchesTest,
6369 OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) {
6370 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
6371 const sp<FakeWindowHandle>& w =
6372 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f);
6373 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6374
6375 touch();
6376
6377 mTouchWindow->assertNoEvents();
6378}
6379
6380TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) {
6381 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
6382 const sp<FakeWindowHandle>& w =
6383 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f);
6384 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6385
6386 touch();
6387
6388 mTouchWindow->consumeAnyMotionDown();
6389}
6390
6391TEST_F(InputDispatcherUntrustedTouchesTest,
6392 OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) {
6393 mDispatcher->setMaximumObscuringOpacityForTouch(1.0f);
6394 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006395 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
6396 OPACITY_ABOVE_THRESHOLD);
6397 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6398
6399 touch();
6400
6401 mTouchWindow->consumeAnyMotionDown();
6402}
6403
6404TEST_F(InputDispatcherUntrustedTouchesTest,
6405 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) {
6406 const sp<FakeWindowHandle>& w1 =
6407 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
6408 OPACITY_BELOW_THRESHOLD);
6409 const sp<FakeWindowHandle>& w2 =
6410 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
6411 OPACITY_BELOW_THRESHOLD);
6412 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
6413
6414 touch();
6415
6416 mTouchWindow->assertNoEvents();
6417}
6418
6419/**
6420 * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the
6421 * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold
6422 * (which alone would result in allowing touches) does not affect the blocking behavior.
6423 */
6424TEST_F(InputDispatcherUntrustedTouchesTest,
6425 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) {
6426 const sp<FakeWindowHandle>& wB =
6427 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
6428 OPACITY_BELOW_THRESHOLD);
6429 const sp<FakeWindowHandle>& wC =
6430 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
6431 OPACITY_BELOW_THRESHOLD);
6432 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
6433
6434 touch();
6435
6436 mTouchWindow->assertNoEvents();
6437}
6438
6439/**
6440 * This test is testing that a window from a different UID but with same application token doesn't
6441 * block the touch. Apps can share the application token for close UI collaboration for example.
6442 */
6443TEST_F(InputDispatcherUntrustedTouchesTest,
6444 WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) {
6445 const sp<FakeWindowHandle>& w =
6446 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
6447 w->setApplicationToken(mTouchWindow->getApplicationToken());
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00006448 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6449
6450 touch();
6451
6452 mTouchWindow->consumeAnyMotionDown();
6453}
6454
arthurhungb89ccb02020-12-30 16:19:01 +08006455class InputDispatcherDragTests : public InputDispatcherTest {
6456protected:
6457 std::shared_ptr<FakeApplicationHandle> mApp;
6458 sp<FakeWindowHandle> mWindow;
6459 sp<FakeWindowHandle> mSecondWindow;
6460 sp<FakeWindowHandle> mDragWindow;
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00006461 sp<FakeWindowHandle> mSpyWindow;
Arthur Hungb75c2aa2022-07-15 09:35:36 +00006462 // Mouse would force no-split, set the id as non-zero to verify if drag state could track it.
6463 static constexpr int32_t MOUSE_POINTER_ID = 1;
arthurhungb89ccb02020-12-30 16:19:01 +08006464
6465 void SetUp() override {
6466 InputDispatcherTest::SetUp();
6467 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006468 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +08006469 mWindow->setFrame(Rect(0, 0, 100, 100));
arthurhungb89ccb02020-12-30 16:19:01 +08006470
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006471 mSecondWindow =
6472 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +08006473 mSecondWindow->setFrame(Rect(100, 0, 200, 100));
arthurhungb89ccb02020-12-30 16:19:01 +08006474
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006475 mSpyWindow =
6476 sp<FakeWindowHandle>::make(mApp, mDispatcher, "SpyWindow", ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00006477 mSpyWindow->setSpy(true);
6478 mSpyWindow->setTrustedOverlay(true);
6479 mSpyWindow->setFrame(Rect(0, 0, 200, 100));
6480
arthurhungb89ccb02020-12-30 16:19:01 +08006481 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00006482 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mSpyWindow, mWindow, mSecondWindow}}});
arthurhungb89ccb02020-12-30 16:19:01 +08006483 }
6484
Arthur Hungb75c2aa2022-07-15 09:35:36 +00006485 void injectDown(int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
6486 switch (fromSource) {
6487 case AINPUT_SOURCE_TOUCHSCREEN:
6488 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6489 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
6490 ADISPLAY_ID_DEFAULT, {50, 50}))
6491 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6492 break;
6493 case AINPUT_SOURCE_STYLUS:
6494 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6495 injectMotionEvent(
6496 mDispatcher,
6497 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
6498 AINPUT_SOURCE_STYLUS)
6499 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
6500 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS)
6501 .x(50)
6502 .y(50))
6503 .build()));
6504 break;
6505 case AINPUT_SOURCE_MOUSE:
6506 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6507 injectMotionEvent(
6508 mDispatcher,
6509 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
6510 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
6511 .pointer(PointerBuilder(MOUSE_POINTER_ID,
6512 AMOTION_EVENT_TOOL_TYPE_MOUSE)
6513 .x(50)
6514 .y(50))
6515 .build()));
6516 break;
6517 default:
6518 FAIL() << "Source " << fromSource << " doesn't support drag and drop";
6519 }
arthurhungb89ccb02020-12-30 16:19:01 +08006520
6521 // Window should receive motion event.
6522 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00006523 // Spy window should also receive motion event
6524 mSpyWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung54745652022-04-20 07:17:41 +00006525 }
6526
6527 // Start performing drag, we will create a drag window and transfer touch to it.
6528 // @param sendDown : if true, send a motion down on first window before perform drag and drop.
6529 // Returns true on success.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00006530 bool startDrag(bool sendDown = true, int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
Arthur Hung54745652022-04-20 07:17:41 +00006531 if (sendDown) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00006532 injectDown(fromSource);
Arthur Hung54745652022-04-20 07:17:41 +00006533 }
arthurhungb89ccb02020-12-30 16:19:01 +08006534
6535 // The drag window covers the entire display
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006536 mDragWindow =
6537 sp<FakeWindowHandle>::make(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00006538 mDragWindow->setTouchableRegion(Region{{0, 0, 0, 0}});
arthurhungb89ccb02020-12-30 16:19:01 +08006539 mDispatcher->setInputWindows(
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00006540 {{ADISPLAY_ID_DEFAULT, {mDragWindow, mSpyWindow, mWindow, mSecondWindow}}});
arthurhungb89ccb02020-12-30 16:19:01 +08006541
6542 // Transfer touch focus to the drag window
Arthur Hung54745652022-04-20 07:17:41 +00006543 bool transferred =
6544 mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(),
6545 true /* isDragDrop */);
6546 if (transferred) {
6547 mWindow->consumeMotionCancel();
6548 mDragWindow->consumeMotionDown();
6549 }
6550 return transferred;
arthurhungb89ccb02020-12-30 16:19:01 +08006551 }
6552};
6553
6554TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00006555 startDrag();
arthurhungb89ccb02020-12-30 16:19:01 +08006556
6557 // Move on window.
6558 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6559 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6560 ADISPLAY_ID_DEFAULT, {50, 50}))
6561 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6562 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
6563 mWindow->consumeDragEvent(false, 50, 50);
6564 mSecondWindow->assertNoEvents();
6565
6566 // Move to another window.
6567 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6568 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6569 ADISPLAY_ID_DEFAULT, {150, 50}))
6570 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6571 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
6572 mWindow->consumeDragEvent(true, 150, 50);
6573 mSecondWindow->consumeDragEvent(false, 50, 50);
6574
6575 // Move back to original window.
6576 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6577 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6578 ADISPLAY_ID_DEFAULT, {50, 50}))
6579 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6580 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
6581 mWindow->consumeDragEvent(false, 50, 50);
6582 mSecondWindow->consumeDragEvent(true, -50, 50);
6583
6584 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6585 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {50, 50}))
6586 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6587 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
6588 mWindow->assertNoEvents();
6589 mSecondWindow->assertNoEvents();
6590}
6591
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00006592TEST_F(InputDispatcherDragTests, DragEnterAndPointerDownPilfersPointers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00006593 startDrag();
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00006594
6595 // No cancel event after drag start
6596 mSpyWindow->assertNoEvents();
6597
6598 const MotionEvent secondFingerDownEvent =
6599 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
6600 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
6601 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
6602 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(60).y(60))
6603 .build();
6604 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6605 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
6606 InputEventInjectionSync::WAIT_FOR_RESULT))
6607 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6608
6609 // Receives cancel for first pointer after next pointer down
6610 mSpyWindow->consumeMotionCancel();
6611 mSpyWindow->consumeMotionDown();
6612
6613 mSpyWindow->assertNoEvents();
6614}
6615
arthurhungf452d0b2021-01-06 00:19:52 +08006616TEST_F(InputDispatcherDragTests, DragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00006617 startDrag();
arthurhungf452d0b2021-01-06 00:19:52 +08006618
6619 // Move on window.
6620 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6621 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6622 ADISPLAY_ID_DEFAULT, {50, 50}))
6623 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6624 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
6625 mWindow->consumeDragEvent(false, 50, 50);
6626 mSecondWindow->assertNoEvents();
6627
6628 // Move to another window.
6629 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6630 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6631 ADISPLAY_ID_DEFAULT, {150, 50}))
6632 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6633 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
6634 mWindow->consumeDragEvent(true, 150, 50);
6635 mSecondWindow->consumeDragEvent(false, 50, 50);
6636
6637 // drop to another window.
6638 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6639 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6640 {150, 50}))
6641 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6642 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
6643 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
6644 mWindow->assertNoEvents();
6645 mSecondWindow->assertNoEvents();
6646}
6647
arthurhung6d4bed92021-03-17 11:59:33 +08006648TEST_F(InputDispatcherDragTests, StylusDragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00006649 startDrag(true, AINPUT_SOURCE_STYLUS);
arthurhung6d4bed92021-03-17 11:59:33 +08006650
6651 // Move on window and keep button pressed.
6652 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6653 injectMotionEvent(mDispatcher,
6654 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
6655 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
6656 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS)
6657 .x(50)
6658 .y(50))
6659 .build()))
6660 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6661 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
6662 mWindow->consumeDragEvent(false, 50, 50);
6663 mSecondWindow->assertNoEvents();
6664
6665 // Move to another window and release button, expect to drop item.
6666 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6667 injectMotionEvent(mDispatcher,
6668 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
6669 .buttonState(0)
6670 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS)
6671 .x(150)
6672 .y(50))
6673 .build()))
6674 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6675 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
6676 mWindow->assertNoEvents();
6677 mSecondWindow->assertNoEvents();
6678 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
6679
6680 // nothing to the window.
6681 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6682 injectMotionEvent(mDispatcher,
6683 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS)
6684 .buttonState(0)
6685 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS)
6686 .x(150)
6687 .y(50))
6688 .build()))
6689 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6690 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
6691 mWindow->assertNoEvents();
6692 mSecondWindow->assertNoEvents();
6693}
6694
Arthur Hung54745652022-04-20 07:17:41 +00006695TEST_F(InputDispatcherDragTests, DragAndDropOnInvalidWindow) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00006696 startDrag();
Arthur Hung6d0571e2021-04-09 20:18:16 +08006697
6698 // Set second window invisible.
6699 mSecondWindow->setVisible(false);
6700 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}});
6701
6702 // Move on window.
6703 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6704 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6705 ADISPLAY_ID_DEFAULT, {50, 50}))
6706 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6707 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
6708 mWindow->consumeDragEvent(false, 50, 50);
6709 mSecondWindow->assertNoEvents();
6710
6711 // Move to another window.
6712 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6713 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6714 ADISPLAY_ID_DEFAULT, {150, 50}))
6715 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6716 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
6717 mWindow->consumeDragEvent(true, 150, 50);
6718 mSecondWindow->assertNoEvents();
6719
6720 // drop to another window.
6721 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6722 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6723 {150, 50}))
6724 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6725 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
6726 mFakePolicy->assertDropTargetEquals(nullptr);
6727 mWindow->assertNoEvents();
6728 mSecondWindow->assertNoEvents();
6729}
6730
Arthur Hung54745652022-04-20 07:17:41 +00006731TEST_F(InputDispatcherDragTests, NoDragAndDropWhenMultiFingers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00006732 // Ensure window could track pointerIds if it didn't support split touch.
6733 mWindow->setPreventSplitting(true);
6734
Arthur Hung54745652022-04-20 07:17:41 +00006735 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6736 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6737 {50, 50}))
6738 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6739 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6740
6741 const MotionEvent secondFingerDownEvent =
6742 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
6743 .displayId(ADISPLAY_ID_DEFAULT)
6744 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
6745 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
6746 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(75).y(50))
6747 .build();
6748 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6749 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
6750 InputEventInjectionSync::WAIT_FOR_RESULT))
6751 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6752 mWindow->consumeMotionPointerDown(1 /* pointerIndex */);
6753
6754 // Should not perform drag and drop when window has multi fingers.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00006755 ASSERT_FALSE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +00006756}
6757
6758TEST_F(InputDispatcherDragTests, DragAndDropWhenSplitTouch) {
6759 // First down on second window.
6760 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6761 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6762 {150, 50}))
6763 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6764
6765 mSecondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6766
6767 // Second down on first window.
6768 const MotionEvent secondFingerDownEvent =
6769 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
6770 .displayId(ADISPLAY_ID_DEFAULT)
6771 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
6772 .pointer(
6773 PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50))
6774 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
6775 .build();
6776 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6777 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
6778 InputEventInjectionSync::WAIT_FOR_RESULT))
6779 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6780 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6781
6782 // Perform drag and drop from first window.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00006783 ASSERT_TRUE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +00006784
6785 // Move on window.
6786 const MotionEvent secondFingerMoveEvent =
6787 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
6788 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
6789 .pointer(
6790 PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50))
6791 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
6792 .build();
6793 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6794 injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT,
6795 InputEventInjectionSync::WAIT_FOR_RESULT));
6796 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
6797 mWindow->consumeDragEvent(false, 50, 50);
6798 mSecondWindow->consumeMotionMove();
6799
6800 // Release the drag pointer should perform drop.
6801 const MotionEvent secondFingerUpEvent =
6802 MotionEventBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
6803 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
6804 .pointer(
6805 PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50))
6806 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
6807 .build();
6808 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6809 injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT,
6810 InputEventInjectionSync::WAIT_FOR_RESULT));
6811 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
6812 mFakePolicy->assertDropTargetEquals(mWindow->getToken());
6813 mWindow->assertNoEvents();
6814 mSecondWindow->consumeMotionMove();
6815}
6816
Arthur Hung3915c1f2022-05-31 07:17:17 +00006817TEST_F(InputDispatcherDragTests, DragAndDropWhenMultiDisplays) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00006818 startDrag();
Arthur Hung3915c1f2022-05-31 07:17:17 +00006819
6820 // Update window of second display.
6821 sp<FakeWindowHandle> windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006822 sp<FakeWindowHandle>::make(mApp, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung3915c1f2022-05-31 07:17:17 +00006823 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
6824
6825 // Let second display has a touch state.
6826 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6827 injectMotionEvent(mDispatcher,
6828 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
6829 AINPUT_SOURCE_TOUCHSCREEN)
6830 .displayId(SECOND_DISPLAY_ID)
6831 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER)
6832 .x(100)
6833 .y(100))
6834 .build()));
6835 windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN,
6836 SECOND_DISPLAY_ID, 0 /* expectedFlag */);
6837 // Update window again.
6838 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
6839
6840 // Move on window.
6841 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6842 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6843 ADISPLAY_ID_DEFAULT, {50, 50}))
6844 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6845 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
6846 mWindow->consumeDragEvent(false, 50, 50);
6847 mSecondWindow->assertNoEvents();
6848
6849 // Move to another window.
6850 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6851 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6852 ADISPLAY_ID_DEFAULT, {150, 50}))
6853 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6854 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
6855 mWindow->consumeDragEvent(true, 150, 50);
6856 mSecondWindow->consumeDragEvent(false, 50, 50);
6857
6858 // drop to another window.
6859 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6860 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6861 {150, 50}))
6862 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6863 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
6864 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
6865 mWindow->assertNoEvents();
6866 mSecondWindow->assertNoEvents();
6867}
6868
Arthur Hungb75c2aa2022-07-15 09:35:36 +00006869TEST_F(InputDispatcherDragTests, MouseDragAndDrop) {
6870 startDrag(true, AINPUT_SOURCE_MOUSE);
6871 // Move on window.
6872 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6873 injectMotionEvent(mDispatcher,
6874 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
6875 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
6876 .pointer(PointerBuilder(MOUSE_POINTER_ID,
6877 AMOTION_EVENT_TOOL_TYPE_MOUSE)
6878 .x(50)
6879 .y(50))
6880 .build()))
6881 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6882 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
6883 mWindow->consumeDragEvent(false, 50, 50);
6884 mSecondWindow->assertNoEvents();
6885
6886 // Move to another window.
6887 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6888 injectMotionEvent(mDispatcher,
6889 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
6890 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
6891 .pointer(PointerBuilder(MOUSE_POINTER_ID,
6892 AMOTION_EVENT_TOOL_TYPE_MOUSE)
6893 .x(150)
6894 .y(50))
6895 .build()))
6896 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6897 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
6898 mWindow->consumeDragEvent(true, 150, 50);
6899 mSecondWindow->consumeDragEvent(false, 50, 50);
6900
6901 // drop to another window.
6902 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6903 injectMotionEvent(mDispatcher,
6904 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
6905 .buttonState(0)
6906 .pointer(PointerBuilder(MOUSE_POINTER_ID,
6907 AMOTION_EVENT_TOOL_TYPE_MOUSE)
6908 .x(150)
6909 .y(50))
6910 .build()))
6911 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6912 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
6913 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
6914 mWindow->assertNoEvents();
6915 mSecondWindow->assertNoEvents();
6916}
6917
Vishnu Nair062a8672021-09-03 16:07:44 -07006918class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {};
6919
6920TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) {
6921 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006922 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
6923 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08006924 window->setDropInput(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07006925 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
6926 window->setFocusable(true);
6927 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
6928 setFocusedWindow(window);
6929 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
6930
6931 // With the flag set, window should not get any input
6932 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
6933 mDispatcher->notifyKey(&keyArgs);
6934 window->assertNoEvents();
6935
6936 NotifyMotionArgs motionArgs =
6937 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6938 ADISPLAY_ID_DEFAULT);
6939 mDispatcher->notifyMotion(&motionArgs);
6940 window->assertNoEvents();
6941
6942 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -08006943 window->setDropInput(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07006944 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
6945
6946 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
6947 mDispatcher->notifyKey(&keyArgs);
6948 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
6949
6950 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6951 ADISPLAY_ID_DEFAULT);
6952 mDispatcher->notifyMotion(&motionArgs);
6953 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6954 window->assertNoEvents();
6955}
6956
6957TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) {
6958 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
6959 std::make_shared<FakeApplicationHandle>();
6960 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006961 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
6962 ADISPLAY_ID_DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -07006963 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
6964 obscuringWindow->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08006965 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07006966 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006967 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
6968 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08006969 window->setDropInputIfObscured(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07006970 window->setOwnerInfo(222, 222);
6971 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
6972 window->setFocusable(true);
6973 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
6974 setFocusedWindow(window);
6975 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
6976
6977 // With the flag set, window should not get any input
6978 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
6979 mDispatcher->notifyKey(&keyArgs);
6980 window->assertNoEvents();
6981
6982 NotifyMotionArgs motionArgs =
6983 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6984 ADISPLAY_ID_DEFAULT);
6985 mDispatcher->notifyMotion(&motionArgs);
6986 window->assertNoEvents();
6987
6988 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -08006989 window->setDropInputIfObscured(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07006990 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
6991
6992 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
6993 mDispatcher->notifyKey(&keyArgs);
6994 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
6995
6996 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6997 ADISPLAY_ID_DEFAULT);
6998 mDispatcher->notifyMotion(&motionArgs);
6999 window->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
7000 window->assertNoEvents();
7001}
7002
7003TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) {
7004 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
7005 std::make_shared<FakeApplicationHandle>();
7006 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007007 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
7008 ADISPLAY_ID_DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -07007009 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
7010 obscuringWindow->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007011 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07007012 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007013 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
7014 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007015 window->setDropInputIfObscured(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07007016 window->setOwnerInfo(222, 222);
7017 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
7018 window->setFocusable(true);
7019 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
7020 setFocusedWindow(window);
7021 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
7022
7023 // With the flag set, window should not get any input
7024 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
7025 mDispatcher->notifyKey(&keyArgs);
7026 window->assertNoEvents();
7027
7028 NotifyMotionArgs motionArgs =
7029 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
7030 ADISPLAY_ID_DEFAULT);
7031 mDispatcher->notifyMotion(&motionArgs);
7032 window->assertNoEvents();
7033
7034 // When the window is no longer obscured because it went on top, it should get input
7035 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, obscuringWindow}}});
7036
7037 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
7038 mDispatcher->notifyKey(&keyArgs);
7039 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
7040
7041 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
7042 ADISPLAY_ID_DEFAULT);
7043 mDispatcher->notifyMotion(&motionArgs);
7044 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7045 window->assertNoEvents();
7046}
7047
Antonio Kantekf16f2832021-09-28 04:39:20 +00007048class InputDispatcherTouchModeChangedTests : public InputDispatcherTest {
7049protected:
7050 std::shared_ptr<FakeApplicationHandle> mApp;
Antonio Kantek15beb512022-06-13 22:35:41 +00007051 std::shared_ptr<FakeApplicationHandle> mSecondaryApp;
Antonio Kantekf16f2832021-09-28 04:39:20 +00007052 sp<FakeWindowHandle> mWindow;
7053 sp<FakeWindowHandle> mSecondWindow;
Antonio Kantek15beb512022-06-13 22:35:41 +00007054 sp<FakeWindowHandle> mThirdWindow;
Antonio Kantekf16f2832021-09-28 04:39:20 +00007055
7056 void SetUp() override {
7057 InputDispatcherTest::SetUp();
7058
7059 mApp = std::make_shared<FakeApplicationHandle>();
Antonio Kantek15beb512022-06-13 22:35:41 +00007060 mSecondaryApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007061 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00007062 mWindow->setFocusable(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00007063 setFocusedWindow(mWindow);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007064 mSecondWindow =
7065 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00007066 mSecondWindow->setFocusable(true);
Antonio Kantek15beb512022-06-13 22:35:41 +00007067 mThirdWindow =
7068 sp<FakeWindowHandle>::make(mSecondaryApp, mDispatcher,
7069 "TestWindow3_SecondaryDisplay", SECOND_DISPLAY_ID);
7070 mThirdWindow->setFocusable(true);
Antonio Kantekf16f2832021-09-28 04:39:20 +00007071
7072 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Antonio Kantek15beb512022-06-13 22:35:41 +00007073 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}},
7074 {SECOND_DISPLAY_ID, {mThirdWindow}}});
7075 mThirdWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
Antonio Kantekf16f2832021-09-28 04:39:20 +00007076 mWindow->consumeFocusEvent(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00007077
Antonio Kantek15beb512022-06-13 22:35:41 +00007078 // Set main display initial touch mode to InputDispatcher::kDefaultInTouchMode.
Prabir Pradhan5735a322022-04-11 17:23:34 +00007079 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, WINDOW_PID,
Antonio Kantek15beb512022-06-13 22:35:41 +00007080 WINDOW_UID, true /* hasPermission */,
7081 ADISPLAY_ID_DEFAULT)) {
Antonio Kantek48710e42022-03-24 14:19:30 -07007082 mWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
7083 mSecondWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +00007084 mThirdWindow->assertNoEvents();
7085 }
7086
7087 // Set secondary display initial touch mode to InputDispatcher::kDefaultInTouchMode.
7088 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, SECONDARY_WINDOW_PID,
7089 SECONDARY_WINDOW_UID, true /* hasPermission */,
7090 SECOND_DISPLAY_ID)) {
7091 mWindow->assertNoEvents();
7092 mSecondWindow->assertNoEvents();
7093 mThirdWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek48710e42022-03-24 14:19:30 -07007094 }
Antonio Kantekf16f2832021-09-28 04:39:20 +00007095 }
7096
Antonio Kantek15beb512022-06-13 22:35:41 +00007097 void changeAndVerifyTouchModeInMainDisplayOnly(bool inTouchMode, int32_t pid, int32_t uid,
7098 bool hasPermission) {
Antonio Kanteka042c022022-07-06 16:51:07 -07007099 ASSERT_TRUE(mDispatcher->setInTouchMode(inTouchMode, pid, uid, hasPermission,
7100 ADISPLAY_ID_DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +00007101 mWindow->consumeTouchModeEvent(inTouchMode);
7102 mSecondWindow->consumeTouchModeEvent(inTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +00007103 mThirdWindow->assertNoEvents();
Antonio Kantekf16f2832021-09-28 04:39:20 +00007104 }
7105};
7106
Antonio Kantek26defcf2022-02-08 01:12:27 +00007107TEST_F(InputDispatcherTouchModeChangedTests, FocusedWindowCanChangeTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -08007108 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek15beb512022-06-13 22:35:41 +00007109 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode,
7110 windowInfo.ownerPid, windowInfo.ownerUid,
7111 false /* hasPermission */);
Antonio Kantekf16f2832021-09-28 04:39:20 +00007112}
7113
Antonio Kantek26defcf2022-02-08 01:12:27 +00007114TEST_F(InputDispatcherTouchModeChangedTests, NonFocusedWindowOwnerCannotChangeTouchMode) {
7115 const WindowInfo& windowInfo = *mWindow->getInfo();
7116 int32_t ownerPid = windowInfo.ownerPid;
7117 int32_t ownerUid = windowInfo.ownerUid;
7118 mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1);
7119 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, ownerPid,
Antonio Kanteka042c022022-07-06 16:51:07 -07007120 ownerUid, false /*hasPermission*/,
7121 ADISPLAY_ID_DEFAULT));
Antonio Kantek26defcf2022-02-08 01:12:27 +00007122 mWindow->assertNoEvents();
7123 mSecondWindow->assertNoEvents();
7124}
7125
7126TEST_F(InputDispatcherTouchModeChangedTests, NonWindowOwnerMayChangeTouchModeOnPermissionGranted) {
7127 const WindowInfo& windowInfo = *mWindow->getInfo();
7128 int32_t ownerPid = windowInfo.ownerPid;
7129 int32_t ownerUid = windowInfo.ownerUid;
7130 mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1);
Antonio Kantek15beb512022-06-13 22:35:41 +00007131 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, ownerPid,
7132 ownerUid, true /*hasPermission*/);
Antonio Kantek26defcf2022-02-08 01:12:27 +00007133}
7134
Antonio Kantekf16f2832021-09-28 04:39:20 +00007135TEST_F(InputDispatcherTouchModeChangedTests, EventIsNotGeneratedIfNotChangingTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -08007136 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek26defcf2022-02-08 01:12:27 +00007137 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode,
7138 windowInfo.ownerPid, windowInfo.ownerUid,
Antonio Kanteka042c022022-07-06 16:51:07 -07007139 true /*hasPermission*/, ADISPLAY_ID_DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +00007140 mWindow->assertNoEvents();
7141 mSecondWindow->assertNoEvents();
7142}
7143
Antonio Kantek15beb512022-06-13 22:35:41 +00007144TEST_F(InputDispatcherTouchModeChangedTests, ChangeTouchOnSecondaryDisplayOnly) {
7145 const WindowInfo& windowInfo = *mThirdWindow->getInfo();
7146 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
7147 windowInfo.ownerPid, windowInfo.ownerUid,
7148 true /*hasPermission*/, SECOND_DISPLAY_ID));
7149 mWindow->assertNoEvents();
7150 mSecondWindow->assertNoEvents();
7151 mThirdWindow->consumeTouchModeEvent(!InputDispatcher::kDefaultInTouchMode);
7152}
7153
Antonio Kantek48710e42022-03-24 14:19:30 -07007154TEST_F(InputDispatcherTouchModeChangedTests, CanChangeTouchModeWhenOwningLastInteractedWindow) {
7155 // Interact with the window first.
7156 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
7157 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
7158 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
7159
7160 // Then remove focus.
7161 mWindow->setFocusable(false);
7162 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
7163
7164 // Assert that caller can switch touch mode by owning one of the last interacted window.
7165 const WindowInfo& windowInfo = *mWindow->getInfo();
7166 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
7167 windowInfo.ownerPid, windowInfo.ownerUid,
Antonio Kanteka042c022022-07-06 16:51:07 -07007168 false /*hasPermission*/, ADISPLAY_ID_DEFAULT));
Antonio Kantek48710e42022-03-24 14:19:30 -07007169}
7170
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007171class InputDispatcherSpyWindowTest : public InputDispatcherTest {
7172public:
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007173 sp<FakeWindowHandle> createSpy() {
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007174 std::shared_ptr<FakeApplicationHandle> application =
7175 std::make_shared<FakeApplicationHandle>();
7176 std::string name = "Fake Spy ";
7177 name += std::to_string(mSpyCount++);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007178 sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher,
7179 name.c_str(), ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007180 spy->setSpy(true);
Prabir Pradhan5c85e052021-12-22 02:27:12 -08007181 spy->setTrustedOverlay(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007182 return spy;
7183 }
7184
7185 sp<FakeWindowHandle> createForeground() {
7186 std::shared_ptr<FakeApplicationHandle> application =
7187 std::make_shared<FakeApplicationHandle>();
7188 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007189 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
7190 ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007191 window->setFocusable(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007192 return window;
7193 }
7194
7195private:
7196 int mSpyCount{0};
7197};
7198
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08007199using InputDispatcherSpyWindowDeathTest = InputDispatcherSpyWindowTest;
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007200/**
Prabir Pradhan5c85e052021-12-22 02:27:12 -08007201 * Adding a spy window that is not a trusted overlay causes Dispatcher to abort.
7202 */
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08007203TEST_F(InputDispatcherSpyWindowDeathTest, UntrustedSpy_AbortsDispatcher) {
7204 ScopedSilentDeath _silentDeath;
7205
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007206 auto spy = createSpy();
Prabir Pradhan5c85e052021-12-22 02:27:12 -08007207 spy->setTrustedOverlay(false);
7208 ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}}),
7209 ".* not a trusted overlay");
7210}
7211
7212/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007213 * Input injection into a display with a spy window but no foreground windows should succeed.
7214 */
7215TEST_F(InputDispatcherSpyWindowTest, NoForegroundWindow) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007216 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007217 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}});
7218
7219 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7220 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
7221 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7222 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7223}
7224
7225/**
7226 * Verify the order in which different input windows receive events. The touched foreground window
7227 * (if there is one) should always receive the event first. When there are multiple spy windows, the
7228 * spy windows will receive the event according to their Z-order, where the top-most spy window will
7229 * receive events before ones belows it.
7230 *
7231 * Here, we set up a scenario with four windows in the following Z order from the top:
7232 * spy1, spy2, window, spy3.
7233 * We then inject an event and verify that the foreground "window" receives it first, followed by
7234 * "spy1" and "spy2". The "spy3" does not receive the event because it is underneath the foreground
7235 * window.
7236 */
7237TEST_F(InputDispatcherSpyWindowTest, ReceivesInputInOrder) {
7238 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007239 auto spy1 = createSpy();
7240 auto spy2 = createSpy();
7241 auto spy3 = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007242 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window, spy3}}});
7243 const std::vector<sp<FakeWindowHandle>> channels{spy1, spy2, window, spy3};
7244 const size_t numChannels = channels.size();
7245
Michael Wright8e9a8562022-02-09 13:44:29 +00007246 base::unique_fd epollFd(epoll_create1(EPOLL_CLOEXEC));
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007247 if (!epollFd.ok()) {
7248 FAIL() << "Failed to create epoll fd";
7249 }
7250
7251 for (size_t i = 0; i < numChannels; i++) {
7252 struct epoll_event event = {.events = EPOLLIN, .data.u64 = i};
7253 if (epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, channels[i]->getChannelFd(), &event) < 0) {
7254 FAIL() << "Failed to add fd to epoll";
7255 }
7256 }
7257
7258 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7259 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
7260 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7261
7262 std::vector<size_t> eventOrder;
7263 std::vector<struct epoll_event> events(numChannels);
7264 for (;;) {
7265 const int nFds = epoll_wait(epollFd.get(), events.data(), static_cast<int>(numChannels),
7266 (100ms).count());
7267 if (nFds < 0) {
7268 FAIL() << "Failed to call epoll_wait";
7269 }
7270 if (nFds == 0) {
7271 break; // epoll_wait timed out
7272 }
7273 for (int i = 0; i < nFds; i++) {
Colin Cross5b799302022-10-18 21:52:41 -07007274 ASSERT_EQ(static_cast<uint32_t>(EPOLLIN), events[i].events);
Siarhei Vishniakou31977182022-09-30 08:51:23 -07007275 eventOrder.push_back(static_cast<size_t>(events[i].data.u64));
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007276 channels[i]->consumeMotionDown();
7277 }
7278 }
7279
7280 // Verify the order in which the events were received.
7281 EXPECT_EQ(3u, eventOrder.size());
7282 EXPECT_EQ(2u, eventOrder[0]); // index 2: window
7283 EXPECT_EQ(0u, eventOrder[1]); // index 0: spy1
7284 EXPECT_EQ(1u, eventOrder[2]); // index 1: spy2
7285}
7286
7287/**
7288 * A spy window using the NOT_TOUCHABLE flag does not receive events.
7289 */
7290TEST_F(InputDispatcherSpyWindowTest, NotTouchable) {
7291 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007292 auto spy = createSpy();
7293 spy->setTouchable(false);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007294 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
7295
7296 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7297 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
7298 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7299 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7300 spy->assertNoEvents();
7301}
7302
7303/**
7304 * A spy window will only receive gestures that originate within its touchable region. Gestures that
7305 * have their ACTION_DOWN outside of the touchable region of the spy window will not be dispatched
7306 * to the window.
7307 */
7308TEST_F(InputDispatcherSpyWindowTest, TouchableRegion) {
7309 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007310 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007311 spy->setTouchableRegion(Region{{0, 0, 20, 20}});
7312 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
7313
7314 // Inject an event outside the spy window's touchable region.
7315 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7316 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
7317 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7318 window->consumeMotionDown();
7319 spy->assertNoEvents();
7320 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7321 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
7322 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7323 window->consumeMotionUp();
7324 spy->assertNoEvents();
7325
7326 // Inject an event inside the spy window's touchable region.
7327 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7328 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7329 {5, 10}))
7330 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7331 window->consumeMotionDown();
7332 spy->consumeMotionDown();
7333}
7334
7335/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007336 * A spy window can listen for touches outside its touchable region using the WATCH_OUTSIDE_TOUCHES
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08007337 * flag, but it will get zero-ed out coordinates if the foreground has a different owner.
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007338 */
7339TEST_F(InputDispatcherSpyWindowTest, WatchOutsideTouches) {
7340 auto window = createForeground();
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08007341 window->setOwnerInfo(12, 34);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007342 auto spy = createSpy();
7343 spy->setWatchOutsideTouch(true);
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08007344 spy->setOwnerInfo(56, 78);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007345 spy->setFrame(Rect{0, 0, 20, 20});
7346 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
7347
7348 // Inject an event outside the spy window's frame and touchable region.
7349 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08007350 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7351 {100, 200}))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007352 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7353 window->consumeMotionDown();
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08007354 spy->consumeMotionOutsideWithZeroedCoords();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007355}
7356
7357/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007358 * Even when a spy window spans over multiple foreground windows, the spy should receive all
7359 * pointers that are down within its bounds.
7360 */
7361TEST_F(InputDispatcherSpyWindowTest, ReceivesMultiplePointers) {
7362 auto windowLeft = createForeground();
7363 windowLeft->setFrame({0, 0, 100, 200});
7364 auto windowRight = createForeground();
7365 windowRight->setFrame({100, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007366 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007367 spy->setFrame({0, 0, 200, 200});
7368 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, windowLeft, windowRight}}});
7369
7370 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7371 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7372 {50, 50}))
7373 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7374 windowLeft->consumeMotionDown();
7375 spy->consumeMotionDown();
7376
7377 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08007378 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007379 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
7380 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
7381 .pointer(
7382 PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50))
7383 .build();
7384 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7385 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
7386 InputEventInjectionSync::WAIT_FOR_RESULT))
7387 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7388 windowRight->consumeMotionDown();
7389 spy->consumeMotionPointerDown(1 /*pointerIndex*/);
7390}
7391
7392/**
7393 * When the first pointer lands outside the spy window and the second pointer lands inside it, the
7394 * the spy should receive the second pointer with ACTION_DOWN.
7395 */
7396TEST_F(InputDispatcherSpyWindowTest, ReceivesSecondPointerAsDown) {
7397 auto window = createForeground();
7398 window->setFrame({0, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007399 auto spyRight = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007400 spyRight->setFrame({100, 0, 200, 200});
7401 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyRight, window}}});
7402
7403 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7404 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7405 {50, 50}))
7406 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7407 window->consumeMotionDown();
7408 spyRight->assertNoEvents();
7409
7410 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08007411 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007412 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
7413 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
7414 .pointer(
7415 PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50))
7416 .build();
7417 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7418 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
7419 InputEventInjectionSync::WAIT_FOR_RESULT))
7420 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7421 window->consumeMotionPointerDown(1 /*pointerIndex*/);
7422 spyRight->consumeMotionDown();
7423}
7424
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007425/**
7426 * The spy window should not be able to affect whether or not touches are split. Only the foreground
7427 * windows should be allowed to control split touch.
7428 */
7429TEST_F(InputDispatcherSpyWindowTest, SplitIfNoForegroundWindowTouched) {
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08007430 // This spy window prevents touch splitting. However, we still expect to split touches
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007431 // because a foreground window has not disabled splitting.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007432 auto spy = createSpy();
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08007433 spy->setPreventSplitting(true);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007434
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007435 auto window = createForeground();
7436 window->setFrame(Rect(0, 0, 100, 100));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007437
7438 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
7439
7440 // First finger down, no window touched.
7441 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7442 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7443 {100, 200}))
7444 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7445 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7446 window->assertNoEvents();
7447
7448 // Second finger down on window, the window should receive touch down.
7449 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08007450 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007451 .displayId(ADISPLAY_ID_DEFAULT)
7452 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
7453 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
7454 .x(100)
7455 .y(200))
7456 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
7457 .build();
7458 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7459 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
7460 InputEventInjectionSync::WAIT_FOR_RESULT))
7461 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7462
7463 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7464 spy->consumeMotionPointerDown(1 /* pointerIndex */);
7465}
7466
7467/**
7468 * A spy window will usually be implemented as an un-focusable window. Verify that these windows
7469 * do not receive key events.
7470 */
7471TEST_F(InputDispatcherSpyWindowTest, UnfocusableSpyDoesNotReceiveKeyEvents) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007472 auto spy = createSpy();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007473 spy->setFocusable(false);
7474
7475 auto window = createForeground();
7476 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
7477 setFocusedWindow(window);
7478 window->consumeFocusEvent(true);
7479
7480 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7481 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
7482 window->consumeKeyDown(ADISPLAY_ID_NONE);
7483
7484 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7485 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
7486 window->consumeKeyUp(ADISPLAY_ID_NONE);
7487
7488 spy->assertNoEvents();
7489}
7490
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00007491using InputDispatcherPilferPointersTest = InputDispatcherSpyWindowTest;
7492
7493/**
7494 * A spy window can pilfer pointers. When this happens, touch gestures used by the spy window that
7495 * are currently sent to any other windows - including other spy windows - will also be cancelled.
7496 */
7497TEST_F(InputDispatcherPilferPointersTest, PilferPointers) {
7498 auto window = createForeground();
7499 auto spy1 = createSpy();
7500 auto spy2 = createSpy();
7501 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window}}});
7502
7503 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7504 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
7505 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7506 window->consumeMotionDown();
7507 spy1->consumeMotionDown();
7508 spy2->consumeMotionDown();
7509
7510 // Pilfer pointers from the second spy window.
7511 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy2->getToken()));
7512 spy2->assertNoEvents();
7513 spy1->consumeMotionCancel();
7514 window->consumeMotionCancel();
7515
7516 // The rest of the gesture should only be sent to the second spy window.
7517 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7518 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
7519 ADISPLAY_ID_DEFAULT))
7520 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7521 spy2->consumeMotionMove();
7522 spy1->assertNoEvents();
7523 window->assertNoEvents();
7524}
7525
7526/**
7527 * A spy window can pilfer pointers for a gesture even after the foreground window has been removed
7528 * in the middle of the gesture.
7529 */
7530TEST_F(InputDispatcherPilferPointersTest, CanPilferAfterWindowIsRemovedMidStream) {
7531 auto window = createForeground();
7532 auto spy = createSpy();
7533 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
7534
7535 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7536 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
7537 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7538 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7539 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7540
7541 window->releaseChannel();
7542
7543 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
7544
7545 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7546 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
7547 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7548 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
7549}
7550
7551/**
7552 * After a spy window pilfers pointers, new pointers that go down in its bounds should be sent to
7553 * the spy, but not to any other windows.
7554 */
7555TEST_F(InputDispatcherPilferPointersTest, ContinuesToReceiveGestureAfterPilfer) {
7556 auto spy = createSpy();
7557 auto window = createForeground();
7558
7559 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
7560
7561 // First finger down on the window and the spy.
7562 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7563 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7564 {100, 200}))
7565 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7566 spy->consumeMotionDown();
7567 window->consumeMotionDown();
7568
7569 // Spy window pilfers the pointers.
7570 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
7571 window->consumeMotionCancel();
7572
7573 // Second finger down on the window and spy, but the window should not receive the pointer down.
7574 const MotionEvent secondFingerDownEvent =
7575 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7576 .displayId(ADISPLAY_ID_DEFAULT)
7577 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
7578 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
7579 .x(100)
7580 .y(200))
7581 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
7582 .build();
7583 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7584 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
7585 InputEventInjectionSync::WAIT_FOR_RESULT))
7586 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7587
7588 spy->consumeMotionPointerDown(1 /*pointerIndex*/);
7589
7590 // Third finger goes down outside all windows, so injection should fail.
7591 const MotionEvent thirdFingerDownEvent =
7592 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7593 .displayId(ADISPLAY_ID_DEFAULT)
7594 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
7595 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
7596 .x(100)
7597 .y(200))
7598 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
7599 .pointer(PointerBuilder(/* id */ 2, AMOTION_EVENT_TOOL_TYPE_FINGER).x(-5).y(-5))
7600 .build();
7601 ASSERT_EQ(InputEventInjectionResult::FAILED,
7602 injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
7603 InputEventInjectionSync::WAIT_FOR_RESULT))
7604 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7605
7606 spy->assertNoEvents();
7607 window->assertNoEvents();
7608}
7609
7610/**
7611 * After a spy window pilfers pointers, only the pointers used by the spy should be canceled
7612 */
7613TEST_F(InputDispatcherPilferPointersTest, PartiallyPilferRequiredPointers) {
7614 auto spy = createSpy();
7615 spy->setFrame(Rect(0, 0, 100, 100));
7616 auto window = createForeground();
7617 window->setFrame(Rect(0, 0, 200, 200));
7618
7619 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
7620
7621 // First finger down on the window only
7622 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7623 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7624 {150, 150}))
7625 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7626 window->consumeMotionDown();
7627
7628 // Second finger down on the spy and window
7629 const MotionEvent secondFingerDownEvent =
7630 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7631 .displayId(ADISPLAY_ID_DEFAULT)
7632 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
7633 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
7634 .x(150)
7635 .y(150))
7636 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(10).y(10))
7637 .build();
7638 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7639 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
7640 InputEventInjectionSync::WAIT_FOR_RESULT))
7641 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7642 spy->consumeMotionDown();
7643 window->consumeMotionPointerDown(1);
7644
7645 // Third finger down on the spy and window
7646 const MotionEvent thirdFingerDownEvent =
7647 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7648 .displayId(ADISPLAY_ID_DEFAULT)
7649 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
7650 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
7651 .x(150)
7652 .y(150))
7653 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(10).y(10))
7654 .pointer(PointerBuilder(/* id */ 2, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
7655 .build();
7656 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7657 injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
7658 InputEventInjectionSync::WAIT_FOR_RESULT))
7659 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7660 spy->consumeMotionPointerDown(1);
7661 window->consumeMotionPointerDown(2);
7662
7663 // Spy window pilfers the pointers.
7664 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
7665 window->consumeMotionPointerUp(/* idx */ 2, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
7666 window->consumeMotionPointerUp(/* idx */ 1, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
7667
7668 spy->assertNoEvents();
7669 window->assertNoEvents();
7670}
7671
7672/**
7673 * After a spy window pilfers pointers, all pilfered pointers that have already been dispatched to
7674 * other windows should be canceled. If this results in the cancellation of all pointers for some
7675 * window, then that window should receive ACTION_CANCEL.
7676 */
7677TEST_F(InputDispatcherPilferPointersTest, PilferAllRequiredPointers) {
7678 auto spy = createSpy();
7679 spy->setFrame(Rect(0, 0, 100, 100));
7680 auto window = createForeground();
7681 window->setFrame(Rect(0, 0, 200, 200));
7682
7683 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
7684
7685 // First finger down on both spy and window
7686 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7687 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7688 {10, 10}))
7689 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7690 window->consumeMotionDown();
7691 spy->consumeMotionDown();
7692
7693 // Second finger down on the spy and window
7694 const MotionEvent secondFingerDownEvent =
7695 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7696 .displayId(ADISPLAY_ID_DEFAULT)
7697 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
7698 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(10).y(10))
7699 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
7700 .build();
7701 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7702 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
7703 InputEventInjectionSync::WAIT_FOR_RESULT))
7704 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7705 spy->consumeMotionPointerDown(1);
7706 window->consumeMotionPointerDown(1);
7707
7708 // Spy window pilfers the pointers.
7709 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
7710 window->consumeMotionCancel();
7711
7712 spy->assertNoEvents();
7713 window->assertNoEvents();
7714}
7715
7716/**
7717 * After a spy window pilfers pointers, new pointers that are not touching the spy window can still
7718 * be sent to other windows
7719 */
7720TEST_F(InputDispatcherPilferPointersTest, CanReceivePointersAfterPilfer) {
7721 auto spy = createSpy();
7722 spy->setFrame(Rect(0, 0, 100, 100));
7723 auto window = createForeground();
7724 window->setFrame(Rect(0, 0, 200, 200));
7725
7726 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
7727
7728 // First finger down on both window and spy
7729 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7730 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7731 {10, 10}))
7732 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7733 window->consumeMotionDown();
7734 spy->consumeMotionDown();
7735
7736 // Spy window pilfers the pointers.
7737 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
7738 window->consumeMotionCancel();
7739
7740 // Second finger down on the window only
7741 const MotionEvent secondFingerDownEvent =
7742 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7743 .displayId(ADISPLAY_ID_DEFAULT)
7744 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
7745 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(10).y(10))
7746 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER)
7747 .x(150)
7748 .y(150))
7749 .build();
7750 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7751 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
7752 InputEventInjectionSync::WAIT_FOR_RESULT))
7753 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7754 window->consumeMotionDown();
7755 window->assertNoEvents();
7756
7757 // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line
7758 spy->consumeMotionMove();
7759 spy->assertNoEvents();
7760}
7761
Prabir Pradhand65552b2021-10-07 11:23:50 -07007762class InputDispatcherStylusInterceptorTest : public InputDispatcherTest {
7763public:
7764 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupStylusOverlayScenario() {
7765 std::shared_ptr<FakeApplicationHandle> overlayApplication =
7766 std::make_shared<FakeApplicationHandle>();
7767 sp<FakeWindowHandle> overlay =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007768 sp<FakeWindowHandle>::make(overlayApplication, mDispatcher,
7769 "Stylus interceptor window", ADISPLAY_ID_DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -07007770 overlay->setFocusable(false);
7771 overlay->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007772 overlay->setTouchable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007773 overlay->setInterceptsStylus(true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07007774 overlay->setTrustedOverlay(true);
7775
7776 std::shared_ptr<FakeApplicationHandle> application =
7777 std::make_shared<FakeApplicationHandle>();
7778 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007779 sp<FakeWindowHandle>::make(application, mDispatcher, "Application window",
7780 ADISPLAY_ID_DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -07007781 window->setFocusable(true);
7782 window->setOwnerInfo(222, 222);
Prabir Pradhand65552b2021-10-07 11:23:50 -07007783
7784 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
7785 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
7786 setFocusedWindow(window);
7787 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
7788 return {std::move(overlay), std::move(window)};
7789 }
7790
7791 void sendFingerEvent(int32_t action) {
7792 NotifyMotionArgs motionArgs =
7793 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
7794 ADISPLAY_ID_DEFAULT, {PointF{20, 20}});
7795 mDispatcher->notifyMotion(&motionArgs);
7796 }
7797
7798 void sendStylusEvent(int32_t action) {
7799 NotifyMotionArgs motionArgs =
7800 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
7801 ADISPLAY_ID_DEFAULT, {PointF{30, 40}});
7802 motionArgs.pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS;
7803 mDispatcher->notifyMotion(&motionArgs);
7804 }
7805};
7806
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08007807using InputDispatcherStylusInterceptorDeathTest = InputDispatcherStylusInterceptorTest;
7808
7809TEST_F(InputDispatcherStylusInterceptorDeathTest, UntrustedOverlay_AbortsDispatcher) {
7810 ScopedSilentDeath _silentDeath;
7811
Prabir Pradhand65552b2021-10-07 11:23:50 -07007812 auto [overlay, window] = setupStylusOverlayScenario();
7813 overlay->setTrustedOverlay(false);
7814 // Configuring an untrusted overlay as a stylus interceptor should cause Dispatcher to abort.
7815 ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}),
7816 ".* not a trusted overlay");
7817}
7818
7819TEST_F(InputDispatcherStylusInterceptorTest, ConsmesOnlyStylusEvents) {
7820 auto [overlay, window] = setupStylusOverlayScenario();
7821 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
7822
7823 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
7824 overlay->consumeMotionDown();
7825 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
7826 overlay->consumeMotionUp();
7827
7828 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
7829 window->consumeMotionDown();
7830 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
7831 window->consumeMotionUp();
7832
7833 overlay->assertNoEvents();
7834 window->assertNoEvents();
7835}
7836
7837TEST_F(InputDispatcherStylusInterceptorTest, SpyWindowStylusInterceptor) {
7838 auto [overlay, window] = setupStylusOverlayScenario();
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007839 overlay->setSpy(true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07007840 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
7841
7842 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
7843 overlay->consumeMotionDown();
7844 window->consumeMotionDown();
7845 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
7846 overlay->consumeMotionUp();
7847 window->consumeMotionUp();
7848
7849 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
7850 window->consumeMotionDown();
7851 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
7852 window->consumeMotionUp();
7853
7854 overlay->assertNoEvents();
7855 window->assertNoEvents();
7856}
7857
Prabir Pradhan6dfbf262022-03-14 15:24:30 +00007858/**
7859 * Set up a scenario to test the behavior used by the stylus handwriting detection feature.
7860 * The scenario is as follows:
7861 * - The stylus interceptor overlay is configured as a spy window.
7862 * - The stylus interceptor spy receives the start of a new stylus gesture.
7863 * - It pilfers pointers and then configures itself to no longer be a spy.
7864 * - The stylus interceptor continues to receive the rest of the gesture.
7865 */
7866TEST_F(InputDispatcherStylusInterceptorTest, StylusHandwritingScenario) {
7867 auto [overlay, window] = setupStylusOverlayScenario();
7868 overlay->setSpy(true);
7869 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
7870
7871 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
7872 overlay->consumeMotionDown();
7873 window->consumeMotionDown();
7874
7875 // The interceptor pilfers the pointers.
7876 EXPECT_EQ(OK, mDispatcher->pilferPointers(overlay->getToken()));
7877 window->consumeMotionCancel();
7878
7879 // The interceptor configures itself so that it is no longer a spy.
7880 overlay->setSpy(false);
7881 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
7882
7883 // It continues to receive the rest of the stylus gesture.
7884 sendStylusEvent(AMOTION_EVENT_ACTION_MOVE);
7885 overlay->consumeMotionMove();
7886 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
7887 overlay->consumeMotionUp();
7888
7889 window->assertNoEvents();
7890}
7891
Prabir Pradhan5735a322022-04-11 17:23:34 +00007892struct User {
7893 int32_t mPid;
7894 int32_t mUid;
7895 uint32_t mPolicyFlags{DEFAULT_POLICY_FLAGS};
7896 std::unique_ptr<InputDispatcher>& mDispatcher;
7897
7898 User(std::unique_ptr<InputDispatcher>& dispatcher, int32_t pid, int32_t uid)
7899 : mPid(pid), mUid(uid), mDispatcher(dispatcher) {}
7900
7901 InputEventInjectionResult injectTargetedMotion(int32_t action) const {
7902 return injectMotionEvent(mDispatcher, action, AINPUT_SOURCE_TOUCHSCREEN,
7903 ADISPLAY_ID_DEFAULT, {100, 200},
7904 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
7905 AMOTION_EVENT_INVALID_CURSOR_POSITION},
7906 INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT,
7907 systemTime(SYSTEM_TIME_MONOTONIC), {mUid}, mPolicyFlags);
7908 }
7909
7910 InputEventInjectionResult injectTargetedKey(int32_t action) const {
7911 return inputdispatcher::injectKey(mDispatcher, action, 0 /* repeatCount*/, ADISPLAY_ID_NONE,
7912 InputEventInjectionSync::WAIT_FOR_RESULT,
7913 INJECT_EVENT_TIMEOUT, false /*allowKeyRepeat*/, {mUid},
7914 mPolicyFlags);
7915 }
7916
7917 sp<FakeWindowHandle> createWindow() const {
7918 std::shared_ptr<FakeApplicationHandle> overlayApplication =
7919 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007920 sp<FakeWindowHandle> window =
7921 sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, "Owned Window",
7922 ADISPLAY_ID_DEFAULT);
Prabir Pradhan5735a322022-04-11 17:23:34 +00007923 window->setOwnerInfo(mPid, mUid);
7924 return window;
7925 }
7926};
7927
7928using InputDispatcherTargetedInjectionTest = InputDispatcherTest;
7929
7930TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedWindow) {
7931 auto owner = User(mDispatcher, 10, 11);
7932 auto window = owner.createWindow();
7933 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
7934
7935 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
7936 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
7937 window->consumeMotionDown();
7938
7939 setFocusedWindow(window);
7940 window->consumeFocusEvent(true);
7941
7942 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
7943 owner.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
7944 window->consumeKeyDown(ADISPLAY_ID_NONE);
7945}
7946
7947TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedWindow) {
7948 auto owner = User(mDispatcher, 10, 11);
7949 auto window = owner.createWindow();
7950 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
7951
7952 auto rando = User(mDispatcher, 20, 21);
7953 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
7954 rando.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
7955
7956 setFocusedWindow(window);
7957 window->consumeFocusEvent(true);
7958
7959 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
7960 rando.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
7961 window->assertNoEvents();
7962}
7963
7964TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedSpyWindow) {
7965 auto owner = User(mDispatcher, 10, 11);
7966 auto window = owner.createWindow();
7967 auto spy = owner.createWindow();
7968 spy->setSpy(true);
7969 spy->setTrustedOverlay(true);
7970 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
7971
7972 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
7973 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
7974 spy->consumeMotionDown();
7975 window->consumeMotionDown();
7976}
7977
7978TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedSpyWindow) {
7979 auto owner = User(mDispatcher, 10, 11);
7980 auto window = owner.createWindow();
7981
7982 auto rando = User(mDispatcher, 20, 21);
7983 auto randosSpy = rando.createWindow();
7984 randosSpy->setSpy(true);
7985 randosSpy->setTrustedOverlay(true);
7986 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}});
7987
7988 // The event is targeted at owner's window, so injection should succeed, but the spy should
7989 // not receive the event.
7990 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
7991 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
7992 randosSpy->assertNoEvents();
7993 window->consumeMotionDown();
7994}
7995
7996TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoAnyWindowWhenNotTargeting) {
7997 auto owner = User(mDispatcher, 10, 11);
7998 auto window = owner.createWindow();
7999
8000 auto rando = User(mDispatcher, 20, 21);
8001 auto randosSpy = rando.createWindow();
8002 randosSpy->setSpy(true);
8003 randosSpy->setTrustedOverlay(true);
8004 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}});
8005
8006 // A user that has injection permission can inject into any window.
8007 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
8008 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
8009 ADISPLAY_ID_DEFAULT));
8010 randosSpy->consumeMotionDown();
8011 window->consumeMotionDown();
8012
8013 setFocusedWindow(randosSpy);
8014 randosSpy->consumeFocusEvent(true);
8015
8016 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher));
8017 randosSpy->consumeKeyDown(ADISPLAY_ID_NONE);
8018 window->assertNoEvents();
8019}
8020
8021TEST_F(InputDispatcherTargetedInjectionTest, CanGenerateActionOutsideToOtherUids) {
8022 auto owner = User(mDispatcher, 10, 11);
8023 auto window = owner.createWindow();
8024
8025 auto rando = User(mDispatcher, 20, 21);
8026 auto randosWindow = rando.createWindow();
8027 randosWindow->setFrame(Rect{-10, -10, -5, -5});
8028 randosWindow->setWatchOutsideTouch(true);
8029 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosWindow, window}}});
8030
8031 // We allow generation of ACTION_OUTSIDE events into windows owned by different uids.
8032 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
8033 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
8034 window->consumeMotionDown();
8035 randosWindow->consumeMotionOutside();
8036}
8037
Garfield Tane84e6f92019-08-29 17:28:41 -07008038} // namespace android::inputdispatcher