blob: 41c174a1f72bce60adf00452d34c7ce4a4ee6f58 [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 }
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -0800113 if (action == AMOTION_EVENT_ACTION_DOWN) {
114 if (!matches) {
115 *result_listener << "; ";
116 }
117 *result_listener << "downTime should match eventTime for ACTION_DOWN events";
118 matches &= arg.getDownTime() == arg.getEventTime();
119 }
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -0800120 if (action == AMOTION_EVENT_ACTION_CANCEL) {
121 if (!matches) {
122 *result_listener << "; ";
123 }
124 *result_listener << "expected FLAG_CANCELED to be set with ACTION_CANCEL, but was not set";
125 matches &= (arg.getFlags() & AMOTION_EVENT_FLAG_CANCELED) != 0;
126 }
127 return matches;
128}
129
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -0800130MATCHER_P(WithDownTime, downTime, "InputEvent with specified downTime") {
131 return arg.getDownTime() == downTime;
132}
133
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -0800134MATCHER_P(WithSource, source, "InputEvent with specified source") {
135 *result_listener << "expected source " << inputEventSourceToString(source) << ", but got "
136 << inputEventSourceToString(arg.getSource());
137 return arg.getSource() == source;
138}
139
Michael Wrightd02c5b62014-02-10 15:10:22 -0800140// --- FakeInputDispatcherPolicy ---
141
142class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface {
143 InputDispatcherConfiguration mConfig;
144
Prabir Pradhanedd96402022-02-15 01:46:16 -0800145 using AnrResult = std::pair<sp<IBinder>, int32_t /*pid*/>;
146
Michael Wrightd02c5b62014-02-10 15:10:22 -0800147protected:
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000148 virtual ~FakeInputDispatcherPolicy() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800149
150public:
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000151 FakeInputDispatcherPolicy() {}
Jackal Guof9696682018-10-05 12:23:23 +0800152
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800153 void assertFilterInputEventWasCalled(const NotifyKeyArgs& args) {
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700154 assertFilterInputEventWasCalledInternal([&args](const InputEvent& event) {
155 ASSERT_EQ(event.getType(), AINPUT_EVENT_TYPE_KEY);
156 EXPECT_EQ(event.getDisplayId(), args.displayId);
157
158 const auto& keyEvent = static_cast<const KeyEvent&>(event);
159 EXPECT_EQ(keyEvent.getEventTime(), args.eventTime);
160 EXPECT_EQ(keyEvent.getAction(), args.action);
161 });
Jackal Guof9696682018-10-05 12:23:23 +0800162 }
163
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700164 void assertFilterInputEventWasCalled(const NotifyMotionArgs& args, vec2 point) {
165 assertFilterInputEventWasCalledInternal([&](const InputEvent& event) {
166 ASSERT_EQ(event.getType(), AINPUT_EVENT_TYPE_MOTION);
167 EXPECT_EQ(event.getDisplayId(), args.displayId);
168
169 const auto& motionEvent = static_cast<const MotionEvent&>(event);
170 EXPECT_EQ(motionEvent.getEventTime(), args.eventTime);
171 EXPECT_EQ(motionEvent.getAction(), args.action);
172 EXPECT_EQ(motionEvent.getX(0), point.x);
173 EXPECT_EQ(motionEvent.getY(0), point.y);
174 EXPECT_EQ(motionEvent.getRawX(0), point.x);
175 EXPECT_EQ(motionEvent.getRawY(0), point.y);
176 });
Jackal Guof9696682018-10-05 12:23:23 +0800177 }
178
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700179 void assertFilterInputEventWasNotCalled() {
180 std::scoped_lock lock(mLock);
181 ASSERT_EQ(nullptr, mFilteredEvent);
182 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800183
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800184 void assertNotifyConfigurationChangedWasCalled(nsecs_t when) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700185 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800186 ASSERT_TRUE(mConfigurationChangedTime)
187 << "Timed out waiting for configuration changed call";
188 ASSERT_EQ(*mConfigurationChangedTime, when);
189 mConfigurationChangedTime = std::nullopt;
190 }
191
192 void assertNotifySwitchWasCalled(const NotifySwitchArgs& args) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700193 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800194 ASSERT_TRUE(mLastNotifySwitch);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800195 // We do not check id because it is not exposed to the policy
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800196 EXPECT_EQ(args.eventTime, mLastNotifySwitch->eventTime);
197 EXPECT_EQ(args.policyFlags, mLastNotifySwitch->policyFlags);
198 EXPECT_EQ(args.switchValues, mLastNotifySwitch->switchValues);
199 EXPECT_EQ(args.switchMask, mLastNotifySwitch->switchMask);
200 mLastNotifySwitch = std::nullopt;
201 }
202
chaviwfd6d3512019-03-25 13:23:49 -0700203 void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700204 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800205 ASSERT_EQ(touchedToken, mOnPointerDownToken);
206 mOnPointerDownToken.clear();
207 }
208
209 void assertOnPointerDownWasNotCalled() {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700210 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800211 ASSERT_TRUE(mOnPointerDownToken == nullptr)
212 << "Expected onPointerDownOutsideFocus to not have been called";
chaviwfd6d3512019-03-25 13:23:49 -0700213 }
214
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700215 // This function must be called soon after the expected ANR timer starts,
216 // because we are also checking how much time has passed.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500217 void assertNotifyNoFocusedWindowAnrWasCalled(
Chris Yea209fde2020-07-22 13:54:51 -0700218 std::chrono::nanoseconds timeout,
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500219 const std::shared_ptr<InputApplicationHandle>& expectedApplication) {
Prabir Pradhanedd96402022-02-15 01:46:16 -0800220 std::unique_lock lock(mLock);
221 android::base::ScopedLockAssertion assumeLocked(mLock);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500222 std::shared_ptr<InputApplicationHandle> application;
Prabir Pradhanedd96402022-02-15 01:46:16 -0800223 ASSERT_NO_FATAL_FAILURE(
224 application = getAnrTokenLockedInterruptible(timeout, mAnrApplications, lock));
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500225 ASSERT_EQ(expectedApplication, application);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700226 }
227
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000228 void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout,
Prabir Pradhanedd96402022-02-15 01:46:16 -0800229 const sp<WindowInfoHandle>& window) {
230 LOG_ALWAYS_FATAL_IF(window == nullptr, "window should not be null");
231 assertNotifyWindowUnresponsiveWasCalled(timeout, window->getToken(),
232 window->getInfo()->ownerPid);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500233 }
234
Prabir Pradhanedd96402022-02-15 01:46:16 -0800235 void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout,
236 const sp<IBinder>& expectedToken,
237 int32_t expectedPid) {
238 std::unique_lock lock(mLock);
239 android::base::ScopedLockAssertion assumeLocked(mLock);
240 AnrResult result;
241 ASSERT_NO_FATAL_FAILURE(result =
242 getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock));
243 const auto& [token, pid] = result;
244 ASSERT_EQ(expectedToken, token);
245 ASSERT_EQ(expectedPid, pid);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500246 }
247
Prabir Pradhanedd96402022-02-15 01:46:16 -0800248 /** Wrap call with ASSERT_NO_FATAL_FAILURE() to ensure the return value is valid. */
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000249 sp<IBinder> getUnresponsiveWindowToken(std::chrono::nanoseconds timeout) {
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500250 std::unique_lock lock(mLock);
251 android::base::ScopedLockAssertion assumeLocked(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800252 AnrResult result = getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock);
253 const auto& [token, _] = result;
254 return token;
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000255 }
256
Prabir Pradhanedd96402022-02-15 01:46:16 -0800257 void assertNotifyWindowResponsiveWasCalled(const sp<IBinder>& expectedToken,
258 int32_t expectedPid) {
259 std::unique_lock lock(mLock);
260 android::base::ScopedLockAssertion assumeLocked(mLock);
261 AnrResult result;
262 ASSERT_NO_FATAL_FAILURE(
263 result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock));
264 const auto& [token, pid] = result;
265 ASSERT_EQ(expectedToken, token);
266 ASSERT_EQ(expectedPid, pid);
267 }
268
269 /** Wrap call with ASSERT_NO_FATAL_FAILURE() to ensure the return value is valid. */
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000270 sp<IBinder> getResponsiveWindowToken() {
271 std::unique_lock lock(mLock);
272 android::base::ScopedLockAssertion assumeLocked(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800273 AnrResult result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock);
274 const auto& [token, _] = result;
275 return token;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700276 }
277
278 void assertNotifyAnrWasNotCalled() {
279 std::scoped_lock lock(mLock);
280 ASSERT_TRUE(mAnrApplications.empty());
Prabir Pradhanedd96402022-02-15 01:46:16 -0800281 ASSERT_TRUE(mAnrWindows.empty());
282 ASSERT_TRUE(mResponsiveWindows.empty())
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500283 << "ANR was not called, but please also consume the 'connection is responsive' "
284 "signal";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700285 }
286
Garfield Tan1c7bc862020-01-28 13:24:04 -0800287 void setKeyRepeatConfiguration(nsecs_t timeout, nsecs_t delay) {
288 mConfig.keyRepeatTimeout = timeout;
289 mConfig.keyRepeatDelay = delay;
290 }
291
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000292 PointerCaptureRequest assertSetPointerCaptureCalled(bool enabled) {
Prabir Pradhan99987712020-11-10 18:43:05 -0800293 std::unique_lock lock(mLock);
294 base::ScopedLockAssertion assumeLocked(mLock);
295
296 if (!mPointerCaptureChangedCondition.wait_for(lock, 100ms,
297 [this, enabled]() REQUIRES(mLock) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000298 return mPointerCaptureRequest->enable ==
Prabir Pradhan99987712020-11-10 18:43:05 -0800299 enabled;
300 })) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000301 ADD_FAILURE() << "Timed out waiting for setPointerCapture(" << enabled
302 << ") to be called.";
303 return {};
Prabir Pradhan99987712020-11-10 18:43:05 -0800304 }
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000305 auto request = *mPointerCaptureRequest;
306 mPointerCaptureRequest.reset();
307 return request;
Prabir Pradhan99987712020-11-10 18:43:05 -0800308 }
309
310 void assertSetPointerCaptureNotCalled() {
311 std::unique_lock lock(mLock);
312 base::ScopedLockAssertion assumeLocked(mLock);
313
314 if (mPointerCaptureChangedCondition.wait_for(lock, 100ms) != std::cv_status::timeout) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000315 FAIL() << "Expected setPointerCapture(request) to not be called, but was called. "
Prabir Pradhan99987712020-11-10 18:43:05 -0800316 "enabled = "
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000317 << std::to_string(mPointerCaptureRequest->enable);
Prabir Pradhan99987712020-11-10 18:43:05 -0800318 }
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000319 mPointerCaptureRequest.reset();
Prabir Pradhan99987712020-11-10 18:43:05 -0800320 }
321
arthurhungf452d0b2021-01-06 00:19:52 +0800322 void assertDropTargetEquals(const sp<IBinder>& targetToken) {
323 std::scoped_lock lock(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800324 ASSERT_TRUE(mNotifyDropWindowWasCalled);
arthurhungf452d0b2021-01-06 00:19:52 +0800325 ASSERT_EQ(targetToken, mDropTargetWindowToken);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800326 mNotifyDropWindowWasCalled = false;
arthurhungf452d0b2021-01-06 00:19:52 +0800327 }
328
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800329 void assertNotifyInputChannelBrokenWasCalled(const sp<IBinder>& token) {
330 std::unique_lock lock(mLock);
331 base::ScopedLockAssertion assumeLocked(mLock);
332 std::optional<sp<IBinder>> receivedToken =
333 getItemFromStorageLockedInterruptible(100ms, mBrokenInputChannels, lock,
334 mNotifyInputChannelBroken);
335 ASSERT_TRUE(receivedToken.has_value());
336 ASSERT_EQ(token, *receivedToken);
337 }
338
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800339 /**
340 * Set policy timeout. A value of zero means next key will not be intercepted.
341 */
342 void setInterceptKeyTimeout(std::chrono::milliseconds timeout) {
343 mInterceptKeyTimeout = timeout;
344 }
345
Michael Wrightd02c5b62014-02-10 15:10:22 -0800346private:
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700347 std::mutex mLock;
348 std::unique_ptr<InputEvent> mFilteredEvent GUARDED_BY(mLock);
349 std::optional<nsecs_t> mConfigurationChangedTime GUARDED_BY(mLock);
350 sp<IBinder> mOnPointerDownToken GUARDED_BY(mLock);
351 std::optional<NotifySwitchArgs> mLastNotifySwitch GUARDED_BY(mLock);
Jackal Guof9696682018-10-05 12:23:23 +0800352
Prabir Pradhan99987712020-11-10 18:43:05 -0800353 std::condition_variable mPointerCaptureChangedCondition;
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000354
355 std::optional<PointerCaptureRequest> mPointerCaptureRequest GUARDED_BY(mLock);
Prabir Pradhan99987712020-11-10 18:43:05 -0800356
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700357 // ANR handling
Chris Yea209fde2020-07-22 13:54:51 -0700358 std::queue<std::shared_ptr<InputApplicationHandle>> mAnrApplications GUARDED_BY(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800359 std::queue<AnrResult> mAnrWindows GUARDED_BY(mLock);
360 std::queue<AnrResult> mResponsiveWindows GUARDED_BY(mLock);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700361 std::condition_variable mNotifyAnr;
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800362 std::queue<sp<IBinder>> mBrokenInputChannels GUARDED_BY(mLock);
363 std::condition_variable mNotifyInputChannelBroken;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700364
arthurhungf452d0b2021-01-06 00:19:52 +0800365 sp<IBinder> mDropTargetWindowToken GUARDED_BY(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800366 bool mNotifyDropWindowWasCalled GUARDED_BY(mLock) = false;
arthurhungf452d0b2021-01-06 00:19:52 +0800367
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800368 std::chrono::milliseconds mInterceptKeyTimeout = 0ms;
369
Prabir Pradhanedd96402022-02-15 01:46:16 -0800370 // All three ANR-related callbacks behave the same way, so we use this generic function to wait
371 // for a specific container to become non-empty. When the container is non-empty, return the
372 // first entry from the container and erase it.
373 template <class T>
374 T getAnrTokenLockedInterruptible(std::chrono::nanoseconds timeout, std::queue<T>& storage,
375 std::unique_lock<std::mutex>& lock) REQUIRES(mLock) {
376 // If there is an ANR, Dispatcher won't be idle because there are still events
377 // in the waitQueue that we need to check on. So we can't wait for dispatcher to be idle
378 // before checking if ANR was called.
379 // Since dispatcher is not guaranteed to call notifyNoFocusedWindowAnr right away, we need
380 // to provide it some time to act. 100ms seems reasonable.
381 std::chrono::duration timeToWait = timeout + 100ms; // provide some slack
382 const std::chrono::time_point start = std::chrono::steady_clock::now();
383 std::optional<T> token =
384 getItemFromStorageLockedInterruptible(timeToWait, storage, lock, mNotifyAnr);
385 if (!token.has_value()) {
386 ADD_FAILURE() << "Did not receive the ANR callback";
387 return {};
388 }
389
390 const std::chrono::duration waited = std::chrono::steady_clock::now() - start;
391 // Ensure that the ANR didn't get raised too early. We can't be too strict here because
392 // the dispatcher started counting before this function was called
393 if (std::chrono::abs(timeout - waited) > 100ms) {
394 ADD_FAILURE() << "ANR was raised too early or too late. Expected "
395 << std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count()
396 << "ms, but waited "
397 << std::chrono::duration_cast<std::chrono::milliseconds>(waited).count()
398 << "ms instead";
399 }
400 return *token;
401 }
402
403 template <class T>
404 std::optional<T> getItemFromStorageLockedInterruptible(std::chrono::nanoseconds timeout,
405 std::queue<T>& storage,
406 std::unique_lock<std::mutex>& lock,
407 std::condition_variable& condition)
408 REQUIRES(mLock) {
409 condition.wait_for(lock, timeout,
410 [&storage]() REQUIRES(mLock) { return !storage.empty(); });
411 if (storage.empty()) {
412 ADD_FAILURE() << "Did not receive the expected callback";
413 return std::nullopt;
414 }
415 T item = storage.front();
416 storage.pop();
417 return std::make_optional(item);
418 }
419
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600420 void notifyConfigurationChanged(nsecs_t when) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700421 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800422 mConfigurationChangedTime = when;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800423 }
424
Prabir Pradhanedd96402022-02-15 01:46:16 -0800425 void notifyWindowUnresponsive(const sp<IBinder>& connectionToken, std::optional<int32_t> pid,
426 const std::string&) override {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700427 std::scoped_lock lock(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800428 ASSERT_TRUE(pid.has_value());
429 mAnrWindows.push({connectionToken, *pid});
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700430 mNotifyAnr.notify_all();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500431 }
432
Prabir Pradhanedd96402022-02-15 01:46:16 -0800433 void notifyWindowResponsive(const sp<IBinder>& connectionToken,
434 std::optional<int32_t> pid) override {
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500435 std::scoped_lock lock(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800436 ASSERT_TRUE(pid.has_value());
437 mResponsiveWindows.push({connectionToken, *pid});
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500438 mNotifyAnr.notify_all();
439 }
440
441 void notifyNoFocusedWindowAnr(
442 const std::shared_ptr<InputApplicationHandle>& applicationHandle) override {
443 std::scoped_lock lock(mLock);
444 mAnrApplications.push(applicationHandle);
445 mNotifyAnr.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800446 }
447
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800448 void notifyInputChannelBroken(const sp<IBinder>& connectionToken) override {
449 std::scoped_lock lock(mLock);
450 mBrokenInputChannels.push(connectionToken);
451 mNotifyInputChannelBroken.notify_all();
452 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800453
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600454 void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {}
Robert Carr740167f2018-10-11 19:03:41 -0700455
Chris Yef59a2f42020-10-16 12:55:26 -0700456 void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType,
457 InputDeviceSensorAccuracy accuracy, nsecs_t timestamp,
458 const std::vector<float>& values) override {}
459
460 void notifySensorAccuracy(int deviceId, InputDeviceSensorType sensorType,
461 InputDeviceSensorAccuracy accuracy) override {}
Bernardo Rufino2e1f6512020-10-08 13:42:07 +0000462
Chris Yefb552902021-02-03 17:18:37 -0800463 void notifyVibratorState(int32_t deviceId, bool isOn) override {}
464
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600465 void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800466 *outConfig = mConfig;
467 }
468
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600469 bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700470 std::scoped_lock lock(mLock);
Jackal Guof9696682018-10-05 12:23:23 +0800471 switch (inputEvent->getType()) {
472 case AINPUT_EVENT_TYPE_KEY: {
473 const KeyEvent* keyEvent = static_cast<const KeyEvent*>(inputEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800474 mFilteredEvent = std::make_unique<KeyEvent>(*keyEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800475 break;
476 }
477
478 case AINPUT_EVENT_TYPE_MOTION: {
479 const MotionEvent* motionEvent = static_cast<const MotionEvent*>(inputEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800480 mFilteredEvent = std::make_unique<MotionEvent>(*motionEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800481 break;
482 }
483 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800484 return true;
485 }
486
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800487 void interceptKeyBeforeQueueing(const KeyEvent* inputEvent, uint32_t&) override {
488 if (inputEvent->getAction() == AKEY_EVENT_ACTION_UP) {
489 // Clear intercept state when we handled the event.
490 mInterceptKeyTimeout = 0ms;
491 }
492 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800493
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600494 void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800495
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600496 nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent*, uint32_t) override {
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800497 nsecs_t delay = std::chrono::nanoseconds(mInterceptKeyTimeout).count();
498 // Clear intercept state so we could dispatch the event in next wake.
499 mInterceptKeyTimeout = 0ms;
500 return delay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800501 }
502
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600503 bool dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent*, uint32_t, KeyEvent*) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800504 return false;
505 }
506
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600507 void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask,
508 uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700509 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800510 /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is
511 * essentially a passthrough for notifySwitch.
512 */
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800513 mLastNotifySwitch = NotifySwitchArgs(1 /*id*/, when, policyFlags, switchValues, switchMask);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800514 }
515
Sean Stoutb4e0a592021-02-23 07:34:53 -0800516 void pokeUserActivity(nsecs_t, int32_t, int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800517
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600518 void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700519 std::scoped_lock lock(mLock);
chaviwfd6d3512019-03-25 13:23:49 -0700520 mOnPointerDownToken = newToken;
521 }
522
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000523 void setPointerCapture(const PointerCaptureRequest& request) override {
Prabir Pradhan99987712020-11-10 18:43:05 -0800524 std::scoped_lock lock(mLock);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000525 mPointerCaptureRequest = {request};
Prabir Pradhan99987712020-11-10 18:43:05 -0800526 mPointerCaptureChangedCondition.notify_all();
527 }
528
arthurhungf452d0b2021-01-06 00:19:52 +0800529 void notifyDropWindow(const sp<IBinder>& token, float x, float y) override {
530 std::scoped_lock lock(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800531 mNotifyDropWindowWasCalled = true;
arthurhungf452d0b2021-01-06 00:19:52 +0800532 mDropTargetWindowToken = token;
533 }
534
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700535 void assertFilterInputEventWasCalledInternal(
536 const std::function<void(const InputEvent&)>& verify) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700537 std::scoped_lock lock(mLock);
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -0800538 ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called.";
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700539 verify(*mFilteredEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800540 mFilteredEvent = nullptr;
Jackal Guof9696682018-10-05 12:23:23 +0800541 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800542};
543
Michael Wrightd02c5b62014-02-10 15:10:22 -0800544// --- InputDispatcherTest ---
545
546class InputDispatcherTest : public testing::Test {
547protected:
548 sp<FakeInputDispatcherPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700549 std::unique_ptr<InputDispatcher> mDispatcher;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800550
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000551 void SetUp() override {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700552 mFakePolicy = sp<FakeInputDispatcherPolicy>::make();
Siarhei Vishniakou289e9242022-02-15 14:50:16 -0800553 mDispatcher = std::make_unique<InputDispatcher>(mFakePolicy, STALE_EVENT_TIMEOUT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800554 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000555 // Start InputDispatcher thread
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700556 ASSERT_EQ(OK, mDispatcher->start());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800557 }
558
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000559 void TearDown() override {
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700560 ASSERT_EQ(OK, mDispatcher->stop());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800561 mFakePolicy.clear();
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700562 mDispatcher.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800563 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700564
565 /**
566 * Used for debugging when writing the test
567 */
568 void dumpDispatcherState() {
569 std::string dump;
570 mDispatcher->dump(dump);
571 std::stringstream ss(dump);
572 std::string to;
573
574 while (std::getline(ss, to, '\n')) {
575 ALOGE("%s", to.c_str());
576 }
577 }
Vishnu Nair958da932020-08-21 17:12:37 -0700578
chaviw3277faf2021-05-19 16:45:23 -0500579 void setFocusedWindow(const sp<WindowInfoHandle>& window,
580 const sp<WindowInfoHandle>& focusedWindow = nullptr) {
Vishnu Nair958da932020-08-21 17:12:37 -0700581 FocusRequest request;
582 request.token = window->getToken();
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +0000583 request.windowName = window->getName();
Vishnu Nair958da932020-08-21 17:12:37 -0700584 if (focusedWindow) {
585 request.focusedToken = focusedWindow->getToken();
586 }
587 request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
588 request.displayId = window->getInfo()->displayId;
589 mDispatcher->setFocusedWindow(request);
590 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800591};
592
Michael Wrightd02c5b62014-02-10 15:10:22 -0800593TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) {
594 KeyEvent event;
595
596 // Rejects undefined key actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800597 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
598 INVALID_HMAC,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600599 /*action*/ -1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME,
600 ARBITRARY_TIME);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800601 ASSERT_EQ(InputEventInjectionResult::FAILED,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000602 mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE,
603 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800604 << "Should reject key events with undefined action.";
605
606 // Rejects ACTION_MULTIPLE since it is not supported despite being defined in the API.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800607 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
608 INVALID_HMAC, AKEY_EVENT_ACTION_MULTIPLE, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600609 ARBITRARY_TIME, ARBITRARY_TIME);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800610 ASSERT_EQ(InputEventInjectionResult::FAILED,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000611 mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE,
612 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800613 << "Should reject key events with ACTION_MULTIPLE.";
614}
615
616TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) {
617 MotionEvent event;
618 PointerProperties pointerProperties[MAX_POINTERS + 1];
619 PointerCoords pointerCoords[MAX_POINTERS + 1];
Siarhei Vishniakou01747382022-01-20 13:23:27 -0800620 for (size_t i = 0; i <= MAX_POINTERS; i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800621 pointerProperties[i].clear();
622 pointerProperties[i].id = i;
623 pointerCoords[i].clear();
624 }
625
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800626 // Some constants commonly used below
627 constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN;
628 constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE;
629 constexpr int32_t metaState = AMETA_NONE;
630 constexpr MotionClassification classification = MotionClassification::NONE;
631
chaviw9eaa22c2020-07-01 16:21:27 -0700632 ui::Transform identityTransform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800633 // Rejects undefined motion actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800634 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
chaviw9eaa22c2020-07-01 16:21:27 -0700635 /*action*/ -1, 0, 0, edgeFlags, metaState, 0, classification,
636 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700637 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
638 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700639 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800640 ASSERT_EQ(InputEventInjectionResult::FAILED,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000641 mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE,
642 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800643 << "Should reject motion events with undefined action.";
644
645 // Rejects pointer down with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800646 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -0800647 POINTER_1_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
648 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
649 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
650 ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500651 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800652 ASSERT_EQ(InputEventInjectionResult::FAILED,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000653 mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE,
654 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800655 << "Should reject motion events with pointer down index too large.";
656
Garfield Tanfbe732e2020-01-24 11:26:14 -0800657 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700658 AMOTION_EVENT_ACTION_POINTER_DOWN |
659 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700660 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
661 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700662 identityTransform, ARBITRARY_TIME, ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500663 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800664 ASSERT_EQ(InputEventInjectionResult::FAILED,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000665 mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE,
666 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800667 << "Should reject motion events with pointer down index too small.";
668
669 // Rejects pointer up with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800670 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -0800671 POINTER_1_UP, 0, 0, edgeFlags, metaState, 0, classification, identityTransform,
672 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
673 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
674 ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500675 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800676 ASSERT_EQ(InputEventInjectionResult::FAILED,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000677 mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE,
678 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800679 << "Should reject motion events with pointer up index too large.";
680
Garfield Tanfbe732e2020-01-24 11:26:14 -0800681 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700682 AMOTION_EVENT_ACTION_POINTER_UP |
683 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700684 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
685 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700686 identityTransform, ARBITRARY_TIME, ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500687 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800688 ASSERT_EQ(InputEventInjectionResult::FAILED,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000689 mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE,
690 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800691 << "Should reject motion events with pointer up index too small.";
692
693 // Rejects motion events with invalid number of pointers.
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*/ 0, 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 0 pointers.";
704
Garfield Tanfbe732e2020-01-24 11:26:14 -0800705 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
706 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700707 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700708 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
709 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700710 /*pointerCount*/ MAX_POINTERS + 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800711 ASSERT_EQ(InputEventInjectionResult::FAILED,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000712 mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE,
713 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800714 << "Should reject motion events with more than MAX_POINTERS pointers.";
715
716 // Rejects motion events with invalid pointer ids.
717 pointerProperties[0].id = -1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800718 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
719 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700720 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700721 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
722 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700723 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800724 ASSERT_EQ(InputEventInjectionResult::FAILED,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000725 mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE,
726 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800727 << "Should reject motion events with pointer ids less than 0.";
728
729 pointerProperties[0].id = MAX_POINTER_ID + 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800730 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
731 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700732 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700733 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
734 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700735 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800736 ASSERT_EQ(InputEventInjectionResult::FAILED,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000737 mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE,
738 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800739 << "Should reject motion events with pointer ids greater than MAX_POINTER_ID.";
740
741 // Rejects motion events with duplicate pointer ids.
742 pointerProperties[0].id = 1;
743 pointerProperties[1].id = 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800744 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
745 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700746 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700747 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
748 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700749 /*pointerCount*/ 2, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800750 ASSERT_EQ(InputEventInjectionResult::FAILED,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000751 mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE,
752 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800753 << "Should reject motion events with duplicate pointer ids.";
754}
755
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800756/* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */
757
758TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) {
759 constexpr nsecs_t eventTime = 20;
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800760 NotifyConfigurationChangedArgs args(10 /*id*/, eventTime);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800761 mDispatcher->notifyConfigurationChanged(&args);
762 ASSERT_TRUE(mDispatcher->waitForIdle());
763
764 mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime);
765}
766
767TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) {
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800768 NotifySwitchArgs args(10 /*id*/, 20 /*eventTime*/, 0 /*policyFlags*/, 1 /*switchValues*/,
769 2 /*switchMask*/);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800770 mDispatcher->notifySwitch(&args);
771
772 // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener
773 args.policyFlags |= POLICY_FLAG_TRUSTED;
774 mFakePolicy->assertNotifySwitchWasCalled(args);
775}
776
Arthur Hungb92218b2018-08-14 12:00:21 +0800777// --- InputDispatcherTest SetInputWindowTest ---
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700778static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms;
Siarhei Vishniakou1c494c52021-08-11 20:25:01 -0700779// Default input dispatching timeout if there is no focused application or paused window
780// from which to determine an appropriate dispatching timeout.
781static const std::chrono::duration DISPATCHING_TIMEOUT = std::chrono::milliseconds(
782 android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS *
783 android::base::HwTimeoutMultiplier());
Arthur Hungb92218b2018-08-14 12:00:21 +0800784
785class FakeApplicationHandle : public InputApplicationHandle {
786public:
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700787 FakeApplicationHandle() {
788 mInfo.name = "Fake Application";
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700789 mInfo.token = sp<BBinder>::make();
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500790 mInfo.dispatchingTimeoutMillis =
791 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700792 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800793 virtual ~FakeApplicationHandle() {}
794
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000795 virtual bool updateInfo() override { return true; }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700796
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500797 void setDispatchingTimeout(std::chrono::milliseconds timeout) {
798 mInfo.dispatchingTimeoutMillis = timeout.count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700799 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800800};
801
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800802class FakeInputReceiver {
Arthur Hungb92218b2018-08-14 12:00:21 +0800803public:
Garfield Tan15601662020-09-22 15:32:38 -0700804 explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name)
chaviwd1c23182019-12-20 18:44:56 -0800805 : mName(name) {
Garfield Tan15601662020-09-22 15:32:38 -0700806 mConsumer = std::make_unique<InputConsumer>(std::move(clientChannel));
chaviwd1c23182019-12-20 18:44:56 -0800807 }
808
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800809 InputEvent* consume() {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700810 InputEvent* event;
811 std::optional<uint32_t> consumeSeq = receiveEvent(&event);
812 if (!consumeSeq) {
813 return nullptr;
814 }
815 finishEvent(*consumeSeq);
816 return event;
817 }
818
819 /**
820 * Receive an event without acknowledging it.
821 * Return the sequence number that could later be used to send finished signal.
822 */
823 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Arthur Hungb92218b2018-08-14 12:00:21 +0800824 uint32_t consumeSeq;
825 InputEvent* event;
Arthur Hungb92218b2018-08-14 12:00:21 +0800826
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800827 std::chrono::time_point start = std::chrono::steady_clock::now();
828 status_t status = WOULD_BLOCK;
829 while (status == WOULD_BLOCK) {
chaviw81e2bb92019-12-18 15:03:51 -0800830 status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq,
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800831 &event);
832 std::chrono::duration elapsed = std::chrono::steady_clock::now() - start;
833 if (elapsed > 100ms) {
834 break;
835 }
836 }
837
838 if (status == WOULD_BLOCK) {
839 // Just means there's no event available.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700840 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800841 }
842
843 if (status != OK) {
844 ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK.";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700845 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800846 }
847 if (event == nullptr) {
848 ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700849 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800850 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700851 if (outEvent != nullptr) {
852 *outEvent = event;
853 }
854 return consumeSeq;
855 }
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800856
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700857 /**
858 * To be used together with "receiveEvent" to complete the consumption of an event.
859 */
860 void finishEvent(uint32_t consumeSeq) {
861 const status_t status = mConsumer->sendFinishedSignal(consumeSeq, true);
862 ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK.";
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800863 }
864
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000865 void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) {
866 const status_t status = mConsumer->sendTimeline(inputEventId, timeline);
867 ASSERT_EQ(OK, status);
868 }
869
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000870 void consumeEvent(int32_t expectedEventType, int32_t expectedAction,
871 std::optional<int32_t> expectedDisplayId,
872 std::optional<int32_t> expectedFlags) {
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800873 InputEvent* event = consume();
874
875 ASSERT_NE(nullptr, event) << mName.c_str()
876 << ": consumer should have returned non-NULL event.";
Arthur Hungb92218b2018-08-14 12:00:21 +0800877 ASSERT_EQ(expectedEventType, event->getType())
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700878 << mName.c_str() << " expected " << inputEventTypeToString(expectedEventType)
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800879 << " event, got " << inputEventTypeToString(event->getType()) << " event";
Arthur Hungb92218b2018-08-14 12:00:21 +0800880
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000881 if (expectedDisplayId.has_value()) {
882 EXPECT_EQ(expectedDisplayId, event->getDisplayId());
883 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800884
Tiger Huang8664f8c2018-10-11 19:14:35 +0800885 switch (expectedEventType) {
886 case AINPUT_EVENT_TYPE_KEY: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800887 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event);
888 EXPECT_EQ(expectedAction, keyEvent.getAction());
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000889 if (expectedFlags.has_value()) {
890 EXPECT_EQ(expectedFlags.value(), keyEvent.getFlags());
891 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800892 break;
893 }
894 case AINPUT_EVENT_TYPE_MOTION: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800895 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000896 assertMotionAction(expectedAction, motionEvent.getAction());
897
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000898 if (expectedFlags.has_value()) {
899 EXPECT_EQ(expectedFlags.value(), motionEvent.getFlags());
900 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800901 break;
902 }
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100903 case AINPUT_EVENT_TYPE_FOCUS: {
904 FAIL() << "Use 'consumeFocusEvent' for FOCUS events";
905 }
Prabir Pradhan99987712020-11-10 18:43:05 -0800906 case AINPUT_EVENT_TYPE_CAPTURE: {
907 FAIL() << "Use 'consumeCaptureEvent' for CAPTURE events";
908 }
Antonio Kantekf16f2832021-09-28 04:39:20 +0000909 case AINPUT_EVENT_TYPE_TOUCH_MODE: {
910 FAIL() << "Use 'consumeTouchModeEvent' for TOUCH_MODE events";
911 }
arthurhungb89ccb02020-12-30 16:19:01 +0800912 case AINPUT_EVENT_TYPE_DRAG: {
913 FAIL() << "Use 'consumeDragEvent' for DRAG events";
914 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800915 default: {
916 FAIL() << mName.c_str() << ": invalid event type: " << expectedEventType;
917 }
918 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800919 }
920
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100921 void consumeFocusEvent(bool hasFocus, bool inTouchMode) {
922 InputEvent* event = consume();
923 ASSERT_NE(nullptr, event) << mName.c_str()
924 << ": consumer should have returned non-NULL event.";
925 ASSERT_EQ(AINPUT_EVENT_TYPE_FOCUS, event->getType())
926 << "Got " << inputEventTypeToString(event->getType())
927 << " event instead of FOCUS event";
928
929 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
930 << mName.c_str() << ": event displayId should always be NONE.";
931
932 FocusEvent* focusEvent = static_cast<FocusEvent*>(event);
933 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100934 }
935
Prabir Pradhan99987712020-11-10 18:43:05 -0800936 void consumeCaptureEvent(bool hasCapture) {
937 const InputEvent* event = consume();
938 ASSERT_NE(nullptr, event) << mName.c_str()
939 << ": consumer should have returned non-NULL event.";
940 ASSERT_EQ(AINPUT_EVENT_TYPE_CAPTURE, event->getType())
941 << "Got " << inputEventTypeToString(event->getType())
942 << " event instead of CAPTURE event";
943
944 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
945 << mName.c_str() << ": event displayId should always be NONE.";
946
947 const auto& captureEvent = static_cast<const CaptureEvent&>(*event);
948 EXPECT_EQ(hasCapture, captureEvent.getPointerCaptureEnabled());
949 }
950
arthurhungb89ccb02020-12-30 16:19:01 +0800951 void consumeDragEvent(bool isExiting, float x, float y) {
952 const InputEvent* event = consume();
953 ASSERT_NE(nullptr, event) << mName.c_str()
954 << ": consumer should have returned non-NULL event.";
955 ASSERT_EQ(AINPUT_EVENT_TYPE_DRAG, event->getType())
956 << "Got " << inputEventTypeToString(event->getType())
957 << " event instead of DRAG event";
958
959 EXPECT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
960 << mName.c_str() << ": event displayId should always be NONE.";
961
962 const auto& dragEvent = static_cast<const DragEvent&>(*event);
963 EXPECT_EQ(isExiting, dragEvent.isExiting());
964 EXPECT_EQ(x, dragEvent.getX());
965 EXPECT_EQ(y, dragEvent.getY());
966 }
967
Antonio Kantekf16f2832021-09-28 04:39:20 +0000968 void consumeTouchModeEvent(bool inTouchMode) {
969 const InputEvent* event = consume();
970 ASSERT_NE(nullptr, event) << mName.c_str()
971 << ": consumer should have returned non-NULL event.";
972 ASSERT_EQ(AINPUT_EVENT_TYPE_TOUCH_MODE, event->getType())
973 << "Got " << inputEventTypeToString(event->getType())
974 << " event instead of TOUCH_MODE event";
975
976 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
977 << mName.c_str() << ": event displayId should always be NONE.";
978 const auto& touchModeEvent = static_cast<const TouchModeEvent&>(*event);
979 EXPECT_EQ(inTouchMode, touchModeEvent.isInTouchMode());
980 }
981
chaviwd1c23182019-12-20 18:44:56 -0800982 void assertNoEvents() {
983 InputEvent* event = consume();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700984 if (event == nullptr) {
985 return;
986 }
987 if (event->getType() == AINPUT_EVENT_TYPE_KEY) {
988 KeyEvent& keyEvent = static_cast<KeyEvent&>(*event);
989 ADD_FAILURE() << "Received key event "
990 << KeyEvent::actionToString(keyEvent.getAction());
991 } else if (event->getType() == AINPUT_EVENT_TYPE_MOTION) {
992 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
993 ADD_FAILURE() << "Received motion event "
994 << MotionEvent::actionToString(motionEvent.getAction());
995 } else if (event->getType() == AINPUT_EVENT_TYPE_FOCUS) {
996 FocusEvent& focusEvent = static_cast<FocusEvent&>(*event);
997 ADD_FAILURE() << "Received focus event, hasFocus = "
998 << (focusEvent.getHasFocus() ? "true" : "false");
Prabir Pradhan99987712020-11-10 18:43:05 -0800999 } else if (event->getType() == AINPUT_EVENT_TYPE_CAPTURE) {
1000 const auto& captureEvent = static_cast<CaptureEvent&>(*event);
1001 ADD_FAILURE() << "Received capture event, pointerCaptureEnabled = "
1002 << (captureEvent.getPointerCaptureEnabled() ? "true" : "false");
Antonio Kantekf16f2832021-09-28 04:39:20 +00001003 } else if (event->getType() == AINPUT_EVENT_TYPE_TOUCH_MODE) {
1004 const auto& touchModeEvent = static_cast<TouchModeEvent&>(*event);
1005 ADD_FAILURE() << "Received touch mode event, inTouchMode = "
1006 << (touchModeEvent.isInTouchMode() ? "true" : "false");
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001007 }
1008 FAIL() << mName.c_str()
1009 << ": should not have received any events, so consume() should return NULL";
chaviwd1c23182019-12-20 18:44:56 -08001010 }
1011
1012 sp<IBinder> getToken() { return mConsumer->getChannel()->getConnectionToken(); }
1013
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001014 int getChannelFd() { return mConsumer->getChannel()->getFd().get(); }
1015
chaviwd1c23182019-12-20 18:44:56 -08001016protected:
1017 std::unique_ptr<InputConsumer> mConsumer;
1018 PreallocatedInputEventFactory mEventFactory;
1019
1020 std::string mName;
1021};
1022
chaviw3277faf2021-05-19 16:45:23 -05001023class FakeWindowHandle : public WindowInfoHandle {
chaviwd1c23182019-12-20 18:44:56 -08001024public:
1025 static const int32_t WIDTH = 600;
1026 static const int32_t HEIGHT = 800;
chaviwd1c23182019-12-20 18:44:56 -08001027
Chris Yea209fde2020-07-22 13:54:51 -07001028 FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001029 const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name,
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001030 int32_t displayId, std::optional<sp<IBinder>> token = std::nullopt)
chaviwd1c23182019-12-20 18:44:56 -08001031 : mName(name) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001032 if (token == std::nullopt) {
Garfield Tan15601662020-09-22 15:32:38 -07001033 base::Result<std::unique_ptr<InputChannel>> channel =
1034 dispatcher->createInputChannel(name);
1035 token = (*channel)->getConnectionToken();
1036 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
chaviwd1c23182019-12-20 18:44:56 -08001037 }
1038
1039 inputApplicationHandle->updateInfo();
1040 mInfo.applicationInfo = *inputApplicationHandle->getInfo();
1041
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001042 mInfo.token = *token;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001043 mInfo.id = sId++;
chaviwd1c23182019-12-20 18:44:56 -08001044 mInfo.name = name;
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -05001045 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001046 mInfo.alpha = 1.0;
chaviwd1c23182019-12-20 18:44:56 -08001047 mInfo.frameLeft = 0;
1048 mInfo.frameTop = 0;
1049 mInfo.frameRight = WIDTH;
1050 mInfo.frameBottom = HEIGHT;
chaviw1ff3d1e2020-07-01 15:53:47 -07001051 mInfo.transform.set(0, 0);
chaviwd1c23182019-12-20 18:44:56 -08001052 mInfo.globalScaleFactor = 1.0;
1053 mInfo.touchableRegion.clear();
1054 mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT));
Prabir Pradhan5735a322022-04-11 17:23:34 +00001055 mInfo.ownerPid = WINDOW_PID;
1056 mInfo.ownerUid = WINDOW_UID;
chaviwd1c23182019-12-20 18:44:56 -08001057 mInfo.displayId = displayId;
Prabir Pradhan51e7db02022-02-07 06:02:57 -08001058 mInfo.inputConfig = WindowInfo::InputConfig::DEFAULT;
chaviwd1c23182019-12-20 18:44:56 -08001059 }
1060
Arthur Hungabbb9d82021-09-01 14:52:30 +00001061 sp<FakeWindowHandle> clone(
1062 const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001063 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t displayId) {
Arthur Hungabbb9d82021-09-01 14:52:30 +00001064 sp<FakeWindowHandle> handle =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001065 sp<FakeWindowHandle>::make(inputApplicationHandle, dispatcher,
1066 mInfo.name + "(Mirror)", displayId, mInfo.token);
Arthur Hungabbb9d82021-09-01 14:52:30 +00001067 return handle;
1068 }
1069
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001070 void setTouchable(bool touchable) {
1071 mInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, !touchable);
1072 }
chaviwd1c23182019-12-20 18:44:56 -08001073
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001074 void setFocusable(bool focusable) {
1075 mInfo.setInputConfig(WindowInfo::InputConfig::NOT_FOCUSABLE, !focusable);
1076 }
1077
1078 void setVisible(bool visible) {
1079 mInfo.setInputConfig(WindowInfo::InputConfig::NOT_VISIBLE, !visible);
1080 }
Vishnu Nair958da932020-08-21 17:12:37 -07001081
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001082 void setDispatchingTimeout(std::chrono::nanoseconds timeout) {
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -05001083 mInfo.dispatchingTimeout = timeout;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001084 }
1085
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001086 void setPaused(bool paused) {
1087 mInfo.setInputConfig(WindowInfo::InputConfig::PAUSE_DISPATCHING, paused);
1088 }
1089
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08001090 void setPreventSplitting(bool preventSplitting) {
1091 mInfo.setInputConfig(WindowInfo::InputConfig::PREVENT_SPLITTING, preventSplitting);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001092 }
1093
1094 void setSlippery(bool slippery) {
1095 mInfo.setInputConfig(WindowInfo::InputConfig::SLIPPERY, slippery);
1096 }
1097
1098 void setWatchOutsideTouch(bool watchOutside) {
1099 mInfo.setInputConfig(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH, watchOutside);
1100 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001101
Prabir Pradhan51e7db02022-02-07 06:02:57 -08001102 void setSpy(bool spy) { mInfo.setInputConfig(WindowInfo::InputConfig::SPY, spy); }
1103
1104 void setInterceptsStylus(bool interceptsStylus) {
1105 mInfo.setInputConfig(WindowInfo::InputConfig::INTERCEPTS_STYLUS, interceptsStylus);
1106 }
1107
1108 void setDropInput(bool dropInput) {
1109 mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT, dropInput);
1110 }
1111
1112 void setDropInputIfObscured(bool dropInputIfObscured) {
1113 mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED, dropInputIfObscured);
1114 }
1115
1116 void setNoInputChannel(bool noInputChannel) {
1117 mInfo.setInputConfig(WindowInfo::InputConfig::NO_INPUT_CHANNEL, noInputChannel);
1118 }
1119
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001120 void setAlpha(float alpha) { mInfo.alpha = alpha; }
1121
chaviw3277faf2021-05-19 16:45:23 -05001122 void setTouchOcclusionMode(TouchOcclusionMode mode) { mInfo.touchOcclusionMode = mode; }
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001123
Bernardo Rufino7393d172021-02-26 13:56:11 +00001124 void setApplicationToken(sp<IBinder> token) { mInfo.applicationInfo.token = token; }
1125
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001126 void setFrame(const Rect& frame, const ui::Transform& displayTransform = ui::Transform()) {
chaviwd1c23182019-12-20 18:44:56 -08001127 mInfo.frameLeft = frame.left;
1128 mInfo.frameTop = frame.top;
1129 mInfo.frameRight = frame.right;
1130 mInfo.frameBottom = frame.bottom;
1131 mInfo.touchableRegion.clear();
1132 mInfo.addTouchableRegion(frame);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001133
1134 const Rect logicalDisplayFrame = displayTransform.transform(frame);
1135 ui::Transform translate;
1136 translate.set(-logicalDisplayFrame.left, -logicalDisplayFrame.top);
1137 mInfo.transform = translate * displayTransform;
chaviwd1c23182019-12-20 18:44:56 -08001138 }
1139
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001140 void setTouchableRegion(const Region& region) { mInfo.touchableRegion = region; }
1141
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001142 void setIsWallpaper(bool isWallpaper) {
1143 mInfo.setInputConfig(WindowInfo::InputConfig::IS_WALLPAPER, isWallpaper);
1144 }
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001145
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001146 void setDupTouchToWallpaper(bool hasWallpaper) {
1147 mInfo.setInputConfig(WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER, hasWallpaper);
1148 }
chaviwd1c23182019-12-20 18:44:56 -08001149
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001150 void setTrustedOverlay(bool trustedOverlay) {
1151 mInfo.setInputConfig(WindowInfo::InputConfig::TRUSTED_OVERLAY, trustedOverlay);
1152 }
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001153
chaviw9eaa22c2020-07-01 16:21:27 -07001154 void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) {
1155 mInfo.transform.set(dsdx, dtdx, dtdy, dsdy);
1156 }
1157
1158 void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); }
chaviwaf87b3e2019-10-01 16:59:28 -07001159
yunho.shinf4a80b82020-11-16 21:13:57 +09001160 void setWindowOffset(float offsetX, float offsetY) { mInfo.transform.set(offsetX, offsetY); }
1161
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001162 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
1163 consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId,
1164 expectedFlags);
1165 }
1166
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001167 void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
1168 consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, expectedDisplayId, expectedFlags);
1169 }
1170
Svet Ganov5d3bc372020-01-26 23:11:07 -08001171 void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001172 int32_t expectedFlags = 0) {
Svet Ganov5d3bc372020-01-26 23:11:07 -08001173 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, expectedDisplayId,
1174 expectedFlags);
1175 }
1176
1177 void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001178 int32_t expectedFlags = 0) {
Svet Ganov5d3bc372020-01-26 23:11:07 -08001179 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, expectedDisplayId,
1180 expectedFlags);
1181 }
1182
1183 void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001184 int32_t expectedFlags = 0) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001185 consumeAnyMotionDown(expectedDisplayId, expectedFlags);
1186 }
1187
1188 void consumeAnyMotionDown(std::optional<int32_t> expectedDisplayId = std::nullopt,
1189 std::optional<int32_t> expectedFlags = std::nullopt) {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001190 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId,
1191 expectedFlags);
1192 }
1193
Svet Ganov5d3bc372020-01-26 23:11:07 -08001194 void consumeMotionPointerDown(int32_t pointerIdx,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001195 int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1196 int32_t expectedFlags = 0) {
1197 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
1198 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001199 consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags);
1200 }
1201
1202 void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001203 int32_t expectedFlags = 0) {
1204 int32_t action = AMOTION_EVENT_ACTION_POINTER_UP |
1205 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001206 consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags);
1207 }
1208
1209 void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001210 int32_t expectedFlags = 0) {
Michael Wright3a240c42019-12-10 20:53:41 +00001211 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId,
1212 expectedFlags);
1213 }
1214
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05001215 void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1216 int32_t expectedFlags = 0) {
1217 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, expectedDisplayId,
1218 expectedFlags);
1219 }
1220
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08001221 void consumeMotionOutsideWithZeroedCoords(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1222 int32_t expectedFlags = 0) {
1223 InputEvent* event = consume();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08001224 ASSERT_NE(nullptr, event);
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08001225 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType());
1226 const MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
1227 EXPECT_EQ(AMOTION_EVENT_ACTION_OUTSIDE, motionEvent.getActionMasked());
1228 EXPECT_EQ(0.f, motionEvent.getRawPointerCoords(0)->getX());
1229 EXPECT_EQ(0.f, motionEvent.getRawPointerCoords(0)->getY());
1230 }
1231
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001232 void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) {
1233 ASSERT_NE(mInputReceiver, nullptr)
1234 << "Cannot consume events from a window with no receiver";
1235 mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode);
1236 }
1237
Prabir Pradhan99987712020-11-10 18:43:05 -08001238 void consumeCaptureEvent(bool hasCapture) {
1239 ASSERT_NE(mInputReceiver, nullptr)
1240 << "Cannot consume events from a window with no receiver";
1241 mInputReceiver->consumeCaptureEvent(hasCapture);
1242 }
1243
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08001244 void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) {
1245 MotionEvent* motionEvent = consumeMotion();
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08001246 ASSERT_NE(nullptr, motionEvent) << "Did not get a motion event, but expected " << matcher;
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08001247 ASSERT_THAT(*motionEvent, matcher);
1248 }
1249
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001250 void consumeEvent(int32_t expectedEventType, int32_t expectedAction,
1251 std::optional<int32_t> expectedDisplayId,
1252 std::optional<int32_t> expectedFlags) {
chaviwd1c23182019-12-20 18:44:56 -08001253 ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver";
1254 mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId,
1255 expectedFlags);
1256 }
1257
arthurhungb89ccb02020-12-30 16:19:01 +08001258 void consumeDragEvent(bool isExiting, float x, float y) {
1259 mInputReceiver->consumeDragEvent(isExiting, x, y);
1260 }
1261
Antonio Kantekf16f2832021-09-28 04:39:20 +00001262 void consumeTouchModeEvent(bool inTouchMode) {
1263 ASSERT_NE(mInputReceiver, nullptr)
1264 << "Cannot consume events from a window with no receiver";
1265 mInputReceiver->consumeTouchModeEvent(inTouchMode);
1266 }
1267
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001268 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001269 if (mInputReceiver == nullptr) {
1270 ADD_FAILURE() << "Invalid receive event on window with no receiver";
1271 return std::nullopt;
1272 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001273 return mInputReceiver->receiveEvent(outEvent);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001274 }
1275
1276 void finishEvent(uint32_t sequenceNum) {
1277 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
1278 mInputReceiver->finishEvent(sequenceNum);
1279 }
1280
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00001281 void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) {
1282 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
1283 mInputReceiver->sendTimeline(inputEventId, timeline);
1284 }
1285
chaviwaf87b3e2019-10-01 16:59:28 -07001286 InputEvent* consume() {
1287 if (mInputReceiver == nullptr) {
1288 return nullptr;
1289 }
1290 return mInputReceiver->consume();
1291 }
1292
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00001293 MotionEvent* consumeMotion() {
1294 InputEvent* event = consume();
1295 if (event == nullptr) {
1296 ADD_FAILURE() << "Consume failed : no event";
1297 return nullptr;
1298 }
1299 if (event->getType() != AINPUT_EVENT_TYPE_MOTION) {
1300 ADD_FAILURE() << "Instead of motion event, got "
1301 << inputEventTypeToString(event->getType());
1302 return nullptr;
1303 }
1304 return static_cast<MotionEvent*>(event);
1305 }
1306
Arthur Hungb92218b2018-08-14 12:00:21 +08001307 void assertNoEvents() {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001308 if (mInputReceiver == nullptr &&
Prabir Pradhan51e7db02022-02-07 06:02:57 -08001309 mInfo.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001310 return; // Can't receive events if the window does not have input channel
1311 }
1312 ASSERT_NE(nullptr, mInputReceiver)
1313 << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL";
chaviwd1c23182019-12-20 18:44:56 -08001314 mInputReceiver->assertNoEvents();
Arthur Hungb92218b2018-08-14 12:00:21 +08001315 }
1316
chaviwaf87b3e2019-10-01 16:59:28 -07001317 sp<IBinder> getToken() { return mInfo.token; }
1318
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001319 const std::string& getName() { return mName; }
1320
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001321 void setOwnerInfo(int32_t ownerPid, int32_t ownerUid) {
1322 mInfo.ownerPid = ownerPid;
1323 mInfo.ownerUid = ownerUid;
1324 }
1325
Prabir Pradhanedd96402022-02-15 01:46:16 -08001326 int32_t getPid() const { return mInfo.ownerPid; }
1327
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001328 void destroyReceiver() { mInputReceiver = nullptr; }
1329
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001330 int getChannelFd() { return mInputReceiver->getChannelFd(); }
1331
chaviwd1c23182019-12-20 18:44:56 -08001332private:
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001333 const std::string mName;
chaviwd1c23182019-12-20 18:44:56 -08001334 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001335 static std::atomic<int32_t> sId; // each window gets a unique id, like in surfaceflinger
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001336};
1337
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001338std::atomic<int32_t> FakeWindowHandle::sId{1};
1339
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001340static InputEventInjectionResult injectKey(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001341 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t repeatCount,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001342 int32_t displayId = ADISPLAY_ID_NONE,
1343 InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001344 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Prabir Pradhan5735a322022-04-11 17:23:34 +00001345 bool allowKeyRepeat = true, std::optional<int32_t> targetUid = {},
1346 uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
Arthur Hungb92218b2018-08-14 12:00:21 +08001347 KeyEvent event;
1348 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1349
1350 // Define a valid key down event.
Garfield Tanfbe732e2020-01-24 11:26:14 -08001351 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001352 INVALID_HMAC, action, /* flags */ 0, AKEYCODE_A, KEY_A, AMETA_NONE,
1353 repeatCount, currentTime, currentTime);
Arthur Hungb92218b2018-08-14 12:00:21 +08001354
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001355 if (!allowKeyRepeat) {
1356 policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT;
1357 }
Arthur Hungb92218b2018-08-14 12:00:21 +08001358 // Inject event until dispatch out.
Prabir Pradhan5735a322022-04-11 17:23:34 +00001359 return dispatcher->injectInputEvent(&event, targetUid, syncMode, injectionTimeout, policyFlags);
Arthur Hungb92218b2018-08-14 12:00:21 +08001360}
1361
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001362static InputEventInjectionResult injectKeyDown(const std::unique_ptr<InputDispatcher>& dispatcher,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001363 int32_t displayId = ADISPLAY_ID_NONE) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001364 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /* repeatCount */ 0, displayId);
1365}
1366
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001367// Inject a down event that has key repeat disabled. This allows InputDispatcher to idle without
1368// sending a subsequent key up. When key repeat is enabled, the dispatcher cannot idle because it
1369// has to be woken up to process the repeating key.
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001370static InputEventInjectionResult injectKeyDownNoRepeat(
1371 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t displayId = ADISPLAY_ID_NONE) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001372 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /* repeatCount */ 0, displayId,
1373 InputEventInjectionSync::WAIT_FOR_RESULT, INJECT_EVENT_TIMEOUT,
1374 /* allowKeyRepeat */ false);
1375}
1376
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001377static InputEventInjectionResult injectKeyUp(const std::unique_ptr<InputDispatcher>& dispatcher,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001378 int32_t displayId = ADISPLAY_ID_NONE) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001379 return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /* repeatCount */ 0, displayId);
1380}
1381
Garfield Tandf26e862020-07-01 20:18:19 -07001382class PointerBuilder {
1383public:
1384 PointerBuilder(int32_t id, int32_t toolType) {
1385 mProperties.clear();
1386 mProperties.id = id;
1387 mProperties.toolType = toolType;
1388 mCoords.clear();
1389 }
1390
1391 PointerBuilder& x(float x) { return axis(AMOTION_EVENT_AXIS_X, x); }
1392
1393 PointerBuilder& y(float y) { return axis(AMOTION_EVENT_AXIS_Y, y); }
1394
1395 PointerBuilder& axis(int32_t axis, float value) {
1396 mCoords.setAxisValue(axis, value);
1397 return *this;
1398 }
1399
1400 PointerProperties buildProperties() const { return mProperties; }
1401
1402 PointerCoords buildCoords() const { return mCoords; }
1403
1404private:
1405 PointerProperties mProperties;
1406 PointerCoords mCoords;
1407};
1408
1409class MotionEventBuilder {
1410public:
1411 MotionEventBuilder(int32_t action, int32_t source) {
1412 mAction = action;
1413 mSource = source;
1414 mEventTime = systemTime(SYSTEM_TIME_MONOTONIC);
1415 }
1416
1417 MotionEventBuilder& eventTime(nsecs_t eventTime) {
1418 mEventTime = eventTime;
1419 return *this;
1420 }
1421
1422 MotionEventBuilder& displayId(int32_t displayId) {
1423 mDisplayId = displayId;
1424 return *this;
1425 }
1426
1427 MotionEventBuilder& actionButton(int32_t actionButton) {
1428 mActionButton = actionButton;
1429 return *this;
1430 }
1431
arthurhung6d4bed92021-03-17 11:59:33 +08001432 MotionEventBuilder& buttonState(int32_t buttonState) {
1433 mButtonState = buttonState;
Garfield Tandf26e862020-07-01 20:18:19 -07001434 return *this;
1435 }
1436
1437 MotionEventBuilder& rawXCursorPosition(float rawXCursorPosition) {
1438 mRawXCursorPosition = rawXCursorPosition;
1439 return *this;
1440 }
1441
1442 MotionEventBuilder& rawYCursorPosition(float rawYCursorPosition) {
1443 mRawYCursorPosition = rawYCursorPosition;
1444 return *this;
1445 }
1446
1447 MotionEventBuilder& pointer(PointerBuilder pointer) {
1448 mPointers.push_back(pointer);
1449 return *this;
1450 }
1451
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08001452 MotionEventBuilder& addFlag(uint32_t flags) {
1453 mFlags |= flags;
1454 return *this;
1455 }
1456
Garfield Tandf26e862020-07-01 20:18:19 -07001457 MotionEvent build() {
1458 std::vector<PointerProperties> pointerProperties;
1459 std::vector<PointerCoords> pointerCoords;
1460 for (const PointerBuilder& pointer : mPointers) {
1461 pointerProperties.push_back(pointer.buildProperties());
1462 pointerCoords.push_back(pointer.buildCoords());
1463 }
1464
1465 // Set mouse cursor position for the most common cases to avoid boilerplate.
1466 if (mSource == AINPUT_SOURCE_MOUSE &&
1467 !MotionEvent::isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition) &&
1468 mPointers.size() == 1) {
1469 mRawXCursorPosition = pointerCoords[0].getX();
1470 mRawYCursorPosition = pointerCoords[0].getY();
1471 }
1472
1473 MotionEvent event;
chaviw9eaa22c2020-07-01 16:21:27 -07001474 ui::Transform identityTransform;
Garfield Tandf26e862020-07-01 20:18:19 -07001475 event.initialize(InputEvent::nextId(), DEVICE_ID, mSource, mDisplayId, INVALID_HMAC,
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08001476 mAction, mActionButton, mFlags, /* edgeFlags */ 0, AMETA_NONE,
chaviw9eaa22c2020-07-01 16:21:27 -07001477 mButtonState, MotionClassification::NONE, identityTransform,
1478 /* xPrecision */ 0, /* yPrecision */ 0, mRawXCursorPosition,
Prabir Pradhanb9b18502021-08-26 12:30:32 -07001479 mRawYCursorPosition, identityTransform, mEventTime, mEventTime,
1480 mPointers.size(), pointerProperties.data(), pointerCoords.data());
Garfield Tandf26e862020-07-01 20:18:19 -07001481
1482 return event;
1483 }
1484
1485private:
1486 int32_t mAction;
1487 int32_t mSource;
1488 nsecs_t mEventTime;
1489 int32_t mDisplayId{ADISPLAY_ID_DEFAULT};
1490 int32_t mActionButton{0};
1491 int32_t mButtonState{0};
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08001492 int32_t mFlags{0};
Garfield Tandf26e862020-07-01 20:18:19 -07001493 float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
1494 float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
1495
1496 std::vector<PointerBuilder> mPointers;
1497};
1498
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001499static InputEventInjectionResult injectMotionEvent(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001500 const std::unique_ptr<InputDispatcher>& dispatcher, const MotionEvent& event,
Garfield Tandf26e862020-07-01 20:18:19 -07001501 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Prabir Pradhan5735a322022-04-11 17:23:34 +00001502 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT,
1503 std::optional<int32_t> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
1504 return dispatcher->injectInputEvent(&event, targetUid, injectionMode, injectionTimeout,
1505 policyFlags);
Garfield Tandf26e862020-07-01 20:18:19 -07001506}
1507
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001508static InputEventInjectionResult injectMotionEvent(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001509 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t source,
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001510 int32_t displayId, const PointF& position = {100, 200},
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001511 const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001512 AMOTION_EVENT_INVALID_CURSOR_POSITION},
1513 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001514 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Prabir Pradhan5735a322022-04-11 17:23:34 +00001515 nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC),
1516 std::optional<int32_t> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
Garfield Tandf26e862020-07-01 20:18:19 -07001517 MotionEvent event = MotionEventBuilder(action, source)
1518 .displayId(displayId)
1519 .eventTime(eventTime)
1520 .rawXCursorPosition(cursorPosition.x)
1521 .rawYCursorPosition(cursorPosition.y)
1522 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
1523 .x(position.x)
1524 .y(position.y))
1525 .build();
Arthur Hungb92218b2018-08-14 12:00:21 +08001526
1527 // Inject event until dispatch out.
Prabir Pradhan5735a322022-04-11 17:23:34 +00001528 return injectMotionEvent(dispatcher, event, injectionTimeout, injectionMode, targetUid,
1529 policyFlags);
Arthur Hungb92218b2018-08-14 12:00:21 +08001530}
1531
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001532static InputEventInjectionResult injectMotionDown(
1533 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t source, int32_t displayId,
1534 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001535 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location);
Garfield Tan00f511d2019-06-12 16:55:40 -07001536}
1537
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001538static InputEventInjectionResult injectMotionUp(const std::unique_ptr<InputDispatcher>& dispatcher,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001539 int32_t source, int32_t displayId,
1540 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001541 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location);
Michael Wright3a240c42019-12-10 20:53:41 +00001542}
1543
Jackal Guof9696682018-10-05 12:23:23 +08001544static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) {
1545 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1546 // Define a valid key event.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001547 NotifyKeyArgs args(/* id */ 0, currentTime, 0 /*readTime*/, DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
1548 displayId, POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0, AKEYCODE_A,
1549 KEY_A, AMETA_NONE, currentTime);
Jackal Guof9696682018-10-05 12:23:23 +08001550
1551 return args;
1552}
1553
chaviwd1c23182019-12-20 18:44:56 -08001554static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId,
1555 const std::vector<PointF>& points) {
1556 size_t pointerCount = points.size();
chaviwaf87b3e2019-10-01 16:59:28 -07001557 if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) {
1558 EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer";
1559 }
1560
chaviwd1c23182019-12-20 18:44:56 -08001561 PointerProperties pointerProperties[pointerCount];
1562 PointerCoords pointerCoords[pointerCount];
Jackal Guof9696682018-10-05 12:23:23 +08001563
chaviwd1c23182019-12-20 18:44:56 -08001564 for (size_t i = 0; i < pointerCount; i++) {
1565 pointerProperties[i].clear();
1566 pointerProperties[i].id = i;
1567 pointerProperties[i].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jackal Guof9696682018-10-05 12:23:23 +08001568
chaviwd1c23182019-12-20 18:44:56 -08001569 pointerCoords[i].clear();
1570 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x);
1571 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y);
1572 }
Jackal Guof9696682018-10-05 12:23:23 +08001573
1574 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1575 // Define a valid motion event.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001576 NotifyMotionArgs args(/* id */ 0, currentTime, 0 /*readTime*/, DEVICE_ID, source, displayId,
Garfield Tan00f511d2019-06-12 16:55:40 -07001577 POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0,
1578 AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
chaviwd1c23182019-12-20 18:44:56 -08001579 AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties,
1580 pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0,
Garfield Tan00f511d2019-06-12 16:55:40 -07001581 AMOTION_EVENT_INVALID_CURSOR_POSITION,
1582 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {});
Jackal Guof9696682018-10-05 12:23:23 +08001583
1584 return args;
1585}
1586
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08001587static NotifyMotionArgs generateTouchArgs(int32_t action, const std::vector<PointF>& points) {
1588 return generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, points);
1589}
1590
chaviwd1c23182019-12-20 18:44:56 -08001591static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) {
1592 return generateMotionArgs(action, source, displayId, {PointF{100, 200}});
1593}
1594
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001595static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs(
1596 const PointerCaptureRequest& request) {
1597 return NotifyPointerCaptureChangedArgs(/* id */ 0, systemTime(SYSTEM_TIME_MONOTONIC), request);
Prabir Pradhan99987712020-11-10 18:43:05 -08001598}
1599
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001600/**
1601 * When a window unexpectedly disposes of its input channel, policy should be notified about the
1602 * broken channel.
1603 */
1604TEST_F(InputDispatcherTest, WhenInputChannelBreaks_PolicyIsNotified) {
1605 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1606 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001607 sp<FakeWindowHandle>::make(application, mDispatcher,
1608 "Window that breaks its input channel", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001609
1610 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1611
1612 // Window closes its channel, but the window remains.
1613 window->destroyReceiver();
1614 mFakePolicy->assertNotifyInputChannelBrokenWasCalled(window->getInfo()->token);
1615}
1616
Arthur Hungb92218b2018-08-14 12:00:21 +08001617TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001618 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001619 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1620 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001621
Arthur Hung72d8dc32020-03-28 00:48:39 +00001622 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001623 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1624 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1625 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001626
1627 // Window should receive motion event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001628 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001629}
1630
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001631TEST_F(InputDispatcherTest, WhenDisplayNotSpecified_InjectMotionToDefaultDisplay) {
1632 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001633 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1634 "Fake Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001635
1636 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1637 // Inject a MotionEvent to an unknown display.
1638 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1639 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_NONE))
1640 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1641
1642 // Window should receive motion event.
1643 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1644}
1645
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001646/**
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08001647 * Calling setInputWindows once should not cause any issues.
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001648 * This test serves as a sanity check for the next test, where setInputWindows is
1649 * called twice.
1650 */
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08001651TEST_F(InputDispatcherTest, SetInputWindowOnceWithSingleTouchWindow) {
Chris Yea209fde2020-07-22 13:54:51 -07001652 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001653 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1654 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001655 window->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001656
1657 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001658 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001659 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1660 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001661 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001662
1663 // Window should receive motion event.
1664 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1665}
1666
1667/**
1668 * Calling setInputWindows twice, with the same info, should not cause any issues.
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001669 */
1670TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001671 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001672 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1673 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001674 window->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001675
1676 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1677 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001678 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001679 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1680 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001681 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001682
1683 // Window should receive motion event.
1684 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1685}
1686
Arthur Hungb92218b2018-08-14 12:00:21 +08001687// The foreground window should receive the first touch down event.
1688TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001689 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001690 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001691 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001692 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001693 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001694
Arthur Hung72d8dc32020-03-28 00:48:39 +00001695 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001696 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1697 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1698 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001699
1700 // Top window should receive the touch down event. Second window should not receive anything.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001701 windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001702 windowSecond->assertNoEvents();
1703}
1704
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001705/**
1706 * Two windows: A top window, and a wallpaper behind the window.
1707 * Touch goes to the top window, and then top window disappears. Ensure that wallpaper window
1708 * gets ACTION_CANCEL.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001709 * 1. foregroundWindow <-- dup touch to wallpaper
1710 * 2. wallpaperWindow <-- is wallpaper
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001711 */
1712TEST_F(InputDispatcherTest, WhenForegroundWindowDisappears_WallpaperTouchIsCanceled) {
1713 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1714 sp<FakeWindowHandle> foregroundWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001715 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001716 foregroundWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001717 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001718 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001719 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001720 constexpr int expectedWallpaperFlags =
1721 AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED;
1722
1723 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}});
1724 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1725 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1726 {100, 200}))
1727 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1728
1729 // Both foreground window and its wallpaper should receive the touch down
1730 foregroundWindow->consumeMotionDown();
1731 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1732
1733 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1734 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
1735 ADISPLAY_ID_DEFAULT, {110, 200}))
1736 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1737
1738 foregroundWindow->consumeMotionMove();
1739 wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1740
1741 // Now the foreground window goes away, but the wallpaper stays
1742 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}});
1743 foregroundWindow->consumeMotionCancel();
1744 // Since the "parent" window of the wallpaper is gone, wallpaper should receive cancel, too.
1745 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1746}
1747
1748/**
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001749 * Same test as WhenForegroundWindowDisappears_WallpaperTouchIsCanceled above,
1750 * with the following differences:
1751 * After ACTION_DOWN, Wallpaper window hangs up its channel, which forces the dispatcher to
1752 * clean up the connection.
1753 * This later may crash dispatcher during ACTION_CANCEL synthesis, if the dispatcher is not careful.
1754 * Ensure that there's no crash in the dispatcher.
1755 */
1756TEST_F(InputDispatcherTest, WhenWallpaperDisappears_NoCrash) {
1757 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1758 sp<FakeWindowHandle> foregroundWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001759 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001760 foregroundWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001761 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001762 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001763 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001764 constexpr int expectedWallpaperFlags =
1765 AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED;
1766
1767 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}});
1768 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1769 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1770 {100, 200}))
1771 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1772
1773 // Both foreground window and its wallpaper should receive the touch down
1774 foregroundWindow->consumeMotionDown();
1775 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1776
1777 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1778 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
1779 ADISPLAY_ID_DEFAULT, {110, 200}))
1780 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1781
1782 foregroundWindow->consumeMotionMove();
1783 wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1784
1785 // Wallpaper closes its channel, but the window remains.
1786 wallpaperWindow->destroyReceiver();
1787 mFakePolicy->assertNotifyInputChannelBrokenWasCalled(wallpaperWindow->getInfo()->token);
1788
1789 // Now the foreground window goes away, but the wallpaper stays, even though its channel
1790 // is no longer valid.
1791 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}});
1792 foregroundWindow->consumeMotionCancel();
1793}
1794
1795/**
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001796 * A single window that receives touch (on top), and a wallpaper window underneath it.
1797 * The top window gets a multitouch gesture.
1798 * Ensure that wallpaper gets the same gesture.
1799 */
1800TEST_F(InputDispatcherTest, WallpaperWindow_ReceivesMultiTouch) {
1801 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1802 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001803 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001804 window->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001805
1806 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001807 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001808 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001809 constexpr int expectedWallpaperFlags =
1810 AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED;
1811
1812 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, wallpaperWindow}}});
1813
1814 // Touch down on top window
1815 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1816 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1817 {100, 100}))
1818 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1819
1820 // Both top window and its wallpaper should receive the touch down
1821 window->consumeMotionDown();
1822 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1823
1824 // Second finger down on the top window
1825 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08001826 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001827 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
1828 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
1829 .x(100)
1830 .y(100))
1831 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER)
1832 .x(150)
1833 .y(150))
1834 .build();
1835 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1836 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
1837 InputEventInjectionSync::WAIT_FOR_RESULT))
1838 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1839
1840 window->consumeMotionPointerDown(1 /* pointerIndex */);
1841 wallpaperWindow->consumeMotionPointerDown(1 /* pointerIndex */, ADISPLAY_ID_DEFAULT,
1842 expectedWallpaperFlags);
1843 window->assertNoEvents();
1844 wallpaperWindow->assertNoEvents();
1845}
1846
1847/**
1848 * Two windows: a window on the left and window on the right.
1849 * A third window, wallpaper, is behind both windows, and spans both top windows.
1850 * The first touch down goes to the left window. A second pointer touches down on the right window.
1851 * The touch is split, so both left and right windows should receive ACTION_DOWN.
1852 * The wallpaper will get the full event, so it should receive ACTION_DOWN followed by
1853 * ACTION_POINTER_DOWN(1).
1854 */
1855TEST_F(InputDispatcherTest, TwoWindows_SplitWallpaperTouch) {
1856 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1857 sp<FakeWindowHandle> leftWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001858 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001859 leftWindow->setFrame(Rect(0, 0, 200, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001860 leftWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001861
1862 sp<FakeWindowHandle> rightWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001863 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001864 rightWindow->setFrame(Rect(200, 0, 400, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001865 rightWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001866
1867 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001868 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001869 wallpaperWindow->setFrame(Rect(0, 0, 400, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001870 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001871 constexpr int expectedWallpaperFlags =
1872 AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED;
1873
1874 mDispatcher->setInputWindows(
1875 {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}});
1876
1877 // Touch down on left window
1878 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1879 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1880 {100, 100}))
1881 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1882
1883 // Both foreground window and its wallpaper should receive the touch down
1884 leftWindow->consumeMotionDown();
1885 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1886
1887 // Second finger down on the right window
1888 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08001889 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001890 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
1891 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
1892 .x(100)
1893 .y(100))
1894 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER)
1895 .x(300)
1896 .y(100))
1897 .build();
1898 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1899 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
1900 InputEventInjectionSync::WAIT_FOR_RESULT))
1901 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1902
1903 leftWindow->consumeMotionMove();
1904 // Since the touch is split, right window gets ACTION_DOWN
1905 rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1906 wallpaperWindow->consumeMotionPointerDown(1 /* pointerIndex */, ADISPLAY_ID_DEFAULT,
1907 expectedWallpaperFlags);
1908
1909 // Now, leftWindow, which received the first finger, disappears.
1910 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {rightWindow, wallpaperWindow}}});
1911 leftWindow->consumeMotionCancel();
1912 // Since a "parent" window of the wallpaper is gone, wallpaper should receive cancel, too.
1913 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1914
1915 // The pointer that's still down on the right window moves, and goes to the right window only.
1916 // As far as the dispatcher's concerned though, both pointers are still present.
1917 const MotionEvent secondFingerMoveEvent =
1918 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
1919 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
1920 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
1921 .x(100)
1922 .y(100))
1923 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER)
1924 .x(310)
1925 .y(110))
1926 .build();
1927 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1928 injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT,
1929 InputEventInjectionSync::WAIT_FOR_RESULT));
1930 rightWindow->consumeMotionMove();
1931
1932 leftWindow->assertNoEvents();
1933 rightWindow->assertNoEvents();
1934 wallpaperWindow->assertNoEvents();
1935}
1936
Arthur Hung74c248d2022-11-23 07:09:59 +00001937TEST_F(InputDispatcherTest, WallpaperWindowReceivesMultiTouch) {
1938 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1939 sp<FakeWindowHandle> window =
1940 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
1941 window->setDupTouchToWallpaper(true);
1942
1943 sp<FakeWindowHandle> wallpaperWindow =
1944 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
1945 wallpaperWindow->setIsWallpaper(true);
1946 constexpr int expectedWallpaperFlags =
1947 AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED;
1948 wallpaperWindow->setPreventSplitting(true);
1949
1950 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, wallpaperWindow}}});
1951
1952 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1953 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1954 {50, 50}))
1955 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1956 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1957 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1958
1959 const MotionEvent secondFingerDownEvent =
1960 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
1961 .displayId(ADISPLAY_ID_DEFAULT)
1962 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
1963 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
1964 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(10).y(10))
1965 .build();
1966 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1967 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
1968 InputEventInjectionSync::WAIT_FOR_RESULT))
1969 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1970
1971 window->consumeMotionPointerDown(1);
1972 wallpaperWindow->consumeMotionPointerDown(1, ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1973
1974 const MotionEvent secondFingerUpEvent =
1975 MotionEventBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
1976 .displayId(ADISPLAY_ID_DEFAULT)
1977 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
1978 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
1979 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(10).y(10))
1980 .build();
1981 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1982 injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT,
1983 InputEventInjectionSync::WAIT_FOR_RESULT))
1984 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1985 window->consumeMotionPointerUp(1);
1986 wallpaperWindow->consumeMotionPointerUp(1, ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1987
1988 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1989 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {50, 50}))
1990 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1991 window->consumeMotionUp(ADISPLAY_ID_DEFAULT);
1992 wallpaperWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1993}
1994
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08001995/**
1996 * On the display, have a single window, and also an area where there's no window.
1997 * First pointer touches the "no window" area of the screen. Second pointer touches the window.
1998 * Make sure that the window receives the second pointer, and first pointer is simply ignored.
1999 */
2000TEST_F(InputDispatcherTest, SplitWorksWhenEmptyAreaIsTouched) {
2001 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2002 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002003 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002004
2005 mDispatcher->setInputWindows({{DISPLAY_ID, {window}}});
2006 NotifyMotionArgs args;
2007
2008 // Touch down on the empty space
2009 mDispatcher->notifyMotion(&(args = generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{-1, -1}})));
2010
2011 mDispatcher->waitForIdle();
2012 window->assertNoEvents();
2013
2014 // Now touch down on the window with another pointer
2015 mDispatcher->notifyMotion(&(args = generateTouchArgs(POINTER_1_DOWN, {{-1, -1}, {10, 10}})));
2016 mDispatcher->waitForIdle();
2017 window->consumeMotionDown();
2018}
2019
2020/**
2021 * Same test as above, but instead of touching the empty space, the first touch goes to
2022 * non-touchable window.
2023 */
2024TEST_F(InputDispatcherTest, SplitWorksWhenNonTouchableWindowIsTouched) {
2025 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2026 sp<FakeWindowHandle> window1 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002027 sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002028 window1->setTouchableRegion(Region{{0, 0, 100, 100}});
2029 window1->setTouchable(false);
2030 sp<FakeWindowHandle> window2 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002031 sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002032 window2->setTouchableRegion(Region{{100, 0, 200, 100}});
2033
2034 mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}});
2035
2036 NotifyMotionArgs args;
2037 // Touch down on the non-touchable window
2038 mDispatcher->notifyMotion(&(args = generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}})));
2039
2040 mDispatcher->waitForIdle();
2041 window1->assertNoEvents();
2042 window2->assertNoEvents();
2043
2044 // Now touch down on the window with another pointer
2045 mDispatcher->notifyMotion(&(args = generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}})));
2046 mDispatcher->waitForIdle();
2047 window2->consumeMotionDown();
2048}
2049
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002050/**
2051 * When splitting touch events the downTime should be adjusted such that the downTime corresponds
2052 * to the event time of the first ACTION_DOWN sent to the particular window.
2053 */
2054TEST_F(InputDispatcherTest, SplitTouchesSendCorrectActionDownTime) {
2055 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2056 sp<FakeWindowHandle> window1 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002057 sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002058 window1->setTouchableRegion(Region{{0, 0, 100, 100}});
2059 sp<FakeWindowHandle> window2 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002060 sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002061 window2->setTouchableRegion(Region{{100, 0, 200, 100}});
2062
2063 mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}});
2064
2065 NotifyMotionArgs args;
2066 // Touch down on the first window
2067 mDispatcher->notifyMotion(&(args = generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}})));
2068
2069 mDispatcher->waitForIdle();
2070 InputEvent* inputEvent1 = window1->consume();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002071 ASSERT_NE(inputEvent1, nullptr);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002072 window2->assertNoEvents();
2073 MotionEvent& motionEvent1 = static_cast<MotionEvent&>(*inputEvent1);
2074 nsecs_t downTimeForWindow1 = motionEvent1.getDownTime();
2075 ASSERT_EQ(motionEvent1.getDownTime(), motionEvent1.getEventTime());
2076
2077 // Now touch down on the window with another pointer
2078 mDispatcher->notifyMotion(&(args = generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}})));
2079 mDispatcher->waitForIdle();
2080 InputEvent* inputEvent2 = window2->consume();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002081 ASSERT_NE(inputEvent2, nullptr);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002082 MotionEvent& motionEvent2 = static_cast<MotionEvent&>(*inputEvent2);
2083 nsecs_t downTimeForWindow2 = motionEvent2.getDownTime();
2084 ASSERT_NE(downTimeForWindow1, downTimeForWindow2);
2085 ASSERT_EQ(motionEvent2.getDownTime(), motionEvent2.getEventTime());
2086
2087 // Now move the pointer on the second window
2088 mDispatcher->notifyMotion(
2089 &(args = generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{50, 50}, {151, 51}})));
2090 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002091 window2->consumeMotionEvent(WithDownTime(downTimeForWindow2));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002092
2093 // Now add new touch down on the second window
2094 mDispatcher->notifyMotion(
2095 &(args = generateTouchArgs(POINTER_2_DOWN, {{50, 50}, {151, 51}, {150, 50}})));
2096 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002097 window2->consumeMotionEvent(WithDownTime(downTimeForWindow2));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002098
2099 // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line
2100 window1->consumeMotionMove();
2101 window1->assertNoEvents();
2102
2103 // Now move the pointer on the first window
2104 mDispatcher->notifyMotion(
2105 &(args = generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{51, 51}, {151, 51}})));
2106 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002107 window1->consumeMotionEvent(WithDownTime(downTimeForWindow1));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002108
2109 mDispatcher->notifyMotion(&(
2110 args = generateTouchArgs(POINTER_3_DOWN, {{51, 51}, {151, 51}, {150, 50}, {50, 50}})));
2111 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002112 window1->consumeMotionEvent(WithDownTime(downTimeForWindow1));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002113}
2114
Garfield Tandf26e862020-07-01 20:18:19 -07002115TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) {
Chris Yea209fde2020-07-22 13:54:51 -07002116 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07002117 sp<FakeWindowHandle> windowLeft =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002118 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07002119 windowLeft->setFrame(Rect(0, 0, 600, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07002120 sp<FakeWindowHandle> windowRight =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002121 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07002122 windowRight->setFrame(Rect(600, 0, 1200, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07002123
2124 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2125
2126 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
2127
2128 // Start cursor position in right window so that we can move the cursor to left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002129 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002130 injectMotionEvent(mDispatcher,
2131 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2132 AINPUT_SOURCE_MOUSE)
2133 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2134 .x(900)
2135 .y(400))
2136 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002137 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2138 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE));
Garfield Tandf26e862020-07-01 20:18:19 -07002139
2140 // Move cursor into left window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002141 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002142 injectMotionEvent(mDispatcher,
2143 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2144 AINPUT_SOURCE_MOUSE)
2145 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2146 .x(300)
2147 .y(400))
2148 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002149 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2150 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2151 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE));
Garfield Tandf26e862020-07-01 20:18:19 -07002152
2153 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002154 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002155 injectMotionEvent(mDispatcher,
2156 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2157 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2158 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2159 .x(300)
2160 .y(400))
2161 .build()));
2162 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2163
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002164 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002165 injectMotionEvent(mDispatcher,
2166 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
2167 AINPUT_SOURCE_MOUSE)
2168 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2169 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2170 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2171 .x(300)
2172 .y(400))
2173 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002174 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
Garfield Tandf26e862020-07-01 20:18:19 -07002175
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002176 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002177 injectMotionEvent(mDispatcher,
2178 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
2179 AINPUT_SOURCE_MOUSE)
2180 .buttonState(0)
2181 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2182 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2183 .x(300)
2184 .y(400))
2185 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002186 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE));
Garfield Tandf26e862020-07-01 20:18:19 -07002187
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002188 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002189 injectMotionEvent(mDispatcher,
2190 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
2191 .buttonState(0)
2192 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2193 .x(300)
2194 .y(400))
2195 .build()));
2196 windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT);
2197
2198 // Move mouse cursor back to right window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002199 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002200 injectMotionEvent(mDispatcher,
2201 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2202 AINPUT_SOURCE_MOUSE)
2203 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2204 .x(900)
2205 .y(400))
2206 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002207 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2208 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2209 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE));
2210
2211 // No more events
2212 windowLeft->assertNoEvents();
2213 windowRight->assertNoEvents();
2214}
2215
2216TEST_F(InputDispatcherTest, HoverWithSpyWindows) {
2217 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2218
2219 sp<FakeWindowHandle> spyWindow =
2220 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
2221 spyWindow->setFrame(Rect(0, 0, 600, 800));
2222 spyWindow->setTrustedOverlay(true);
2223 spyWindow->setSpy(true);
2224 sp<FakeWindowHandle> window =
2225 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2226 window->setFrame(Rect(0, 0, 600, 800));
2227
2228 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2229 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
2230
2231 // Send mouse cursor to the window
2232 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2233 injectMotionEvent(mDispatcher,
2234 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
2235 AINPUT_SOURCE_MOUSE)
2236 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2237 .x(100)
2238 .y(100))
2239 .build()));
2240
2241 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2242 WithSource(AINPUT_SOURCE_MOUSE)));
2243 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2244 WithSource(AINPUT_SOURCE_MOUSE)));
2245
2246 window->assertNoEvents();
2247 spyWindow->assertNoEvents();
Garfield Tandf26e862020-07-01 20:18:19 -07002248}
2249
2250// This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected
2251// directly in this test.
2252TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) {
Chris Yea209fde2020-07-22 13:54:51 -07002253 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07002254 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002255 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07002256 window->setFrame(Rect(0, 0, 1200, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07002257
2258 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2259
2260 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2261
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002262 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002263 injectMotionEvent(mDispatcher,
2264 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
2265 AINPUT_SOURCE_MOUSE)
2266 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2267 .x(300)
2268 .y(400))
2269 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002270 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07002271
2272 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002273 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002274 injectMotionEvent(mDispatcher,
2275 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2276 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2277 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2278 .x(300)
2279 .y(400))
2280 .build()));
2281 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2282
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002283 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002284 injectMotionEvent(mDispatcher,
2285 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
2286 AINPUT_SOURCE_MOUSE)
2287 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2288 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2289 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2290 .x(300)
2291 .y(400))
2292 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002293 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
Garfield Tandf26e862020-07-01 20:18:19 -07002294
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002295 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002296 injectMotionEvent(mDispatcher,
2297 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
2298 AINPUT_SOURCE_MOUSE)
2299 .buttonState(0)
2300 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2301 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2302 .x(300)
2303 .y(400))
2304 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002305 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE));
Garfield Tandf26e862020-07-01 20:18:19 -07002306
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002307 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002308 injectMotionEvent(mDispatcher,
2309 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
2310 .buttonState(0)
2311 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2312 .x(300)
2313 .y(400))
2314 .build()));
2315 window->consumeMotionUp(ADISPLAY_ID_DEFAULT);
2316
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002317 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002318 injectMotionEvent(mDispatcher,
2319 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT,
2320 AINPUT_SOURCE_MOUSE)
2321 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2322 .x(300)
2323 .y(400))
2324 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002325 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
Garfield Tandf26e862020-07-01 20:18:19 -07002326}
2327
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08002328/**
2329 * Inject a mouse hover event followed by a tap from touchscreen.
2330 * In the current implementation, the tap does not cause a HOVER_EXIT event.
2331 */
2332TEST_F(InputDispatcherTest, MouseHoverAndTouchTap) {
2333 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2334 sp<FakeWindowHandle> window =
2335 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2336 window->setFrame(Rect(0, 0, 100, 100));
2337
2338 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2339
2340 // Inject a hover_move from mouse.
2341 NotifyMotionArgs motionArgs =
2342 generateMotionArgs(AMOTION_EVENT_ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE,
2343 ADISPLAY_ID_DEFAULT, {{50, 50}});
2344 motionArgs.xCursorPosition = 50;
2345 motionArgs.yCursorPosition = 50;
2346 mDispatcher->notifyMotion(&motionArgs);
2347 ASSERT_NO_FATAL_FAILURE(
2348 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2349 WithSource(AINPUT_SOURCE_MOUSE))));
2350 ASSERT_NO_FATAL_FAILURE(
2351 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2352 WithSource(AINPUT_SOURCE_MOUSE))));
2353
2354 // Tap on the window
2355 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2356 ADISPLAY_ID_DEFAULT, {{10, 10}});
2357 mDispatcher->notifyMotion(&motionArgs);
2358 ASSERT_NO_FATAL_FAILURE(
2359 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2360 WithSource(AINPUT_SOURCE_TOUCHSCREEN))));
2361
2362 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
2363 ADISPLAY_ID_DEFAULT, {{10, 10}});
2364 mDispatcher->notifyMotion(&motionArgs);
2365 ASSERT_NO_FATAL_FAILURE(
2366 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
2367 WithSource(AINPUT_SOURCE_TOUCHSCREEN))));
2368}
2369
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02002370TEST_F(InputDispatcherTest, HoverEnterMoveRemoveWindowsInSecondDisplay) {
2371 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2372 sp<FakeWindowHandle> windowDefaultDisplay =
2373 sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay",
2374 ADISPLAY_ID_DEFAULT);
2375 windowDefaultDisplay->setFrame(Rect(0, 0, 600, 800));
2376 sp<FakeWindowHandle> windowSecondDisplay =
2377 sp<FakeWindowHandle>::make(application, mDispatcher, "SecondDisplay",
2378 SECOND_DISPLAY_ID);
2379 windowSecondDisplay->setFrame(Rect(0, 0, 600, 800));
2380
2381 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}},
2382 {SECOND_DISPLAY_ID, {windowSecondDisplay}}});
2383
2384 // Set cursor position in window in default display and check that hover enter and move
2385 // events are generated.
2386 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2387 injectMotionEvent(mDispatcher,
2388 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2389 AINPUT_SOURCE_MOUSE)
2390 .displayId(ADISPLAY_ID_DEFAULT)
2391 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2392 .x(300)
2393 .y(600))
2394 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002395 windowDefaultDisplay->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2396 windowDefaultDisplay->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE));
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02002397
2398 // Remove all windows in secondary display and check that no event happens on window in
2399 // primary display.
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002400 mDispatcher->setInputWindows(
2401 {{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}}, {SECOND_DISPLAY_ID, {}}});
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02002402 windowDefaultDisplay->assertNoEvents();
2403
2404 // Move cursor position in window in default display and check that only hover move
2405 // event is generated and not hover enter event.
2406 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}},
2407 {SECOND_DISPLAY_ID, {windowSecondDisplay}}});
2408 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2409 injectMotionEvent(mDispatcher,
2410 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2411 AINPUT_SOURCE_MOUSE)
2412 .displayId(ADISPLAY_ID_DEFAULT)
2413 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2414 .x(400)
2415 .y(700))
2416 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002417 windowDefaultDisplay->consumeMotionEvent(
2418 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2419 WithSource(AINPUT_SOURCE_MOUSE)));
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02002420 windowDefaultDisplay->assertNoEvents();
2421}
2422
Garfield Tan00f511d2019-06-12 16:55:40 -07002423TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) {
Chris Yea209fde2020-07-22 13:54:51 -07002424 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tan00f511d2019-06-12 16:55:40 -07002425
2426 sp<FakeWindowHandle> windowLeft =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002427 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07002428 windowLeft->setFrame(Rect(0, 0, 600, 800));
Garfield Tan00f511d2019-06-12 16:55:40 -07002429 sp<FakeWindowHandle> windowRight =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002430 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07002431 windowRight->setFrame(Rect(600, 0, 1200, 800));
Garfield Tan00f511d2019-06-12 16:55:40 -07002432
2433 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2434
Arthur Hung72d8dc32020-03-28 00:48:39 +00002435 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
Garfield Tan00f511d2019-06-12 16:55:40 -07002436
2437 // Inject an event with coordinate in the area of right window, with mouse cursor in the area of
2438 // left window. This event should be dispatched to the left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002439 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tan00f511d2019-06-12 16:55:40 -07002440 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07002441 ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400}));
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002442 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07002443 windowRight->assertNoEvents();
2444}
2445
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002446TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) {
Chris Yea209fde2020-07-22 13:54:51 -07002447 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002448 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
2449 "Fake Window", ADISPLAY_ID_DEFAULT);
Vishnu Nair47074b82020-08-14 11:54:47 -07002450 window->setFocusable(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002451
Arthur Hung72d8dc32020-03-28 00:48:39 +00002452 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002453 setFocusedWindow(window);
2454
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002455 window->consumeFocusEvent(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002456
2457 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
2458 mDispatcher->notifyKey(&keyArgs);
2459
2460 // Window should receive key down event.
2461 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2462
2463 // When device reset happens, that key stream should be terminated with FLAG_CANCELED
2464 // on the app side.
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002465 NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002466 mDispatcher->notifyDeviceReset(&args);
2467 window->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
2468 AKEY_EVENT_FLAG_CANCELED);
2469}
2470
2471TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) {
Chris Yea209fde2020-07-22 13:54:51 -07002472 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002473 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
2474 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002475
Arthur Hung72d8dc32020-03-28 00:48:39 +00002476 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002477
2478 NotifyMotionArgs motionArgs =
2479 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2480 ADISPLAY_ID_DEFAULT);
2481 mDispatcher->notifyMotion(&motionArgs);
2482
2483 // Window should receive motion down event.
2484 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2485
2486 // When device reset happens, that motion stream should be terminated with ACTION_CANCEL
2487 // on the app side.
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002488 NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002489 mDispatcher->notifyDeviceReset(&args);
2490 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, ADISPLAY_ID_DEFAULT,
2491 0 /*expectedFlags*/);
2492}
2493
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08002494TEST_F(InputDispatcherTest, InterceptKeyByPolicy) {
2495 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002496 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
2497 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08002498 window->setFocusable(true);
2499
2500 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2501 setFocusedWindow(window);
2502
2503 window->consumeFocusEvent(true);
2504
2505 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
2506 const std::chrono::milliseconds interceptKeyTimeout = 50ms;
2507 const nsecs_t injectTime = keyArgs.eventTime;
2508 mFakePolicy->setInterceptKeyTimeout(interceptKeyTimeout);
2509 mDispatcher->notifyKey(&keyArgs);
2510 // The dispatching time should be always greater than or equal to intercept key timeout.
2511 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2512 ASSERT_TRUE((systemTime(SYSTEM_TIME_MONOTONIC) - injectTime) >=
2513 std::chrono::nanoseconds(interceptKeyTimeout).count());
2514}
2515
2516TEST_F(InputDispatcherTest, InterceptKeyIfKeyUp) {
2517 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002518 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
2519 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08002520 window->setFocusable(true);
2521
2522 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2523 setFocusedWindow(window);
2524
2525 window->consumeFocusEvent(true);
2526
2527 NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
2528 NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
2529 mFakePolicy->setInterceptKeyTimeout(150ms);
2530 mDispatcher->notifyKey(&keyDown);
2531 mDispatcher->notifyKey(&keyUp);
2532
2533 // Window should receive key event immediately when same key up.
2534 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2535 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
2536}
2537
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07002538/**
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00002539 * This test documents the behavior of WATCH_OUTSIDE_TOUCH. The window will get ACTION_OUTSIDE when
2540 * a another pointer causes ACTION_DOWN to be sent to another window for the first time. Only one
2541 * ACTION_OUTSIDE event is sent per gesture.
2542 */
2543TEST_F(InputDispatcherTest, ActionOutsideSentOnlyWhenAWindowIsTouched) {
2544 // There are three windows that do not overlap. `window` wants to WATCH_OUTSIDE_TOUCH.
2545 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002546 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
2547 "First Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00002548 window->setWatchOutsideTouch(true);
2549 window->setFrame(Rect{0, 0, 100, 100});
2550 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002551 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
2552 ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00002553 secondWindow->setFrame(Rect{100, 100, 200, 200});
2554 sp<FakeWindowHandle> thirdWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002555 sp<FakeWindowHandle>::make(application, mDispatcher, "Third Window",
2556 ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00002557 thirdWindow->setFrame(Rect{200, 200, 300, 300});
2558 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, secondWindow, thirdWindow}}});
2559
2560 // First pointer lands outside all windows. `window` does not get ACTION_OUTSIDE.
2561 NotifyMotionArgs motionArgs =
2562 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2563 ADISPLAY_ID_DEFAULT, {PointF{-10, -10}});
2564 mDispatcher->notifyMotion(&motionArgs);
2565 window->assertNoEvents();
2566 secondWindow->assertNoEvents();
2567
2568 // The second pointer lands inside `secondWindow`, which should receive a DOWN event.
2569 // Now, `window` should get ACTION_OUTSIDE.
2570 motionArgs = generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2571 {PointF{-10, -10}, PointF{105, 105}});
2572 mDispatcher->notifyMotion(&motionArgs);
2573 window->consumeMotionOutside();
2574 secondWindow->consumeMotionDown();
2575 thirdWindow->assertNoEvents();
2576
2577 // The third pointer lands inside `thirdWindow`, which should receive a DOWN event. There is
2578 // no ACTION_OUTSIDE sent to `window` because one has already been sent for this gesture.
2579 motionArgs = generateMotionArgs(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2580 {PointF{-10, -10}, PointF{105, 105}, PointF{205, 205}});
2581 mDispatcher->notifyMotion(&motionArgs);
2582 window->assertNoEvents();
2583 secondWindow->consumeMotionMove();
2584 thirdWindow->consumeMotionDown();
2585}
2586
Prabir Pradhan814fe082022-07-22 20:22:18 +00002587TEST_F(InputDispatcherTest, OnWindowInfosChanged_RemoveAllWindowsOnDisplay) {
2588 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002589 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
2590 "Fake Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan814fe082022-07-22 20:22:18 +00002591 window->setFocusable(true);
2592
2593 mDispatcher->onWindowInfosChanged({*window->getInfo()}, {});
2594 setFocusedWindow(window);
2595
2596 window->consumeFocusEvent(true);
2597
2598 NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
2599 NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
2600 mDispatcher->notifyKey(&keyDown);
2601 mDispatcher->notifyKey(&keyUp);
2602
2603 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2604 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
2605
2606 // All windows are removed from the display. Ensure that we can no longer dispatch to it.
2607 mDispatcher->onWindowInfosChanged({}, {});
2608
2609 window->consumeFocusEvent(false);
2610
2611 mDispatcher->notifyKey(&keyDown);
2612 mDispatcher->notifyKey(&keyUp);
2613 window->assertNoEvents();
2614}
2615
Arthur Hung96483742022-11-15 03:30:48 +00002616TEST_F(InputDispatcherTest, NonSplitTouchableWindowReceivesMultiTouch) {
2617 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2618 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
2619 "Fake Window", ADISPLAY_ID_DEFAULT);
2620 // Ensure window is non-split and have some transform.
2621 window->setPreventSplitting(true);
2622 window->setWindowOffset(20, 40);
2623 mDispatcher->onWindowInfosChanged({*window->getInfo()}, {});
2624
2625 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2626 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2627 {50, 50}))
2628 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2629 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2630
2631 const MotionEvent secondFingerDownEvent =
2632 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2633 .displayId(ADISPLAY_ID_DEFAULT)
2634 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
2635 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
2636 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER)
2637 .x(-30)
2638 .y(-50))
2639 .build();
2640 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2641 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
2642 InputEventInjectionSync::WAIT_FOR_RESULT))
2643 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2644
2645 const MotionEvent* event = window->consumeMotion();
2646 EXPECT_EQ(POINTER_1_DOWN, event->getAction());
2647 EXPECT_EQ(70, event->getX(0)); // 50 + 20
2648 EXPECT_EQ(90, event->getY(0)); // 50 + 40
2649 EXPECT_EQ(-10, event->getX(1)); // -30 + 20
2650 EXPECT_EQ(-10, event->getY(1)); // -50 + 40
2651}
2652
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00002653/**
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07002654 * Ensure the correct coordinate spaces are used by InputDispatcher.
2655 *
2656 * InputDispatcher works in the display space, so its coordinate system is relative to the display
2657 * panel. Windows get events in the window space, and get raw coordinates in the logical display
2658 * space.
2659 */
2660class InputDispatcherDisplayProjectionTest : public InputDispatcherTest {
2661public:
2662 void SetUp() override {
2663 InputDispatcherTest::SetUp();
2664 mDisplayInfos.clear();
2665 mWindowInfos.clear();
2666 }
2667
2668 void addDisplayInfo(int displayId, const ui::Transform& transform) {
2669 gui::DisplayInfo info;
2670 info.displayId = displayId;
2671 info.transform = transform;
2672 mDisplayInfos.push_back(std::move(info));
2673 mDispatcher->onWindowInfosChanged(mWindowInfos, mDisplayInfos);
2674 }
2675
2676 void addWindow(const sp<WindowInfoHandle>& windowHandle) {
2677 mWindowInfos.push_back(*windowHandle->getInfo());
2678 mDispatcher->onWindowInfosChanged(mWindowInfos, mDisplayInfos);
2679 }
2680
2681 // Set up a test scenario where the display has a scaled projection and there are two windows
2682 // on the display.
2683 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupScaledDisplayScenario() {
2684 // The display has a projection that has a scale factor of 2 and 4 in the x and y directions
2685 // respectively.
2686 ui::Transform displayTransform;
2687 displayTransform.set(2, 0, 0, 4);
2688 addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform);
2689
2690 std::shared_ptr<FakeApplicationHandle> application =
2691 std::make_shared<FakeApplicationHandle>();
2692
2693 // Add two windows to the display. Their frames are represented in the display space.
2694 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002695 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
2696 ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07002697 firstWindow->setFrame(Rect(0, 0, 100, 200), displayTransform);
2698 addWindow(firstWindow);
2699
2700 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002701 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
2702 ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07002703 secondWindow->setFrame(Rect(100, 200, 200, 400), displayTransform);
2704 addWindow(secondWindow);
2705 return {std::move(firstWindow), std::move(secondWindow)};
2706 }
2707
2708private:
2709 std::vector<gui::DisplayInfo> mDisplayInfos;
2710 std::vector<gui::WindowInfo> mWindowInfos;
2711};
2712
2713TEST_F(InputDispatcherDisplayProjectionTest, HitTestsInDisplaySpace) {
2714 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
2715 // Send down to the first window. The point is represented in the display space. The point is
2716 // selected so that if the hit test was done with the transform applied to it, then it would
2717 // end up in the incorrect window.
2718 NotifyMotionArgs downMotionArgs =
2719 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2720 ADISPLAY_ID_DEFAULT, {PointF{75, 55}});
2721 mDispatcher->notifyMotion(&downMotionArgs);
2722
2723 firstWindow->consumeMotionDown();
2724 secondWindow->assertNoEvents();
2725}
2726
2727// Ensure that when a MotionEvent is injected through the InputDispatcher::injectInputEvent() API,
2728// the event should be treated as being in the logical display space.
2729TEST_F(InputDispatcherDisplayProjectionTest, InjectionInLogicalDisplaySpace) {
2730 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
2731 // Send down to the first window. The point is represented in the logical display space. The
2732 // point is selected so that if the hit test was done in logical display space, then it would
2733 // end up in the incorrect window.
2734 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2735 PointF{75 * 2, 55 * 4});
2736
2737 firstWindow->consumeMotionDown();
2738 secondWindow->assertNoEvents();
2739}
2740
Prabir Pradhandaa2f142021-12-10 09:30:08 +00002741// Ensure that when a MotionEvent that has a custom transform is injected, the post-transformed
2742// event should be treated as being in the logical display space.
2743TEST_F(InputDispatcherDisplayProjectionTest, InjectionWithTransformInLogicalDisplaySpace) {
2744 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
2745
2746 const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0};
2747 ui::Transform injectedEventTransform;
2748 injectedEventTransform.set(matrix);
2749 const vec2 expectedPoint{75, 55}; // The injected point in the logical display space.
2750 const vec2 untransformedPoint = injectedEventTransform.inverse().transform(expectedPoint);
2751
2752 MotionEvent event = MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2753 .displayId(ADISPLAY_ID_DEFAULT)
2754 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
2755 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
2756 .x(untransformedPoint.x)
2757 .y(untransformedPoint.y))
2758 .build();
2759 event.transform(matrix);
2760
2761 injectMotionEvent(mDispatcher, event, INJECT_EVENT_TIMEOUT,
2762 InputEventInjectionSync::WAIT_FOR_RESULT);
2763
2764 firstWindow->consumeMotionDown();
2765 secondWindow->assertNoEvents();
2766}
2767
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07002768TEST_F(InputDispatcherDisplayProjectionTest, WindowGetsEventsInCorrectCoordinateSpace) {
2769 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
2770
2771 // Send down to the second window.
2772 NotifyMotionArgs downMotionArgs =
2773 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2774 ADISPLAY_ID_DEFAULT, {PointF{150, 220}});
2775 mDispatcher->notifyMotion(&downMotionArgs);
2776
2777 firstWindow->assertNoEvents();
2778 const MotionEvent* event = secondWindow->consumeMotion();
2779 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, event->getAction());
2780
2781 // Ensure that the events from the "getRaw" API are in logical display coordinates.
2782 EXPECT_EQ(300, event->getRawX(0));
2783 EXPECT_EQ(880, event->getRawY(0));
2784
2785 // Ensure that the x and y values are in the window's coordinate space.
2786 // The left-top of the second window is at (100, 200) in display space, which is (200, 800) in
2787 // the logical display space. This will be the origin of the window space.
2788 EXPECT_EQ(100, event->getX(0));
2789 EXPECT_EQ(80, event->getY(0));
2790}
2791
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002792using TransferFunction = std::function<bool(const std::unique_ptr<InputDispatcher>& dispatcher,
2793 sp<IBinder>, sp<IBinder>)>;
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00002794
2795class TransferTouchFixture : public InputDispatcherTest,
2796 public ::testing::WithParamInterface<TransferFunction> {};
2797
2798TEST_P(TransferTouchFixture, TransferTouch_OnePointer) {
Chris Yea209fde2020-07-22 13:54:51 -07002799 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08002800
2801 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002802 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002803 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
2804 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002805 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002806 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
2807 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08002808
2809 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00002810 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08002811
2812 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002813 NotifyMotionArgs downMotionArgs =
2814 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2815 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08002816 mDispatcher->notifyMotion(&downMotionArgs);
2817 // Only the first window should get the down event
2818 firstWindow->consumeMotionDown();
2819 secondWindow->assertNoEvents();
2820
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00002821 // Transfer touch to the second window
2822 TransferFunction f = GetParam();
2823 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
2824 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08002825 // The first window gets cancel and the second gets down
2826 firstWindow->consumeMotionCancel();
2827 secondWindow->consumeMotionDown();
2828
2829 // Send up event to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002830 NotifyMotionArgs upMotionArgs =
2831 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
2832 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08002833 mDispatcher->notifyMotion(&upMotionArgs);
2834 // The first window gets no events and the second gets up
2835 firstWindow->assertNoEvents();
2836 secondWindow->consumeMotionUp();
2837}
2838
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07002839/**
2840 * When 'transferTouch' API is invoked, dispatcher needs to find the "best" window to take touch
2841 * from. When we have spy windows, there are several windows to choose from: either spy, or the
2842 * 'real' (non-spy) window. Always prefer the 'real' window because that's what would be most
2843 * natural to the user.
2844 * In this test, we are sending a pointer to both spy window and first window. We then try to
2845 * transfer touch to the second window. The dispatcher should identify the first window as the
2846 * one that should lose the gesture, and therefore the action should be to move the gesture from
2847 * the first window to the second.
2848 * The main goal here is to test the behaviour of 'transferTouch' API, but it's still valid to test
2849 * the other API, as well.
2850 */
2851TEST_P(TransferTouchFixture, TransferTouch_MultipleWindowsWithSpy) {
2852 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2853
2854 // Create a couple of windows + a spy window
2855 sp<FakeWindowHandle> spyWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002856 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07002857 spyWindow->setTrustedOverlay(true);
2858 spyWindow->setSpy(true);
2859 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002860 sp<FakeWindowHandle>::make(application, mDispatcher, "First", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07002861 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002862 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07002863
2864 // Add the windows to the dispatcher
2865 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, firstWindow, secondWindow}}});
2866
2867 // Send down to the first window
2868 NotifyMotionArgs downMotionArgs =
2869 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2870 ADISPLAY_ID_DEFAULT);
2871 mDispatcher->notifyMotion(&downMotionArgs);
2872 // Only the first window and spy should get the down event
2873 spyWindow->consumeMotionDown();
2874 firstWindow->consumeMotionDown();
2875
2876 // Transfer touch to the second window. Non-spy window should be preferred over the spy window
2877 // if f === 'transferTouch'.
2878 TransferFunction f = GetParam();
2879 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
2880 ASSERT_TRUE(success);
2881 // The first window gets cancel and the second gets down
2882 firstWindow->consumeMotionCancel();
2883 secondWindow->consumeMotionDown();
2884
2885 // Send up event to the second window
2886 NotifyMotionArgs upMotionArgs =
2887 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
2888 ADISPLAY_ID_DEFAULT);
2889 mDispatcher->notifyMotion(&upMotionArgs);
2890 // The first window gets no events and the second+spy get up
2891 firstWindow->assertNoEvents();
2892 spyWindow->consumeMotionUp();
2893 secondWindow->consumeMotionUp();
2894}
2895
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00002896TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07002897 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08002898
2899 PointF touchPoint = {10, 10};
2900
2901 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002902 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002903 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
2904 ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08002905 firstWindow->setPreventSplitting(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002906 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002907 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
2908 ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08002909 secondWindow->setPreventSplitting(true);
Svet Ganov5d3bc372020-01-26 23:11:07 -08002910
2911 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00002912 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08002913
2914 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002915 NotifyMotionArgs downMotionArgs =
2916 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2917 ADISPLAY_ID_DEFAULT, {touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08002918 mDispatcher->notifyMotion(&downMotionArgs);
2919 // Only the first window should get the down event
2920 firstWindow->consumeMotionDown();
2921 secondWindow->assertNoEvents();
2922
2923 // Send pointer down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002924 NotifyMotionArgs pointerDownMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002925 generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002926 {touchPoint, touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08002927 mDispatcher->notifyMotion(&pointerDownMotionArgs);
2928 // Only the first window should get the pointer down event
2929 firstWindow->consumeMotionPointerDown(1);
2930 secondWindow->assertNoEvents();
2931
2932 // Transfer touch focus to the second window
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00002933 TransferFunction f = GetParam();
2934 bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
2935 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08002936 // The first window gets cancel and the second gets down and pointer down
2937 firstWindow->consumeMotionCancel();
2938 secondWindow->consumeMotionDown();
2939 secondWindow->consumeMotionPointerDown(1);
2940
2941 // Send pointer up to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002942 NotifyMotionArgs pointerUpMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002943 generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002944 {touchPoint, touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08002945 mDispatcher->notifyMotion(&pointerUpMotionArgs);
2946 // The first window gets nothing and the second gets pointer up
2947 firstWindow->assertNoEvents();
2948 secondWindow->consumeMotionPointerUp(1);
2949
2950 // Send up event to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002951 NotifyMotionArgs upMotionArgs =
2952 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
2953 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08002954 mDispatcher->notifyMotion(&upMotionArgs);
2955 // The first window gets nothing and the second gets up
2956 firstWindow->assertNoEvents();
2957 secondWindow->consumeMotionUp();
2958}
2959
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00002960// For the cases of single pointer touch and two pointers non-split touch, the api's
2961// 'transferTouch' and 'transferTouchFocus' are equivalent in behaviour. They only differ
2962// for the case where there are multiple pointers split across several windows.
2963INSTANTIATE_TEST_SUITE_P(TransferFunctionTests, TransferTouchFixture,
2964 ::testing::Values(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002965 [&](const std::unique_ptr<InputDispatcher>& dispatcher,
2966 sp<IBinder> /*ignored*/, sp<IBinder> destChannelToken) {
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07002967 return dispatcher->transferTouch(destChannelToken,
2968 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00002969 },
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002970 [&](const std::unique_ptr<InputDispatcher>& dispatcher,
2971 sp<IBinder> from, sp<IBinder> to) {
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00002972 return dispatcher->transferTouchFocus(from, to,
2973 false /*isDragAndDrop*/);
2974 }));
2975
Svet Ganov5d3bc372020-01-26 23:11:07 -08002976TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07002977 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08002978
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002979 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002980 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
2981 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08002982 firstWindow->setFrame(Rect(0, 0, 600, 400));
Svet Ganov5d3bc372020-01-26 23:11:07 -08002983
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002984 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002985 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
2986 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08002987 secondWindow->setFrame(Rect(0, 400, 600, 800));
Svet Ganov5d3bc372020-01-26 23:11:07 -08002988
2989 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00002990 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08002991
2992 PointF pointInFirst = {300, 200};
2993 PointF pointInSecond = {300, 600};
2994
2995 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002996 NotifyMotionArgs firstDownMotionArgs =
2997 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2998 ADISPLAY_ID_DEFAULT, {pointInFirst});
Svet Ganov5d3bc372020-01-26 23:11:07 -08002999 mDispatcher->notifyMotion(&firstDownMotionArgs);
3000 // Only the first window should get the down event
3001 firstWindow->consumeMotionDown();
3002 secondWindow->assertNoEvents();
3003
3004 // Send down to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003005 NotifyMotionArgs secondDownMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003006 generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003007 {pointInFirst, pointInSecond});
Svet Ganov5d3bc372020-01-26 23:11:07 -08003008 mDispatcher->notifyMotion(&secondDownMotionArgs);
3009 // The first window gets a move and the second a down
3010 firstWindow->consumeMotionMove();
3011 secondWindow->consumeMotionDown();
3012
3013 // Transfer touch focus to the second window
3014 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
3015 // The first window gets cancel and the new gets pointer down (it already saw down)
3016 firstWindow->consumeMotionCancel();
3017 secondWindow->consumeMotionPointerDown(1);
3018
3019 // Send pointer up to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003020 NotifyMotionArgs pointerUpMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003021 generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003022 {pointInFirst, pointInSecond});
Svet Ganov5d3bc372020-01-26 23:11:07 -08003023 mDispatcher->notifyMotion(&pointerUpMotionArgs);
3024 // The first window gets nothing and the second gets pointer up
3025 firstWindow->assertNoEvents();
3026 secondWindow->consumeMotionPointerUp(1);
3027
3028 // Send up event to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003029 NotifyMotionArgs upMotionArgs =
3030 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3031 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08003032 mDispatcher->notifyMotion(&upMotionArgs);
3033 // The first window gets nothing and the second gets up
3034 firstWindow->assertNoEvents();
3035 secondWindow->consumeMotionUp();
3036}
3037
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003038// Same as TransferTouchFocus_TwoPointersSplitTouch, but using 'transferTouch' api.
3039// Unlike 'transferTouchFocus', calling 'transferTouch' when there are two windows receiving
3040// touch is not supported, so the touch should continue on those windows and the transferred-to
3041// window should get nothing.
3042TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) {
3043 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3044
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003045 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003046 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
3047 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003048 firstWindow->setFrame(Rect(0, 0, 600, 400));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003049
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003050 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003051 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3052 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003053 secondWindow->setFrame(Rect(0, 400, 600, 800));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003054
3055 // Add the windows to the dispatcher
3056 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
3057
3058 PointF pointInFirst = {300, 200};
3059 PointF pointInSecond = {300, 600};
3060
3061 // Send down to the first window
3062 NotifyMotionArgs firstDownMotionArgs =
3063 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3064 ADISPLAY_ID_DEFAULT, {pointInFirst});
3065 mDispatcher->notifyMotion(&firstDownMotionArgs);
3066 // Only the first window should get the down event
3067 firstWindow->consumeMotionDown();
3068 secondWindow->assertNoEvents();
3069
3070 // Send down to the second window
3071 NotifyMotionArgs secondDownMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003072 generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003073 {pointInFirst, pointInSecond});
3074 mDispatcher->notifyMotion(&secondDownMotionArgs);
3075 // The first window gets a move and the second a down
3076 firstWindow->consumeMotionMove();
3077 secondWindow->consumeMotionDown();
3078
3079 // Transfer touch focus to the second window
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07003080 const bool transferred =
3081 mDispatcher->transferTouch(secondWindow->getToken(), ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003082 // The 'transferTouch' call should not succeed, because there are 2 touched windows
3083 ASSERT_FALSE(transferred);
3084 firstWindow->assertNoEvents();
3085 secondWindow->assertNoEvents();
3086
3087 // The rest of the dispatch should proceed as normal
3088 // Send pointer up to the second window
3089 NotifyMotionArgs pointerUpMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003090 generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00003091 {pointInFirst, pointInSecond});
3092 mDispatcher->notifyMotion(&pointerUpMotionArgs);
3093 // The first window gets MOVE and the second gets pointer up
3094 firstWindow->consumeMotionMove();
3095 secondWindow->consumeMotionUp();
3096
3097 // Send up event to the first window
3098 NotifyMotionArgs upMotionArgs =
3099 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3100 ADISPLAY_ID_DEFAULT);
3101 mDispatcher->notifyMotion(&upMotionArgs);
3102 // The first window gets nothing and the second gets up
3103 firstWindow->consumeMotionUp();
3104 secondWindow->assertNoEvents();
3105}
3106
Arthur Hungabbb9d82021-09-01 14:52:30 +00003107// This case will create two windows and one mirrored window on the default display and mirror
3108// two windows on the second display. It will test if 'transferTouchFocus' works fine if we put
3109// the windows info of second display before default display.
3110TEST_F(InputDispatcherTest, TransferTouchFocus_CloneSurface) {
3111 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3112 sp<FakeWindowHandle> firstWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003113 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00003114 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003115 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003116 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00003117 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003118
3119 sp<FakeWindowHandle> mirrorWindowInPrimary =
3120 firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT);
3121 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003122
3123 sp<FakeWindowHandle> firstWindowInSecondary =
3124 firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
3125 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003126
3127 sp<FakeWindowHandle> secondWindowInSecondary =
3128 secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
3129 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003130
3131 // Update window info, let it find window handle of second display first.
3132 mDispatcher->setInputWindows(
3133 {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}},
3134 {ADISPLAY_ID_DEFAULT,
3135 {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}});
3136
3137 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3138 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3139 {50, 50}))
3140 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3141
3142 // Window should receive motion event.
3143 firstWindowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3144
3145 // Transfer touch focus
3146 ASSERT_TRUE(mDispatcher->transferTouchFocus(firstWindowInPrimary->getToken(),
3147 secondWindowInPrimary->getToken()));
3148 // The first window gets cancel.
3149 firstWindowInPrimary->consumeMotionCancel();
3150 secondWindowInPrimary->consumeMotionDown();
3151
3152 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3153 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
3154 ADISPLAY_ID_DEFAULT, {150, 50}))
3155 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3156 firstWindowInPrimary->assertNoEvents();
3157 secondWindowInPrimary->consumeMotionMove();
3158
3159 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3160 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3161 {150, 50}))
3162 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3163 firstWindowInPrimary->assertNoEvents();
3164 secondWindowInPrimary->consumeMotionUp();
3165}
3166
3167// Same as TransferTouchFocus_CloneSurface, but this touch on the secondary display and use
3168// 'transferTouch' api.
3169TEST_F(InputDispatcherTest, TransferTouch_CloneSurface) {
3170 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3171 sp<FakeWindowHandle> firstWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003172 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00003173 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003174 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003175 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00003176 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003177
3178 sp<FakeWindowHandle> mirrorWindowInPrimary =
3179 firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT);
3180 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003181
3182 sp<FakeWindowHandle> firstWindowInSecondary =
3183 firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
3184 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003185
3186 sp<FakeWindowHandle> secondWindowInSecondary =
3187 secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
3188 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003189
3190 // Update window info, let it find window handle of second display first.
3191 mDispatcher->setInputWindows(
3192 {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}},
3193 {ADISPLAY_ID_DEFAULT,
3194 {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}});
3195
3196 // Touch on second display.
3197 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3198 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {50, 50}))
3199 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3200
3201 // Window should receive motion event.
3202 firstWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID);
3203
3204 // Transfer touch focus
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07003205 ASSERT_TRUE(mDispatcher->transferTouch(secondWindowInSecondary->getToken(), SECOND_DISPLAY_ID));
Arthur Hungabbb9d82021-09-01 14:52:30 +00003206
3207 // The first window gets cancel.
3208 firstWindowInPrimary->consumeMotionCancel(SECOND_DISPLAY_ID);
3209 secondWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID);
3210
3211 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3212 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
3213 SECOND_DISPLAY_ID, {150, 50}))
3214 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3215 firstWindowInPrimary->assertNoEvents();
3216 secondWindowInPrimary->consumeMotionMove(SECOND_DISPLAY_ID);
3217
3218 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3219 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {150, 50}))
3220 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3221 firstWindowInPrimary->assertNoEvents();
3222 secondWindowInPrimary->consumeMotionUp(SECOND_DISPLAY_ID);
3223}
3224
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003225TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07003226 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003227 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3228 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003229
Vishnu Nair47074b82020-08-14 11:54:47 -07003230 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00003231 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003232 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003233
3234 window->consumeFocusEvent(true);
3235
3236 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
3237 mDispatcher->notifyKey(&keyArgs);
3238
3239 // Window should receive key down event.
3240 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3241}
3242
3243TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07003244 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003245 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3246 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003247
Arthur Hung72d8dc32020-03-28 00:48:39 +00003248 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003249
3250 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
3251 mDispatcher->notifyKey(&keyArgs);
3252 mDispatcher->waitForIdle();
3253
3254 window->assertNoEvents();
3255}
3256
3257// If a window is touchable, but does not have focus, it should receive motion events, but not keys
3258TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) {
Chris Yea209fde2020-07-22 13:54:51 -07003259 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003260 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3261 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003262
Arthur Hung72d8dc32020-03-28 00:48:39 +00003263 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003264
3265 // Send key
3266 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
3267 mDispatcher->notifyKey(&keyArgs);
3268 // Send motion
3269 NotifyMotionArgs motionArgs =
3270 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3271 ADISPLAY_ID_DEFAULT);
3272 mDispatcher->notifyMotion(&motionArgs);
3273
3274 // Window should receive only the motion event
3275 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3276 window->assertNoEvents(); // Key event or focus event will not be received
3277}
3278
arthurhungea3f4fc2020-12-21 23:18:53 +08003279TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) {
3280 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3281
arthurhungea3f4fc2020-12-21 23:18:53 +08003282 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003283 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
3284 ADISPLAY_ID_DEFAULT);
arthurhungea3f4fc2020-12-21 23:18:53 +08003285 firstWindow->setFrame(Rect(0, 0, 600, 400));
arthurhungea3f4fc2020-12-21 23:18:53 +08003286
arthurhungea3f4fc2020-12-21 23:18:53 +08003287 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003288 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3289 ADISPLAY_ID_DEFAULT);
arthurhungea3f4fc2020-12-21 23:18:53 +08003290 secondWindow->setFrame(Rect(0, 400, 600, 800));
arthurhungea3f4fc2020-12-21 23:18:53 +08003291
3292 // Add the windows to the dispatcher
3293 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
3294
3295 PointF pointInFirst = {300, 200};
3296 PointF pointInSecond = {300, 600};
3297
3298 // Send down to the first window
3299 NotifyMotionArgs firstDownMotionArgs =
3300 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3301 ADISPLAY_ID_DEFAULT, {pointInFirst});
3302 mDispatcher->notifyMotion(&firstDownMotionArgs);
3303 // Only the first window should get the down event
3304 firstWindow->consumeMotionDown();
3305 secondWindow->assertNoEvents();
3306
3307 // Send down to the second window
3308 NotifyMotionArgs secondDownMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003309 generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
arthurhungea3f4fc2020-12-21 23:18:53 +08003310 {pointInFirst, pointInSecond});
3311 mDispatcher->notifyMotion(&secondDownMotionArgs);
3312 // The first window gets a move and the second a down
3313 firstWindow->consumeMotionMove();
3314 secondWindow->consumeMotionDown();
3315
3316 // Send pointer cancel to the second window
3317 NotifyMotionArgs pointerUpMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003318 generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
arthurhungea3f4fc2020-12-21 23:18:53 +08003319 {pointInFirst, pointInSecond});
3320 pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED;
3321 mDispatcher->notifyMotion(&pointerUpMotionArgs);
3322 // The first window gets move and the second gets cancel.
3323 firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
3324 secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
3325
3326 // Send up event.
3327 NotifyMotionArgs upMotionArgs =
3328 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3329 ADISPLAY_ID_DEFAULT);
3330 mDispatcher->notifyMotion(&upMotionArgs);
3331 // The first window gets up and the second gets nothing.
3332 firstWindow->consumeMotionUp();
3333 secondWindow->assertNoEvents();
3334}
3335
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00003336TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) {
3337 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3338
3339 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003340 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00003341 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3342 std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline;
3343 graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2;
3344 graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3;
3345
3346 window->sendTimeline(1 /*inputEventId*/, graphicsTimeline);
3347 window->assertNoEvents();
3348 mDispatcher->waitForIdle();
3349}
3350
chaviwd1c23182019-12-20 18:44:56 -08003351class FakeMonitorReceiver {
Michael Wright3a240c42019-12-10 20:53:41 +00003352public:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003353 FakeMonitorReceiver(const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003354 int32_t displayId) {
Garfield Tan15601662020-09-22 15:32:38 -07003355 base::Result<std::unique_ptr<InputChannel>> channel =
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08003356 dispatcher->createInputMonitor(displayId, name, MONITOR_PID);
Garfield Tan15601662020-09-22 15:32:38 -07003357 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
Michael Wright3a240c42019-12-10 20:53:41 +00003358 }
3359
chaviwd1c23182019-12-20 18:44:56 -08003360 sp<IBinder> getToken() { return mInputReceiver->getToken(); }
3361
3362 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
3363 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN,
3364 expectedDisplayId, expectedFlags);
3365 }
3366
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003367 std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); }
3368
3369 void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); }
3370
chaviwd1c23182019-12-20 18:44:56 -08003371 void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
3372 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN,
3373 expectedDisplayId, expectedFlags);
3374 }
3375
Siarhei Vishniakouca205502021-07-16 21:31:58 +00003376 void consumeMotionMove(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
3377 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE,
3378 expectedDisplayId, expectedFlags);
3379 }
3380
chaviwd1c23182019-12-20 18:44:56 -08003381 void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
3382 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP,
3383 expectedDisplayId, expectedFlags);
3384 }
3385
Siarhei Vishniakouca205502021-07-16 21:31:58 +00003386 void consumeMotionCancel(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
3387 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL,
3388 expectedDisplayId, expectedFlags);
3389 }
3390
Arthur Hungfbfa5722021-11-16 02:45:54 +00003391 void consumeMotionPointerDown(int32_t pointerIdx) {
3392 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
3393 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
3394 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, ADISPLAY_ID_DEFAULT,
3395 0 /*expectedFlags*/);
3396 }
3397
Evan Rosky84f07f02021-04-16 10:42:42 -07003398 MotionEvent* consumeMotion() {
3399 InputEvent* event = mInputReceiver->consume();
3400 if (!event) {
3401 ADD_FAILURE() << "No event was produced";
3402 return nullptr;
3403 }
3404 if (event->getType() != AINPUT_EVENT_TYPE_MOTION) {
3405 ADD_FAILURE() << "Received event of type " << event->getType() << " instead of motion";
3406 return nullptr;
3407 }
3408 return static_cast<MotionEvent*>(event);
3409 }
3410
chaviwd1c23182019-12-20 18:44:56 -08003411 void assertNoEvents() { mInputReceiver->assertNoEvents(); }
3412
3413private:
3414 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Michael Wright3a240c42019-12-10 20:53:41 +00003415};
3416
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003417using InputDispatcherMonitorTest = InputDispatcherTest;
3418
Siarhei Vishniakouca205502021-07-16 21:31:58 +00003419/**
3420 * Two entities that receive touch: A window, and a global monitor.
3421 * The touch goes to the window, and then the window disappears.
3422 * The monitor does not get cancel right away. But if more events come in, the touch gets canceled
3423 * for the monitor, as well.
3424 * 1. foregroundWindow
3425 * 2. monitor <-- global monitor (doesn't observe z order, receives all events)
3426 */
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003427TEST_F(InputDispatcherMonitorTest, MonitorTouchIsCanceledWhenForegroundWindowDisappears) {
Siarhei Vishniakouca205502021-07-16 21:31:58 +00003428 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3429 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003430 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00003431
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003432 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00003433
3434 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3435 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3436 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3437 {100, 200}))
3438 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3439
3440 // Both the foreground window and the global monitor should receive the touch down
3441 window->consumeMotionDown();
3442 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
3443
3444 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3445 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
3446 ADISPLAY_ID_DEFAULT, {110, 200}))
3447 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3448
3449 window->consumeMotionMove();
3450 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
3451
3452 // Now the foreground window goes away
3453 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
3454 window->consumeMotionCancel();
3455 monitor.assertNoEvents(); // Global monitor does not get a cancel yet
3456
3457 // If more events come in, there will be no more foreground window to send them to. This will
3458 // cause a cancel for the monitor, as well.
3459 ASSERT_EQ(InputEventInjectionResult::FAILED,
3460 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
3461 ADISPLAY_ID_DEFAULT, {120, 200}))
3462 << "Injection should fail because the window was removed";
3463 window->assertNoEvents();
3464 // Global monitor now gets the cancel
3465 monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
3466}
3467
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003468TEST_F(InputDispatcherMonitorTest, ReceivesMotionEvents) {
Chris Yea209fde2020-07-22 13:54:51 -07003469 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003470 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3471 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00003472 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00003473
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003474 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00003475
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003476 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00003477 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003478 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00003479 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08003480 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00003481}
3482
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003483TEST_F(InputDispatcherMonitorTest, MonitorCannotPilferPointers) {
3484 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00003485
Chris Yea209fde2020-07-22 13:54:51 -07003486 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003487 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3488 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00003489 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00003490
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003491 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00003492 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003493 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
chaviwd1c23182019-12-20 18:44:56 -08003494 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003495 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00003496
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003497 // Pilfer pointers from the monitor.
3498 // This should not do anything and the window should continue to receive events.
3499 EXPECT_NE(OK, mDispatcher->pilferPointers(monitor.getToken()));
Michael Wright3a240c42019-12-10 20:53:41 +00003500
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003501 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003502 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
3503 ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003504 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003505
3506 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
3507 window->consumeMotionMove(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00003508}
3509
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003510TEST_F(InputDispatcherMonitorTest, NoWindowTransform) {
Evan Rosky84f07f02021-04-16 10:42:42 -07003511 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003512 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3513 "Fake Window", ADISPLAY_ID_DEFAULT);
Evan Rosky84f07f02021-04-16 10:42:42 -07003514 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3515 window->setWindowOffset(20, 40);
3516 window->setWindowTransform(0, 1, -1, 0);
3517
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003518 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Evan Rosky84f07f02021-04-16 10:42:42 -07003519
3520 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3521 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
3522 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3523 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3524 MotionEvent* event = monitor.consumeMotion();
3525 // Even though window has transform, gesture monitor must not.
3526 ASSERT_EQ(ui::Transform(), event->getTransform());
3527}
3528
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003529TEST_F(InputDispatcherMonitorTest, InjectionFailsWithNoWindow) {
Arthur Hungb3307ee2021-10-14 10:57:37 +00003530 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003531 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Arthur Hungb3307ee2021-10-14 10:57:37 +00003532
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003533 ASSERT_EQ(InputEventInjectionResult::FAILED,
Arthur Hungb3307ee2021-10-14 10:57:37 +00003534 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003535 << "Injection should fail if there is a monitor, but no touchable window";
3536 monitor.assertNoEvents();
Arthur Hungb3307ee2021-10-14 10:57:37 +00003537}
3538
chaviw81e2bb92019-12-18 15:03:51 -08003539TEST_F(InputDispatcherTest, TestMoveEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07003540 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003541 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3542 "Fake Window", ADISPLAY_ID_DEFAULT);
chaviw81e2bb92019-12-18 15:03:51 -08003543
Arthur Hung72d8dc32020-03-28 00:48:39 +00003544 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
chaviw81e2bb92019-12-18 15:03:51 -08003545
3546 NotifyMotionArgs motionArgs =
3547 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3548 ADISPLAY_ID_DEFAULT);
3549
3550 mDispatcher->notifyMotion(&motionArgs);
3551 // Window should receive motion down event.
3552 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3553
3554 motionArgs.action = AMOTION_EVENT_ACTION_MOVE;
Garfield Tanc51d1ba2020-01-28 13:24:04 -08003555 motionArgs.id += 1;
chaviw81e2bb92019-12-18 15:03:51 -08003556 motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
3557 motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
3558 motionArgs.pointerCoords[0].getX() - 10);
3559
3560 mDispatcher->notifyMotion(&motionArgs);
3561 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT,
3562 0 /*expectedFlags*/);
3563}
3564
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003565/**
3566 * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to
3567 * the device default right away. In the test scenario, we check both the default value,
3568 * and the action of enabling / disabling.
3569 */
3570TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) {
Chris Yea209fde2020-07-22 13:54:51 -07003571 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003572 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3573 "Test window", ADISPLAY_ID_DEFAULT);
Antonio Kantekea47acb2021-12-23 12:41:25 -08003574 const WindowInfo& windowInfo = *window->getInfo();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003575
3576 // Set focused application.
3577 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07003578 window->setFocusable(true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003579
3580 SCOPED_TRACE("Check default value of touch mode");
Arthur Hung72d8dc32020-03-28 00:48:39 +00003581 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003582 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003583 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
3584
3585 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07003586 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00003587 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003588 window->consumeFocusEvent(false /*hasFocus*/, true /*inTouchMode*/);
3589
3590 SCOPED_TRACE("Disable touch mode");
Antonio Kantekea47acb2021-12-23 12:41:25 -08003591 mDispatcher->setInTouchMode(false, windowInfo.ownerPid, windowInfo.ownerUid,
Antonio Kanteka042c022022-07-06 16:51:07 -07003592 true /*hasPermission*/, ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00003593 window->consumeTouchModeEvent(false);
Vishnu Nair47074b82020-08-14 11:54:47 -07003594 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00003595 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003596 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003597 window->consumeFocusEvent(true /*hasFocus*/, false /*inTouchMode*/);
3598
3599 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07003600 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00003601 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003602 window->consumeFocusEvent(false /*hasFocus*/, false /*inTouchMode*/);
3603
3604 SCOPED_TRACE("Enable touch mode again");
Antonio Kantekea47acb2021-12-23 12:41:25 -08003605 mDispatcher->setInTouchMode(true, windowInfo.ownerPid, windowInfo.ownerUid,
Antonio Kanteka042c022022-07-06 16:51:07 -07003606 true /*hasPermission*/, ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00003607 window->consumeTouchModeEvent(true);
Vishnu Nair47074b82020-08-14 11:54:47 -07003608 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00003609 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003610 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003611 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
3612
3613 window->assertNoEvents();
3614}
3615
Gang Wange9087892020-01-07 12:17:14 -05003616TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07003617 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003618 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3619 "Test window", ADISPLAY_ID_DEFAULT);
Gang Wange9087892020-01-07 12:17:14 -05003620
3621 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07003622 window->setFocusable(true);
Gang Wange9087892020-01-07 12:17:14 -05003623
Arthur Hung72d8dc32020-03-28 00:48:39 +00003624 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003625 setFocusedWindow(window);
3626
Gang Wange9087892020-01-07 12:17:14 -05003627 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
3628
3629 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
3630 mDispatcher->notifyKey(&keyArgs);
3631
3632 InputEvent* event = window->consume();
3633 ASSERT_NE(event, nullptr);
3634
3635 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
3636 ASSERT_NE(verified, nullptr);
3637 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY);
3638
3639 ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos);
3640 ASSERT_EQ(keyArgs.deviceId, verified->deviceId);
3641 ASSERT_EQ(keyArgs.source, verified->source);
3642 ASSERT_EQ(keyArgs.displayId, verified->displayId);
3643
3644 const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified);
3645
3646 ASSERT_EQ(keyArgs.action, verifiedKey.action);
Gang Wange9087892020-01-07 12:17:14 -05003647 ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08003648 ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos);
Gang Wange9087892020-01-07 12:17:14 -05003649 ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode);
3650 ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode);
3651 ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState);
3652 ASSERT_EQ(0, verifiedKey.repeatCount);
3653}
3654
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08003655TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07003656 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003657 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3658 "Test window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08003659
3660 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3661
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07003662 ui::Transform transform;
3663 transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
3664
3665 gui::DisplayInfo displayInfo;
3666 displayInfo.displayId = ADISPLAY_ID_DEFAULT;
3667 displayInfo.transform = transform;
3668
3669 mDispatcher->onWindowInfosChanged({*window->getInfo()}, {displayInfo});
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08003670
3671 NotifyMotionArgs motionArgs =
3672 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3673 ADISPLAY_ID_DEFAULT);
3674 mDispatcher->notifyMotion(&motionArgs);
3675
3676 InputEvent* event = window->consume();
3677 ASSERT_NE(event, nullptr);
3678
3679 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
3680 ASSERT_NE(verified, nullptr);
3681 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION);
3682
3683 EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos);
3684 EXPECT_EQ(motionArgs.deviceId, verified->deviceId);
3685 EXPECT_EQ(motionArgs.source, verified->source);
3686 EXPECT_EQ(motionArgs.displayId, verified->displayId);
3687
3688 const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified);
3689
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07003690 const vec2 rawXY =
3691 MotionEvent::calculateTransformedXY(motionArgs.source, transform,
3692 motionArgs.pointerCoords[0].getXYValue());
3693 EXPECT_EQ(rawXY.x, verifiedMotion.rawX);
3694 EXPECT_EQ(rawXY.y, verifiedMotion.rawY);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08003695 EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08003696 EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08003697 EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08003698 EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState);
3699 EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState);
3700}
3701
chaviw09c8d2d2020-08-24 15:48:26 -07003702/**
3703 * Ensure that separate calls to sign the same data are generating the same key.
3704 * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance
3705 * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky
3706 * tests.
3707 */
3708TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) {
3709 KeyEvent event = getTestKeyEvent();
3710 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
3711
3712 std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent);
3713 std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent);
3714 ASSERT_EQ(hmac1, hmac2);
3715}
3716
3717/**
3718 * Ensure that changes in VerifiedKeyEvent produce a different hmac.
3719 */
3720TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) {
3721 KeyEvent event = getTestKeyEvent();
3722 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
3723 std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent);
3724
3725 verifiedEvent.deviceId += 1;
3726 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
3727
3728 verifiedEvent.source += 1;
3729 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
3730
3731 verifiedEvent.eventTimeNanos += 1;
3732 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
3733
3734 verifiedEvent.displayId += 1;
3735 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
3736
3737 verifiedEvent.action += 1;
3738 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
3739
3740 verifiedEvent.downTimeNanos += 1;
3741 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
3742
3743 verifiedEvent.flags += 1;
3744 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
3745
3746 verifiedEvent.keyCode += 1;
3747 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
3748
3749 verifiedEvent.scanCode += 1;
3750 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
3751
3752 verifiedEvent.metaState += 1;
3753 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
3754
3755 verifiedEvent.repeatCount += 1;
3756 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
3757}
3758
Vishnu Nair958da932020-08-21 17:12:37 -07003759TEST_F(InputDispatcherTest, SetFocusedWindow) {
3760 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3761 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003762 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07003763 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003764 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07003765 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3766
3767 // Top window is also focusable but is not granted focus.
3768 windowTop->setFocusable(true);
3769 windowSecond->setFocusable(true);
3770 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
3771 setFocusedWindow(windowSecond);
3772
3773 windowSecond->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003774 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
3775 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07003776
3777 // Focused window should receive event.
3778 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
3779 windowTop->assertNoEvents();
3780}
3781
3782TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) {
3783 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3784 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003785 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07003786 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3787
3788 window->setFocusable(true);
3789 // Release channel for window is no longer valid.
3790 window->releaseChannel();
3791 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3792 setFocusedWindow(window);
3793
3794 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003795 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
3796 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07003797
3798 // window channel is invalid, so it should not receive any input event.
3799 window->assertNoEvents();
3800}
3801
3802TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) {
3803 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3804 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003805 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08003806 window->setFocusable(false);
Vishnu Nair958da932020-08-21 17:12:37 -07003807 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3808
Vishnu Nair958da932020-08-21 17:12:37 -07003809 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3810 setFocusedWindow(window);
3811
3812 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003813 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
3814 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07003815
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08003816 // window is not focusable, so it should not receive any input event.
Vishnu Nair958da932020-08-21 17:12:37 -07003817 window->assertNoEvents();
3818}
3819
3820TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) {
3821 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3822 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003823 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07003824 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003825 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07003826 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3827
3828 windowTop->setFocusable(true);
3829 windowSecond->setFocusable(true);
3830 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
3831 setFocusedWindow(windowTop);
3832 windowTop->consumeFocusEvent(true);
3833
3834 setFocusedWindow(windowSecond, windowTop);
3835 windowSecond->consumeFocusEvent(true);
3836 windowTop->consumeFocusEvent(false);
3837
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003838 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
3839 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07003840
3841 // Focused window should receive event.
3842 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
3843}
3844
3845TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestFocusTokenNotFocused) {
3846 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3847 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003848 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07003849 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003850 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07003851 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3852
3853 windowTop->setFocusable(true);
3854 windowSecond->setFocusable(true);
3855 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
3856 setFocusedWindow(windowSecond, windowTop);
3857
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003858 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
3859 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07003860
3861 // Event should be dropped.
3862 windowTop->assertNoEvents();
3863 windowSecond->assertNoEvents();
3864}
3865
3866TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) {
3867 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3868 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003869 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07003870 sp<FakeWindowHandle> previousFocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003871 sp<FakeWindowHandle>::make(application, mDispatcher, "previousFocusedWindow",
3872 ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07003873 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3874
3875 window->setFocusable(true);
3876 previousFocusedWindow->setFocusable(true);
3877 window->setVisible(false);
3878 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}});
3879 setFocusedWindow(previousFocusedWindow);
3880 previousFocusedWindow->consumeFocusEvent(true);
3881
3882 // Requesting focus on invisible window takes focus from currently focused window.
3883 setFocusedWindow(window);
3884 previousFocusedWindow->consumeFocusEvent(false);
3885
3886 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003887 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Vishnu Nair958da932020-08-21 17:12:37 -07003888 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003889 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07003890
3891 // Window does not get focus event or key down.
3892 window->assertNoEvents();
3893
3894 // Window becomes visible.
3895 window->setVisible(true);
3896 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3897
3898 // Window receives focus event.
3899 window->consumeFocusEvent(true);
3900 // Focused window receives key down.
3901 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3902}
3903
Vishnu Nair599f1412021-06-21 10:39:58 -07003904TEST_F(InputDispatcherTest, DisplayRemoved) {
3905 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3906 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003907 sp<FakeWindowHandle>::make(application, mDispatcher, "window", ADISPLAY_ID_DEFAULT);
Vishnu Nair599f1412021-06-21 10:39:58 -07003908 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3909
3910 // window is granted focus.
3911 window->setFocusable(true);
3912 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3913 setFocusedWindow(window);
3914 window->consumeFocusEvent(true);
3915
3916 // When a display is removed window loses focus.
3917 mDispatcher->displayRemoved(ADISPLAY_ID_DEFAULT);
3918 window->consumeFocusEvent(false);
3919}
3920
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003921/**
3922 * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY,
3923 * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top
3924 * of the 'slipperyEnterWindow'.
3925 *
3926 * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such
3927 * a way so that the touched location is no longer covered by the top window.
3928 *
3929 * Next, inject a MOVE event. Because the top window already moved earlier, this event is now
3930 * positioned over the bottom (slipperyEnterWindow) only. And because the top window had
3931 * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive
3932 * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting
3933 * with ACTION_DOWN).
3934 * Thus, the touch has been transferred from the top window into the bottom window, because the top
3935 * window moved itself away from the touched location and had Flag::SLIPPERY.
3936 *
3937 * Even though the top window moved away from the touched location, it is still obscuring the bottom
3938 * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_
3939 * OBSCURED should be set for the MotionEvent that reaches the bottom window.
3940 *
3941 * In this test, we ensure that the event received by the bottom window has
3942 * FLAG_WINDOW_IS_PARTIALLY_OBSCURED.
3943 */
3944TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) {
Prabir Pradhan5735a322022-04-11 17:23:34 +00003945 constexpr int32_t SLIPPERY_PID = WINDOW_PID + 1;
3946 constexpr int32_t SLIPPERY_UID = WINDOW_UID + 1;
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003947
3948 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3949 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3950
3951 sp<FakeWindowHandle> slipperyExitWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003952 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08003953 slipperyExitWindow->setSlippery(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003954 // Make sure this one overlaps the bottom window
3955 slipperyExitWindow->setFrame(Rect(25, 25, 75, 75));
3956 // Change the owner uid/pid of the window so that it is considered to be occluding the bottom
3957 // one. Windows with the same owner are not considered to be occluding each other.
3958 slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID);
3959
3960 sp<FakeWindowHandle> slipperyEnterWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003961 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003962 slipperyExitWindow->setFrame(Rect(0, 0, 100, 100));
3963
3964 mDispatcher->setInputWindows(
3965 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
3966
3967 // Use notifyMotion instead of injecting to avoid dealing with injection permissions
3968 NotifyMotionArgs args = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3969 ADISPLAY_ID_DEFAULT, {{50, 50}});
3970 mDispatcher->notifyMotion(&args);
3971 slipperyExitWindow->consumeMotionDown();
3972 slipperyExitWindow->setFrame(Rect(70, 70, 100, 100));
3973 mDispatcher->setInputWindows(
3974 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
3975
3976 args = generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
3977 ADISPLAY_ID_DEFAULT, {{51, 51}});
3978 mDispatcher->notifyMotion(&args);
3979
3980 slipperyExitWindow->consumeMotionCancel();
3981
3982 slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT,
3983 AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
3984}
3985
Garfield Tan1c7bc862020-01-28 13:24:04 -08003986class InputDispatcherKeyRepeatTest : public InputDispatcherTest {
3987protected:
3988 static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms
3989 static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms
3990
Chris Yea209fde2020-07-22 13:54:51 -07003991 std::shared_ptr<FakeApplicationHandle> mApp;
Garfield Tan1c7bc862020-01-28 13:24:04 -08003992 sp<FakeWindowHandle> mWindow;
3993
3994 virtual void SetUp() override {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003995 mFakePolicy = sp<FakeInputDispatcherPolicy>::make();
Garfield Tan1c7bc862020-01-28 13:24:04 -08003996 mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003997 mDispatcher = std::make_unique<InputDispatcher>(mFakePolicy);
Garfield Tan1c7bc862020-01-28 13:24:04 -08003998 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
3999 ASSERT_EQ(OK, mDispatcher->start());
4000
4001 setUpWindow();
4002 }
4003
4004 void setUpWindow() {
Chris Yea209fde2020-07-22 13:54:51 -07004005 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004006 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Garfield Tan1c7bc862020-01-28 13:24:04 -08004007
Vishnu Nair47074b82020-08-14 11:54:47 -07004008 mWindow->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004009 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004010 setFocusedWindow(mWindow);
Garfield Tan1c7bc862020-01-28 13:24:04 -08004011 mWindow->consumeFocusEvent(true);
4012 }
4013
Chris Ye2ad95392020-09-01 13:44:44 -07004014 void sendAndConsumeKeyDown(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08004015 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07004016 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08004017 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event
4018 mDispatcher->notifyKey(&keyArgs);
4019
4020 // Window should receive key down event.
4021 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4022 }
4023
4024 void expectKeyRepeatOnce(int32_t repeatCount) {
4025 SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount));
4026 InputEvent* repeatEvent = mWindow->consume();
4027 ASSERT_NE(nullptr, repeatEvent);
4028
4029 uint32_t eventType = repeatEvent->getType();
4030 ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, eventType);
4031
4032 KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent);
4033 uint32_t eventAction = repeatKeyEvent->getAction();
4034 EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction);
4035 EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount());
4036 }
4037
Chris Ye2ad95392020-09-01 13:44:44 -07004038 void sendAndConsumeKeyUp(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08004039 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07004040 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08004041 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event
4042 mDispatcher->notifyKey(&keyArgs);
4043
4044 // Window should receive key down event.
4045 mWindow->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
4046 0 /*expectedFlags*/);
4047 }
4048};
4049
4050TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) {
Chris Ye2ad95392020-09-01 13:44:44 -07004051 sendAndConsumeKeyDown(1 /* deviceId */);
4052 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
4053 expectKeyRepeatOnce(repeatCount);
4054 }
4055}
4056
4057TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) {
4058 sendAndConsumeKeyDown(1 /* deviceId */);
4059 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
4060 expectKeyRepeatOnce(repeatCount);
4061 }
4062 sendAndConsumeKeyDown(2 /* deviceId */);
4063 /* repeatCount will start from 1 for deviceId 2 */
Garfield Tan1c7bc862020-01-28 13:24:04 -08004064 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
4065 expectKeyRepeatOnce(repeatCount);
4066 }
4067}
4068
4069TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) {
Chris Ye2ad95392020-09-01 13:44:44 -07004070 sendAndConsumeKeyDown(1 /* deviceId */);
Garfield Tan1c7bc862020-01-28 13:24:04 -08004071 expectKeyRepeatOnce(1 /*repeatCount*/);
Chris Ye2ad95392020-09-01 13:44:44 -07004072 sendAndConsumeKeyUp(1 /* deviceId */);
4073 mWindow->assertNoEvents();
4074}
4075
4076TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) {
4077 sendAndConsumeKeyDown(1 /* deviceId */);
4078 expectKeyRepeatOnce(1 /*repeatCount*/);
4079 sendAndConsumeKeyDown(2 /* deviceId */);
4080 expectKeyRepeatOnce(1 /*repeatCount*/);
4081 // Stale key up from device 1.
4082 sendAndConsumeKeyUp(1 /* deviceId */);
4083 // Device 2 is still down, keep repeating
4084 expectKeyRepeatOnce(2 /*repeatCount*/);
4085 expectKeyRepeatOnce(3 /*repeatCount*/);
4086 // Device 2 key up
4087 sendAndConsumeKeyUp(2 /* deviceId */);
4088 mWindow->assertNoEvents();
4089}
4090
4091TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) {
4092 sendAndConsumeKeyDown(1 /* deviceId */);
4093 expectKeyRepeatOnce(1 /*repeatCount*/);
4094 sendAndConsumeKeyDown(2 /* deviceId */);
4095 expectKeyRepeatOnce(1 /*repeatCount*/);
4096 // Device 2 which holds the key repeating goes up, expect the repeating to stop.
4097 sendAndConsumeKeyUp(2 /* deviceId */);
4098 // Device 1 still holds key down, but the repeating was already stopped
Garfield Tan1c7bc862020-01-28 13:24:04 -08004099 mWindow->assertNoEvents();
4100}
4101
liushenxiang42232912021-05-21 20:24:09 +08004102TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) {
4103 sendAndConsumeKeyDown(DEVICE_ID);
4104 expectKeyRepeatOnce(1 /*repeatCount*/);
4105 NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID);
4106 mDispatcher->notifyDeviceReset(&args);
4107 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT,
4108 AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS);
4109 mWindow->assertNoEvents();
4110}
4111
Garfield Tan1c7bc862020-01-28 13:24:04 -08004112TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) {
Chris Ye2ad95392020-09-01 13:44:44 -07004113 sendAndConsumeKeyDown(1 /* deviceId */);
Garfield Tan1c7bc862020-01-28 13:24:04 -08004114 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
4115 InputEvent* repeatEvent = mWindow->consume();
4116 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
4117 EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER,
4118 IdGenerator::getSource(repeatEvent->getId()));
4119 }
4120}
4121
4122TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) {
Chris Ye2ad95392020-09-01 13:44:44 -07004123 sendAndConsumeKeyDown(1 /* deviceId */);
Garfield Tan1c7bc862020-01-28 13:24:04 -08004124
4125 std::unordered_set<int32_t> idSet;
4126 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
4127 InputEvent* repeatEvent = mWindow->consume();
4128 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
4129 int32_t id = repeatEvent->getId();
4130 EXPECT_EQ(idSet.end(), idSet.find(id));
4131 idSet.insert(id);
4132 }
4133}
4134
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004135/* Test InputDispatcher for MultiDisplay */
4136class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest {
4137public:
Prabir Pradhan3608aad2019-10-02 17:08:26 -07004138 virtual void SetUp() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004139 InputDispatcherTest::SetUp();
Arthur Hungb92218b2018-08-14 12:00:21 +08004140
Chris Yea209fde2020-07-22 13:54:51 -07004141 application1 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004142 windowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004143 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08004144
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004145 // Set focus window for primary display, but focused display would be second one.
4146 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1);
Vishnu Nair47074b82020-08-14 11:54:47 -07004147 windowInPrimary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004148 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004149 setFocusedWindow(windowInPrimary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004150 windowInPrimary->consumeFocusEvent(true);
Arthur Hungb92218b2018-08-14 12:00:21 +08004151
Chris Yea209fde2020-07-22 13:54:51 -07004152 application2 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004153 windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004154 sp<FakeWindowHandle>::make(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004155 // Set focus to second display window.
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004156 // Set focus display to second one.
4157 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
4158 // Set focus window for second display.
4159 mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2);
Vishnu Nair47074b82020-08-14 11:54:47 -07004160 windowInSecondary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004161 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004162 setFocusedWindow(windowInSecondary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004163 windowInSecondary->consumeFocusEvent(true);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004164 }
4165
Prabir Pradhan3608aad2019-10-02 17:08:26 -07004166 virtual void TearDown() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004167 InputDispatcherTest::TearDown();
4168
Chris Yea209fde2020-07-22 13:54:51 -07004169 application1.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004170 windowInPrimary.clear();
Chris Yea209fde2020-07-22 13:54:51 -07004171 application2.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004172 windowInSecondary.clear();
4173 }
4174
4175protected:
Chris Yea209fde2020-07-22 13:54:51 -07004176 std::shared_ptr<FakeApplicationHandle> application1;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004177 sp<FakeWindowHandle> windowInPrimary;
Chris Yea209fde2020-07-22 13:54:51 -07004178 std::shared_ptr<FakeApplicationHandle> application2;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004179 sp<FakeWindowHandle> windowInSecondary;
4180};
4181
4182TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) {
4183 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004184 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4185 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
4186 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004187 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08004188 windowInSecondary->assertNoEvents();
4189
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004190 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004191 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4192 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
4193 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08004194 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004195 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +08004196}
4197
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004198TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) {
Tiger Huang721e26f2018-07-24 22:26:19 +08004199 // Test inject a key down with display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08004200 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4201 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004202 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004203 windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Tiger Huang721e26f2018-07-24 22:26:19 +08004204 windowInSecondary->assertNoEvents();
4205
4206 // Test inject a key down without display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08004207 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004208 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08004209 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004210 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hungb92218b2018-08-14 12:00:21 +08004211
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08004212 // Remove all windows in secondary display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00004213 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}});
Arthur Hungb92218b2018-08-14 12:00:21 +08004214
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004215 // Old focus should receive a cancel event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004216 windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE,
4217 AKEY_EVENT_FLAG_CANCELED);
Arthur Hungb92218b2018-08-14 12:00:21 +08004218
4219 // Test inject a key down, should timeout because of no target window.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08004220 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004221 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Arthur Hungb92218b2018-08-14 12:00:21 +08004222 windowInPrimary->assertNoEvents();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004223 windowInSecondary->consumeFocusEvent(false);
Arthur Hungb92218b2018-08-14 12:00:21 +08004224 windowInSecondary->assertNoEvents();
4225}
4226
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004227// Test per-display input monitors for motion event.
4228TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) {
chaviwd1c23182019-12-20 18:44:56 -08004229 FakeMonitorReceiver monitorInPrimary =
4230 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
4231 FakeMonitorReceiver monitorInSecondary =
4232 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004233
4234 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004235 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4236 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
4237 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004238 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08004239 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004240 windowInSecondary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08004241 monitorInSecondary.assertNoEvents();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004242
4243 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004244 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4245 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
4246 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004247 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08004248 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004249 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
chaviwd1c23182019-12-20 18:44:56 -08004250 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004251
4252 // Test inject a non-pointer motion event.
4253 // If specific a display, it will dispatch to the focused window of particular display,
4254 // or it will dispatch to the focused window of focused display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004255 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4256 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE))
4257 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004258 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08004259 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004260 windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08004261 monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004262}
4263
4264// Test per-display input monitors for key event.
4265TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) {
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004266 // Input monitor per display.
chaviwd1c23182019-12-20 18:44:56 -08004267 FakeMonitorReceiver monitorInPrimary =
4268 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
4269 FakeMonitorReceiver monitorInSecondary =
4270 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004271
4272 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004273 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4274 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004275 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08004276 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004277 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08004278 monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004279}
4280
Vishnu Nair958da932020-08-21 17:12:37 -07004281TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) {
4282 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004283 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07004284 secondWindowInPrimary->setFocusable(true);
4285 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}});
4286 setFocusedWindow(secondWindowInPrimary);
4287 windowInPrimary->consumeFocusEvent(false);
4288 secondWindowInPrimary->consumeFocusEvent(true);
4289
4290 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004291 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
4292 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004293 windowInPrimary->assertNoEvents();
4294 windowInSecondary->assertNoEvents();
4295 secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4296}
4297
Arthur Hungdfd528e2021-12-08 13:23:04 +00004298TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CancelTouch_MultiDisplay) {
4299 FakeMonitorReceiver monitorInPrimary =
4300 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
4301 FakeMonitorReceiver monitorInSecondary =
4302 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
4303
4304 // Test touch down on primary display.
4305 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4306 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
4307 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4308 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4309 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
4310
4311 // Test touch down on second display.
4312 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4313 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
4314 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4315 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
4316 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
4317
4318 // Trigger cancel touch.
4319 mDispatcher->cancelCurrentTouch();
4320 windowInPrimary->consumeMotionCancel(ADISPLAY_ID_DEFAULT);
4321 monitorInPrimary.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
4322 windowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID);
4323 monitorInSecondary.consumeMotionCancel(SECOND_DISPLAY_ID);
4324
4325 // Test inject a move motion event, no window/monitor should receive the event.
4326 ASSERT_EQ(InputEventInjectionResult::FAILED,
4327 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4328 ADISPLAY_ID_DEFAULT, {110, 200}))
4329 << "Inject motion event should return InputEventInjectionResult::FAILED";
4330 windowInPrimary->assertNoEvents();
4331 monitorInPrimary.assertNoEvents();
4332
4333 ASSERT_EQ(InputEventInjectionResult::FAILED,
4334 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4335 SECOND_DISPLAY_ID, {110, 200}))
4336 << "Inject motion event should return InputEventInjectionResult::FAILED";
4337 windowInSecondary->assertNoEvents();
4338 monitorInSecondary.assertNoEvents();
4339}
4340
Jackal Guof9696682018-10-05 12:23:23 +08004341class InputFilterTest : public InputDispatcherTest {
4342protected:
Prabir Pradhan81420cc2021-09-06 10:28:50 -07004343 void testNotifyMotion(int32_t displayId, bool expectToBeFiltered,
4344 const ui::Transform& transform = ui::Transform()) {
Jackal Guof9696682018-10-05 12:23:23 +08004345 NotifyMotionArgs motionArgs;
4346
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004347 motionArgs =
4348 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Jackal Guof9696682018-10-05 12:23:23 +08004349 mDispatcher->notifyMotion(&motionArgs);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004350 motionArgs =
4351 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Jackal Guof9696682018-10-05 12:23:23 +08004352 mDispatcher->notifyMotion(&motionArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004353 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08004354 if (expectToBeFiltered) {
Prabir Pradhan81420cc2021-09-06 10:28:50 -07004355 const auto xy = transform.transform(motionArgs.pointerCoords->getXYValue());
4356 mFakePolicy->assertFilterInputEventWasCalled(motionArgs, xy);
Jackal Guof9696682018-10-05 12:23:23 +08004357 } else {
4358 mFakePolicy->assertFilterInputEventWasNotCalled();
4359 }
4360 }
4361
4362 void testNotifyKey(bool expectToBeFiltered) {
4363 NotifyKeyArgs keyArgs;
4364
4365 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
4366 mDispatcher->notifyKey(&keyArgs);
4367 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP);
4368 mDispatcher->notifyKey(&keyArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004369 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08004370
4371 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08004372 mFakePolicy->assertFilterInputEventWasCalled(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08004373 } else {
4374 mFakePolicy->assertFilterInputEventWasNotCalled();
4375 }
4376 }
4377};
4378
4379// Test InputFilter for MotionEvent
4380TEST_F(InputFilterTest, MotionEvent_InputFilter) {
4381 // Since the InputFilter is disabled by default, check if touch events aren't filtered.
4382 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
4383 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
4384
4385 // Enable InputFilter
4386 mDispatcher->setInputFilterEnabled(true);
4387 // Test touch on both primary and second display, and check if both events are filtered.
4388 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true);
4389 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true);
4390
4391 // Disable InputFilter
4392 mDispatcher->setInputFilterEnabled(false);
4393 // Test touch on both primary and second display, and check if both events aren't filtered.
4394 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
4395 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
4396}
4397
4398// Test InputFilter for KeyEvent
4399TEST_F(InputFilterTest, KeyEvent_InputFilter) {
4400 // Since the InputFilter is disabled by default, check if key event aren't filtered.
4401 testNotifyKey(/*expectToBeFiltered*/ false);
4402
4403 // Enable InputFilter
4404 mDispatcher->setInputFilterEnabled(true);
4405 // Send a key event, and check if it is filtered.
4406 testNotifyKey(/*expectToBeFiltered*/ true);
4407
4408 // Disable InputFilter
4409 mDispatcher->setInputFilterEnabled(false);
4410 // Send a key event, and check if it isn't filtered.
4411 testNotifyKey(/*expectToBeFiltered*/ false);
4412}
4413
Prabir Pradhan81420cc2021-09-06 10:28:50 -07004414// Ensure that MotionEvents sent to the InputFilter through InputListener are converted to the
4415// logical display coordinate space.
4416TEST_F(InputFilterTest, MotionEvent_UsesLogicalDisplayCoordinates_notifyMotion) {
4417 ui::Transform firstDisplayTransform;
4418 firstDisplayTransform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
4419 ui::Transform secondDisplayTransform;
4420 secondDisplayTransform.set({-6.6, -5.5, -4.4, -3.3, -2.2, -1.1, 0, 0, 1});
4421
4422 std::vector<gui::DisplayInfo> displayInfos(2);
4423 displayInfos[0].displayId = ADISPLAY_ID_DEFAULT;
4424 displayInfos[0].transform = firstDisplayTransform;
4425 displayInfos[1].displayId = SECOND_DISPLAY_ID;
4426 displayInfos[1].transform = secondDisplayTransform;
4427
4428 mDispatcher->onWindowInfosChanged({}, displayInfos);
4429
4430 // Enable InputFilter
4431 mDispatcher->setInputFilterEnabled(true);
4432
4433 // Ensure the correct transforms are used for the displays.
4434 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true, firstDisplayTransform);
4435 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true, secondDisplayTransform);
4436}
4437
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004438class InputFilterInjectionPolicyTest : public InputDispatcherTest {
4439protected:
4440 virtual void SetUp() override {
4441 InputDispatcherTest::SetUp();
4442
4443 /**
4444 * We don't need to enable input filter to test the injected event policy, but we enabled it
4445 * here to make the tests more realistic, since this policy only matters when inputfilter is
4446 * on.
4447 */
4448 mDispatcher->setInputFilterEnabled(true);
4449
4450 std::shared_ptr<InputApplicationHandle> application =
4451 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004452 mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Test Window",
4453 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004454
4455 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
4456 mWindow->setFocusable(true);
4457 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
4458 setFocusedWindow(mWindow);
4459 mWindow->consumeFocusEvent(true);
4460 }
4461
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00004462 void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
4463 int32_t flags) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004464 KeyEvent event;
4465
4466 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
4467 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD,
4468 ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A,
4469 KEY_A, AMETA_NONE, 0 /*repeatCount*/, eventTime, eventTime);
4470 const int32_t additionalPolicyFlags =
4471 POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT;
4472 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Prabir Pradhan5735a322022-04-11 17:23:34 +00004473 mDispatcher->injectInputEvent(&event, {} /*targetUid*/,
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004474 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
4475 policyFlags | additionalPolicyFlags));
4476
4477 InputEvent* received = mWindow->consume();
4478 ASSERT_NE(nullptr, received);
4479 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00004480 ASSERT_EQ(received->getType(), AINPUT_EVENT_TYPE_KEY);
4481 KeyEvent& keyEvent = static_cast<KeyEvent&>(*received);
4482 ASSERT_EQ(flags, keyEvent.getFlags());
4483 }
4484
4485 void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
4486 int32_t flags) {
4487 MotionEvent event;
4488 PointerProperties pointerProperties[1];
4489 PointerCoords pointerCoords[1];
4490 pointerProperties[0].clear();
4491 pointerProperties[0].id = 0;
4492 pointerCoords[0].clear();
4493 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300);
4494 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400);
4495
4496 ui::Transform identityTransform;
4497 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
4498 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN,
4499 DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0,
4500 AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE,
4501 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -07004502 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, eventTime,
Evan Rosky09576692021-07-01 12:22:09 -07004503 eventTime,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00004504 /*pointerCount*/ 1, pointerProperties, pointerCoords);
4505
4506 const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER;
4507 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Prabir Pradhan5735a322022-04-11 17:23:34 +00004508 mDispatcher->injectInputEvent(&event, {} /*targetUid*/,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00004509 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
4510 policyFlags | additionalPolicyFlags));
4511
4512 InputEvent* received = mWindow->consume();
4513 ASSERT_NE(nullptr, received);
4514 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
4515 ASSERT_EQ(received->getType(), AINPUT_EVENT_TYPE_MOTION);
4516 MotionEvent& motionEvent = static_cast<MotionEvent&>(*received);
4517 ASSERT_EQ(flags, motionEvent.getFlags());
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004518 }
4519
4520private:
4521 sp<FakeWindowHandle> mWindow;
4522};
4523
4524TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) {
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00004525 // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input
4526 // filter. Without it, the event will no different from a regularly injected event, and the
4527 // injected device id will be overwritten.
4528 testInjectedKey(POLICY_FLAG_FILTERED, 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/,
4529 0 /*flags*/);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004530}
4531
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00004532TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004533 testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00004534 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/,
4535 AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
4536}
4537
4538TEST_F(InputFilterInjectionPolicyTest,
4539 MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
4540 testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
4541 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/,
4542 AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004543}
4544
4545TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) {
4546 testInjectedKey(0 /*policyFlags*/, 3 /*injectedDeviceId*/,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00004547 VIRTUAL_KEYBOARD_ID /*resolvedDeviceId*/, 0 /*flags*/);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004548}
4549
chaviwfd6d3512019-03-25 13:23:49 -07004550class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest {
Prabir Pradhan3608aad2019-10-02 17:08:26 -07004551 virtual void SetUp() override {
chaviwfd6d3512019-03-25 13:23:49 -07004552 InputDispatcherTest::SetUp();
4553
Chris Yea209fde2020-07-22 13:54:51 -07004554 std::shared_ptr<FakeApplicationHandle> application =
4555 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004556 mUnfocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004557 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07004558 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
chaviwfd6d3512019-03-25 13:23:49 -07004559
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08004560 mFocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004561 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08004562 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
chaviwfd6d3512019-03-25 13:23:49 -07004563
4564 // Set focused application.
4565 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07004566 mFocusedWindow->setFocusable(true);
chaviwfd6d3512019-03-25 13:23:49 -07004567
4568 // Expect one focus window exist in display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00004569 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004570 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004571 mFocusedWindow->consumeFocusEvent(true);
chaviwfd6d3512019-03-25 13:23:49 -07004572 }
4573
Prabir Pradhan3608aad2019-10-02 17:08:26 -07004574 virtual void TearDown() override {
chaviwfd6d3512019-03-25 13:23:49 -07004575 InputDispatcherTest::TearDown();
4576
4577 mUnfocusedWindow.clear();
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08004578 mFocusedWindow.clear();
chaviwfd6d3512019-03-25 13:23:49 -07004579 }
4580
4581protected:
4582 sp<FakeWindowHandle> mUnfocusedWindow;
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08004583 sp<FakeWindowHandle> mFocusedWindow;
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07004584 static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60};
chaviwfd6d3512019-03-25 13:23:49 -07004585};
4586
4587// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
4588// DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received
4589// the onPointerDownOutsideFocus callback.
4590TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004591 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07004592 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4593 {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004594 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07004595 mUnfocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07004596
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004597 ASSERT_TRUE(mDispatcher->waitForIdle());
chaviwfd6d3512019-03-25 13:23:49 -07004598 mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken());
4599}
4600
4601// Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action
4602// DOWN on the window that doesn't have focus. Ensure no window received the
4603// onPointerDownOutsideFocus callback.
4604TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004605 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07004606 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004607 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07004608 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07004609
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004610 ASSERT_TRUE(mDispatcher->waitForIdle());
4611 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07004612}
4613
4614// Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't
4615// have focus. Ensure no window received the onPointerDownOutsideFocus callback.
4616TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08004617 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4618 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004619 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07004620 mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07004621
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004622 ASSERT_TRUE(mDispatcher->waitForIdle());
4623 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07004624}
4625
4626// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
4627// DOWN on the window that already has focus. Ensure no window received the
4628// onPointerDownOutsideFocus callback.
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004629TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004630 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08004631 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07004632 FOCUSED_WINDOW_TOUCH_POINT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004633 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07004634 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07004635
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004636 ASSERT_TRUE(mDispatcher->waitForIdle());
4637 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07004638}
4639
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08004640// Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag
4641// NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback.
4642TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) {
4643 const MotionEvent event =
4644 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
4645 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
4646 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(20).y(20))
4647 .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE)
4648 .build();
4649 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(mDispatcher, event))
4650 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4651 mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
4652
4653 ASSERT_TRUE(mDispatcher->waitForIdle());
4654 mFakePolicy->assertOnPointerDownWasNotCalled();
4655 // Ensure that the unfocused window did not receive any FOCUS events.
4656 mUnfocusedWindow->assertNoEvents();
4657}
4658
chaviwaf87b3e2019-10-01 16:59:28 -07004659// These tests ensures we can send touch events to a single client when there are multiple input
4660// windows that point to the same client token.
4661class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest {
4662 virtual void SetUp() override {
4663 InputDispatcherTest::SetUp();
4664
Chris Yea209fde2020-07-22 13:54:51 -07004665 std::shared_ptr<FakeApplicationHandle> application =
4666 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004667 mWindow1 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 1",
4668 ADISPLAY_ID_DEFAULT);
chaviwaf87b3e2019-10-01 16:59:28 -07004669 mWindow1->setFrame(Rect(0, 0, 100, 100));
4670
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004671 mWindow2 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 2",
4672 ADISPLAY_ID_DEFAULT, mWindow1->getToken());
chaviwaf87b3e2019-10-01 16:59:28 -07004673 mWindow2->setFrame(Rect(100, 100, 200, 200));
4674
Arthur Hung72d8dc32020-03-28 00:48:39 +00004675 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
chaviwaf87b3e2019-10-01 16:59:28 -07004676 }
4677
4678protected:
4679 sp<FakeWindowHandle> mWindow1;
4680 sp<FakeWindowHandle> mWindow2;
4681
4682 // Helper function to convert the point from screen coordinates into the window's space
chaviw3277faf2021-05-19 16:45:23 -05004683 static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) {
chaviw1ff3d1e2020-07-01 15:53:47 -07004684 vec2 vals = windowInfo->transform.transform(point.x, point.y);
4685 return {vals.x, vals.y};
chaviwaf87b3e2019-10-01 16:59:28 -07004686 }
4687
4688 void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction,
4689 const std::vector<PointF>& points) {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004690 const std::string name = window->getName();
chaviwaf87b3e2019-10-01 16:59:28 -07004691 InputEvent* event = window->consume();
4692
4693 ASSERT_NE(nullptr, event) << name.c_str()
4694 << ": consumer should have returned non-NULL event.";
4695
4696 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType())
4697 << name.c_str() << "expected " << inputEventTypeToString(AINPUT_EVENT_TYPE_MOTION)
4698 << " event, got " << inputEventTypeToString(event->getType()) << " event";
4699
4700 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004701 assertMotionAction(expectedAction, motionEvent.getAction());
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08004702 ASSERT_EQ(points.size(), motionEvent.getPointerCount());
chaviwaf87b3e2019-10-01 16:59:28 -07004703
4704 for (size_t i = 0; i < points.size(); i++) {
4705 float expectedX = points[i].x;
4706 float expectedY = points[i].y;
4707
4708 EXPECT_EQ(expectedX, motionEvent.getX(i))
4709 << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str()
4710 << ", got " << motionEvent.getX(i);
4711 EXPECT_EQ(expectedY, motionEvent.getY(i))
4712 << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str()
4713 << ", got " << motionEvent.getY(i);
4714 }
4715 }
chaviw9eaa22c2020-07-01 16:21:27 -07004716
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08004717 void touchAndAssertPositions(int32_t action, const std::vector<PointF>& touchedPoints,
chaviw9eaa22c2020-07-01 16:21:27 -07004718 std::vector<PointF> expectedPoints) {
4719 NotifyMotionArgs motionArgs = generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN,
4720 ADISPLAY_ID_DEFAULT, touchedPoints);
4721 mDispatcher->notifyMotion(&motionArgs);
4722
4723 // Always consume from window1 since it's the window that has the InputReceiver
4724 consumeMotionEvent(mWindow1, action, expectedPoints);
4725 }
chaviwaf87b3e2019-10-01 16:59:28 -07004726};
4727
4728TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) {
4729 // Touch Window 1
4730 PointF touchedPoint = {10, 10};
4731 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07004732 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07004733
4734 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07004735 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07004736
4737 // Touch Window 2
4738 touchedPoint = {150, 150};
4739 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07004740 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07004741}
4742
chaviw9eaa22c2020-07-01 16:21:27 -07004743TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) {
4744 // Set scale value for window2
chaviwaf87b3e2019-10-01 16:59:28 -07004745 mWindow2->setWindowScale(0.5f, 0.5f);
4746
4747 // Touch Window 1
4748 PointF touchedPoint = {10, 10};
4749 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07004750 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07004751 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07004752 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07004753
4754 // Touch Window 2
4755 touchedPoint = {150, 150};
4756 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07004757 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
4758 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07004759
chaviw9eaa22c2020-07-01 16:21:27 -07004760 // Update the transform so rotation is set
4761 mWindow2->setWindowTransform(0, -1, 1, 0);
4762 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
4763 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07004764}
4765
chaviw9eaa22c2020-07-01 16:21:27 -07004766TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004767 mWindow2->setWindowScale(0.5f, 0.5f);
4768
4769 // Touch Window 1
4770 std::vector<PointF> touchedPoints = {PointF{10, 10}};
4771 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07004772 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004773
4774 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07004775 touchedPoints.push_back(PointF{150, 150});
4776 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08004777 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004778
chaviw9eaa22c2020-07-01 16:21:27 -07004779 // Release Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08004780 touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07004781 expectedPoints.pop_back();
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004782
chaviw9eaa22c2020-07-01 16:21:27 -07004783 // Update the transform so rotation is set for Window 2
4784 mWindow2->setWindowTransform(0, -1, 1, 0);
4785 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08004786 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004787}
4788
chaviw9eaa22c2020-07-01 16:21:27 -07004789TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004790 mWindow2->setWindowScale(0.5f, 0.5f);
4791
4792 // Touch Window 1
4793 std::vector<PointF> touchedPoints = {PointF{10, 10}};
4794 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07004795 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004796
4797 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07004798 touchedPoints.push_back(PointF{150, 150});
4799 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004800
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08004801 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004802
4803 // Move both windows
4804 touchedPoints = {{20, 20}, {175, 175}};
4805 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
4806 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
4807
chaviw9eaa22c2020-07-01 16:21:27 -07004808 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004809
chaviw9eaa22c2020-07-01 16:21:27 -07004810 // Release Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08004811 touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07004812 expectedPoints.pop_back();
4813
4814 // Touch Window 2
4815 mWindow2->setWindowTransform(0, -1, 1, 0);
4816 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08004817 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07004818
4819 // Move both windows
4820 touchedPoints = {{20, 20}, {175, 175}};
4821 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
4822 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
4823
4824 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004825}
4826
4827TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) {
4828 mWindow1->setWindowScale(0.5f, 0.5f);
4829
4830 // Touch Window 1
4831 std::vector<PointF> touchedPoints = {PointF{10, 10}};
4832 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07004833 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004834
4835 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07004836 touchedPoints.push_back(PointF{150, 150});
4837 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004838
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08004839 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004840
4841 // Move both windows
4842 touchedPoints = {{20, 20}, {175, 175}};
4843 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
4844 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
4845
chaviw9eaa22c2020-07-01 16:21:27 -07004846 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004847}
4848
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004849class InputDispatcherSingleWindowAnr : public InputDispatcherTest {
4850 virtual void SetUp() override {
4851 InputDispatcherTest::SetUp();
4852
Chris Yea209fde2020-07-22 13:54:51 -07004853 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004854 mApplication->setDispatchingTimeout(20ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004855 mWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "TestWindow",
4856 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004857 mWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05004858 mWindow->setDispatchingTimeout(30ms);
Vishnu Nair47074b82020-08-14 11:54:47 -07004859 mWindow->setFocusable(true);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004860
4861 // Set focused application.
4862 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
4863
4864 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004865 setFocusedWindow(mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004866 mWindow->consumeFocusEvent(true);
4867 }
4868
4869 virtual void TearDown() override {
4870 InputDispatcherTest::TearDown();
4871 mWindow.clear();
4872 }
4873
4874protected:
Chris Yea209fde2020-07-22 13:54:51 -07004875 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004876 sp<FakeWindowHandle> mWindow;
4877 static constexpr PointF WINDOW_LOCATION = {20, 20};
4878
4879 void tapOnWindow() {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004880 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004881 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4882 WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004883 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004884 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4885 WINDOW_LOCATION));
4886 }
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004887
4888 sp<FakeWindowHandle> addSpyWindow() {
4889 sp<FakeWindowHandle> spy =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004890 sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004891 spy->setTrustedOverlay(true);
4892 spy->setFocusable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08004893 spy->setSpy(true);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004894 spy->setDispatchingTimeout(30ms);
4895 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, mWindow}}});
4896 return spy;
4897 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004898};
4899
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004900// Send a tap and respond, which should not cause an ANR.
4901TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) {
4902 tapOnWindow();
4903 mWindow->consumeMotionDown();
4904 mWindow->consumeMotionUp();
4905 ASSERT_TRUE(mDispatcher->waitForIdle());
4906 mFakePolicy->assertNotifyAnrWasNotCalled();
4907}
4908
4909// Send a regular key and respond, which should not cause an ANR.
4910TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08004911 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004912 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
4913 ASSERT_TRUE(mDispatcher->waitForIdle());
4914 mFakePolicy->assertNotifyAnrWasNotCalled();
4915}
4916
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05004917TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) {
4918 mWindow->setFocusable(false);
4919 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
4920 mWindow->consumeFocusEvent(false);
4921
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004922 InputEventInjectionResult result =
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05004923 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08004924 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/,
4925 false /* allowKeyRepeat */);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004926 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05004927 // Key will not go to window because we have no focused window.
4928 // The 'no focused window' ANR timer should start instead.
4929
4930 // Now, the focused application goes away.
4931 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr);
4932 // The key should get dropped and there should be no ANR.
4933
4934 ASSERT_TRUE(mDispatcher->waitForIdle());
4935 mFakePolicy->assertNotifyAnrWasNotCalled();
4936}
4937
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004938// Send an event to the app and have the app not respond right away.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004939// When ANR is raised, policy will tell the dispatcher to cancel the events for that window.
4940// So InputDispatcher will enqueue ACTION_CANCEL event as well.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004941TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004942 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004943 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4944 WINDOW_LOCATION));
4945
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004946 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN
4947 ASSERT_TRUE(sequenceNum);
4948 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08004949 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004950
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004951 mWindow->finishEvent(*sequenceNum);
4952 mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL,
4953 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004954 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08004955 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004956}
4957
4958// Send a key to the app and have the app not respond right away.
4959TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) {
4960 // Inject a key, and don't respond - expect that ANR is called.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08004961 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004962 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent();
4963 ASSERT_TRUE(sequenceNum);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004964 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08004965 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004966 ASSERT_TRUE(mDispatcher->waitForIdle());
4967}
4968
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004969// We have a focused application, but no focused window
4970TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) {
Vishnu Nair47074b82020-08-14 11:54:47 -07004971 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004972 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
4973 mWindow->consumeFocusEvent(false);
4974
4975 // taps on the window work as normal
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004976 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004977 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4978 WINDOW_LOCATION));
4979 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
4980 mDispatcher->waitForIdle();
4981 mFakePolicy->assertNotifyAnrWasNotCalled();
4982
4983 // Once a focused event arrives, we get an ANR for this application
4984 // We specify the injection timeout to be smaller than the application timeout, to ensure that
4985 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004986 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004987 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08004988 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, false /* allowKeyRepeat */);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004989 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004990 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Vishnu Naire4df8752022-09-08 09:17:55 -07004991 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004992 ASSERT_TRUE(mDispatcher->waitForIdle());
4993}
4994
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08004995/**
4996 * Make sure the stale key is dropped before causing an ANR. So even if there's no focused window,
4997 * there will not be an ANR.
4998 */
4999TEST_F(InputDispatcherSingleWindowAnr, StaleKeyEventDoesNotAnr) {
5000 mWindow->setFocusable(false);
5001 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
5002 mWindow->consumeFocusEvent(false);
5003
5004 KeyEvent event;
5005 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC) -
5006 std::chrono::nanoseconds(STALE_EVENT_TIMEOUT).count();
5007
5008 // Define a valid key down event that is stale (too old).
5009 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
5010 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, /* flags */ 0, AKEYCODE_A, KEY_A,
5011 AMETA_NONE, 1 /*repeatCount*/, eventTime, eventTime);
5012
5013 const int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER;
5014
5015 InputEventInjectionResult result =
Prabir Pradhan5735a322022-04-11 17:23:34 +00005016 mDispatcher->injectInputEvent(&event, {} /* targetUid */,
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08005017 InputEventInjectionSync::WAIT_FOR_RESULT,
5018 INJECT_EVENT_TIMEOUT, policyFlags);
5019 ASSERT_EQ(InputEventInjectionResult::FAILED, result)
5020 << "Injection should fail because the event is stale";
5021
5022 ASSERT_TRUE(mDispatcher->waitForIdle());
5023 mFakePolicy->assertNotifyAnrWasNotCalled();
5024 mWindow->assertNoEvents();
5025}
5026
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005027// We have a focused application, but no focused window
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005028// Make sure that we don't notify policy twice about the same ANR.
5029TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) {
Vishnu Nair47074b82020-08-14 11:54:47 -07005030 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005031 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
5032 mWindow->consumeFocusEvent(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005033
5034 // Once a focused event arrives, we get an ANR for this application
5035 // We specify the injection timeout to be smaller than the application timeout, to ensure that
5036 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005037 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005038 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005039 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, false /* allowKeyRepeat */);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005040 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Vishnu Naire4df8752022-09-08 09:17:55 -07005041 const std::chrono::duration appTimeout =
5042 mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
5043 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005044
Vishnu Naire4df8752022-09-08 09:17:55 -07005045 std::this_thread::sleep_for(appTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005046 // ANR should not be raised again. It is up to policy to do that if it desires.
5047 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005048
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005049 // If we now get a focused window, the ANR should stop, but the policy handles that via
5050 // 'notifyFocusChanged' callback. This is implemented in the policy so we can't test it here.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005051 ASSERT_TRUE(mDispatcher->waitForIdle());
5052}
5053
5054// We have a focused application, but no focused window
5055TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) {
Vishnu Nair47074b82020-08-14 11:54:47 -07005056 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005057 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
5058 mWindow->consumeFocusEvent(false);
5059
5060 // Once a focused event arrives, we get an ANR for this application
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005061 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005062 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005063 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
5064 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005065
Vishnu Naire4df8752022-09-08 09:17:55 -07005066 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
5067 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005068
5069 // Future focused events get dropped right away
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005070 ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005071 ASSERT_TRUE(mDispatcher->waitForIdle());
5072 mWindow->assertNoEvents();
5073}
5074
5075/**
5076 * Ensure that the implementation is valid. Since we are using multiset to keep track of the
5077 * ANR timeouts, we are allowing entries with identical timestamps in the same connection.
5078 * If we process 1 of the events, but ANR on the second event with the same timestamp,
5079 * the ANR mechanism should still work.
5080 *
5081 * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the
5082 * DOWN event, while not responding on the second one.
5083 */
5084TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) {
5085 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
5086 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, 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 // Now send ACTION_UP, with identical timestamp
5093 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
5094 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
5095 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
5096 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005097 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005098
5099 // We have now sent down and up. Let's consume first event and then ANR on the second.
5100 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5101 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005102 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005103}
5104
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005105// A spy window can receive an ANR
5106TEST_F(InputDispatcherSingleWindowAnr, SpyWindowAnr) {
5107 sp<FakeWindowHandle> spy = addSpyWindow();
5108
5109 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5110 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5111 WINDOW_LOCATION));
5112 mWindow->consumeMotionDown();
5113
5114 std::optional<uint32_t> sequenceNum = spy->receiveEvent(); // ACTION_DOWN
5115 ASSERT_TRUE(sequenceNum);
5116 const std::chrono::duration timeout = spy->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005117 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, spy);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005118
5119 spy->finishEvent(*sequenceNum);
5120 spy->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, ADISPLAY_ID_DEFAULT,
5121 0 /*flags*/);
5122 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08005123 mFakePolicy->assertNotifyWindowResponsiveWasCalled(spy->getToken(), mWindow->getPid());
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005124}
5125
5126// If an app is not responding to a key event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005127// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005128TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnKey) {
5129 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005130
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005131 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5132 injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005133 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005134 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005135
5136 // Stuck on the ACTION_UP
5137 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005138 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005139
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005140 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005141 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005142 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5143 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005144
5145 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion
5146 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08005147 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005148 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005149 spy->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005150}
5151
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005152// If an app is not responding to a motion event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005153// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005154TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnMotion) {
5155 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005156
5157 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005158 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5159 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005160
5161 mWindow->consumeMotionDown();
5162 // Stuck on the ACTION_UP
5163 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005164 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005165
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005166 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005167 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005168 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5169 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005170
5171 mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion
5172 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08005173 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005174 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005175 spy->assertNoEvents();
5176}
5177
5178TEST_F(InputDispatcherSingleWindowAnr, UnresponsiveMonitorAnr) {
5179 mDispatcher->setMonitorDispatchingTimeoutForTest(30ms);
5180
5181 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
5182
5183 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5184 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5185 WINDOW_LOCATION));
5186
5187 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5188 const std::optional<uint32_t> consumeSeq = monitor.receiveEvent();
5189 ASSERT_TRUE(consumeSeq);
5190
Prabir Pradhanedd96402022-02-15 01:46:16 -08005191 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(30ms, monitor.getToken(), MONITOR_PID);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005192
5193 monitor.finishEvent(*consumeSeq);
5194 monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
5195
5196 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08005197 mFakePolicy->assertNotifyWindowResponsiveWasCalled(monitor.getToken(), MONITOR_PID);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005198}
5199
5200// If a window is unresponsive, then you get anr. if the window later catches up and starts to
5201// process events, you don't get an anr. When the window later becomes unresponsive again, you
5202// get an ANR again.
5203// 1. tap -> block on ACTION_UP -> receive ANR
5204// 2. consume all pending events (= queue becomes healthy again)
5205// 3. tap again -> block on ACTION_UP again -> receive ANR second time
5206TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) {
5207 tapOnWindow();
5208
5209 mWindow->consumeMotionDown();
5210 // Block on ACTION_UP
5211 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005212 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005213 mWindow->consumeMotionUp(); // Now the connection should be healthy again
5214 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08005215 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005216 mWindow->assertNoEvents();
5217
5218 tapOnWindow();
5219 mWindow->consumeMotionDown();
Prabir Pradhanedd96402022-02-15 01:46:16 -08005220 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005221 mWindow->consumeMotionUp();
5222
5223 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08005224 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005225 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005226 mWindow->assertNoEvents();
5227}
5228
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005229// If a connection remains unresponsive for a while, make sure policy is only notified once about
5230// it.
5231TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005232 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005233 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5234 WINDOW_LOCATION));
5235
5236 const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005237 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005238 std::this_thread::sleep_for(windowTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005239 // 'notifyConnectionUnresponsive' should only be called once per connection
5240 mFakePolicy->assertNotifyAnrWasNotCalled();
5241 // When the ANR happened, dispatcher should abort the current event stream via ACTION_CANCEL
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005242 mWindow->consumeMotionDown();
5243 mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL,
5244 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
5245 mWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005246 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08005247 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005248 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005249}
5250
5251/**
5252 * If a window is processing a motion event, and then a key event comes in, the key event should
5253 * not to to the focused window until the motion is processed.
5254 *
5255 * Warning!!!
5256 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
5257 * and the injection timeout that we specify when injecting the key.
5258 * We must have the injection timeout (10ms) be smaller than
5259 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
5260 *
5261 * If that value changes, this test should also change.
5262 */
5263TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) {
5264 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
5265 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
5266
5267 tapOnWindow();
5268 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
5269 ASSERT_TRUE(downSequenceNum);
5270 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
5271 ASSERT_TRUE(upSequenceNum);
5272 // Don't finish the events yet, and send a key
5273 // Injection will "succeed" because we will eventually give up and send the key to the focused
5274 // window even if motions are still being processed. But because the injection timeout is short,
5275 // we will receive INJECTION_TIMED_OUT as the result.
5276
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005277 InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005278 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005279 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
5280 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005281 // Key will not be sent to the window, yet, because the window is still processing events
5282 // and the key remains pending, waiting for the touch events to be processed
5283 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
5284 ASSERT_FALSE(keySequenceNum);
5285
5286 std::this_thread::sleep_for(500ms);
5287 // if we wait long enough though, dispatcher will give up, and still send the key
5288 // to the focused window, even though we have not yet finished the motion event
5289 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5290 mWindow->finishEvent(*downSequenceNum);
5291 mWindow->finishEvent(*upSequenceNum);
5292}
5293
5294/**
5295 * If a window is processing a motion event, and then a key event comes in, the key event should
5296 * not go to the focused window until the motion is processed.
5297 * If then a new motion comes in, then the pending key event should be going to the currently
5298 * focused window right away.
5299 */
5300TEST_F(InputDispatcherSingleWindowAnr,
5301 PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) {
5302 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
5303 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
5304
5305 tapOnWindow();
5306 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
5307 ASSERT_TRUE(downSequenceNum);
5308 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
5309 ASSERT_TRUE(upSequenceNum);
5310 // Don't finish the events yet, and send a key
5311 // Injection is async, so it will succeed
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005312 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005313 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005314 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005315 // At this point, key is still pending, and should not be sent to the application yet.
5316 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
5317 ASSERT_FALSE(keySequenceNum);
5318
5319 // Now tap down again. It should cause the pending key to go to the focused window right away.
5320 tapOnWindow();
5321 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd
5322 // the other events yet. We can finish events in any order.
5323 mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN
5324 mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP
5325 mWindow->consumeMotionDown();
5326 mWindow->consumeMotionUp();
5327 mWindow->assertNoEvents();
5328}
5329
5330class InputDispatcherMultiWindowAnr : public InputDispatcherTest {
5331 virtual void SetUp() override {
5332 InputDispatcherTest::SetUp();
5333
Chris Yea209fde2020-07-22 13:54:51 -07005334 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005335 mApplication->setDispatchingTimeout(10ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005336 mUnfocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Unfocused",
5337 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005338 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005339 // Adding FLAG_WATCH_OUTSIDE_TOUCH to receive ACTION_OUTSIDE when another window is tapped
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08005340 mUnfocusedWindow->setWatchOutsideTouch(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005341
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005342 mFocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Focused",
5343 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05005344 mFocusedWindow->setDispatchingTimeout(30ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005345 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005346
5347 // Set focused application.
5348 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
Vishnu Nair47074b82020-08-14 11:54:47 -07005349 mFocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005350
5351 // Expect one focus window exist in display.
5352 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005353 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005354 mFocusedWindow->consumeFocusEvent(true);
5355 }
5356
5357 virtual void TearDown() override {
5358 InputDispatcherTest::TearDown();
5359
5360 mUnfocusedWindow.clear();
5361 mFocusedWindow.clear();
5362 }
5363
5364protected:
Chris Yea209fde2020-07-22 13:54:51 -07005365 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005366 sp<FakeWindowHandle> mUnfocusedWindow;
5367 sp<FakeWindowHandle> mFocusedWindow;
5368 static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20};
5369 static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75};
5370 static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40};
5371
5372 void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); }
5373
5374 void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); }
5375
5376private:
5377 void tap(const PointF& location) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005378 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005379 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5380 location));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005381 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005382 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5383 location));
5384 }
5385};
5386
5387// If we have 2 windows that are both unresponsive, the one with the shortest timeout
5388// should be ANR'd first.
5389TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005390 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005391 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5392 FOCUSED_WINDOW_LOCATION))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005393 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005394 mFocusedWindow->consumeMotionDown();
5395 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
5396 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
5397 // We consumed all events, so no ANR
5398 ASSERT_TRUE(mDispatcher->waitForIdle());
5399 mFakePolicy->assertNotifyAnrWasNotCalled();
5400
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005401 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005402 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5403 FOCUSED_WINDOW_LOCATION));
5404 std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent();
5405 ASSERT_TRUE(unfocusedSequenceNum);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005406
5407 const std::chrono::duration timeout =
5408 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005409 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005410 // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event
5411 // sequence to make it consistent
5412 mFocusedWindow->consumeMotionCancel();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005413 mUnfocusedWindow->finishEvent(*unfocusedSequenceNum);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005414 mFocusedWindow->consumeMotionDown();
5415 // This cancel is generated because the connection was unresponsive
5416 mFocusedWindow->consumeMotionCancel();
5417 mFocusedWindow->assertNoEvents();
5418 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005419 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08005420 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
5421 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005422 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005423}
5424
5425// If we have 2 windows with identical timeouts that are both unresponsive,
5426// it doesn't matter which order they should have ANR.
5427// But we should receive ANR for both.
5428TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) {
5429 // Set the timeout for unfocused window to match the focused window
5430 mUnfocusedWindow->setDispatchingTimeout(10ms);
5431 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
5432
5433 tapOnFocusedWindow();
5434 // we should have ACTION_DOWN/ACTION_UP on focused window and ACTION_OUTSIDE on unfocused window
Prabir Pradhanedd96402022-02-15 01:46:16 -08005435 sp<IBinder> anrConnectionToken1, anrConnectionToken2;
5436 ASSERT_NO_FATAL_FAILURE(anrConnectionToken1 = mFakePolicy->getUnresponsiveWindowToken(10ms));
5437 ASSERT_NO_FATAL_FAILURE(anrConnectionToken2 = mFakePolicy->getUnresponsiveWindowToken(0ms));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005438
5439 // We don't know which window will ANR first. But both of them should happen eventually.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005440 ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 ||
5441 mFocusedWindow->getToken() == anrConnectionToken2);
5442 ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 ||
5443 mUnfocusedWindow->getToken() == anrConnectionToken2);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005444
5445 ASSERT_TRUE(mDispatcher->waitForIdle());
5446 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005447
5448 mFocusedWindow->consumeMotionDown();
5449 mFocusedWindow->consumeMotionUp();
5450 mUnfocusedWindow->consumeMotionOutside();
5451
Prabir Pradhanedd96402022-02-15 01:46:16 -08005452 sp<IBinder> responsiveToken1, responsiveToken2;
5453 ASSERT_NO_FATAL_FAILURE(responsiveToken1 = mFakePolicy->getResponsiveWindowToken());
5454 ASSERT_NO_FATAL_FAILURE(responsiveToken2 = mFakePolicy->getResponsiveWindowToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005455
5456 // Both applications should be marked as responsive, in any order
5457 ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 ||
5458 mFocusedWindow->getToken() == responsiveToken2);
5459 ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 ||
5460 mUnfocusedWindow->getToken() == responsiveToken2);
5461 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005462}
5463
5464// If a window is already not responding, the second tap on the same window should be ignored.
5465// We should also log an error to account for the dropped event (not tested here).
5466// At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events.
5467TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) {
5468 tapOnFocusedWindow();
5469 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
5470 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
5471 // Receive the events, but don't respond
5472 std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN
5473 ASSERT_TRUE(downEventSequenceNum);
5474 std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP
5475 ASSERT_TRUE(upEventSequenceNum);
5476 const std::chrono::duration timeout =
5477 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005478 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005479
5480 // Tap once again
5481 // We cannot use "tapOnFocusedWindow" because it asserts the injection result to be success
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005482 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005483 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5484 FOCUSED_WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005485 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005486 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5487 FOCUSED_WINDOW_LOCATION));
5488 // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a
5489 // valid touch target
5490 mUnfocusedWindow->assertNoEvents();
5491
5492 // Consume the first tap
5493 mFocusedWindow->finishEvent(*downEventSequenceNum);
5494 mFocusedWindow->finishEvent(*upEventSequenceNum);
5495 ASSERT_TRUE(mDispatcher->waitForIdle());
5496 // The second tap did not go to the focused window
5497 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005498 // Since all events are finished, connection should be deemed healthy again
Prabir Pradhanedd96402022-02-15 01:46:16 -08005499 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
5500 mFocusedWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005501 mFakePolicy->assertNotifyAnrWasNotCalled();
5502}
5503
5504// If you tap outside of all windows, there will not be ANR
5505TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005506 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005507 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5508 LOCATION_OUTSIDE_ALL_WINDOWS));
5509 ASSERT_TRUE(mDispatcher->waitForIdle());
5510 mFakePolicy->assertNotifyAnrWasNotCalled();
5511}
5512
5513// Since the focused window is paused, tapping on it should not produce any events
5514TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) {
5515 mFocusedWindow->setPaused(true);
5516 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
5517
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005518 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005519 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5520 FOCUSED_WINDOW_LOCATION));
5521
5522 std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT));
5523 ASSERT_TRUE(mDispatcher->waitForIdle());
5524 // Should not ANR because the window is paused, and touches shouldn't go to it
5525 mFakePolicy->assertNotifyAnrWasNotCalled();
5526
5527 mFocusedWindow->assertNoEvents();
5528 mUnfocusedWindow->assertNoEvents();
5529}
5530
5531/**
5532 * If a window is processing a motion event, and then a key event comes in, the key event should
5533 * not to to the focused window until the motion is processed.
5534 * If a different window becomes focused at this time, the key should go to that window instead.
5535 *
5536 * Warning!!!
5537 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
5538 * and the injection timeout that we specify when injecting the key.
5539 * We must have the injection timeout (10ms) be smaller than
5540 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
5541 *
5542 * If that value changes, this test should also change.
5543 */
5544TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) {
5545 // Set a long ANR timeout to prevent it from triggering
5546 mFocusedWindow->setDispatchingTimeout(2s);
5547 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
5548
5549 tapOnUnfocusedWindow();
5550 std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent();
5551 ASSERT_TRUE(downSequenceNum);
5552 std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent();
5553 ASSERT_TRUE(upSequenceNum);
5554 // Don't finish the events yet, and send a key
5555 // Injection will succeed because we will eventually give up and send the key to the focused
5556 // window even if motions are still being processed.
5557
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005558 InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005559 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005560 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/);
5561 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005562 // Key will not be sent to the window, yet, because the window is still processing events
5563 // and the key remains pending, waiting for the touch events to be processed
5564 std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent();
5565 ASSERT_FALSE(keySequenceNum);
5566
5567 // Switch the focus to the "unfocused" window that we tapped. Expect the key to go there
Vishnu Nair47074b82020-08-14 11:54:47 -07005568 mFocusedWindow->setFocusable(false);
5569 mUnfocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005570 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005571 setFocusedWindow(mUnfocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005572
5573 // Focus events should precede the key events
5574 mUnfocusedWindow->consumeFocusEvent(true);
5575 mFocusedWindow->consumeFocusEvent(false);
5576
5577 // Finish the tap events, which should unblock dispatcher
5578 mUnfocusedWindow->finishEvent(*downSequenceNum);
5579 mUnfocusedWindow->finishEvent(*upSequenceNum);
5580
5581 // Now that all queues are cleared and no backlog in the connections, the key event
5582 // can finally go to the newly focused "mUnfocusedWindow".
5583 mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5584 mFocusedWindow->assertNoEvents();
5585 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005586 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005587}
5588
5589// When the touch stream is split across 2 windows, and one of them does not respond,
5590// then ANR should be raised and the touch should be canceled for the unresponsive window.
5591// The other window should not be affected by that.
5592TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) {
5593 // Touch Window 1
5594 NotifyMotionArgs motionArgs =
5595 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5596 ADISPLAY_ID_DEFAULT, {FOCUSED_WINDOW_LOCATION});
5597 mDispatcher->notifyMotion(&motionArgs);
5598 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
5599 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
5600
5601 // Touch Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08005602 motionArgs = generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5603 {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005604 mDispatcher->notifyMotion(&motionArgs);
5605
5606 const std::chrono::duration timeout =
5607 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08005608 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005609
5610 mUnfocusedWindow->consumeMotionDown();
5611 mFocusedWindow->consumeMotionDown();
5612 // Focused window may or may not receive ACTION_MOVE
5613 // But it should definitely receive ACTION_CANCEL due to the ANR
5614 InputEvent* event;
5615 std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event);
5616 ASSERT_TRUE(moveOrCancelSequenceNum);
5617 mFocusedWindow->finishEvent(*moveOrCancelSequenceNum);
5618 ASSERT_NE(nullptr, event);
5619 ASSERT_EQ(event->getType(), AINPUT_EVENT_TYPE_MOTION);
5620 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
5621 if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) {
5622 mFocusedWindow->consumeMotionCancel();
5623 } else {
5624 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction());
5625 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005626 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08005627 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
5628 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005629
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005630 mUnfocusedWindow->assertNoEvents();
5631 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005632 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005633}
5634
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05005635/**
5636 * If we have no focused window, and a key comes in, we start the ANR timer.
5637 * The focused application should add a focused window before the timer runs out to prevent ANR.
5638 *
5639 * If the user touches another application during this time, the key should be dropped.
5640 * Next, if a new focused window comes in, without toggling the focused application,
5641 * then no ANR should occur.
5642 *
5643 * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication',
5644 * but in some cases the policy may not update the focused application.
5645 */
5646TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) {
5647 std::shared_ptr<FakeApplicationHandle> focusedApplication =
5648 std::make_shared<FakeApplicationHandle>();
5649 focusedApplication->setDispatchingTimeout(60ms);
5650 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication);
5651 // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused.
5652 mFocusedWindow->setFocusable(false);
5653
5654 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
5655 mFocusedWindow->consumeFocusEvent(false);
5656
5657 // Send a key. The ANR timer should start because there is no focused window.
5658 // 'focusedApplication' will get blamed if this timer completes.
5659 // Key will not be sent anywhere because we have no focused window. It will remain pending.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005660 InputEventInjectionResult result =
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05005661 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005662 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/,
5663 false /* allowKeyRepeat */);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005664 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05005665
5666 // Wait until dispatcher starts the "no focused window" timer. If we don't wait here,
5667 // then the injected touches won't cause the focused event to get dropped.
5668 // The dispatcher only checks for whether the queue should be pruned upon queueing.
5669 // If we inject the touch right away and the ANR timer hasn't started, the touch event would
5670 // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'.
5671 // For this test, it means that the key would get delivered to the window once it becomes
5672 // focused.
5673 std::this_thread::sleep_for(10ms);
5674
5675 // Touch unfocused window. This should force the pending key to get dropped.
5676 NotifyMotionArgs motionArgs =
5677 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5678 ADISPLAY_ID_DEFAULT, {UNFOCUSED_WINDOW_LOCATION});
5679 mDispatcher->notifyMotion(&motionArgs);
5680
5681 // We do not consume the motion right away, because that would require dispatcher to first
5682 // process (== drop) the key event, and by that time, ANR will be raised.
5683 // Set the focused window first.
5684 mFocusedWindow->setFocusable(true);
5685 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
5686 setFocusedWindow(mFocusedWindow);
5687 mFocusedWindow->consumeFocusEvent(true);
5688 // We do not call "setFocusedApplication" here, even though the newly focused window belongs
5689 // to another application. This could be a bug / behaviour in the policy.
5690
5691 mUnfocusedWindow->consumeMotionDown();
5692
5693 ASSERT_TRUE(mDispatcher->waitForIdle());
5694 // Should not ANR because we actually have a focused window. It was just added too slowly.
5695 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled());
5696}
5697
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05005698// These tests ensure we cannot send touch events to a window that's positioned behind a window
5699// that has feature NO_INPUT_CHANNEL.
5700// Layout:
5701// Top (closest to user)
5702// mNoInputWindow (above all windows)
5703// mBottomWindow
5704// Bottom (furthest from user)
5705class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest {
5706 virtual void SetUp() override {
5707 InputDispatcherTest::SetUp();
5708
5709 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005710 mNoInputWindow =
5711 sp<FakeWindowHandle>::make(mApplication, mDispatcher,
5712 "Window without input channel", ADISPLAY_ID_DEFAULT,
5713 std::make_optional<sp<IBinder>>(nullptr) /*token*/);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08005714 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05005715 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
5716 // It's perfectly valid for this window to not have an associated input channel
5717
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005718 mBottomWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Bottom window",
5719 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05005720 mBottomWindow->setFrame(Rect(0, 0, 100, 100));
5721
5722 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
5723 }
5724
5725protected:
5726 std::shared_ptr<FakeApplicationHandle> mApplication;
5727 sp<FakeWindowHandle> mNoInputWindow;
5728 sp<FakeWindowHandle> mBottomWindow;
5729};
5730
5731TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) {
5732 PointF touchedPoint = {10, 10};
5733
5734 NotifyMotionArgs motionArgs =
5735 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5736 ADISPLAY_ID_DEFAULT, {touchedPoint});
5737 mDispatcher->notifyMotion(&motionArgs);
5738
5739 mNoInputWindow->assertNoEvents();
5740 // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have
5741 // an input channel, it is not marked as FLAG_NOT_TOUCHABLE,
5742 // and therefore should prevent mBottomWindow from receiving touches
5743 mBottomWindow->assertNoEvents();
5744}
5745
5746/**
5747 * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel,
5748 * ensure that this window does not receive any touches, and blocks touches to windows underneath.
5749 */
5750TEST_F(InputDispatcherMultiWindowOcclusionTests,
5751 NoInputChannelFeature_DropsTouchesWithValidChannel) {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005752 mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher,
5753 "Window with input channel and NO_INPUT_CHANNEL",
5754 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05005755
Prabir Pradhan51e7db02022-02-07 06:02:57 -08005756 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05005757 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
5758 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
5759
5760 PointF touchedPoint = {10, 10};
5761
5762 NotifyMotionArgs motionArgs =
5763 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5764 ADISPLAY_ID_DEFAULT, {touchedPoint});
5765 mDispatcher->notifyMotion(&motionArgs);
5766
5767 mNoInputWindow->assertNoEvents();
5768 mBottomWindow->assertNoEvents();
5769}
5770
Vishnu Nair958da932020-08-21 17:12:37 -07005771class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest {
5772protected:
5773 std::shared_ptr<FakeApplicationHandle> mApp;
5774 sp<FakeWindowHandle> mWindow;
5775 sp<FakeWindowHandle> mMirror;
5776
5777 virtual void SetUp() override {
5778 InputDispatcherTest::SetUp();
5779 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005780 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
5781 mMirror = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindowMirror",
5782 ADISPLAY_ID_DEFAULT, mWindow->getToken());
Vishnu Nair958da932020-08-21 17:12:37 -07005783 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
5784 mWindow->setFocusable(true);
5785 mMirror->setFocusable(true);
5786 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
5787 }
5788};
5789
5790TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) {
5791 // Request focus on a mirrored window
5792 setFocusedWindow(mMirror);
5793
5794 // window gets focused
5795 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005796 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5797 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005798 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
5799}
5800
5801// A focused & mirrored window remains focused only if the window and its mirror are both
5802// focusable.
5803TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) {
5804 setFocusedWindow(mMirror);
5805
5806 // window gets focused
5807 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005808 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5809 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005810 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005811 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
5812 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005813 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
5814
5815 mMirror->setFocusable(false);
5816 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
5817
5818 // window loses focus since one of the windows associated with the token in not focusable
5819 mWindow->consumeFocusEvent(false);
5820
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005821 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
5822 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07005823 mWindow->assertNoEvents();
5824}
5825
5826// A focused & mirrored window remains focused until the window and its mirror both become
5827// invisible.
5828TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) {
5829 setFocusedWindow(mMirror);
5830
5831 // window gets focused
5832 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005833 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5834 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005835 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005836 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
5837 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005838 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
5839
5840 mMirror->setVisible(false);
5841 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
5842
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005843 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5844 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005845 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005846 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
5847 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005848 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
5849
5850 mWindow->setVisible(false);
5851 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
5852
5853 // window loses focus only after all windows associated with the token become invisible.
5854 mWindow->consumeFocusEvent(false);
5855
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005856 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
5857 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07005858 mWindow->assertNoEvents();
5859}
5860
5861// A focused & mirrored window remains focused until both windows are removed.
5862TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) {
5863 setFocusedWindow(mMirror);
5864
5865 // window gets focused
5866 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005867 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5868 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005869 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005870 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
5871 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005872 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
5873
5874 // single window is removed but the window token remains focused
5875 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}});
5876
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005877 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5878 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005879 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005880 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
5881 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005882 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
5883
5884 // Both windows are removed
5885 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
5886 mWindow->consumeFocusEvent(false);
5887
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005888 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
5889 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07005890 mWindow->assertNoEvents();
5891}
5892
5893// Focus request can be pending until one window becomes visible.
5894TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) {
5895 // Request focus on an invisible mirror.
5896 mWindow->setVisible(false);
5897 mMirror->setVisible(false);
5898 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
5899 setFocusedWindow(mMirror);
5900
5901 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005902 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Vishnu Nair958da932020-08-21 17:12:37 -07005903 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005904 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07005905
5906 mMirror->setVisible(true);
5907 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
5908
5909 // window gets focused
5910 mWindow->consumeFocusEvent(true);
5911 // window gets the pending key event
5912 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5913}
Prabir Pradhan99987712020-11-10 18:43:05 -08005914
5915class InputDispatcherPointerCaptureTests : public InputDispatcherTest {
5916protected:
5917 std::shared_ptr<FakeApplicationHandle> mApp;
5918 sp<FakeWindowHandle> mWindow;
5919 sp<FakeWindowHandle> mSecondWindow;
5920
5921 void SetUp() override {
5922 InputDispatcherTest::SetUp();
5923 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005924 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -08005925 mWindow->setFocusable(true);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005926 mSecondWindow =
5927 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -08005928 mSecondWindow->setFocusable(true);
5929
5930 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
5931 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}});
5932
5933 setFocusedWindow(mWindow);
5934 mWindow->consumeFocusEvent(true);
5935 }
5936
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00005937 void notifyPointerCaptureChanged(const PointerCaptureRequest& request) {
5938 const NotifyPointerCaptureChangedArgs args = generatePointerCaptureChangedArgs(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08005939 mDispatcher->notifyPointerCaptureChanged(&args);
5940 }
5941
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00005942 PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window,
5943 bool enabled) {
Prabir Pradhan99987712020-11-10 18:43:05 -08005944 mDispatcher->requestPointerCapture(window->getToken(), enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00005945 auto request = mFakePolicy->assertSetPointerCaptureCalled(enabled);
5946 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08005947 window->consumeCaptureEvent(enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00005948 return request;
Prabir Pradhan99987712020-11-10 18:43:05 -08005949 }
5950};
5951
5952TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) {
5953 // Ensure that capture cannot be obtained for unfocused windows.
5954 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
5955 mFakePolicy->assertSetPointerCaptureNotCalled();
5956 mSecondWindow->assertNoEvents();
5957
5958 // Ensure that capture can be enabled from the focus window.
5959 requestAndVerifyPointerCapture(mWindow, true);
5960
5961 // Ensure that capture cannot be disabled from a window that does not have capture.
5962 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false);
5963 mFakePolicy->assertSetPointerCaptureNotCalled();
5964
5965 // Ensure that capture can be disabled from the window with capture.
5966 requestAndVerifyPointerCapture(mWindow, false);
5967}
5968
5969TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00005970 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08005971
5972 setFocusedWindow(mSecondWindow);
5973
5974 // Ensure that the capture disabled event was sent first.
5975 mWindow->consumeCaptureEvent(false);
5976 mWindow->consumeFocusEvent(false);
5977 mSecondWindow->consumeFocusEvent(true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00005978 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08005979
5980 // Ensure that additional state changes from InputReader are not sent to the window.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00005981 notifyPointerCaptureChanged({});
5982 notifyPointerCaptureChanged(request);
5983 notifyPointerCaptureChanged({});
Prabir Pradhan99987712020-11-10 18:43:05 -08005984 mWindow->assertNoEvents();
5985 mSecondWindow->assertNoEvents();
5986 mFakePolicy->assertSetPointerCaptureNotCalled();
5987}
5988
5989TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00005990 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08005991
5992 // InputReader unexpectedly disables and enables pointer capture.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00005993 notifyPointerCaptureChanged({});
5994 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08005995
5996 // Ensure that Pointer Capture is disabled.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00005997 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08005998 mWindow->consumeCaptureEvent(false);
5999 mWindow->assertNoEvents();
6000}
6001
Prabir Pradhan167e6d92021-02-04 16:18:17 -08006002TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) {
6003 requestAndVerifyPointerCapture(mWindow, true);
6004
6005 // The first window loses focus.
6006 setFocusedWindow(mSecondWindow);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006007 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08006008 mWindow->consumeCaptureEvent(false);
6009
6010 // Request Pointer Capture from the second window before the notification from InputReader
6011 // arrives.
6012 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006013 auto request = mFakePolicy->assertSetPointerCaptureCalled(true);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08006014
6015 // InputReader notifies Pointer Capture was disabled (because of the focus change).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006016 notifyPointerCaptureChanged({});
Prabir Pradhan167e6d92021-02-04 16:18:17 -08006017
6018 // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006019 notifyPointerCaptureChanged(request);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08006020
6021 mSecondWindow->consumeFocusEvent(true);
6022 mSecondWindow->consumeCaptureEvent(true);
6023}
6024
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006025TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) {
6026 // App repeatedly enables and disables capture.
6027 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
6028 auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
6029 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
6030 mFakePolicy->assertSetPointerCaptureCalled(false);
6031 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
6032 auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
6033
6034 // InputReader notifies that PointerCapture has been enabled for the first request. Since the
6035 // first request is now stale, this should do nothing.
6036 notifyPointerCaptureChanged(firstRequest);
6037 mWindow->assertNoEvents();
6038
6039 // InputReader notifies that the second request was enabled.
6040 notifyPointerCaptureChanged(secondRequest);
6041 mWindow->consumeCaptureEvent(true);
6042}
6043
Prabir Pradhan7092e262022-05-03 16:51:09 +00006044TEST_F(InputDispatcherPointerCaptureTests, RapidToggleRequests) {
6045 requestAndVerifyPointerCapture(mWindow, true);
6046
6047 // App toggles pointer capture off and on.
6048 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
6049 mFakePolicy->assertSetPointerCaptureCalled(false);
6050
6051 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
6052 auto enableRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
6053
6054 // InputReader notifies that the latest "enable" request was processed, while skipping over the
6055 // preceding "disable" request.
6056 notifyPointerCaptureChanged(enableRequest);
6057
6058 // Since pointer capture was never disabled during the rapid toggle, the window does not receive
6059 // any notifications.
6060 mWindow->assertNoEvents();
6061}
6062
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006063class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest {
6064protected:
6065 constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8;
Bernardo Rufino7393d172021-02-26 13:56:11 +00006066
6067 constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9;
6068 static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY);
6069
6070 constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7;
6071 static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
6072
6073 // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold
6074 constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5;
6075 static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
6076 static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) <
6077 MAXIMUM_OBSCURING_OPACITY);
6078
Bernardo Rufino6d52e542021-02-15 18:38:10 +00006079 static const int32_t TOUCHED_APP_UID = 10001;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006080 static const int32_t APP_B_UID = 10002;
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006081 static const int32_t APP_C_UID = 10003;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006082
6083 sp<FakeWindowHandle> mTouchWindow;
6084
6085 virtual void SetUp() override {
6086 InputDispatcherTest::SetUp();
Bernardo Rufino6d52e542021-02-15 18:38:10 +00006087 mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched");
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006088 mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY);
6089 }
6090
6091 virtual void TearDown() override {
6092 InputDispatcherTest::TearDown();
6093 mTouchWindow.clear();
6094 }
6095
chaviw3277faf2021-05-19 16:45:23 -05006096 sp<FakeWindowHandle> getOccludingWindow(int32_t uid, std::string name, TouchOcclusionMode mode,
6097 float alpha = 1.0f) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006098 sp<FakeWindowHandle> window = getWindow(uid, name);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08006099 window->setTouchable(false);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006100 window->setTouchOcclusionMode(mode);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006101 window->setAlpha(alpha);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006102 return window;
6103 }
6104
6105 sp<FakeWindowHandle> getWindow(int32_t uid, std::string name) {
6106 std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>();
6107 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006108 sp<FakeWindowHandle>::make(app, mDispatcher, name, ADISPLAY_ID_DEFAULT);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006109 // Generate an arbitrary PID based on the UID
6110 window->setOwnerInfo(1777 + (uid % 10000), uid);
6111 return window;
6112 }
6113
6114 void touch(const std::vector<PointF>& points = {PointF{100, 200}}) {
6115 NotifyMotionArgs args =
6116 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6117 ADISPLAY_ID_DEFAULT, points);
6118 mDispatcher->notifyMotion(&args);
6119 }
6120};
6121
6122TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006123 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006124 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006125 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006126
6127 touch();
6128
6129 mTouchWindow->assertNoEvents();
6130}
6131
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00006132TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufino7393d172021-02-26 13:56:11 +00006133 WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) {
6134 const sp<FakeWindowHandle>& w =
6135 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f);
6136 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6137
6138 touch();
6139
6140 mTouchWindow->assertNoEvents();
6141}
6142
6143TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00006144 WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) {
6145 const sp<FakeWindowHandle>& w =
6146 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
6147 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6148
6149 touch();
6150
6151 w->assertNoEvents();
6152}
6153
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006154TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006155 const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW);
6156 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006157
6158 touch();
6159
6160 mTouchWindow->consumeAnyMotionDown();
6161}
6162
6163TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006164 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006165 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006166 w->setFrame(Rect(0, 0, 50, 50));
6167 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006168
6169 touch({PointF{100, 100}});
6170
6171 mTouchWindow->consumeAnyMotionDown();
6172}
6173
6174TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006175 const sp<FakeWindowHandle>& w =
Bernardo Rufino6d52e542021-02-15 18:38:10 +00006176 getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006177 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6178
6179 touch();
6180
6181 mTouchWindow->consumeAnyMotionDown();
6182}
6183
6184TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) {
6185 const sp<FakeWindowHandle>& w =
6186 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
6187 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006188
6189 touch();
6190
6191 mTouchWindow->consumeAnyMotionDown();
6192}
6193
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00006194TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) {
6195 const sp<FakeWindowHandle>& w =
6196 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
6197 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6198
6199 touch();
6200
6201 w->assertNoEvents();
6202}
6203
6204/**
6205 * This is important to make sure apps can't indirectly learn the position of touches (outside vs
6206 * inside) while letting them pass-through. Note that even though touch passes through the occluding
6207 * window, the occluding window will still receive ACTION_OUTSIDE event.
6208 */
6209TEST_F(InputDispatcherUntrustedTouchesTest,
6210 WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) {
6211 const sp<FakeWindowHandle>& w =
6212 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08006213 w->setWatchOutsideTouch(true);
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00006214 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6215
6216 touch();
6217
6218 w->consumeMotionOutside();
6219}
6220
6221TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) {
6222 const sp<FakeWindowHandle>& w =
6223 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08006224 w->setWatchOutsideTouch(true);
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00006225 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6226
6227 touch();
6228
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08006229 w->consumeMotionOutsideWithZeroedCoords();
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00006230}
6231
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006232TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006233 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006234 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
6235 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006236 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6237
6238 touch();
6239
6240 mTouchWindow->consumeAnyMotionDown();
6241}
6242
6243TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) {
6244 const sp<FakeWindowHandle>& w =
6245 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
6246 MAXIMUM_OBSCURING_OPACITY);
6247 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006248
6249 touch();
6250
6251 mTouchWindow->consumeAnyMotionDown();
6252}
6253
6254TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006255 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006256 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
6257 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006258 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6259
6260 touch();
6261
6262 mTouchWindow->assertNoEvents();
6263}
6264
6265TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) {
6266 // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91
6267 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006268 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
6269 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006270 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006271 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
6272 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006273 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
6274
6275 touch();
6276
6277 mTouchWindow->assertNoEvents();
6278}
6279
6280TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) {
6281 // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75
6282 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006283 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
6284 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006285 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006286 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
6287 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006288 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
6289
6290 touch();
6291
6292 mTouchWindow->consumeAnyMotionDown();
6293}
6294
6295TEST_F(InputDispatcherUntrustedTouchesTest,
6296 WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) {
6297 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006298 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
6299 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006300 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006301 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
6302 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006303 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
6304
6305 touch();
6306
6307 mTouchWindow->consumeAnyMotionDown();
6308}
6309
6310TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) {
6311 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006312 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
6313 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006314 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006315 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
6316 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00006317 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00006318
6319 touch();
6320
6321 mTouchWindow->assertNoEvents();
6322}
6323
Bernardo Rufino6d52e542021-02-15 18:38:10 +00006324TEST_F(InputDispatcherUntrustedTouchesTest,
6325 WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) {
6326 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006327 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
6328 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00006329 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006330 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
6331 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00006332 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
6333
6334 touch();
6335
6336 mTouchWindow->assertNoEvents();
6337}
6338
6339TEST_F(InputDispatcherUntrustedTouchesTest,
6340 WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) {
6341 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006342 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
6343 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00006344 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006345 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
6346 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00006347 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
6348
6349 touch();
6350
6351 mTouchWindow->consumeAnyMotionDown();
6352}
6353
6354TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) {
6355 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006356 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
6357 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00006358 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6359
6360 touch();
6361
6362 mTouchWindow->consumeAnyMotionDown();
6363}
6364
6365TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) {
6366 const sp<FakeWindowHandle>& w =
6367 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED);
6368 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6369
6370 touch();
6371
6372 mTouchWindow->consumeAnyMotionDown();
6373}
6374
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00006375TEST_F(InputDispatcherUntrustedTouchesTest,
6376 OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) {
6377 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
6378 const sp<FakeWindowHandle>& w =
6379 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f);
6380 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6381
6382 touch();
6383
6384 mTouchWindow->assertNoEvents();
6385}
6386
6387TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) {
6388 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
6389 const sp<FakeWindowHandle>& w =
6390 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f);
6391 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6392
6393 touch();
6394
6395 mTouchWindow->consumeAnyMotionDown();
6396}
6397
6398TEST_F(InputDispatcherUntrustedTouchesTest,
6399 OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) {
6400 mDispatcher->setMaximumObscuringOpacityForTouch(1.0f);
6401 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00006402 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
6403 OPACITY_ABOVE_THRESHOLD);
6404 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6405
6406 touch();
6407
6408 mTouchWindow->consumeAnyMotionDown();
6409}
6410
6411TEST_F(InputDispatcherUntrustedTouchesTest,
6412 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) {
6413 const sp<FakeWindowHandle>& w1 =
6414 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
6415 OPACITY_BELOW_THRESHOLD);
6416 const sp<FakeWindowHandle>& w2 =
6417 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
6418 OPACITY_BELOW_THRESHOLD);
6419 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
6420
6421 touch();
6422
6423 mTouchWindow->assertNoEvents();
6424}
6425
6426/**
6427 * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the
6428 * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold
6429 * (which alone would result in allowing touches) does not affect the blocking behavior.
6430 */
6431TEST_F(InputDispatcherUntrustedTouchesTest,
6432 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) {
6433 const sp<FakeWindowHandle>& wB =
6434 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
6435 OPACITY_BELOW_THRESHOLD);
6436 const sp<FakeWindowHandle>& wC =
6437 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
6438 OPACITY_BELOW_THRESHOLD);
6439 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
6440
6441 touch();
6442
6443 mTouchWindow->assertNoEvents();
6444}
6445
6446/**
6447 * This test is testing that a window from a different UID but with same application token doesn't
6448 * block the touch. Apps can share the application token for close UI collaboration for example.
6449 */
6450TEST_F(InputDispatcherUntrustedTouchesTest,
6451 WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) {
6452 const sp<FakeWindowHandle>& w =
6453 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
6454 w->setApplicationToken(mTouchWindow->getApplicationToken());
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00006455 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
6456
6457 touch();
6458
6459 mTouchWindow->consumeAnyMotionDown();
6460}
6461
arthurhungb89ccb02020-12-30 16:19:01 +08006462class InputDispatcherDragTests : public InputDispatcherTest {
6463protected:
6464 std::shared_ptr<FakeApplicationHandle> mApp;
6465 sp<FakeWindowHandle> mWindow;
6466 sp<FakeWindowHandle> mSecondWindow;
6467 sp<FakeWindowHandle> mDragWindow;
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00006468 sp<FakeWindowHandle> mSpyWindow;
Arthur Hungb75c2aa2022-07-15 09:35:36 +00006469 // Mouse would force no-split, set the id as non-zero to verify if drag state could track it.
6470 static constexpr int32_t MOUSE_POINTER_ID = 1;
arthurhungb89ccb02020-12-30 16:19:01 +08006471
6472 void SetUp() override {
6473 InputDispatcherTest::SetUp();
6474 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006475 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +08006476 mWindow->setFrame(Rect(0, 0, 100, 100));
arthurhungb89ccb02020-12-30 16:19:01 +08006477
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006478 mSecondWindow =
6479 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +08006480 mSecondWindow->setFrame(Rect(100, 0, 200, 100));
arthurhungb89ccb02020-12-30 16:19:01 +08006481
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006482 mSpyWindow =
6483 sp<FakeWindowHandle>::make(mApp, mDispatcher, "SpyWindow", ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00006484 mSpyWindow->setSpy(true);
6485 mSpyWindow->setTrustedOverlay(true);
6486 mSpyWindow->setFrame(Rect(0, 0, 200, 100));
6487
arthurhungb89ccb02020-12-30 16:19:01 +08006488 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00006489 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mSpyWindow, mWindow, mSecondWindow}}});
arthurhungb89ccb02020-12-30 16:19:01 +08006490 }
6491
Arthur Hungb75c2aa2022-07-15 09:35:36 +00006492 void injectDown(int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
6493 switch (fromSource) {
6494 case AINPUT_SOURCE_TOUCHSCREEN:
6495 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6496 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
6497 ADISPLAY_ID_DEFAULT, {50, 50}))
6498 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6499 break;
6500 case AINPUT_SOURCE_STYLUS:
6501 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6502 injectMotionEvent(
6503 mDispatcher,
6504 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
6505 AINPUT_SOURCE_STYLUS)
6506 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
6507 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS)
6508 .x(50)
6509 .y(50))
6510 .build()));
6511 break;
6512 case AINPUT_SOURCE_MOUSE:
6513 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6514 injectMotionEvent(
6515 mDispatcher,
6516 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
6517 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
6518 .pointer(PointerBuilder(MOUSE_POINTER_ID,
6519 AMOTION_EVENT_TOOL_TYPE_MOUSE)
6520 .x(50)
6521 .y(50))
6522 .build()));
6523 break;
6524 default:
6525 FAIL() << "Source " << fromSource << " doesn't support drag and drop";
6526 }
arthurhungb89ccb02020-12-30 16:19:01 +08006527
6528 // Window should receive motion event.
6529 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00006530 // Spy window should also receive motion event
6531 mSpyWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung54745652022-04-20 07:17:41 +00006532 }
6533
6534 // Start performing drag, we will create a drag window and transfer touch to it.
6535 // @param sendDown : if true, send a motion down on first window before perform drag and drop.
6536 // Returns true on success.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00006537 bool startDrag(bool sendDown = true, int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
Arthur Hung54745652022-04-20 07:17:41 +00006538 if (sendDown) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00006539 injectDown(fromSource);
Arthur Hung54745652022-04-20 07:17:41 +00006540 }
arthurhungb89ccb02020-12-30 16:19:01 +08006541
6542 // The drag window covers the entire display
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006543 mDragWindow =
6544 sp<FakeWindowHandle>::make(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00006545 mDragWindow->setTouchableRegion(Region{{0, 0, 0, 0}});
arthurhungb89ccb02020-12-30 16:19:01 +08006546 mDispatcher->setInputWindows(
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00006547 {{ADISPLAY_ID_DEFAULT, {mDragWindow, mSpyWindow, mWindow, mSecondWindow}}});
arthurhungb89ccb02020-12-30 16:19:01 +08006548
6549 // Transfer touch focus to the drag window
Arthur Hung54745652022-04-20 07:17:41 +00006550 bool transferred =
6551 mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(),
6552 true /* isDragDrop */);
6553 if (transferred) {
6554 mWindow->consumeMotionCancel();
6555 mDragWindow->consumeMotionDown();
6556 }
6557 return transferred;
arthurhungb89ccb02020-12-30 16:19:01 +08006558 }
6559};
6560
6561TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00006562 startDrag();
arthurhungb89ccb02020-12-30 16:19:01 +08006563
6564 // Move on window.
6565 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6566 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6567 ADISPLAY_ID_DEFAULT, {50, 50}))
6568 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6569 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
6570 mWindow->consumeDragEvent(false, 50, 50);
6571 mSecondWindow->assertNoEvents();
6572
6573 // Move to another window.
6574 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6575 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6576 ADISPLAY_ID_DEFAULT, {150, 50}))
6577 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6578 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
6579 mWindow->consumeDragEvent(true, 150, 50);
6580 mSecondWindow->consumeDragEvent(false, 50, 50);
6581
6582 // Move back to original window.
6583 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6584 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6585 ADISPLAY_ID_DEFAULT, {50, 50}))
6586 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6587 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
6588 mWindow->consumeDragEvent(false, 50, 50);
6589 mSecondWindow->consumeDragEvent(true, -50, 50);
6590
6591 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6592 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {50, 50}))
6593 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6594 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
6595 mWindow->assertNoEvents();
6596 mSecondWindow->assertNoEvents();
6597}
6598
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00006599TEST_F(InputDispatcherDragTests, DragEnterAndPointerDownPilfersPointers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00006600 startDrag();
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00006601
6602 // No cancel event after drag start
6603 mSpyWindow->assertNoEvents();
6604
6605 const MotionEvent secondFingerDownEvent =
6606 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
6607 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
6608 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
6609 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(60).y(60))
6610 .build();
6611 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6612 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
6613 InputEventInjectionSync::WAIT_FOR_RESULT))
6614 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6615
6616 // Receives cancel for first pointer after next pointer down
6617 mSpyWindow->consumeMotionCancel();
6618 mSpyWindow->consumeMotionDown();
6619
6620 mSpyWindow->assertNoEvents();
6621}
6622
arthurhungf452d0b2021-01-06 00:19:52 +08006623TEST_F(InputDispatcherDragTests, DragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00006624 startDrag();
arthurhungf452d0b2021-01-06 00:19:52 +08006625
6626 // Move on window.
6627 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6628 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6629 ADISPLAY_ID_DEFAULT, {50, 50}))
6630 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6631 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
6632 mWindow->consumeDragEvent(false, 50, 50);
6633 mSecondWindow->assertNoEvents();
6634
6635 // Move to another window.
6636 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6637 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6638 ADISPLAY_ID_DEFAULT, {150, 50}))
6639 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6640 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
6641 mWindow->consumeDragEvent(true, 150, 50);
6642 mSecondWindow->consumeDragEvent(false, 50, 50);
6643
6644 // drop to another window.
6645 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6646 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6647 {150, 50}))
6648 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6649 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
6650 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
6651 mWindow->assertNoEvents();
6652 mSecondWindow->assertNoEvents();
6653}
6654
arthurhung6d4bed92021-03-17 11:59:33 +08006655TEST_F(InputDispatcherDragTests, StylusDragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00006656 startDrag(true, AINPUT_SOURCE_STYLUS);
arthurhung6d4bed92021-03-17 11:59:33 +08006657
6658 // Move on window and keep button pressed.
6659 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6660 injectMotionEvent(mDispatcher,
6661 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
6662 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
6663 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS)
6664 .x(50)
6665 .y(50))
6666 .build()))
6667 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6668 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
6669 mWindow->consumeDragEvent(false, 50, 50);
6670 mSecondWindow->assertNoEvents();
6671
6672 // Move to another window and release button, expect to drop item.
6673 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6674 injectMotionEvent(mDispatcher,
6675 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
6676 .buttonState(0)
6677 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS)
6678 .x(150)
6679 .y(50))
6680 .build()))
6681 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6682 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
6683 mWindow->assertNoEvents();
6684 mSecondWindow->assertNoEvents();
6685 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
6686
6687 // nothing to the window.
6688 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6689 injectMotionEvent(mDispatcher,
6690 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS)
6691 .buttonState(0)
6692 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS)
6693 .x(150)
6694 .y(50))
6695 .build()))
6696 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6697 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
6698 mWindow->assertNoEvents();
6699 mSecondWindow->assertNoEvents();
6700}
6701
Arthur Hung54745652022-04-20 07:17:41 +00006702TEST_F(InputDispatcherDragTests, DragAndDropOnInvalidWindow) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00006703 startDrag();
Arthur Hung6d0571e2021-04-09 20:18:16 +08006704
6705 // Set second window invisible.
6706 mSecondWindow->setVisible(false);
6707 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}});
6708
6709 // Move on window.
6710 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6711 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6712 ADISPLAY_ID_DEFAULT, {50, 50}))
6713 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6714 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
6715 mWindow->consumeDragEvent(false, 50, 50);
6716 mSecondWindow->assertNoEvents();
6717
6718 // Move to another window.
6719 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6720 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6721 ADISPLAY_ID_DEFAULT, {150, 50}))
6722 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6723 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
6724 mWindow->consumeDragEvent(true, 150, 50);
6725 mSecondWindow->assertNoEvents();
6726
6727 // drop to another window.
6728 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6729 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6730 {150, 50}))
6731 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6732 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
6733 mFakePolicy->assertDropTargetEquals(nullptr);
6734 mWindow->assertNoEvents();
6735 mSecondWindow->assertNoEvents();
6736}
6737
Arthur Hung54745652022-04-20 07:17:41 +00006738TEST_F(InputDispatcherDragTests, NoDragAndDropWhenMultiFingers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00006739 // Ensure window could track pointerIds if it didn't support split touch.
6740 mWindow->setPreventSplitting(true);
6741
Arthur Hung54745652022-04-20 07:17:41 +00006742 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6743 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6744 {50, 50}))
6745 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6746 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6747
6748 const MotionEvent secondFingerDownEvent =
6749 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
6750 .displayId(ADISPLAY_ID_DEFAULT)
6751 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
6752 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
6753 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(75).y(50))
6754 .build();
6755 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6756 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
6757 InputEventInjectionSync::WAIT_FOR_RESULT))
6758 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6759 mWindow->consumeMotionPointerDown(1 /* pointerIndex */);
6760
6761 // Should not perform drag and drop when window has multi fingers.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00006762 ASSERT_FALSE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +00006763}
6764
6765TEST_F(InputDispatcherDragTests, DragAndDropWhenSplitTouch) {
6766 // First down on second window.
6767 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6768 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6769 {150, 50}))
6770 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6771
6772 mSecondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6773
6774 // Second down on first window.
6775 const MotionEvent secondFingerDownEvent =
6776 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
6777 .displayId(ADISPLAY_ID_DEFAULT)
6778 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
6779 .pointer(
6780 PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50))
6781 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
6782 .build();
6783 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6784 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
6785 InputEventInjectionSync::WAIT_FOR_RESULT))
6786 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6787 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6788
6789 // Perform drag and drop from first window.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00006790 ASSERT_TRUE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +00006791
6792 // Move on window.
6793 const MotionEvent secondFingerMoveEvent =
6794 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
6795 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
6796 .pointer(
6797 PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50))
6798 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
6799 .build();
6800 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6801 injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT,
6802 InputEventInjectionSync::WAIT_FOR_RESULT));
6803 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
6804 mWindow->consumeDragEvent(false, 50, 50);
6805 mSecondWindow->consumeMotionMove();
6806
6807 // Release the drag pointer should perform drop.
6808 const MotionEvent secondFingerUpEvent =
6809 MotionEventBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
6810 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
6811 .pointer(
6812 PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50))
6813 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
6814 .build();
6815 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6816 injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT,
6817 InputEventInjectionSync::WAIT_FOR_RESULT));
6818 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
6819 mFakePolicy->assertDropTargetEquals(mWindow->getToken());
6820 mWindow->assertNoEvents();
6821 mSecondWindow->consumeMotionMove();
6822}
6823
Arthur Hung3915c1f2022-05-31 07:17:17 +00006824TEST_F(InputDispatcherDragTests, DragAndDropWhenMultiDisplays) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00006825 startDrag();
Arthur Hung3915c1f2022-05-31 07:17:17 +00006826
6827 // Update window of second display.
6828 sp<FakeWindowHandle> windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006829 sp<FakeWindowHandle>::make(mApp, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung3915c1f2022-05-31 07:17:17 +00006830 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
6831
6832 // Let second display has a touch state.
6833 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6834 injectMotionEvent(mDispatcher,
6835 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
6836 AINPUT_SOURCE_TOUCHSCREEN)
6837 .displayId(SECOND_DISPLAY_ID)
6838 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER)
6839 .x(100)
6840 .y(100))
6841 .build()));
6842 windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN,
6843 SECOND_DISPLAY_ID, 0 /* expectedFlag */);
6844 // Update window again.
6845 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
6846
6847 // Move on window.
6848 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6849 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6850 ADISPLAY_ID_DEFAULT, {50, 50}))
6851 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6852 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
6853 mWindow->consumeDragEvent(false, 50, 50);
6854 mSecondWindow->assertNoEvents();
6855
6856 // Move to another window.
6857 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6858 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6859 ADISPLAY_ID_DEFAULT, {150, 50}))
6860 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6861 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
6862 mWindow->consumeDragEvent(true, 150, 50);
6863 mSecondWindow->consumeDragEvent(false, 50, 50);
6864
6865 // drop to another window.
6866 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6867 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6868 {150, 50}))
6869 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6870 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
6871 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
6872 mWindow->assertNoEvents();
6873 mSecondWindow->assertNoEvents();
6874}
6875
Arthur Hungb75c2aa2022-07-15 09:35:36 +00006876TEST_F(InputDispatcherDragTests, MouseDragAndDrop) {
6877 startDrag(true, AINPUT_SOURCE_MOUSE);
6878 // Move on window.
6879 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6880 injectMotionEvent(mDispatcher,
6881 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
6882 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
6883 .pointer(PointerBuilder(MOUSE_POINTER_ID,
6884 AMOTION_EVENT_TOOL_TYPE_MOUSE)
6885 .x(50)
6886 .y(50))
6887 .build()))
6888 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6889 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
6890 mWindow->consumeDragEvent(false, 50, 50);
6891 mSecondWindow->assertNoEvents();
6892
6893 // Move to another window.
6894 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6895 injectMotionEvent(mDispatcher,
6896 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
6897 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
6898 .pointer(PointerBuilder(MOUSE_POINTER_ID,
6899 AMOTION_EVENT_TOOL_TYPE_MOUSE)
6900 .x(150)
6901 .y(50))
6902 .build()))
6903 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6904 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
6905 mWindow->consumeDragEvent(true, 150, 50);
6906 mSecondWindow->consumeDragEvent(false, 50, 50);
6907
6908 // drop to another window.
6909 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6910 injectMotionEvent(mDispatcher,
6911 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
6912 .buttonState(0)
6913 .pointer(PointerBuilder(MOUSE_POINTER_ID,
6914 AMOTION_EVENT_TOOL_TYPE_MOUSE)
6915 .x(150)
6916 .y(50))
6917 .build()))
6918 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6919 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
6920 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
6921 mWindow->assertNoEvents();
6922 mSecondWindow->assertNoEvents();
6923}
6924
Vishnu Nair062a8672021-09-03 16:07:44 -07006925class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {};
6926
6927TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) {
6928 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006929 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
6930 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08006931 window->setDropInput(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07006932 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
6933 window->setFocusable(true);
6934 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
6935 setFocusedWindow(window);
6936 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
6937
6938 // With the flag set, window should not get any input
6939 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
6940 mDispatcher->notifyKey(&keyArgs);
6941 window->assertNoEvents();
6942
6943 NotifyMotionArgs motionArgs =
6944 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6945 ADISPLAY_ID_DEFAULT);
6946 mDispatcher->notifyMotion(&motionArgs);
6947 window->assertNoEvents();
6948
6949 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -08006950 window->setDropInput(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07006951 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
6952
6953 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
6954 mDispatcher->notifyKey(&keyArgs);
6955 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
6956
6957 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6958 ADISPLAY_ID_DEFAULT);
6959 mDispatcher->notifyMotion(&motionArgs);
6960 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6961 window->assertNoEvents();
6962}
6963
6964TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) {
6965 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
6966 std::make_shared<FakeApplicationHandle>();
6967 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006968 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
6969 ADISPLAY_ID_DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -07006970 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
6971 obscuringWindow->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08006972 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07006973 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006974 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
6975 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08006976 window->setDropInputIfObscured(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07006977 window->setOwnerInfo(222, 222);
6978 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
6979 window->setFocusable(true);
6980 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
6981 setFocusedWindow(window);
6982 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
6983
6984 // With the flag set, window should not get any input
6985 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
6986 mDispatcher->notifyKey(&keyArgs);
6987 window->assertNoEvents();
6988
6989 NotifyMotionArgs motionArgs =
6990 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6991 ADISPLAY_ID_DEFAULT);
6992 mDispatcher->notifyMotion(&motionArgs);
6993 window->assertNoEvents();
6994
6995 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -08006996 window->setDropInputIfObscured(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07006997 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
6998
6999 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
7000 mDispatcher->notifyKey(&keyArgs);
7001 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
7002
7003 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
7004 ADISPLAY_ID_DEFAULT);
7005 mDispatcher->notifyMotion(&motionArgs);
7006 window->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
7007 window->assertNoEvents();
7008}
7009
7010TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) {
7011 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
7012 std::make_shared<FakeApplicationHandle>();
7013 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007014 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
7015 ADISPLAY_ID_DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -07007016 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
7017 obscuringWindow->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007018 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07007019 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007020 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
7021 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007022 window->setDropInputIfObscured(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07007023 window->setOwnerInfo(222, 222);
7024 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
7025 window->setFocusable(true);
7026 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
7027 setFocusedWindow(window);
7028 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
7029
7030 // With the flag set, window should not get any input
7031 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
7032 mDispatcher->notifyKey(&keyArgs);
7033 window->assertNoEvents();
7034
7035 NotifyMotionArgs motionArgs =
7036 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
7037 ADISPLAY_ID_DEFAULT);
7038 mDispatcher->notifyMotion(&motionArgs);
7039 window->assertNoEvents();
7040
7041 // When the window is no longer obscured because it went on top, it should get input
7042 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, obscuringWindow}}});
7043
7044 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
7045 mDispatcher->notifyKey(&keyArgs);
7046 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
7047
7048 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
7049 ADISPLAY_ID_DEFAULT);
7050 mDispatcher->notifyMotion(&motionArgs);
7051 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7052 window->assertNoEvents();
7053}
7054
Antonio Kantekf16f2832021-09-28 04:39:20 +00007055class InputDispatcherTouchModeChangedTests : public InputDispatcherTest {
7056protected:
7057 std::shared_ptr<FakeApplicationHandle> mApp;
Antonio Kantek15beb512022-06-13 22:35:41 +00007058 std::shared_ptr<FakeApplicationHandle> mSecondaryApp;
Antonio Kantekf16f2832021-09-28 04:39:20 +00007059 sp<FakeWindowHandle> mWindow;
7060 sp<FakeWindowHandle> mSecondWindow;
Antonio Kantek15beb512022-06-13 22:35:41 +00007061 sp<FakeWindowHandle> mThirdWindow;
Antonio Kantekf16f2832021-09-28 04:39:20 +00007062
7063 void SetUp() override {
7064 InputDispatcherTest::SetUp();
7065
7066 mApp = std::make_shared<FakeApplicationHandle>();
Antonio Kantek15beb512022-06-13 22:35:41 +00007067 mSecondaryApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007068 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00007069 mWindow->setFocusable(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00007070 setFocusedWindow(mWindow);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007071 mSecondWindow =
7072 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00007073 mSecondWindow->setFocusable(true);
Antonio Kantek15beb512022-06-13 22:35:41 +00007074 mThirdWindow =
7075 sp<FakeWindowHandle>::make(mSecondaryApp, mDispatcher,
7076 "TestWindow3_SecondaryDisplay", SECOND_DISPLAY_ID);
7077 mThirdWindow->setFocusable(true);
Antonio Kantekf16f2832021-09-28 04:39:20 +00007078
7079 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Antonio Kantek15beb512022-06-13 22:35:41 +00007080 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}},
7081 {SECOND_DISPLAY_ID, {mThirdWindow}}});
7082 mThirdWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
Antonio Kantekf16f2832021-09-28 04:39:20 +00007083 mWindow->consumeFocusEvent(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00007084
Antonio Kantek15beb512022-06-13 22:35:41 +00007085 // Set main display initial touch mode to InputDispatcher::kDefaultInTouchMode.
Prabir Pradhan5735a322022-04-11 17:23:34 +00007086 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, WINDOW_PID,
Antonio Kantek15beb512022-06-13 22:35:41 +00007087 WINDOW_UID, true /* hasPermission */,
7088 ADISPLAY_ID_DEFAULT)) {
Antonio Kantek48710e42022-03-24 14:19:30 -07007089 mWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
7090 mSecondWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +00007091 mThirdWindow->assertNoEvents();
7092 }
7093
7094 // Set secondary display initial touch mode to InputDispatcher::kDefaultInTouchMode.
7095 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, SECONDARY_WINDOW_PID,
7096 SECONDARY_WINDOW_UID, true /* hasPermission */,
7097 SECOND_DISPLAY_ID)) {
7098 mWindow->assertNoEvents();
7099 mSecondWindow->assertNoEvents();
7100 mThirdWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek48710e42022-03-24 14:19:30 -07007101 }
Antonio Kantekf16f2832021-09-28 04:39:20 +00007102 }
7103
Antonio Kantek15beb512022-06-13 22:35:41 +00007104 void changeAndVerifyTouchModeInMainDisplayOnly(bool inTouchMode, int32_t pid, int32_t uid,
7105 bool hasPermission) {
Antonio Kanteka042c022022-07-06 16:51:07 -07007106 ASSERT_TRUE(mDispatcher->setInTouchMode(inTouchMode, pid, uid, hasPermission,
7107 ADISPLAY_ID_DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +00007108 mWindow->consumeTouchModeEvent(inTouchMode);
7109 mSecondWindow->consumeTouchModeEvent(inTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +00007110 mThirdWindow->assertNoEvents();
Antonio Kantekf16f2832021-09-28 04:39:20 +00007111 }
7112};
7113
Antonio Kantek26defcf2022-02-08 01:12:27 +00007114TEST_F(InputDispatcherTouchModeChangedTests, FocusedWindowCanChangeTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -08007115 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek15beb512022-06-13 22:35:41 +00007116 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode,
7117 windowInfo.ownerPid, windowInfo.ownerUid,
7118 false /* hasPermission */);
Antonio Kantekf16f2832021-09-28 04:39:20 +00007119}
7120
Antonio Kantek26defcf2022-02-08 01:12:27 +00007121TEST_F(InputDispatcherTouchModeChangedTests, NonFocusedWindowOwnerCannotChangeTouchMode) {
7122 const WindowInfo& windowInfo = *mWindow->getInfo();
7123 int32_t ownerPid = windowInfo.ownerPid;
7124 int32_t ownerUid = windowInfo.ownerUid;
7125 mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1);
7126 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, ownerPid,
Antonio Kanteka042c022022-07-06 16:51:07 -07007127 ownerUid, false /*hasPermission*/,
7128 ADISPLAY_ID_DEFAULT));
Antonio Kantek26defcf2022-02-08 01:12:27 +00007129 mWindow->assertNoEvents();
7130 mSecondWindow->assertNoEvents();
7131}
7132
7133TEST_F(InputDispatcherTouchModeChangedTests, NonWindowOwnerMayChangeTouchModeOnPermissionGranted) {
7134 const WindowInfo& windowInfo = *mWindow->getInfo();
7135 int32_t ownerPid = windowInfo.ownerPid;
7136 int32_t ownerUid = windowInfo.ownerUid;
7137 mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1);
Antonio Kantek15beb512022-06-13 22:35:41 +00007138 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, ownerPid,
7139 ownerUid, true /*hasPermission*/);
Antonio Kantek26defcf2022-02-08 01:12:27 +00007140}
7141
Antonio Kantekf16f2832021-09-28 04:39:20 +00007142TEST_F(InputDispatcherTouchModeChangedTests, EventIsNotGeneratedIfNotChangingTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -08007143 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek26defcf2022-02-08 01:12:27 +00007144 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode,
7145 windowInfo.ownerPid, windowInfo.ownerUid,
Antonio Kanteka042c022022-07-06 16:51:07 -07007146 true /*hasPermission*/, ADISPLAY_ID_DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +00007147 mWindow->assertNoEvents();
7148 mSecondWindow->assertNoEvents();
7149}
7150
Antonio Kantek15beb512022-06-13 22:35:41 +00007151TEST_F(InputDispatcherTouchModeChangedTests, ChangeTouchOnSecondaryDisplayOnly) {
7152 const WindowInfo& windowInfo = *mThirdWindow->getInfo();
7153 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
7154 windowInfo.ownerPid, windowInfo.ownerUid,
7155 true /*hasPermission*/, SECOND_DISPLAY_ID));
7156 mWindow->assertNoEvents();
7157 mSecondWindow->assertNoEvents();
7158 mThirdWindow->consumeTouchModeEvent(!InputDispatcher::kDefaultInTouchMode);
7159}
7160
Antonio Kantek48710e42022-03-24 14:19:30 -07007161TEST_F(InputDispatcherTouchModeChangedTests, CanChangeTouchModeWhenOwningLastInteractedWindow) {
7162 // Interact with the window first.
7163 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
7164 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
7165 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
7166
7167 // Then remove focus.
7168 mWindow->setFocusable(false);
7169 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
7170
7171 // Assert that caller can switch touch mode by owning one of the last interacted window.
7172 const WindowInfo& windowInfo = *mWindow->getInfo();
7173 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
7174 windowInfo.ownerPid, windowInfo.ownerUid,
Antonio Kanteka042c022022-07-06 16:51:07 -07007175 false /*hasPermission*/, ADISPLAY_ID_DEFAULT));
Antonio Kantek48710e42022-03-24 14:19:30 -07007176}
7177
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007178class InputDispatcherSpyWindowTest : public InputDispatcherTest {
7179public:
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007180 sp<FakeWindowHandle> createSpy() {
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007181 std::shared_ptr<FakeApplicationHandle> application =
7182 std::make_shared<FakeApplicationHandle>();
7183 std::string name = "Fake Spy ";
7184 name += std::to_string(mSpyCount++);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007185 sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher,
7186 name.c_str(), ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007187 spy->setSpy(true);
Prabir Pradhan5c85e052021-12-22 02:27:12 -08007188 spy->setTrustedOverlay(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007189 return spy;
7190 }
7191
7192 sp<FakeWindowHandle> createForeground() {
7193 std::shared_ptr<FakeApplicationHandle> application =
7194 std::make_shared<FakeApplicationHandle>();
7195 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007196 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
7197 ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007198 window->setFocusable(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007199 return window;
7200 }
7201
7202private:
7203 int mSpyCount{0};
7204};
7205
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08007206using InputDispatcherSpyWindowDeathTest = InputDispatcherSpyWindowTest;
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007207/**
Prabir Pradhan5c85e052021-12-22 02:27:12 -08007208 * Adding a spy window that is not a trusted overlay causes Dispatcher to abort.
7209 */
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08007210TEST_F(InputDispatcherSpyWindowDeathTest, UntrustedSpy_AbortsDispatcher) {
7211 ScopedSilentDeath _silentDeath;
7212
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007213 auto spy = createSpy();
Prabir Pradhan5c85e052021-12-22 02:27:12 -08007214 spy->setTrustedOverlay(false);
7215 ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}}),
7216 ".* not a trusted overlay");
7217}
7218
7219/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007220 * Input injection into a display with a spy window but no foreground windows should succeed.
7221 */
7222TEST_F(InputDispatcherSpyWindowTest, NoForegroundWindow) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007223 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007224 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}});
7225
7226 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7227 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
7228 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7229 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7230}
7231
7232/**
7233 * Verify the order in which different input windows receive events. The touched foreground window
7234 * (if there is one) should always receive the event first. When there are multiple spy windows, the
7235 * spy windows will receive the event according to their Z-order, where the top-most spy window will
7236 * receive events before ones belows it.
7237 *
7238 * Here, we set up a scenario with four windows in the following Z order from the top:
7239 * spy1, spy2, window, spy3.
7240 * We then inject an event and verify that the foreground "window" receives it first, followed by
7241 * "spy1" and "spy2". The "spy3" does not receive the event because it is underneath the foreground
7242 * window.
7243 */
7244TEST_F(InputDispatcherSpyWindowTest, ReceivesInputInOrder) {
7245 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007246 auto spy1 = createSpy();
7247 auto spy2 = createSpy();
7248 auto spy3 = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007249 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window, spy3}}});
7250 const std::vector<sp<FakeWindowHandle>> channels{spy1, spy2, window, spy3};
7251 const size_t numChannels = channels.size();
7252
Michael Wright8e9a8562022-02-09 13:44:29 +00007253 base::unique_fd epollFd(epoll_create1(EPOLL_CLOEXEC));
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007254 if (!epollFd.ok()) {
7255 FAIL() << "Failed to create epoll fd";
7256 }
7257
7258 for (size_t i = 0; i < numChannels; i++) {
7259 struct epoll_event event = {.events = EPOLLIN, .data.u64 = i};
7260 if (epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, channels[i]->getChannelFd(), &event) < 0) {
7261 FAIL() << "Failed to add fd to epoll";
7262 }
7263 }
7264
7265 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7266 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
7267 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7268
7269 std::vector<size_t> eventOrder;
7270 std::vector<struct epoll_event> events(numChannels);
7271 for (;;) {
7272 const int nFds = epoll_wait(epollFd.get(), events.data(), static_cast<int>(numChannels),
7273 (100ms).count());
7274 if (nFds < 0) {
7275 FAIL() << "Failed to call epoll_wait";
7276 }
7277 if (nFds == 0) {
7278 break; // epoll_wait timed out
7279 }
7280 for (int i = 0; i < nFds; i++) {
Colin Cross5b799302022-10-18 21:52:41 -07007281 ASSERT_EQ(static_cast<uint32_t>(EPOLLIN), events[i].events);
Siarhei Vishniakou31977182022-09-30 08:51:23 -07007282 eventOrder.push_back(static_cast<size_t>(events[i].data.u64));
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007283 channels[i]->consumeMotionDown();
7284 }
7285 }
7286
7287 // Verify the order in which the events were received.
7288 EXPECT_EQ(3u, eventOrder.size());
7289 EXPECT_EQ(2u, eventOrder[0]); // index 2: window
7290 EXPECT_EQ(0u, eventOrder[1]); // index 0: spy1
7291 EXPECT_EQ(1u, eventOrder[2]); // index 1: spy2
7292}
7293
7294/**
7295 * A spy window using the NOT_TOUCHABLE flag does not receive events.
7296 */
7297TEST_F(InputDispatcherSpyWindowTest, NotTouchable) {
7298 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007299 auto spy = createSpy();
7300 spy->setTouchable(false);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007301 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
7302
7303 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7304 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
7305 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7306 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7307 spy->assertNoEvents();
7308}
7309
7310/**
7311 * A spy window will only receive gestures that originate within its touchable region. Gestures that
7312 * have their ACTION_DOWN outside of the touchable region of the spy window will not be dispatched
7313 * to the window.
7314 */
7315TEST_F(InputDispatcherSpyWindowTest, TouchableRegion) {
7316 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007317 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007318 spy->setTouchableRegion(Region{{0, 0, 20, 20}});
7319 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
7320
7321 // Inject an event outside the spy window's touchable region.
7322 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7323 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
7324 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7325 window->consumeMotionDown();
7326 spy->assertNoEvents();
7327 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7328 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
7329 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7330 window->consumeMotionUp();
7331 spy->assertNoEvents();
7332
7333 // Inject an event inside the spy window's touchable region.
7334 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7335 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7336 {5, 10}))
7337 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7338 window->consumeMotionDown();
7339 spy->consumeMotionDown();
7340}
7341
7342/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007343 * A spy window can listen for touches outside its touchable region using the WATCH_OUTSIDE_TOUCHES
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08007344 * flag, but it will get zero-ed out coordinates if the foreground has a different owner.
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007345 */
7346TEST_F(InputDispatcherSpyWindowTest, WatchOutsideTouches) {
7347 auto window = createForeground();
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08007348 window->setOwnerInfo(12, 34);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007349 auto spy = createSpy();
7350 spy->setWatchOutsideTouch(true);
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08007351 spy->setOwnerInfo(56, 78);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007352 spy->setFrame(Rect{0, 0, 20, 20});
7353 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
7354
7355 // Inject an event outside the spy window's frame and touchable region.
7356 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08007357 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7358 {100, 200}))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007359 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7360 window->consumeMotionDown();
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08007361 spy->consumeMotionOutsideWithZeroedCoords();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007362}
7363
7364/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007365 * Even when a spy window spans over multiple foreground windows, the spy should receive all
7366 * pointers that are down within its bounds.
7367 */
7368TEST_F(InputDispatcherSpyWindowTest, ReceivesMultiplePointers) {
7369 auto windowLeft = createForeground();
7370 windowLeft->setFrame({0, 0, 100, 200});
7371 auto windowRight = createForeground();
7372 windowRight->setFrame({100, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007373 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007374 spy->setFrame({0, 0, 200, 200});
7375 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, windowLeft, windowRight}}});
7376
7377 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7378 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7379 {50, 50}))
7380 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7381 windowLeft->consumeMotionDown();
7382 spy->consumeMotionDown();
7383
7384 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08007385 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007386 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
7387 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
7388 .pointer(
7389 PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50))
7390 .build();
7391 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7392 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
7393 InputEventInjectionSync::WAIT_FOR_RESULT))
7394 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7395 windowRight->consumeMotionDown();
7396 spy->consumeMotionPointerDown(1 /*pointerIndex*/);
7397}
7398
7399/**
7400 * When the first pointer lands outside the spy window and the second pointer lands inside it, the
7401 * the spy should receive the second pointer with ACTION_DOWN.
7402 */
7403TEST_F(InputDispatcherSpyWindowTest, ReceivesSecondPointerAsDown) {
7404 auto window = createForeground();
7405 window->setFrame({0, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007406 auto spyRight = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007407 spyRight->setFrame({100, 0, 200, 200});
7408 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyRight, window}}});
7409
7410 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7411 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7412 {50, 50}))
7413 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7414 window->consumeMotionDown();
7415 spyRight->assertNoEvents();
7416
7417 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08007418 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -08007419 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
7420 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
7421 .pointer(
7422 PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50))
7423 .build();
7424 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7425 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
7426 InputEventInjectionSync::WAIT_FOR_RESULT))
7427 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7428 window->consumeMotionPointerDown(1 /*pointerIndex*/);
7429 spyRight->consumeMotionDown();
7430}
7431
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007432/**
7433 * The spy window should not be able to affect whether or not touches are split. Only the foreground
7434 * windows should be allowed to control split touch.
7435 */
7436TEST_F(InputDispatcherSpyWindowTest, SplitIfNoForegroundWindowTouched) {
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08007437 // This spy window prevents touch splitting. However, we still expect to split touches
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007438 // because a foreground window has not disabled splitting.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007439 auto spy = createSpy();
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08007440 spy->setPreventSplitting(true);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007441
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007442 auto window = createForeground();
7443 window->setFrame(Rect(0, 0, 100, 100));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007444
7445 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
7446
7447 // First finger down, no window touched.
7448 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7449 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7450 {100, 200}))
7451 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7452 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7453 window->assertNoEvents();
7454
7455 // Second finger down on window, the window should receive touch down.
7456 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08007457 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007458 .displayId(ADISPLAY_ID_DEFAULT)
7459 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
7460 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
7461 .x(100)
7462 .y(200))
7463 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
7464 .build();
7465 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7466 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
7467 InputEventInjectionSync::WAIT_FOR_RESULT))
7468 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7469
7470 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7471 spy->consumeMotionPointerDown(1 /* pointerIndex */);
7472}
7473
7474/**
7475 * A spy window will usually be implemented as an un-focusable window. Verify that these windows
7476 * do not receive key events.
7477 */
7478TEST_F(InputDispatcherSpyWindowTest, UnfocusableSpyDoesNotReceiveKeyEvents) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007479 auto spy = createSpy();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007480 spy->setFocusable(false);
7481
7482 auto window = createForeground();
7483 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
7484 setFocusedWindow(window);
7485 window->consumeFocusEvent(true);
7486
7487 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7488 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
7489 window->consumeKeyDown(ADISPLAY_ID_NONE);
7490
7491 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7492 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
7493 window->consumeKeyUp(ADISPLAY_ID_NONE);
7494
7495 spy->assertNoEvents();
7496}
7497
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00007498using InputDispatcherPilferPointersTest = InputDispatcherSpyWindowTest;
7499
7500/**
7501 * A spy window can pilfer pointers. When this happens, touch gestures used by the spy window that
7502 * are currently sent to any other windows - including other spy windows - will also be cancelled.
7503 */
7504TEST_F(InputDispatcherPilferPointersTest, PilferPointers) {
7505 auto window = createForeground();
7506 auto spy1 = createSpy();
7507 auto spy2 = createSpy();
7508 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window}}});
7509
7510 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7511 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
7512 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7513 window->consumeMotionDown();
7514 spy1->consumeMotionDown();
7515 spy2->consumeMotionDown();
7516
7517 // Pilfer pointers from the second spy window.
7518 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy2->getToken()));
7519 spy2->assertNoEvents();
7520 spy1->consumeMotionCancel();
7521 window->consumeMotionCancel();
7522
7523 // The rest of the gesture should only be sent to the second spy window.
7524 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7525 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
7526 ADISPLAY_ID_DEFAULT))
7527 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7528 spy2->consumeMotionMove();
7529 spy1->assertNoEvents();
7530 window->assertNoEvents();
7531}
7532
7533/**
7534 * A spy window can pilfer pointers for a gesture even after the foreground window has been removed
7535 * in the middle of the gesture.
7536 */
7537TEST_F(InputDispatcherPilferPointersTest, CanPilferAfterWindowIsRemovedMidStream) {
7538 auto window = createForeground();
7539 auto spy = createSpy();
7540 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
7541
7542 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7543 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
7544 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7545 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7546 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7547
7548 window->releaseChannel();
7549
7550 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
7551
7552 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7553 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
7554 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7555 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
7556}
7557
7558/**
7559 * After a spy window pilfers pointers, new pointers that go down in its bounds should be sent to
7560 * the spy, but not to any other windows.
7561 */
7562TEST_F(InputDispatcherPilferPointersTest, ContinuesToReceiveGestureAfterPilfer) {
7563 auto spy = createSpy();
7564 auto window = createForeground();
7565
7566 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
7567
7568 // First finger down on the window and the spy.
7569 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7570 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7571 {100, 200}))
7572 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7573 spy->consumeMotionDown();
7574 window->consumeMotionDown();
7575
7576 // Spy window pilfers the pointers.
7577 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
7578 window->consumeMotionCancel();
7579
7580 // Second finger down on the window and spy, but the window should not receive the pointer down.
7581 const MotionEvent secondFingerDownEvent =
7582 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7583 .displayId(ADISPLAY_ID_DEFAULT)
7584 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
7585 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
7586 .x(100)
7587 .y(200))
7588 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
7589 .build();
7590 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7591 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
7592 InputEventInjectionSync::WAIT_FOR_RESULT))
7593 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7594
7595 spy->consumeMotionPointerDown(1 /*pointerIndex*/);
7596
7597 // Third finger goes down outside all windows, so injection should fail.
7598 const MotionEvent thirdFingerDownEvent =
7599 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7600 .displayId(ADISPLAY_ID_DEFAULT)
7601 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
7602 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
7603 .x(100)
7604 .y(200))
7605 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
7606 .pointer(PointerBuilder(/* id */ 2, AMOTION_EVENT_TOOL_TYPE_FINGER).x(-5).y(-5))
7607 .build();
7608 ASSERT_EQ(InputEventInjectionResult::FAILED,
7609 injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
7610 InputEventInjectionSync::WAIT_FOR_RESULT))
7611 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7612
7613 spy->assertNoEvents();
7614 window->assertNoEvents();
7615}
7616
7617/**
7618 * After a spy window pilfers pointers, only the pointers used by the spy should be canceled
7619 */
7620TEST_F(InputDispatcherPilferPointersTest, PartiallyPilferRequiredPointers) {
7621 auto spy = createSpy();
7622 spy->setFrame(Rect(0, 0, 100, 100));
7623 auto window = createForeground();
7624 window->setFrame(Rect(0, 0, 200, 200));
7625
7626 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
7627
7628 // First finger down on the window only
7629 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7630 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7631 {150, 150}))
7632 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7633 window->consumeMotionDown();
7634
7635 // Second finger down on the spy and window
7636 const MotionEvent secondFingerDownEvent =
7637 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7638 .displayId(ADISPLAY_ID_DEFAULT)
7639 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
7640 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
7641 .x(150)
7642 .y(150))
7643 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(10).y(10))
7644 .build();
7645 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7646 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
7647 InputEventInjectionSync::WAIT_FOR_RESULT))
7648 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7649 spy->consumeMotionDown();
7650 window->consumeMotionPointerDown(1);
7651
7652 // Third finger down on the spy and window
7653 const MotionEvent thirdFingerDownEvent =
7654 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7655 .displayId(ADISPLAY_ID_DEFAULT)
7656 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
7657 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
7658 .x(150)
7659 .y(150))
7660 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(10).y(10))
7661 .pointer(PointerBuilder(/* id */ 2, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
7662 .build();
7663 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7664 injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
7665 InputEventInjectionSync::WAIT_FOR_RESULT))
7666 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7667 spy->consumeMotionPointerDown(1);
7668 window->consumeMotionPointerDown(2);
7669
7670 // Spy window pilfers the pointers.
7671 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
7672 window->consumeMotionPointerUp(/* idx */ 2, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
7673 window->consumeMotionPointerUp(/* idx */ 1, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
7674
7675 spy->assertNoEvents();
7676 window->assertNoEvents();
7677}
7678
7679/**
7680 * After a spy window pilfers pointers, all pilfered pointers that have already been dispatched to
7681 * other windows should be canceled. If this results in the cancellation of all pointers for some
7682 * window, then that window should receive ACTION_CANCEL.
7683 */
7684TEST_F(InputDispatcherPilferPointersTest, PilferAllRequiredPointers) {
7685 auto spy = createSpy();
7686 spy->setFrame(Rect(0, 0, 100, 100));
7687 auto window = createForeground();
7688 window->setFrame(Rect(0, 0, 200, 200));
7689
7690 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
7691
7692 // First finger down on both spy and window
7693 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7694 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7695 {10, 10}))
7696 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7697 window->consumeMotionDown();
7698 spy->consumeMotionDown();
7699
7700 // Second finger down on the spy and window
7701 const MotionEvent secondFingerDownEvent =
7702 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7703 .displayId(ADISPLAY_ID_DEFAULT)
7704 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
7705 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(10).y(10))
7706 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
7707 .build();
7708 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7709 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
7710 InputEventInjectionSync::WAIT_FOR_RESULT))
7711 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7712 spy->consumeMotionPointerDown(1);
7713 window->consumeMotionPointerDown(1);
7714
7715 // Spy window pilfers the pointers.
7716 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
7717 window->consumeMotionCancel();
7718
7719 spy->assertNoEvents();
7720 window->assertNoEvents();
7721}
7722
7723/**
7724 * After a spy window pilfers pointers, new pointers that are not touching the spy window can still
7725 * be sent to other windows
7726 */
7727TEST_F(InputDispatcherPilferPointersTest, CanReceivePointersAfterPilfer) {
7728 auto spy = createSpy();
7729 spy->setFrame(Rect(0, 0, 100, 100));
7730 auto window = createForeground();
7731 window->setFrame(Rect(0, 0, 200, 200));
7732
7733 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
7734
7735 // First finger down on both window and spy
7736 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7737 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7738 {10, 10}))
7739 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7740 window->consumeMotionDown();
7741 spy->consumeMotionDown();
7742
7743 // Spy window pilfers the pointers.
7744 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
7745 window->consumeMotionCancel();
7746
7747 // Second finger down on the window only
7748 const MotionEvent secondFingerDownEvent =
7749 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7750 .displayId(ADISPLAY_ID_DEFAULT)
7751 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
7752 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(10).y(10))
7753 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER)
7754 .x(150)
7755 .y(150))
7756 .build();
7757 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7758 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
7759 InputEventInjectionSync::WAIT_FOR_RESULT))
7760 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7761 window->consumeMotionDown();
7762 window->assertNoEvents();
7763
7764 // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line
7765 spy->consumeMotionMove();
7766 spy->assertNoEvents();
7767}
7768
Prabir Pradhand65552b2021-10-07 11:23:50 -07007769class InputDispatcherStylusInterceptorTest : public InputDispatcherTest {
7770public:
7771 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupStylusOverlayScenario() {
7772 std::shared_ptr<FakeApplicationHandle> overlayApplication =
7773 std::make_shared<FakeApplicationHandle>();
7774 sp<FakeWindowHandle> overlay =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007775 sp<FakeWindowHandle>::make(overlayApplication, mDispatcher,
7776 "Stylus interceptor window", ADISPLAY_ID_DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -07007777 overlay->setFocusable(false);
7778 overlay->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007779 overlay->setTouchable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007780 overlay->setInterceptsStylus(true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07007781 overlay->setTrustedOverlay(true);
7782
7783 std::shared_ptr<FakeApplicationHandle> application =
7784 std::make_shared<FakeApplicationHandle>();
7785 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007786 sp<FakeWindowHandle>::make(application, mDispatcher, "Application window",
7787 ADISPLAY_ID_DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -07007788 window->setFocusable(true);
7789 window->setOwnerInfo(222, 222);
Prabir Pradhand65552b2021-10-07 11:23:50 -07007790
7791 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
7792 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
7793 setFocusedWindow(window);
7794 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
7795 return {std::move(overlay), std::move(window)};
7796 }
7797
7798 void sendFingerEvent(int32_t action) {
7799 NotifyMotionArgs motionArgs =
7800 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
7801 ADISPLAY_ID_DEFAULT, {PointF{20, 20}});
7802 mDispatcher->notifyMotion(&motionArgs);
7803 }
7804
7805 void sendStylusEvent(int32_t action) {
7806 NotifyMotionArgs motionArgs =
7807 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
7808 ADISPLAY_ID_DEFAULT, {PointF{30, 40}});
7809 motionArgs.pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS;
7810 mDispatcher->notifyMotion(&motionArgs);
7811 }
7812};
7813
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08007814using InputDispatcherStylusInterceptorDeathTest = InputDispatcherStylusInterceptorTest;
7815
7816TEST_F(InputDispatcherStylusInterceptorDeathTest, UntrustedOverlay_AbortsDispatcher) {
7817 ScopedSilentDeath _silentDeath;
7818
Prabir Pradhand65552b2021-10-07 11:23:50 -07007819 auto [overlay, window] = setupStylusOverlayScenario();
7820 overlay->setTrustedOverlay(false);
7821 // Configuring an untrusted overlay as a stylus interceptor should cause Dispatcher to abort.
7822 ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}),
7823 ".* not a trusted overlay");
7824}
7825
7826TEST_F(InputDispatcherStylusInterceptorTest, ConsmesOnlyStylusEvents) {
7827 auto [overlay, window] = setupStylusOverlayScenario();
7828 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
7829
7830 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
7831 overlay->consumeMotionDown();
7832 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
7833 overlay->consumeMotionUp();
7834
7835 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
7836 window->consumeMotionDown();
7837 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
7838 window->consumeMotionUp();
7839
7840 overlay->assertNoEvents();
7841 window->assertNoEvents();
7842}
7843
7844TEST_F(InputDispatcherStylusInterceptorTest, SpyWindowStylusInterceptor) {
7845 auto [overlay, window] = setupStylusOverlayScenario();
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007846 overlay->setSpy(true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07007847 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
7848
7849 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
7850 overlay->consumeMotionDown();
7851 window->consumeMotionDown();
7852 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
7853 overlay->consumeMotionUp();
7854 window->consumeMotionUp();
7855
7856 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
7857 window->consumeMotionDown();
7858 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
7859 window->consumeMotionUp();
7860
7861 overlay->assertNoEvents();
7862 window->assertNoEvents();
7863}
7864
Prabir Pradhan6dfbf262022-03-14 15:24:30 +00007865/**
7866 * Set up a scenario to test the behavior used by the stylus handwriting detection feature.
7867 * The scenario is as follows:
7868 * - The stylus interceptor overlay is configured as a spy window.
7869 * - The stylus interceptor spy receives the start of a new stylus gesture.
7870 * - It pilfers pointers and then configures itself to no longer be a spy.
7871 * - The stylus interceptor continues to receive the rest of the gesture.
7872 */
7873TEST_F(InputDispatcherStylusInterceptorTest, StylusHandwritingScenario) {
7874 auto [overlay, window] = setupStylusOverlayScenario();
7875 overlay->setSpy(true);
7876 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
7877
7878 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
7879 overlay->consumeMotionDown();
7880 window->consumeMotionDown();
7881
7882 // The interceptor pilfers the pointers.
7883 EXPECT_EQ(OK, mDispatcher->pilferPointers(overlay->getToken()));
7884 window->consumeMotionCancel();
7885
7886 // The interceptor configures itself so that it is no longer a spy.
7887 overlay->setSpy(false);
7888 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
7889
7890 // It continues to receive the rest of the stylus gesture.
7891 sendStylusEvent(AMOTION_EVENT_ACTION_MOVE);
7892 overlay->consumeMotionMove();
7893 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
7894 overlay->consumeMotionUp();
7895
7896 window->assertNoEvents();
7897}
7898
Prabir Pradhan5735a322022-04-11 17:23:34 +00007899struct User {
7900 int32_t mPid;
7901 int32_t mUid;
7902 uint32_t mPolicyFlags{DEFAULT_POLICY_FLAGS};
7903 std::unique_ptr<InputDispatcher>& mDispatcher;
7904
7905 User(std::unique_ptr<InputDispatcher>& dispatcher, int32_t pid, int32_t uid)
7906 : mPid(pid), mUid(uid), mDispatcher(dispatcher) {}
7907
7908 InputEventInjectionResult injectTargetedMotion(int32_t action) const {
7909 return injectMotionEvent(mDispatcher, action, AINPUT_SOURCE_TOUCHSCREEN,
7910 ADISPLAY_ID_DEFAULT, {100, 200},
7911 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
7912 AMOTION_EVENT_INVALID_CURSOR_POSITION},
7913 INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT,
7914 systemTime(SYSTEM_TIME_MONOTONIC), {mUid}, mPolicyFlags);
7915 }
7916
7917 InputEventInjectionResult injectTargetedKey(int32_t action) const {
7918 return inputdispatcher::injectKey(mDispatcher, action, 0 /* repeatCount*/, ADISPLAY_ID_NONE,
7919 InputEventInjectionSync::WAIT_FOR_RESULT,
7920 INJECT_EVENT_TIMEOUT, false /*allowKeyRepeat*/, {mUid},
7921 mPolicyFlags);
7922 }
7923
7924 sp<FakeWindowHandle> createWindow() const {
7925 std::shared_ptr<FakeApplicationHandle> overlayApplication =
7926 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007927 sp<FakeWindowHandle> window =
7928 sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, "Owned Window",
7929 ADISPLAY_ID_DEFAULT);
Prabir Pradhan5735a322022-04-11 17:23:34 +00007930 window->setOwnerInfo(mPid, mUid);
7931 return window;
7932 }
7933};
7934
7935using InputDispatcherTargetedInjectionTest = InputDispatcherTest;
7936
7937TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedWindow) {
7938 auto owner = User(mDispatcher, 10, 11);
7939 auto window = owner.createWindow();
7940 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
7941
7942 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
7943 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
7944 window->consumeMotionDown();
7945
7946 setFocusedWindow(window);
7947 window->consumeFocusEvent(true);
7948
7949 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
7950 owner.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
7951 window->consumeKeyDown(ADISPLAY_ID_NONE);
7952}
7953
7954TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedWindow) {
7955 auto owner = User(mDispatcher, 10, 11);
7956 auto window = owner.createWindow();
7957 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
7958
7959 auto rando = User(mDispatcher, 20, 21);
7960 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
7961 rando.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
7962
7963 setFocusedWindow(window);
7964 window->consumeFocusEvent(true);
7965
7966 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
7967 rando.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
7968 window->assertNoEvents();
7969}
7970
7971TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedSpyWindow) {
7972 auto owner = User(mDispatcher, 10, 11);
7973 auto window = owner.createWindow();
7974 auto spy = owner.createWindow();
7975 spy->setSpy(true);
7976 spy->setTrustedOverlay(true);
7977 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
7978
7979 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
7980 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
7981 spy->consumeMotionDown();
7982 window->consumeMotionDown();
7983}
7984
7985TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedSpyWindow) {
7986 auto owner = User(mDispatcher, 10, 11);
7987 auto window = owner.createWindow();
7988
7989 auto rando = User(mDispatcher, 20, 21);
7990 auto randosSpy = rando.createWindow();
7991 randosSpy->setSpy(true);
7992 randosSpy->setTrustedOverlay(true);
7993 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}});
7994
7995 // The event is targeted at owner's window, so injection should succeed, but the spy should
7996 // not receive the event.
7997 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
7998 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
7999 randosSpy->assertNoEvents();
8000 window->consumeMotionDown();
8001}
8002
8003TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoAnyWindowWhenNotTargeting) {
8004 auto owner = User(mDispatcher, 10, 11);
8005 auto window = owner.createWindow();
8006
8007 auto rando = User(mDispatcher, 20, 21);
8008 auto randosSpy = rando.createWindow();
8009 randosSpy->setSpy(true);
8010 randosSpy->setTrustedOverlay(true);
8011 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}});
8012
8013 // A user that has injection permission can inject into any window.
8014 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
8015 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
8016 ADISPLAY_ID_DEFAULT));
8017 randosSpy->consumeMotionDown();
8018 window->consumeMotionDown();
8019
8020 setFocusedWindow(randosSpy);
8021 randosSpy->consumeFocusEvent(true);
8022
8023 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher));
8024 randosSpy->consumeKeyDown(ADISPLAY_ID_NONE);
8025 window->assertNoEvents();
8026}
8027
8028TEST_F(InputDispatcherTargetedInjectionTest, CanGenerateActionOutsideToOtherUids) {
8029 auto owner = User(mDispatcher, 10, 11);
8030 auto window = owner.createWindow();
8031
8032 auto rando = User(mDispatcher, 20, 21);
8033 auto randosWindow = rando.createWindow();
8034 randosWindow->setFrame(Rect{-10, -10, -5, -5});
8035 randosWindow->setWatchOutsideTouch(true);
8036 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosWindow, window}}});
8037
8038 // We allow generation of ACTION_OUTSIDE events into windows owned by different uids.
8039 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
8040 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
8041 window->consumeMotionDown();
8042 randosWindow->consumeMotionOutside();
8043}
8044
Garfield Tane84e6f92019-08-29 17:28:41 -07008045} // namespace android::inputdispatcher