blob: 7d8b58f5a5e1c4132161d405df5197942a3e3a0e [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>
Garfield Tan1c7bc862020-01-28 13:24:04 -080020#include <android-base/stringprintf.h>
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -070021#include <android-base/thread_annotations.h>
Robert Carr803535b2018-08-02 16:38:15 -070022#include <binder/Binder.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080023#include <gtest/gtest.h>
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100024#include <input/Input.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080025#include <linux/input.h>
Prabir Pradhan07e05b62021-11-19 03:57:24 -080026#include <sys/epoll.h>
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100027
Garfield Tan1c7bc862020-01-28 13:24:04 -080028#include <cinttypes>
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -070029#include <thread>
Garfield Tan1c7bc862020-01-28 13:24:04 -080030#include <unordered_set>
chaviwd1c23182019-12-20 18:44:56 -080031#include <vector>
Michael Wrightd02c5b62014-02-10 15:10:22 -080032
Garfield Tan1c7bc862020-01-28 13:24:04 -080033using android::base::StringPrintf;
chaviw3277faf2021-05-19 16:45:23 -050034using android::gui::FocusRequest;
35using android::gui::TouchOcclusionMode;
36using android::gui::WindowInfo;
37using android::gui::WindowInfoHandle;
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080038using android::os::InputEventInjectionResult;
39using android::os::InputEventInjectionSync;
Michael Wright44753b12020-07-08 13:48:11 +010040using namespace android::flag_operators;
Garfield Tan1c7bc862020-01-28 13:24:04 -080041
Garfield Tane84e6f92019-08-29 17:28:41 -070042namespace android::inputdispatcher {
Michael Wrightd02c5b62014-02-10 15:10:22 -080043
44// An arbitrary time value.
45static const nsecs_t ARBITRARY_TIME = 1234;
46
47// An arbitrary device id.
48static const int32_t DEVICE_ID = 1;
49
Jeff Brownf086ddb2014-02-11 14:28:48 -080050// An arbitrary display id.
Arthur Hungabbb9d82021-09-01 14:52:30 +000051static constexpr int32_t DISPLAY_ID = ADISPLAY_ID_DEFAULT;
52static constexpr int32_t SECOND_DISPLAY_ID = 1;
Jeff Brownf086ddb2014-02-11 14:28:48 -080053
Michael Wrightd02c5b62014-02-10 15:10:22 -080054// An arbitrary injector pid / uid pair that has permission to inject events.
55static const int32_t INJECTOR_PID = 999;
56static const int32_t INJECTOR_UID = 1001;
57
Siarhei Vishniakou58cfc602020-12-14 23:21:30 +000058// An arbitrary pid of the gesture monitor window
59static constexpr int32_t MONITOR_PID = 2001;
60
chaviwd1c23182019-12-20 18:44:56 -080061struct PointF {
62 float x;
63 float y;
64};
Michael Wrightd02c5b62014-02-10 15:10:22 -080065
Gang Wang342c9272020-01-13 13:15:04 -050066/**
67 * Return a DOWN key event with KEYCODE_A.
68 */
69static KeyEvent getTestKeyEvent() {
70 KeyEvent event;
71
Garfield Tanfbe732e2020-01-24 11:26:14 -080072 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
73 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
74 ARBITRARY_TIME, ARBITRARY_TIME);
Gang Wang342c9272020-01-13 13:15:04 -050075 return event;
76}
77
Siarhei Vishniakouca205502021-07-16 21:31:58 +000078static void assertMotionAction(int32_t expectedAction, int32_t receivedAction) {
79 ASSERT_EQ(expectedAction, receivedAction)
80 << "expected " << MotionEvent::actionToString(expectedAction) << ", got "
81 << MotionEvent::actionToString(receivedAction);
82}
83
Michael Wrightd02c5b62014-02-10 15:10:22 -080084// --- FakeInputDispatcherPolicy ---
85
86class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface {
87 InputDispatcherConfiguration mConfig;
88
89protected:
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100090 virtual ~FakeInputDispatcherPolicy() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -080091
92public:
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100093 FakeInputDispatcherPolicy() {}
Jackal Guof9696682018-10-05 12:23:23 +080094
Siarhei Vishniakou8935a802019-11-15 16:41:44 -080095 void assertFilterInputEventWasCalled(const NotifyKeyArgs& args) {
Prabir Pradhan81420cc2021-09-06 10:28:50 -070096 assertFilterInputEventWasCalledInternal([&args](const InputEvent& event) {
97 ASSERT_EQ(event.getType(), AINPUT_EVENT_TYPE_KEY);
98 EXPECT_EQ(event.getDisplayId(), args.displayId);
99
100 const auto& keyEvent = static_cast<const KeyEvent&>(event);
101 EXPECT_EQ(keyEvent.getEventTime(), args.eventTime);
102 EXPECT_EQ(keyEvent.getAction(), args.action);
103 });
Jackal Guof9696682018-10-05 12:23:23 +0800104 }
105
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700106 void assertFilterInputEventWasCalled(const NotifyMotionArgs& args, vec2 point) {
107 assertFilterInputEventWasCalledInternal([&](const InputEvent& event) {
108 ASSERT_EQ(event.getType(), AINPUT_EVENT_TYPE_MOTION);
109 EXPECT_EQ(event.getDisplayId(), args.displayId);
110
111 const auto& motionEvent = static_cast<const MotionEvent&>(event);
112 EXPECT_EQ(motionEvent.getEventTime(), args.eventTime);
113 EXPECT_EQ(motionEvent.getAction(), args.action);
114 EXPECT_EQ(motionEvent.getX(0), point.x);
115 EXPECT_EQ(motionEvent.getY(0), point.y);
116 EXPECT_EQ(motionEvent.getRawX(0), point.x);
117 EXPECT_EQ(motionEvent.getRawY(0), point.y);
118 });
Jackal Guof9696682018-10-05 12:23:23 +0800119 }
120
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700121 void assertFilterInputEventWasNotCalled() {
122 std::scoped_lock lock(mLock);
123 ASSERT_EQ(nullptr, mFilteredEvent);
124 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800125
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800126 void assertNotifyConfigurationChangedWasCalled(nsecs_t when) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700127 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800128 ASSERT_TRUE(mConfigurationChangedTime)
129 << "Timed out waiting for configuration changed call";
130 ASSERT_EQ(*mConfigurationChangedTime, when);
131 mConfigurationChangedTime = std::nullopt;
132 }
133
134 void assertNotifySwitchWasCalled(const NotifySwitchArgs& args) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700135 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800136 ASSERT_TRUE(mLastNotifySwitch);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800137 // We do not check id because it is not exposed to the policy
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800138 EXPECT_EQ(args.eventTime, mLastNotifySwitch->eventTime);
139 EXPECT_EQ(args.policyFlags, mLastNotifySwitch->policyFlags);
140 EXPECT_EQ(args.switchValues, mLastNotifySwitch->switchValues);
141 EXPECT_EQ(args.switchMask, mLastNotifySwitch->switchMask);
142 mLastNotifySwitch = std::nullopt;
143 }
144
chaviwfd6d3512019-03-25 13:23:49 -0700145 void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700146 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800147 ASSERT_EQ(touchedToken, mOnPointerDownToken);
148 mOnPointerDownToken.clear();
149 }
150
151 void assertOnPointerDownWasNotCalled() {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700152 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800153 ASSERT_TRUE(mOnPointerDownToken == nullptr)
154 << "Expected onPointerDownOutsideFocus to not have been called";
chaviwfd6d3512019-03-25 13:23:49 -0700155 }
156
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700157 // This function must be called soon after the expected ANR timer starts,
158 // because we are also checking how much time has passed.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500159 void assertNotifyNoFocusedWindowAnrWasCalled(
Chris Yea209fde2020-07-22 13:54:51 -0700160 std::chrono::nanoseconds timeout,
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500161 const std::shared_ptr<InputApplicationHandle>& expectedApplication) {
162 std::shared_ptr<InputApplicationHandle> application;
163 { // acquire lock
164 std::unique_lock lock(mLock);
165 android::base::ScopedLockAssertion assumeLocked(mLock);
166 ASSERT_NO_FATAL_FAILURE(
167 application = getAnrTokenLockedInterruptible(timeout, mAnrApplications, lock));
168 } // release lock
169 ASSERT_EQ(expectedApplication, application);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700170 }
171
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000172 void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout,
173 const sp<IBinder>& expectedConnectionToken) {
174 sp<IBinder> connectionToken = getUnresponsiveWindowToken(timeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500175 ASSERT_EQ(expectedConnectionToken, connectionToken);
176 }
177
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000178 void assertNotifyWindowResponsiveWasCalled(const sp<IBinder>& expectedConnectionToken) {
179 sp<IBinder> connectionToken = getResponsiveWindowToken();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500180 ASSERT_EQ(expectedConnectionToken, connectionToken);
181 }
182
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000183 void assertNotifyMonitorUnresponsiveWasCalled(std::chrono::nanoseconds timeout) {
184 int32_t pid = getUnresponsiveMonitorPid(timeout);
185 ASSERT_EQ(MONITOR_PID, pid);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500186 }
187
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000188 void assertNotifyMonitorResponsiveWasCalled() {
189 int32_t pid = getResponsiveMonitorPid();
190 ASSERT_EQ(MONITOR_PID, pid);
191 }
192
193 sp<IBinder> getUnresponsiveWindowToken(std::chrono::nanoseconds timeout) {
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500194 std::unique_lock lock(mLock);
195 android::base::ScopedLockAssertion assumeLocked(mLock);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000196 return getAnrTokenLockedInterruptible(timeout, mAnrWindowTokens, lock);
197 }
198
199 sp<IBinder> getResponsiveWindowToken() {
200 std::unique_lock lock(mLock);
201 android::base::ScopedLockAssertion assumeLocked(mLock);
202 return getAnrTokenLockedInterruptible(0s, mResponsiveWindowTokens, lock);
203 }
204
205 int32_t getUnresponsiveMonitorPid(std::chrono::nanoseconds timeout) {
206 std::unique_lock lock(mLock);
207 android::base::ScopedLockAssertion assumeLocked(mLock);
208 return getAnrTokenLockedInterruptible(timeout, mAnrMonitorPids, lock);
209 }
210
211 int32_t getResponsiveMonitorPid() {
212 std::unique_lock lock(mLock);
213 android::base::ScopedLockAssertion assumeLocked(mLock);
214 return getAnrTokenLockedInterruptible(0s, mResponsiveMonitorPids, lock);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500215 }
216
217 // All three ANR-related callbacks behave the same way, so we use this generic function to wait
218 // for a specific container to become non-empty. When the container is non-empty, return the
219 // first entry from the container and erase it.
220 template <class T>
221 T getAnrTokenLockedInterruptible(std::chrono::nanoseconds timeout, std::queue<T>& storage,
222 std::unique_lock<std::mutex>& lock) REQUIRES(mLock) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700223 // If there is an ANR, Dispatcher won't be idle because there are still events
224 // in the waitQueue that we need to check on. So we can't wait for dispatcher to be idle
225 // before checking if ANR was called.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500226 // Since dispatcher is not guaranteed to call notifyNoFocusedWindowAnr right away, we need
227 // to provide it some time to act. 100ms seems reasonable.
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800228 std::chrono::duration timeToWait = timeout + 100ms; // provide some slack
229 const std::chrono::time_point start = std::chrono::steady_clock::now();
230 std::optional<T> token =
231 getItemFromStorageLockedInterruptible(timeToWait, storage, lock, mNotifyAnr);
232 if (!token.has_value()) {
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500233 ADD_FAILURE() << "Did not receive the ANR callback";
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000234 return {};
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700235 }
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800236
237 const std::chrono::duration waited = std::chrono::steady_clock::now() - start;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700238 // Ensure that the ANR didn't get raised too early. We can't be too strict here because
239 // the dispatcher started counting before this function was called
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700240 if (std::chrono::abs(timeout - waited) > 100ms) {
241 ADD_FAILURE() << "ANR was raised too early or too late. Expected "
242 << std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count()
243 << "ms, but waited "
244 << std::chrono::duration_cast<std::chrono::milliseconds>(waited).count()
245 << "ms instead";
246 }
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800247 return *token;
248 }
249
250 template <class T>
251 std::optional<T> getItemFromStorageLockedInterruptible(std::chrono::nanoseconds timeout,
252 std::queue<T>& storage,
253 std::unique_lock<std::mutex>& lock,
254 std::condition_variable& condition)
255 REQUIRES(mLock) {
256 condition.wait_for(lock, timeout,
257 [&storage]() REQUIRES(mLock) { return !storage.empty(); });
258 if (storage.empty()) {
259 ADD_FAILURE() << "Did not receive the expected callback";
260 return std::nullopt;
261 }
262 T item = storage.front();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500263 storage.pop();
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800264 return std::make_optional(item);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700265 }
266
267 void assertNotifyAnrWasNotCalled() {
268 std::scoped_lock lock(mLock);
269 ASSERT_TRUE(mAnrApplications.empty());
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000270 ASSERT_TRUE(mAnrWindowTokens.empty());
271 ASSERT_TRUE(mAnrMonitorPids.empty());
272 ASSERT_TRUE(mResponsiveWindowTokens.empty())
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500273 << "ANR was not called, but please also consume the 'connection is responsive' "
274 "signal";
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000275 ASSERT_TRUE(mResponsiveMonitorPids.empty())
276 << "Monitor ANR was not called, but please also consume the 'monitor is responsive'"
277 " signal";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700278 }
279
Garfield Tan1c7bc862020-01-28 13:24:04 -0800280 void setKeyRepeatConfiguration(nsecs_t timeout, nsecs_t delay) {
281 mConfig.keyRepeatTimeout = timeout;
282 mConfig.keyRepeatDelay = delay;
283 }
284
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000285 PointerCaptureRequest assertSetPointerCaptureCalled(bool enabled) {
Prabir Pradhan99987712020-11-10 18:43:05 -0800286 std::unique_lock lock(mLock);
287 base::ScopedLockAssertion assumeLocked(mLock);
288
289 if (!mPointerCaptureChangedCondition.wait_for(lock, 100ms,
290 [this, enabled]() REQUIRES(mLock) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000291 return mPointerCaptureRequest->enable ==
Prabir Pradhan99987712020-11-10 18:43:05 -0800292 enabled;
293 })) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000294 ADD_FAILURE() << "Timed out waiting for setPointerCapture(" << enabled
295 << ") to be called.";
296 return {};
Prabir Pradhan99987712020-11-10 18:43:05 -0800297 }
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000298 auto request = *mPointerCaptureRequest;
299 mPointerCaptureRequest.reset();
300 return request;
Prabir Pradhan99987712020-11-10 18:43:05 -0800301 }
302
303 void assertSetPointerCaptureNotCalled() {
304 std::unique_lock lock(mLock);
305 base::ScopedLockAssertion assumeLocked(mLock);
306
307 if (mPointerCaptureChangedCondition.wait_for(lock, 100ms) != std::cv_status::timeout) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000308 FAIL() << "Expected setPointerCapture(request) to not be called, but was called. "
Prabir Pradhan99987712020-11-10 18:43:05 -0800309 "enabled = "
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000310 << std::to_string(mPointerCaptureRequest->enable);
Prabir Pradhan99987712020-11-10 18:43:05 -0800311 }
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000312 mPointerCaptureRequest.reset();
Prabir Pradhan99987712020-11-10 18:43:05 -0800313 }
314
arthurhungf452d0b2021-01-06 00:19:52 +0800315 void assertDropTargetEquals(const sp<IBinder>& targetToken) {
316 std::scoped_lock lock(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800317 ASSERT_TRUE(mNotifyDropWindowWasCalled);
arthurhungf452d0b2021-01-06 00:19:52 +0800318 ASSERT_EQ(targetToken, mDropTargetWindowToken);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800319 mNotifyDropWindowWasCalled = false;
arthurhungf452d0b2021-01-06 00:19:52 +0800320 }
321
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800322 void assertNotifyInputChannelBrokenWasCalled(const sp<IBinder>& token) {
323 std::unique_lock lock(mLock);
324 base::ScopedLockAssertion assumeLocked(mLock);
325 std::optional<sp<IBinder>> receivedToken =
326 getItemFromStorageLockedInterruptible(100ms, mBrokenInputChannels, lock,
327 mNotifyInputChannelBroken);
328 ASSERT_TRUE(receivedToken.has_value());
329 ASSERT_EQ(token, *receivedToken);
330 }
331
Michael Wrightd02c5b62014-02-10 15:10:22 -0800332private:
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700333 std::mutex mLock;
334 std::unique_ptr<InputEvent> mFilteredEvent GUARDED_BY(mLock);
335 std::optional<nsecs_t> mConfigurationChangedTime GUARDED_BY(mLock);
336 sp<IBinder> mOnPointerDownToken GUARDED_BY(mLock);
337 std::optional<NotifySwitchArgs> mLastNotifySwitch GUARDED_BY(mLock);
Jackal Guof9696682018-10-05 12:23:23 +0800338
Prabir Pradhan99987712020-11-10 18:43:05 -0800339 std::condition_variable mPointerCaptureChangedCondition;
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000340
341 std::optional<PointerCaptureRequest> mPointerCaptureRequest GUARDED_BY(mLock);
Prabir Pradhan99987712020-11-10 18:43:05 -0800342
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700343 // ANR handling
Chris Yea209fde2020-07-22 13:54:51 -0700344 std::queue<std::shared_ptr<InputApplicationHandle>> mAnrApplications GUARDED_BY(mLock);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000345 std::queue<sp<IBinder>> mAnrWindowTokens GUARDED_BY(mLock);
346 std::queue<sp<IBinder>> mResponsiveWindowTokens GUARDED_BY(mLock);
347 std::queue<int32_t> mAnrMonitorPids GUARDED_BY(mLock);
348 std::queue<int32_t> mResponsiveMonitorPids GUARDED_BY(mLock);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700349 std::condition_variable mNotifyAnr;
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800350 std::queue<sp<IBinder>> mBrokenInputChannels GUARDED_BY(mLock);
351 std::condition_variable mNotifyInputChannelBroken;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700352
arthurhungf452d0b2021-01-06 00:19:52 +0800353 sp<IBinder> mDropTargetWindowToken GUARDED_BY(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800354 bool mNotifyDropWindowWasCalled GUARDED_BY(mLock) = false;
arthurhungf452d0b2021-01-06 00:19:52 +0800355
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600356 void notifyConfigurationChanged(nsecs_t when) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700357 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800358 mConfigurationChangedTime = when;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800359 }
360
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000361 void notifyWindowUnresponsive(const sp<IBinder>& connectionToken, const std::string&) override {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700362 std::scoped_lock lock(mLock);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000363 mAnrWindowTokens.push(connectionToken);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700364 mNotifyAnr.notify_all();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500365 }
366
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000367 void notifyMonitorUnresponsive(int32_t pid, const std::string&) override {
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500368 std::scoped_lock lock(mLock);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000369 mAnrMonitorPids.push(pid);
370 mNotifyAnr.notify_all();
371 }
372
373 void notifyWindowResponsive(const sp<IBinder>& connectionToken) override {
374 std::scoped_lock lock(mLock);
375 mResponsiveWindowTokens.push(connectionToken);
376 mNotifyAnr.notify_all();
377 }
378
379 void notifyMonitorResponsive(int32_t pid) override {
380 std::scoped_lock lock(mLock);
381 mResponsiveMonitorPids.push(pid);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500382 mNotifyAnr.notify_all();
383 }
384
385 void notifyNoFocusedWindowAnr(
386 const std::shared_ptr<InputApplicationHandle>& applicationHandle) override {
387 std::scoped_lock lock(mLock);
388 mAnrApplications.push(applicationHandle);
389 mNotifyAnr.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800390 }
391
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800392 void notifyInputChannelBroken(const sp<IBinder>& connectionToken) override {
393 std::scoped_lock lock(mLock);
394 mBrokenInputChannels.push(connectionToken);
395 mNotifyInputChannelBroken.notify_all();
396 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800397
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600398 void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {}
Robert Carr740167f2018-10-11 19:03:41 -0700399
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600400 void notifyUntrustedTouch(const std::string& obscuringPackage) override {}
Chris Yef59a2f42020-10-16 12:55:26 -0700401 void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType,
402 InputDeviceSensorAccuracy accuracy, nsecs_t timestamp,
403 const std::vector<float>& values) override {}
404
405 void notifySensorAccuracy(int deviceId, InputDeviceSensorType sensorType,
406 InputDeviceSensorAccuracy accuracy) override {}
Bernardo Rufino2e1f6512020-10-08 13:42:07 +0000407
Chris Yefb552902021-02-03 17:18:37 -0800408 void notifyVibratorState(int32_t deviceId, bool isOn) override {}
409
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600410 void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800411 *outConfig = mConfig;
412 }
413
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600414 bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700415 std::scoped_lock lock(mLock);
Jackal Guof9696682018-10-05 12:23:23 +0800416 switch (inputEvent->getType()) {
417 case AINPUT_EVENT_TYPE_KEY: {
418 const KeyEvent* keyEvent = static_cast<const KeyEvent*>(inputEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800419 mFilteredEvent = std::make_unique<KeyEvent>(*keyEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800420 break;
421 }
422
423 case AINPUT_EVENT_TYPE_MOTION: {
424 const MotionEvent* motionEvent = static_cast<const MotionEvent*>(inputEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800425 mFilteredEvent = std::make_unique<MotionEvent>(*motionEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800426 break;
427 }
428 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800429 return true;
430 }
431
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600432 void interceptKeyBeforeQueueing(const KeyEvent*, uint32_t&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800433
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600434 void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800435
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600436 nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent*, uint32_t) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800437 return 0;
438 }
439
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600440 bool dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent*, uint32_t, KeyEvent*) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800441 return false;
442 }
443
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600444 void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask,
445 uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700446 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800447 /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is
448 * essentially a passthrough for notifySwitch.
449 */
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800450 mLastNotifySwitch = NotifySwitchArgs(1 /*id*/, when, policyFlags, switchValues, switchMask);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800451 }
452
Sean Stoutb4e0a592021-02-23 07:34:53 -0800453 void pokeUserActivity(nsecs_t, int32_t, int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800454
Prabir Pradhan93f342c2021-03-11 15:05:30 -0800455 bool checkInjectEventsPermissionNonReentrant(int32_t pid, int32_t uid) override {
456 return pid == INJECTOR_PID && uid == INJECTOR_UID;
457 }
Jackal Guof9696682018-10-05 12:23:23 +0800458
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600459 void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700460 std::scoped_lock lock(mLock);
chaviwfd6d3512019-03-25 13:23:49 -0700461 mOnPointerDownToken = newToken;
462 }
463
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000464 void setPointerCapture(const PointerCaptureRequest& request) override {
Prabir Pradhan99987712020-11-10 18:43:05 -0800465 std::scoped_lock lock(mLock);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000466 mPointerCaptureRequest = {request};
Prabir Pradhan99987712020-11-10 18:43:05 -0800467 mPointerCaptureChangedCondition.notify_all();
468 }
469
arthurhungf452d0b2021-01-06 00:19:52 +0800470 void notifyDropWindow(const sp<IBinder>& token, float x, float y) override {
471 std::scoped_lock lock(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800472 mNotifyDropWindowWasCalled = true;
arthurhungf452d0b2021-01-06 00:19:52 +0800473 mDropTargetWindowToken = token;
474 }
475
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700476 void assertFilterInputEventWasCalledInternal(
477 const std::function<void(const InputEvent&)>& verify) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700478 std::scoped_lock lock(mLock);
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -0800479 ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called.";
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700480 verify(*mFilteredEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800481 mFilteredEvent = nullptr;
Jackal Guof9696682018-10-05 12:23:23 +0800482 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800483};
484
Michael Wrightd02c5b62014-02-10 15:10:22 -0800485// --- InputDispatcherTest ---
486
487class InputDispatcherTest : public testing::Test {
488protected:
489 sp<FakeInputDispatcherPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700490 std::unique_ptr<InputDispatcher> mDispatcher;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800491
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000492 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800493 mFakePolicy = new FakeInputDispatcherPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700494 mDispatcher = std::make_unique<InputDispatcher>(mFakePolicy);
Arthur Hungb92218b2018-08-14 12:00:21 +0800495 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000496 // Start InputDispatcher thread
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700497 ASSERT_EQ(OK, mDispatcher->start());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800498 }
499
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000500 void TearDown() override {
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700501 ASSERT_EQ(OK, mDispatcher->stop());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800502 mFakePolicy.clear();
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700503 mDispatcher.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800504 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700505
506 /**
507 * Used for debugging when writing the test
508 */
509 void dumpDispatcherState() {
510 std::string dump;
511 mDispatcher->dump(dump);
512 std::stringstream ss(dump);
513 std::string to;
514
515 while (std::getline(ss, to, '\n')) {
516 ALOGE("%s", to.c_str());
517 }
518 }
Vishnu Nair958da932020-08-21 17:12:37 -0700519
chaviw3277faf2021-05-19 16:45:23 -0500520 void setFocusedWindow(const sp<WindowInfoHandle>& window,
521 const sp<WindowInfoHandle>& focusedWindow = nullptr) {
Vishnu Nair958da932020-08-21 17:12:37 -0700522 FocusRequest request;
523 request.token = window->getToken();
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +0000524 request.windowName = window->getName();
Vishnu Nair958da932020-08-21 17:12:37 -0700525 if (focusedWindow) {
526 request.focusedToken = focusedWindow->getToken();
527 }
528 request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
529 request.displayId = window->getInfo()->displayId;
530 mDispatcher->setFocusedWindow(request);
531 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800532};
533
Michael Wrightd02c5b62014-02-10 15:10:22 -0800534TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) {
535 KeyEvent event;
536
537 // Rejects undefined key actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800538 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
539 INVALID_HMAC,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600540 /*action*/ -1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME,
541 ARBITRARY_TIME);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800542 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700543 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800544 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800545 << "Should reject key events with undefined action.";
546
547 // Rejects ACTION_MULTIPLE since it is not supported despite being defined in the API.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800548 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
549 INVALID_HMAC, AKEY_EVENT_ACTION_MULTIPLE, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600550 ARBITRARY_TIME, ARBITRARY_TIME);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800551 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700552 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800553 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800554 << "Should reject key events with ACTION_MULTIPLE.";
555}
556
557TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) {
558 MotionEvent event;
559 PointerProperties pointerProperties[MAX_POINTERS + 1];
560 PointerCoords pointerCoords[MAX_POINTERS + 1];
Siarhei Vishniakou01747382022-01-20 13:23:27 -0800561 for (size_t i = 0; i <= MAX_POINTERS; i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800562 pointerProperties[i].clear();
563 pointerProperties[i].id = i;
564 pointerCoords[i].clear();
565 }
566
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800567 // Some constants commonly used below
568 constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN;
569 constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE;
570 constexpr int32_t metaState = AMETA_NONE;
571 constexpr MotionClassification classification = MotionClassification::NONE;
572
chaviw9eaa22c2020-07-01 16:21:27 -0700573 ui::Transform identityTransform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800574 // Rejects undefined motion actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800575 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
chaviw9eaa22c2020-07-01 16:21:27 -0700576 /*action*/ -1, 0, 0, edgeFlags, metaState, 0, classification,
577 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700578 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
579 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700580 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800581 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700582 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800583 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800584 << "Should reject motion events with undefined action.";
585
586 // Rejects pointer down with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800587 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700588 AMOTION_EVENT_ACTION_POINTER_DOWN |
589 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700590 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
591 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700592 identityTransform, ARBITRARY_TIME, ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500593 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800594 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700595 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800596 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800597 << "Should reject motion events with pointer down index too large.";
598
Garfield Tanfbe732e2020-01-24 11:26:14 -0800599 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700600 AMOTION_EVENT_ACTION_POINTER_DOWN |
601 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700602 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
603 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700604 identityTransform, ARBITRARY_TIME, ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500605 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800606 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700607 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800608 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800609 << "Should reject motion events with pointer down index too small.";
610
611 // Rejects pointer up with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800612 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700613 AMOTION_EVENT_ACTION_POINTER_UP |
614 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700615 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
616 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700617 identityTransform, ARBITRARY_TIME, ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500618 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800619 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700620 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800621 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800622 << "Should reject motion events with pointer up index too large.";
623
Garfield Tanfbe732e2020-01-24 11:26:14 -0800624 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700625 AMOTION_EVENT_ACTION_POINTER_UP |
626 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700627 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
628 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700629 identityTransform, ARBITRARY_TIME, ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500630 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800631 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700632 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800633 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800634 << "Should reject motion events with pointer up index too small.";
635
636 // Rejects motion events with invalid number of pointers.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800637 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
638 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700639 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700640 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
641 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700642 /*pointerCount*/ 0, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800643 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700644 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800645 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800646 << "Should reject motion events with 0 pointers.";
647
Garfield Tanfbe732e2020-01-24 11:26:14 -0800648 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
649 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700650 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700651 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
652 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700653 /*pointerCount*/ MAX_POINTERS + 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800654 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700655 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800656 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800657 << "Should reject motion events with more than MAX_POINTERS pointers.";
658
659 // Rejects motion events with invalid pointer ids.
660 pointerProperties[0].id = -1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800661 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
662 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700663 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700664 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
665 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700666 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800667 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700668 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800669 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800670 << "Should reject motion events with pointer ids less than 0.";
671
672 pointerProperties[0].id = MAX_POINTER_ID + 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800673 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
674 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700675 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700676 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
677 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700678 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800679 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700680 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800681 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800682 << "Should reject motion events with pointer ids greater than MAX_POINTER_ID.";
683
684 // Rejects motion events with duplicate pointer ids.
685 pointerProperties[0].id = 1;
686 pointerProperties[1].id = 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800687 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
688 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700689 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700690 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
691 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700692 /*pointerCount*/ 2, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800693 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700694 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800695 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800696 << "Should reject motion events with duplicate pointer ids.";
697}
698
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800699/* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */
700
701TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) {
702 constexpr nsecs_t eventTime = 20;
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800703 NotifyConfigurationChangedArgs args(10 /*id*/, eventTime);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800704 mDispatcher->notifyConfigurationChanged(&args);
705 ASSERT_TRUE(mDispatcher->waitForIdle());
706
707 mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime);
708}
709
710TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) {
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800711 NotifySwitchArgs args(10 /*id*/, 20 /*eventTime*/, 0 /*policyFlags*/, 1 /*switchValues*/,
712 2 /*switchMask*/);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800713 mDispatcher->notifySwitch(&args);
714
715 // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener
716 args.policyFlags |= POLICY_FLAG_TRUSTED;
717 mFakePolicy->assertNotifySwitchWasCalled(args);
718}
719
Arthur Hungb92218b2018-08-14 12:00:21 +0800720// --- InputDispatcherTest SetInputWindowTest ---
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700721static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms;
Siarhei Vishniakou1c494c52021-08-11 20:25:01 -0700722// Default input dispatching timeout if there is no focused application or paused window
723// from which to determine an appropriate dispatching timeout.
724static const std::chrono::duration DISPATCHING_TIMEOUT = std::chrono::milliseconds(
725 android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS *
726 android::base::HwTimeoutMultiplier());
Arthur Hungb92218b2018-08-14 12:00:21 +0800727
728class FakeApplicationHandle : public InputApplicationHandle {
729public:
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700730 FakeApplicationHandle() {
731 mInfo.name = "Fake Application";
732 mInfo.token = new BBinder();
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500733 mInfo.dispatchingTimeoutMillis =
734 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700735 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800736 virtual ~FakeApplicationHandle() {}
737
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000738 virtual bool updateInfo() override { return true; }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700739
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500740 void setDispatchingTimeout(std::chrono::milliseconds timeout) {
741 mInfo.dispatchingTimeoutMillis = timeout.count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700742 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800743};
744
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800745class FakeInputReceiver {
Arthur Hungb92218b2018-08-14 12:00:21 +0800746public:
Garfield Tan15601662020-09-22 15:32:38 -0700747 explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name)
chaviwd1c23182019-12-20 18:44:56 -0800748 : mName(name) {
Garfield Tan15601662020-09-22 15:32:38 -0700749 mConsumer = std::make_unique<InputConsumer>(std::move(clientChannel));
chaviwd1c23182019-12-20 18:44:56 -0800750 }
751
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800752 InputEvent* consume() {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700753 InputEvent* event;
754 std::optional<uint32_t> consumeSeq = receiveEvent(&event);
755 if (!consumeSeq) {
756 return nullptr;
757 }
758 finishEvent(*consumeSeq);
759 return event;
760 }
761
762 /**
763 * Receive an event without acknowledging it.
764 * Return the sequence number that could later be used to send finished signal.
765 */
766 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Arthur Hungb92218b2018-08-14 12:00:21 +0800767 uint32_t consumeSeq;
768 InputEvent* event;
Arthur Hungb92218b2018-08-14 12:00:21 +0800769
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800770 std::chrono::time_point start = std::chrono::steady_clock::now();
771 status_t status = WOULD_BLOCK;
772 while (status == WOULD_BLOCK) {
chaviw81e2bb92019-12-18 15:03:51 -0800773 status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq,
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800774 &event);
775 std::chrono::duration elapsed = std::chrono::steady_clock::now() - start;
776 if (elapsed > 100ms) {
777 break;
778 }
779 }
780
781 if (status == WOULD_BLOCK) {
782 // Just means there's no event available.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700783 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800784 }
785
786 if (status != OK) {
787 ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK.";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700788 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800789 }
790 if (event == nullptr) {
791 ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700792 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800793 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700794 if (outEvent != nullptr) {
795 *outEvent = event;
796 }
797 return consumeSeq;
798 }
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800799
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700800 /**
801 * To be used together with "receiveEvent" to complete the consumption of an event.
802 */
803 void finishEvent(uint32_t consumeSeq) {
804 const status_t status = mConsumer->sendFinishedSignal(consumeSeq, true);
805 ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK.";
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800806 }
807
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000808 void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) {
809 const status_t status = mConsumer->sendTimeline(inputEventId, timeline);
810 ASSERT_EQ(OK, status);
811 }
812
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000813 void consumeEvent(int32_t expectedEventType, int32_t expectedAction,
814 std::optional<int32_t> expectedDisplayId,
815 std::optional<int32_t> expectedFlags) {
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800816 InputEvent* event = consume();
817
818 ASSERT_NE(nullptr, event) << mName.c_str()
819 << ": consumer should have returned non-NULL event.";
Arthur Hungb92218b2018-08-14 12:00:21 +0800820 ASSERT_EQ(expectedEventType, event->getType())
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700821 << mName.c_str() << " expected " << inputEventTypeToString(expectedEventType)
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800822 << " event, got " << inputEventTypeToString(event->getType()) << " event";
Arthur Hungb92218b2018-08-14 12:00:21 +0800823
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000824 if (expectedDisplayId.has_value()) {
825 EXPECT_EQ(expectedDisplayId, event->getDisplayId());
826 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800827
Tiger Huang8664f8c2018-10-11 19:14:35 +0800828 switch (expectedEventType) {
829 case AINPUT_EVENT_TYPE_KEY: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800830 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event);
831 EXPECT_EQ(expectedAction, keyEvent.getAction());
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000832 if (expectedFlags.has_value()) {
833 EXPECT_EQ(expectedFlags.value(), keyEvent.getFlags());
834 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800835 break;
836 }
837 case AINPUT_EVENT_TYPE_MOTION: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800838 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000839 assertMotionAction(expectedAction, motionEvent.getAction());
840
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000841 if (expectedFlags.has_value()) {
842 EXPECT_EQ(expectedFlags.value(), motionEvent.getFlags());
843 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800844 break;
845 }
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100846 case AINPUT_EVENT_TYPE_FOCUS: {
847 FAIL() << "Use 'consumeFocusEvent' for FOCUS events";
848 }
Prabir Pradhan99987712020-11-10 18:43:05 -0800849 case AINPUT_EVENT_TYPE_CAPTURE: {
850 FAIL() << "Use 'consumeCaptureEvent' for CAPTURE events";
851 }
Antonio Kantekf16f2832021-09-28 04:39:20 +0000852 case AINPUT_EVENT_TYPE_TOUCH_MODE: {
853 FAIL() << "Use 'consumeTouchModeEvent' for TOUCH_MODE events";
854 }
arthurhungb89ccb02020-12-30 16:19:01 +0800855 case AINPUT_EVENT_TYPE_DRAG: {
856 FAIL() << "Use 'consumeDragEvent' for DRAG events";
857 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800858 default: {
859 FAIL() << mName.c_str() << ": invalid event type: " << expectedEventType;
860 }
861 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800862 }
863
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100864 void consumeFocusEvent(bool hasFocus, bool inTouchMode) {
865 InputEvent* event = consume();
866 ASSERT_NE(nullptr, event) << mName.c_str()
867 << ": consumer should have returned non-NULL event.";
868 ASSERT_EQ(AINPUT_EVENT_TYPE_FOCUS, event->getType())
869 << "Got " << inputEventTypeToString(event->getType())
870 << " event instead of FOCUS event";
871
872 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
873 << mName.c_str() << ": event displayId should always be NONE.";
874
875 FocusEvent* focusEvent = static_cast<FocusEvent*>(event);
876 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100877 }
878
Prabir Pradhan99987712020-11-10 18:43:05 -0800879 void consumeCaptureEvent(bool hasCapture) {
880 const InputEvent* event = consume();
881 ASSERT_NE(nullptr, event) << mName.c_str()
882 << ": consumer should have returned non-NULL event.";
883 ASSERT_EQ(AINPUT_EVENT_TYPE_CAPTURE, event->getType())
884 << "Got " << inputEventTypeToString(event->getType())
885 << " event instead of CAPTURE event";
886
887 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
888 << mName.c_str() << ": event displayId should always be NONE.";
889
890 const auto& captureEvent = static_cast<const CaptureEvent&>(*event);
891 EXPECT_EQ(hasCapture, captureEvent.getPointerCaptureEnabled());
892 }
893
arthurhungb89ccb02020-12-30 16:19:01 +0800894 void consumeDragEvent(bool isExiting, float x, float y) {
895 const InputEvent* event = consume();
896 ASSERT_NE(nullptr, event) << mName.c_str()
897 << ": consumer should have returned non-NULL event.";
898 ASSERT_EQ(AINPUT_EVENT_TYPE_DRAG, event->getType())
899 << "Got " << inputEventTypeToString(event->getType())
900 << " event instead of DRAG event";
901
902 EXPECT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
903 << mName.c_str() << ": event displayId should always be NONE.";
904
905 const auto& dragEvent = static_cast<const DragEvent&>(*event);
906 EXPECT_EQ(isExiting, dragEvent.isExiting());
907 EXPECT_EQ(x, dragEvent.getX());
908 EXPECT_EQ(y, dragEvent.getY());
909 }
910
Antonio Kantekf16f2832021-09-28 04:39:20 +0000911 void consumeTouchModeEvent(bool inTouchMode) {
912 const InputEvent* event = consume();
913 ASSERT_NE(nullptr, event) << mName.c_str()
914 << ": consumer should have returned non-NULL event.";
915 ASSERT_EQ(AINPUT_EVENT_TYPE_TOUCH_MODE, event->getType())
916 << "Got " << inputEventTypeToString(event->getType())
917 << " event instead of TOUCH_MODE event";
918
919 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
920 << mName.c_str() << ": event displayId should always be NONE.";
921 const auto& touchModeEvent = static_cast<const TouchModeEvent&>(*event);
922 EXPECT_EQ(inTouchMode, touchModeEvent.isInTouchMode());
923 }
924
chaviwd1c23182019-12-20 18:44:56 -0800925 void assertNoEvents() {
926 InputEvent* event = consume();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700927 if (event == nullptr) {
928 return;
929 }
930 if (event->getType() == AINPUT_EVENT_TYPE_KEY) {
931 KeyEvent& keyEvent = static_cast<KeyEvent&>(*event);
932 ADD_FAILURE() << "Received key event "
933 << KeyEvent::actionToString(keyEvent.getAction());
934 } else if (event->getType() == AINPUT_EVENT_TYPE_MOTION) {
935 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
936 ADD_FAILURE() << "Received motion event "
937 << MotionEvent::actionToString(motionEvent.getAction());
938 } else if (event->getType() == AINPUT_EVENT_TYPE_FOCUS) {
939 FocusEvent& focusEvent = static_cast<FocusEvent&>(*event);
940 ADD_FAILURE() << "Received focus event, hasFocus = "
941 << (focusEvent.getHasFocus() ? "true" : "false");
Prabir Pradhan99987712020-11-10 18:43:05 -0800942 } else if (event->getType() == AINPUT_EVENT_TYPE_CAPTURE) {
943 const auto& captureEvent = static_cast<CaptureEvent&>(*event);
944 ADD_FAILURE() << "Received capture event, pointerCaptureEnabled = "
945 << (captureEvent.getPointerCaptureEnabled() ? "true" : "false");
Antonio Kantekf16f2832021-09-28 04:39:20 +0000946 } else if (event->getType() == AINPUT_EVENT_TYPE_TOUCH_MODE) {
947 const auto& touchModeEvent = static_cast<TouchModeEvent&>(*event);
948 ADD_FAILURE() << "Received touch mode event, inTouchMode = "
949 << (touchModeEvent.isInTouchMode() ? "true" : "false");
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700950 }
951 FAIL() << mName.c_str()
952 << ": should not have received any events, so consume() should return NULL";
chaviwd1c23182019-12-20 18:44:56 -0800953 }
954
955 sp<IBinder> getToken() { return mConsumer->getChannel()->getConnectionToken(); }
956
Prabir Pradhan07e05b62021-11-19 03:57:24 -0800957 int getChannelFd() { return mConsumer->getChannel()->getFd().get(); }
958
chaviwd1c23182019-12-20 18:44:56 -0800959protected:
960 std::unique_ptr<InputConsumer> mConsumer;
961 PreallocatedInputEventFactory mEventFactory;
962
963 std::string mName;
964};
965
chaviw3277faf2021-05-19 16:45:23 -0500966class FakeWindowHandle : public WindowInfoHandle {
chaviwd1c23182019-12-20 18:44:56 -0800967public:
968 static const int32_t WIDTH = 600;
969 static const int32_t HEIGHT = 800;
chaviwd1c23182019-12-20 18:44:56 -0800970
Chris Yea209fde2020-07-22 13:54:51 -0700971 FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700972 const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name,
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -0500973 int32_t displayId, std::optional<sp<IBinder>> token = std::nullopt)
chaviwd1c23182019-12-20 18:44:56 -0800974 : mName(name) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -0500975 if (token == std::nullopt) {
Garfield Tan15601662020-09-22 15:32:38 -0700976 base::Result<std::unique_ptr<InputChannel>> channel =
977 dispatcher->createInputChannel(name);
978 token = (*channel)->getConnectionToken();
979 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
chaviwd1c23182019-12-20 18:44:56 -0800980 }
981
982 inputApplicationHandle->updateInfo();
983 mInfo.applicationInfo = *inputApplicationHandle->getInfo();
984
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -0500985 mInfo.token = *token;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -0700986 mInfo.id = sId++;
chaviwd1c23182019-12-20 18:44:56 -0800987 mInfo.name = name;
chaviw3277faf2021-05-19 16:45:23 -0500988 mInfo.type = WindowInfo::Type::APPLICATION;
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500989 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000990 mInfo.alpha = 1.0;
chaviwd1c23182019-12-20 18:44:56 -0800991 mInfo.frameLeft = 0;
992 mInfo.frameTop = 0;
993 mInfo.frameRight = WIDTH;
994 mInfo.frameBottom = HEIGHT;
chaviw1ff3d1e2020-07-01 15:53:47 -0700995 mInfo.transform.set(0, 0);
chaviwd1c23182019-12-20 18:44:56 -0800996 mInfo.globalScaleFactor = 1.0;
997 mInfo.touchableRegion.clear();
998 mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT));
999 mInfo.visible = true;
Vishnu Nair47074b82020-08-14 11:54:47 -07001000 mInfo.focusable = false;
chaviwd1c23182019-12-20 18:44:56 -08001001 mInfo.hasWallpaper = false;
1002 mInfo.paused = false;
chaviwd1c23182019-12-20 18:44:56 -08001003 mInfo.ownerPid = INJECTOR_PID;
1004 mInfo.ownerUid = INJECTOR_UID;
chaviwd1c23182019-12-20 18:44:56 -08001005 mInfo.displayId = displayId;
Prabir Pradhand65552b2021-10-07 11:23:50 -07001006 mInfo.trustedOverlay = false;
chaviwd1c23182019-12-20 18:44:56 -08001007 }
1008
Arthur Hungabbb9d82021-09-01 14:52:30 +00001009 sp<FakeWindowHandle> clone(
1010 const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001011 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t displayId) {
Arthur Hungabbb9d82021-09-01 14:52:30 +00001012 sp<FakeWindowHandle> handle =
1013 new FakeWindowHandle(inputApplicationHandle, dispatcher, mInfo.name + "(Mirror)",
1014 displayId, mInfo.token);
1015 return handle;
1016 }
1017
Vishnu Nair47074b82020-08-14 11:54:47 -07001018 void setFocusable(bool focusable) { mInfo.focusable = focusable; }
chaviwd1c23182019-12-20 18:44:56 -08001019
Vishnu Nair958da932020-08-21 17:12:37 -07001020 void setVisible(bool visible) { mInfo.visible = visible; }
1021
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001022 void setDispatchingTimeout(std::chrono::nanoseconds timeout) {
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -05001023 mInfo.dispatchingTimeout = timeout;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001024 }
1025
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001026 void setPaused(bool paused) { mInfo.paused = paused; }
1027
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001028 void setAlpha(float alpha) { mInfo.alpha = alpha; }
1029
chaviw3277faf2021-05-19 16:45:23 -05001030 void setTouchOcclusionMode(TouchOcclusionMode mode) { mInfo.touchOcclusionMode = mode; }
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001031
Bernardo Rufino7393d172021-02-26 13:56:11 +00001032 void setApplicationToken(sp<IBinder> token) { mInfo.applicationInfo.token = token; }
1033
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001034 void setFrame(const Rect& frame, const ui::Transform& displayTransform = ui::Transform()) {
chaviwd1c23182019-12-20 18:44:56 -08001035 mInfo.frameLeft = frame.left;
1036 mInfo.frameTop = frame.top;
1037 mInfo.frameRight = frame.right;
1038 mInfo.frameBottom = frame.bottom;
1039 mInfo.touchableRegion.clear();
1040 mInfo.addTouchableRegion(frame);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001041
1042 const Rect logicalDisplayFrame = displayTransform.transform(frame);
1043 ui::Transform translate;
1044 translate.set(-logicalDisplayFrame.left, -logicalDisplayFrame.top);
1045 mInfo.transform = translate * displayTransform;
chaviwd1c23182019-12-20 18:44:56 -08001046 }
1047
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001048 void setTouchableRegion(const Region& region) { mInfo.touchableRegion = region; }
1049
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001050 void setType(WindowInfo::Type type) { mInfo.type = type; }
1051
1052 void setHasWallpaper(bool hasWallpaper) { mInfo.hasWallpaper = hasWallpaper; }
1053
chaviw3277faf2021-05-19 16:45:23 -05001054 void addFlags(Flags<WindowInfo::Flag> flags) { mInfo.flags |= flags; }
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00001055
chaviw3277faf2021-05-19 16:45:23 -05001056 void setFlags(Flags<WindowInfo::Flag> flags) { mInfo.flags = flags; }
chaviwd1c23182019-12-20 18:44:56 -08001057
Prabir Pradhand65552b2021-10-07 11:23:50 -07001058 void setInputFeatures(Flags<WindowInfo::Feature> features) { mInfo.inputFeatures = features; }
1059
1060 void setTrustedOverlay(bool trustedOverlay) { mInfo.trustedOverlay = trustedOverlay; }
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001061
chaviw9eaa22c2020-07-01 16:21:27 -07001062 void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) {
1063 mInfo.transform.set(dsdx, dtdx, dtdy, dsdy);
1064 }
1065
1066 void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); }
chaviwaf87b3e2019-10-01 16:59:28 -07001067
yunho.shinf4a80b82020-11-16 21:13:57 +09001068 void setWindowOffset(float offsetX, float offsetY) { mInfo.transform.set(offsetX, offsetY); }
1069
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001070 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
1071 consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId,
1072 expectedFlags);
1073 }
1074
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001075 void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
1076 consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, expectedDisplayId, expectedFlags);
1077 }
1078
Svet Ganov5d3bc372020-01-26 23:11:07 -08001079 void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001080 int32_t expectedFlags = 0) {
Svet Ganov5d3bc372020-01-26 23:11:07 -08001081 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, expectedDisplayId,
1082 expectedFlags);
1083 }
1084
1085 void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001086 int32_t expectedFlags = 0) {
Svet Ganov5d3bc372020-01-26 23:11:07 -08001087 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, expectedDisplayId,
1088 expectedFlags);
1089 }
1090
1091 void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001092 int32_t expectedFlags = 0) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001093 consumeAnyMotionDown(expectedDisplayId, expectedFlags);
1094 }
1095
1096 void consumeAnyMotionDown(std::optional<int32_t> expectedDisplayId = std::nullopt,
1097 std::optional<int32_t> expectedFlags = std::nullopt) {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001098 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId,
1099 expectedFlags);
1100 }
1101
Svet Ganov5d3bc372020-01-26 23:11:07 -08001102 void consumeMotionPointerDown(int32_t pointerIdx,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001103 int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1104 int32_t expectedFlags = 0) {
1105 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
1106 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001107 consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags);
1108 }
1109
1110 void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001111 int32_t expectedFlags = 0) {
1112 int32_t action = AMOTION_EVENT_ACTION_POINTER_UP |
1113 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001114 consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags);
1115 }
1116
1117 void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001118 int32_t expectedFlags = 0) {
Michael Wright3a240c42019-12-10 20:53:41 +00001119 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId,
1120 expectedFlags);
1121 }
1122
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05001123 void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1124 int32_t expectedFlags = 0) {
1125 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, expectedDisplayId,
1126 expectedFlags);
1127 }
1128
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001129 void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) {
1130 ASSERT_NE(mInputReceiver, nullptr)
1131 << "Cannot consume events from a window with no receiver";
1132 mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode);
1133 }
1134
Prabir Pradhan99987712020-11-10 18:43:05 -08001135 void consumeCaptureEvent(bool hasCapture) {
1136 ASSERT_NE(mInputReceiver, nullptr)
1137 << "Cannot consume events from a window with no receiver";
1138 mInputReceiver->consumeCaptureEvent(hasCapture);
1139 }
1140
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001141 void consumeEvent(int32_t expectedEventType, int32_t expectedAction,
1142 std::optional<int32_t> expectedDisplayId,
1143 std::optional<int32_t> expectedFlags) {
chaviwd1c23182019-12-20 18:44:56 -08001144 ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver";
1145 mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId,
1146 expectedFlags);
1147 }
1148
arthurhungb89ccb02020-12-30 16:19:01 +08001149 void consumeDragEvent(bool isExiting, float x, float y) {
1150 mInputReceiver->consumeDragEvent(isExiting, x, y);
1151 }
1152
Antonio Kantekf16f2832021-09-28 04:39:20 +00001153 void consumeTouchModeEvent(bool inTouchMode) {
1154 ASSERT_NE(mInputReceiver, nullptr)
1155 << "Cannot consume events from a window with no receiver";
1156 mInputReceiver->consumeTouchModeEvent(inTouchMode);
1157 }
1158
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001159 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001160 if (mInputReceiver == nullptr) {
1161 ADD_FAILURE() << "Invalid receive event on window with no receiver";
1162 return std::nullopt;
1163 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001164 return mInputReceiver->receiveEvent(outEvent);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001165 }
1166
1167 void finishEvent(uint32_t sequenceNum) {
1168 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
1169 mInputReceiver->finishEvent(sequenceNum);
1170 }
1171
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00001172 void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) {
1173 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
1174 mInputReceiver->sendTimeline(inputEventId, timeline);
1175 }
1176
chaviwaf87b3e2019-10-01 16:59:28 -07001177 InputEvent* consume() {
1178 if (mInputReceiver == nullptr) {
1179 return nullptr;
1180 }
1181 return mInputReceiver->consume();
1182 }
1183
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00001184 MotionEvent* consumeMotion() {
1185 InputEvent* event = consume();
1186 if (event == nullptr) {
1187 ADD_FAILURE() << "Consume failed : no event";
1188 return nullptr;
1189 }
1190 if (event->getType() != AINPUT_EVENT_TYPE_MOTION) {
1191 ADD_FAILURE() << "Instead of motion event, got "
1192 << inputEventTypeToString(event->getType());
1193 return nullptr;
1194 }
1195 return static_cast<MotionEvent*>(event);
1196 }
1197
Arthur Hungb92218b2018-08-14 12:00:21 +08001198 void assertNoEvents() {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001199 if (mInputReceiver == nullptr &&
chaviw3277faf2021-05-19 16:45:23 -05001200 mInfo.inputFeatures.test(WindowInfo::Feature::NO_INPUT_CHANNEL)) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001201 return; // Can't receive events if the window does not have input channel
1202 }
1203 ASSERT_NE(nullptr, mInputReceiver)
1204 << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL";
chaviwd1c23182019-12-20 18:44:56 -08001205 mInputReceiver->assertNoEvents();
Arthur Hungb92218b2018-08-14 12:00:21 +08001206 }
1207
chaviwaf87b3e2019-10-01 16:59:28 -07001208 sp<IBinder> getToken() { return mInfo.token; }
1209
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001210 const std::string& getName() { return mName; }
1211
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001212 void setOwnerInfo(int32_t ownerPid, int32_t ownerUid) {
1213 mInfo.ownerPid = ownerPid;
1214 mInfo.ownerUid = ownerUid;
1215 }
1216
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001217 void destroyReceiver() { mInputReceiver = nullptr; }
1218
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001219 int getChannelFd() { return mInputReceiver->getChannelFd(); }
1220
chaviwd1c23182019-12-20 18:44:56 -08001221private:
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001222 const std::string mName;
chaviwd1c23182019-12-20 18:44:56 -08001223 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001224 static std::atomic<int32_t> sId; // each window gets a unique id, like in surfaceflinger
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001225};
1226
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001227std::atomic<int32_t> FakeWindowHandle::sId{1};
1228
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001229static InputEventInjectionResult injectKey(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001230 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t repeatCount,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001231 int32_t displayId = ADISPLAY_ID_NONE,
1232 InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001233 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
1234 bool allowKeyRepeat = true) {
Arthur Hungb92218b2018-08-14 12:00:21 +08001235 KeyEvent event;
1236 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1237
1238 // Define a valid key down event.
Garfield Tanfbe732e2020-01-24 11:26:14 -08001239 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001240 INVALID_HMAC, action, /* flags */ 0, AKEYCODE_A, KEY_A, AMETA_NONE,
1241 repeatCount, currentTime, currentTime);
Arthur Hungb92218b2018-08-14 12:00:21 +08001242
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001243 int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER;
1244 if (!allowKeyRepeat) {
1245 policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT;
1246 }
Arthur Hungb92218b2018-08-14 12:00:21 +08001247 // Inject event until dispatch out.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001248 return dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, syncMode,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001249 injectionTimeout, policyFlags);
Arthur Hungb92218b2018-08-14 12:00:21 +08001250}
1251
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001252static InputEventInjectionResult injectKeyDown(const std::unique_ptr<InputDispatcher>& dispatcher,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001253 int32_t displayId = ADISPLAY_ID_NONE) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001254 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /* repeatCount */ 0, displayId);
1255}
1256
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001257// Inject a down event that has key repeat disabled. This allows InputDispatcher to idle without
1258// sending a subsequent key up. When key repeat is enabled, the dispatcher cannot idle because it
1259// has to be woken up to process the repeating key.
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001260static InputEventInjectionResult injectKeyDownNoRepeat(
1261 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t displayId = ADISPLAY_ID_NONE) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001262 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /* repeatCount */ 0, displayId,
1263 InputEventInjectionSync::WAIT_FOR_RESULT, INJECT_EVENT_TIMEOUT,
1264 /* allowKeyRepeat */ false);
1265}
1266
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001267static InputEventInjectionResult injectKeyUp(const std::unique_ptr<InputDispatcher>& dispatcher,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001268 int32_t displayId = ADISPLAY_ID_NONE) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001269 return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /* repeatCount */ 0, displayId);
1270}
1271
Garfield Tandf26e862020-07-01 20:18:19 -07001272class PointerBuilder {
1273public:
1274 PointerBuilder(int32_t id, int32_t toolType) {
1275 mProperties.clear();
1276 mProperties.id = id;
1277 mProperties.toolType = toolType;
1278 mCoords.clear();
1279 }
1280
1281 PointerBuilder& x(float x) { return axis(AMOTION_EVENT_AXIS_X, x); }
1282
1283 PointerBuilder& y(float y) { return axis(AMOTION_EVENT_AXIS_Y, y); }
1284
1285 PointerBuilder& axis(int32_t axis, float value) {
1286 mCoords.setAxisValue(axis, value);
1287 return *this;
1288 }
1289
1290 PointerProperties buildProperties() const { return mProperties; }
1291
1292 PointerCoords buildCoords() const { return mCoords; }
1293
1294private:
1295 PointerProperties mProperties;
1296 PointerCoords mCoords;
1297};
1298
1299class MotionEventBuilder {
1300public:
1301 MotionEventBuilder(int32_t action, int32_t source) {
1302 mAction = action;
1303 mSource = source;
1304 mEventTime = systemTime(SYSTEM_TIME_MONOTONIC);
1305 }
1306
1307 MotionEventBuilder& eventTime(nsecs_t eventTime) {
1308 mEventTime = eventTime;
1309 return *this;
1310 }
1311
1312 MotionEventBuilder& displayId(int32_t displayId) {
1313 mDisplayId = displayId;
1314 return *this;
1315 }
1316
1317 MotionEventBuilder& actionButton(int32_t actionButton) {
1318 mActionButton = actionButton;
1319 return *this;
1320 }
1321
arthurhung6d4bed92021-03-17 11:59:33 +08001322 MotionEventBuilder& buttonState(int32_t buttonState) {
1323 mButtonState = buttonState;
Garfield Tandf26e862020-07-01 20:18:19 -07001324 return *this;
1325 }
1326
1327 MotionEventBuilder& rawXCursorPosition(float rawXCursorPosition) {
1328 mRawXCursorPosition = rawXCursorPosition;
1329 return *this;
1330 }
1331
1332 MotionEventBuilder& rawYCursorPosition(float rawYCursorPosition) {
1333 mRawYCursorPosition = rawYCursorPosition;
1334 return *this;
1335 }
1336
1337 MotionEventBuilder& pointer(PointerBuilder pointer) {
1338 mPointers.push_back(pointer);
1339 return *this;
1340 }
1341
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08001342 MotionEventBuilder& addFlag(uint32_t flags) {
1343 mFlags |= flags;
1344 return *this;
1345 }
1346
Garfield Tandf26e862020-07-01 20:18:19 -07001347 MotionEvent build() {
1348 std::vector<PointerProperties> pointerProperties;
1349 std::vector<PointerCoords> pointerCoords;
1350 for (const PointerBuilder& pointer : mPointers) {
1351 pointerProperties.push_back(pointer.buildProperties());
1352 pointerCoords.push_back(pointer.buildCoords());
1353 }
1354
1355 // Set mouse cursor position for the most common cases to avoid boilerplate.
1356 if (mSource == AINPUT_SOURCE_MOUSE &&
1357 !MotionEvent::isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition) &&
1358 mPointers.size() == 1) {
1359 mRawXCursorPosition = pointerCoords[0].getX();
1360 mRawYCursorPosition = pointerCoords[0].getY();
1361 }
1362
1363 MotionEvent event;
chaviw9eaa22c2020-07-01 16:21:27 -07001364 ui::Transform identityTransform;
Garfield Tandf26e862020-07-01 20:18:19 -07001365 event.initialize(InputEvent::nextId(), DEVICE_ID, mSource, mDisplayId, INVALID_HMAC,
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08001366 mAction, mActionButton, mFlags, /* edgeFlags */ 0, AMETA_NONE,
chaviw9eaa22c2020-07-01 16:21:27 -07001367 mButtonState, MotionClassification::NONE, identityTransform,
1368 /* xPrecision */ 0, /* yPrecision */ 0, mRawXCursorPosition,
Prabir Pradhanb9b18502021-08-26 12:30:32 -07001369 mRawYCursorPosition, identityTransform, mEventTime, mEventTime,
1370 mPointers.size(), pointerProperties.data(), pointerCoords.data());
Garfield Tandf26e862020-07-01 20:18:19 -07001371
1372 return event;
1373 }
1374
1375private:
1376 int32_t mAction;
1377 int32_t mSource;
1378 nsecs_t mEventTime;
1379 int32_t mDisplayId{ADISPLAY_ID_DEFAULT};
1380 int32_t mActionButton{0};
1381 int32_t mButtonState{0};
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08001382 int32_t mFlags{0};
Garfield Tandf26e862020-07-01 20:18:19 -07001383 float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
1384 float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
1385
1386 std::vector<PointerBuilder> mPointers;
1387};
1388
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001389static InputEventInjectionResult injectMotionEvent(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001390 const std::unique_ptr<InputDispatcher>& dispatcher, const MotionEvent& event,
Garfield Tandf26e862020-07-01 20:18:19 -07001391 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001392 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT) {
Garfield Tandf26e862020-07-01 20:18:19 -07001393 return dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, injectionMode,
1394 injectionTimeout,
1395 POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
1396}
1397
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001398static InputEventInjectionResult injectMotionEvent(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001399 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t source,
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001400 int32_t displayId, const PointF& position = {100, 200},
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001401 const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001402 AMOTION_EVENT_INVALID_CURSOR_POSITION},
1403 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001404 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001405 nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC)) {
Garfield Tandf26e862020-07-01 20:18:19 -07001406 MotionEvent event = MotionEventBuilder(action, source)
1407 .displayId(displayId)
1408 .eventTime(eventTime)
1409 .rawXCursorPosition(cursorPosition.x)
1410 .rawYCursorPosition(cursorPosition.y)
1411 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
1412 .x(position.x)
1413 .y(position.y))
1414 .build();
Arthur Hungb92218b2018-08-14 12:00:21 +08001415
1416 // Inject event until dispatch out.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001417 return injectMotionEvent(dispatcher, event, injectionTimeout, injectionMode);
Arthur Hungb92218b2018-08-14 12:00:21 +08001418}
1419
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001420static InputEventInjectionResult injectMotionDown(
1421 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t source, int32_t displayId,
1422 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001423 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location);
Garfield Tan00f511d2019-06-12 16:55:40 -07001424}
1425
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001426static InputEventInjectionResult injectMotionUp(const std::unique_ptr<InputDispatcher>& dispatcher,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001427 int32_t source, int32_t displayId,
1428 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001429 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location);
Michael Wright3a240c42019-12-10 20:53:41 +00001430}
1431
Jackal Guof9696682018-10-05 12:23:23 +08001432static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) {
1433 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1434 // Define a valid key event.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001435 NotifyKeyArgs args(/* id */ 0, currentTime, 0 /*readTime*/, DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
1436 displayId, POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0, AKEYCODE_A,
1437 KEY_A, AMETA_NONE, currentTime);
Jackal Guof9696682018-10-05 12:23:23 +08001438
1439 return args;
1440}
1441
chaviwd1c23182019-12-20 18:44:56 -08001442static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId,
1443 const std::vector<PointF>& points) {
1444 size_t pointerCount = points.size();
chaviwaf87b3e2019-10-01 16:59:28 -07001445 if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) {
1446 EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer";
1447 }
1448
chaviwd1c23182019-12-20 18:44:56 -08001449 PointerProperties pointerProperties[pointerCount];
1450 PointerCoords pointerCoords[pointerCount];
Jackal Guof9696682018-10-05 12:23:23 +08001451
chaviwd1c23182019-12-20 18:44:56 -08001452 for (size_t i = 0; i < pointerCount; i++) {
1453 pointerProperties[i].clear();
1454 pointerProperties[i].id = i;
1455 pointerProperties[i].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jackal Guof9696682018-10-05 12:23:23 +08001456
chaviwd1c23182019-12-20 18:44:56 -08001457 pointerCoords[i].clear();
1458 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x);
1459 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y);
1460 }
Jackal Guof9696682018-10-05 12:23:23 +08001461
1462 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1463 // Define a valid motion event.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001464 NotifyMotionArgs args(/* id */ 0, currentTime, 0 /*readTime*/, DEVICE_ID, source, displayId,
Garfield Tan00f511d2019-06-12 16:55:40 -07001465 POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0,
1466 AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
chaviwd1c23182019-12-20 18:44:56 -08001467 AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties,
1468 pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0,
Garfield Tan00f511d2019-06-12 16:55:40 -07001469 AMOTION_EVENT_INVALID_CURSOR_POSITION,
1470 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {});
Jackal Guof9696682018-10-05 12:23:23 +08001471
1472 return args;
1473}
1474
chaviwd1c23182019-12-20 18:44:56 -08001475static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) {
1476 return generateMotionArgs(action, source, displayId, {PointF{100, 200}});
1477}
1478
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001479static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs(
1480 const PointerCaptureRequest& request) {
1481 return NotifyPointerCaptureChangedArgs(/* id */ 0, systemTime(SYSTEM_TIME_MONOTONIC), request);
Prabir Pradhan99987712020-11-10 18:43:05 -08001482}
1483
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001484/**
1485 * When a window unexpectedly disposes of its input channel, policy should be notified about the
1486 * broken channel.
1487 */
1488TEST_F(InputDispatcherTest, WhenInputChannelBreaks_PolicyIsNotified) {
1489 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1490 sp<FakeWindowHandle> window =
1491 new FakeWindowHandle(application, mDispatcher, "Window that breaks its input channel",
1492 ADISPLAY_ID_DEFAULT);
1493
1494 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1495
1496 // Window closes its channel, but the window remains.
1497 window->destroyReceiver();
1498 mFakePolicy->assertNotifyInputChannelBrokenWasCalled(window->getInfo()->token);
1499}
1500
Arthur Hungb92218b2018-08-14 12:00:21 +08001501TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001502 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001503 sp<FakeWindowHandle> window =
1504 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001505
Arthur Hung72d8dc32020-03-28 00:48:39 +00001506 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001507 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1508 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1509 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001510
1511 // Window should receive motion event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001512 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001513}
1514
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001515TEST_F(InputDispatcherTest, WhenDisplayNotSpecified_InjectMotionToDefaultDisplay) {
1516 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1517 sp<FakeWindowHandle> window =
1518 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1519
1520 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1521 // Inject a MotionEvent to an unknown display.
1522 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1523 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_NONE))
1524 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1525
1526 // Window should receive motion event.
1527 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1528}
1529
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001530/**
1531 * Calling setInputWindows once with FLAG_NOT_TOUCH_MODAL should not cause any issues.
1532 * To ensure that window receives only events that were directly inside of it, add
1533 * FLAG_NOT_TOUCH_MODAL. This will enforce using the touchableRegion of the input
1534 * when finding touched windows.
1535 * This test serves as a sanity check for the next test, where setInputWindows is
1536 * called twice.
1537 */
1538TEST_F(InputDispatcherTest, SetInputWindowOnce_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001539 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001540 sp<FakeWindowHandle> window =
1541 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1542 window->setFrame(Rect(0, 0, 100, 100));
chaviw3277faf2021-05-19 16:45:23 -05001543 window->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001544
1545 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001546 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001547 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1548 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001549 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001550
1551 // Window should receive motion event.
1552 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1553}
1554
1555/**
1556 * Calling setInputWindows twice, with the same info, should not cause any issues.
1557 * To ensure that window receives only events that were directly inside of it, add
1558 * FLAG_NOT_TOUCH_MODAL. This will enforce using the touchableRegion of the input
1559 * when finding touched windows.
1560 */
1561TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001562 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001563 sp<FakeWindowHandle> window =
1564 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1565 window->setFrame(Rect(0, 0, 100, 100));
chaviw3277faf2021-05-19 16:45:23 -05001566 window->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001567
1568 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1569 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001570 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001571 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1572 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001573 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001574
1575 // Window should receive motion event.
1576 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1577}
1578
Arthur Hungb92218b2018-08-14 12:00:21 +08001579// The foreground window should receive the first touch down event.
1580TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001581 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001582 sp<FakeWindowHandle> windowTop =
1583 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
1584 sp<FakeWindowHandle> windowSecond =
1585 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001586
Arthur Hung72d8dc32020-03-28 00:48:39 +00001587 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001588 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1589 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1590 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001591
1592 // Top window should receive the touch down event. Second window should not receive anything.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001593 windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001594 windowSecond->assertNoEvents();
1595}
1596
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001597/**
1598 * Two windows: A top window, and a wallpaper behind the window.
1599 * Touch goes to the top window, and then top window disappears. Ensure that wallpaper window
1600 * gets ACTION_CANCEL.
1601 * 1. foregroundWindow <-- has wallpaper (hasWallpaper=true)
1602 * 2. wallpaperWindow <-- is wallpaper (type=InputWindowInfo::Type::WALLPAPER)
1603 */
1604TEST_F(InputDispatcherTest, WhenForegroundWindowDisappears_WallpaperTouchIsCanceled) {
1605 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1606 sp<FakeWindowHandle> foregroundWindow =
1607 new FakeWindowHandle(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
1608 foregroundWindow->setHasWallpaper(true);
1609 sp<FakeWindowHandle> wallpaperWindow =
1610 new FakeWindowHandle(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
1611 wallpaperWindow->setType(WindowInfo::Type::WALLPAPER);
1612 constexpr int expectedWallpaperFlags =
1613 AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED;
1614
1615 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}});
1616 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1617 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1618 {100, 200}))
1619 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1620
1621 // Both foreground window and its wallpaper should receive the touch down
1622 foregroundWindow->consumeMotionDown();
1623 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1624
1625 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1626 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
1627 ADISPLAY_ID_DEFAULT, {110, 200}))
1628 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1629
1630 foregroundWindow->consumeMotionMove();
1631 wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1632
1633 // Now the foreground window goes away, but the wallpaper stays
1634 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}});
1635 foregroundWindow->consumeMotionCancel();
1636 // Since the "parent" window of the wallpaper is gone, wallpaper should receive cancel, too.
1637 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1638}
1639
1640/**
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001641 * Same test as WhenForegroundWindowDisappears_WallpaperTouchIsCanceled above,
1642 * with the following differences:
1643 * After ACTION_DOWN, Wallpaper window hangs up its channel, which forces the dispatcher to
1644 * clean up the connection.
1645 * This later may crash dispatcher during ACTION_CANCEL synthesis, if the dispatcher is not careful.
1646 * Ensure that there's no crash in the dispatcher.
1647 */
1648TEST_F(InputDispatcherTest, WhenWallpaperDisappears_NoCrash) {
1649 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1650 sp<FakeWindowHandle> foregroundWindow =
1651 new FakeWindowHandle(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
1652 foregroundWindow->setHasWallpaper(true);
1653 sp<FakeWindowHandle> wallpaperWindow =
1654 new FakeWindowHandle(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
1655 wallpaperWindow->setType(WindowInfo::Type::WALLPAPER);
1656 constexpr int expectedWallpaperFlags =
1657 AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED;
1658
1659 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}});
1660 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1661 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1662 {100, 200}))
1663 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1664
1665 // Both foreground window and its wallpaper should receive the touch down
1666 foregroundWindow->consumeMotionDown();
1667 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1668
1669 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1670 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
1671 ADISPLAY_ID_DEFAULT, {110, 200}))
1672 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1673
1674 foregroundWindow->consumeMotionMove();
1675 wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1676
1677 // Wallpaper closes its channel, but the window remains.
1678 wallpaperWindow->destroyReceiver();
1679 mFakePolicy->assertNotifyInputChannelBrokenWasCalled(wallpaperWindow->getInfo()->token);
1680
1681 // Now the foreground window goes away, but the wallpaper stays, even though its channel
1682 // is no longer valid.
1683 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}});
1684 foregroundWindow->consumeMotionCancel();
1685}
1686
1687/**
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001688 * A single window that receives touch (on top), and a wallpaper window underneath it.
1689 * The top window gets a multitouch gesture.
1690 * Ensure that wallpaper gets the same gesture.
1691 */
1692TEST_F(InputDispatcherTest, WallpaperWindow_ReceivesMultiTouch) {
1693 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1694 sp<FakeWindowHandle> window =
1695 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
1696 window->setHasWallpaper(true);
1697
1698 sp<FakeWindowHandle> wallpaperWindow =
1699 new FakeWindowHandle(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
1700 wallpaperWindow->setType(WindowInfo::Type::WALLPAPER);
1701 constexpr int expectedWallpaperFlags =
1702 AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED;
1703
1704 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, wallpaperWindow}}});
1705
1706 // Touch down on top window
1707 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1708 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1709 {100, 100}))
1710 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1711
1712 // Both top window and its wallpaper should receive the touch down
1713 window->consumeMotionDown();
1714 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1715
1716 // Second finger down on the top window
1717 const MotionEvent secondFingerDownEvent =
1718 MotionEventBuilder(AMOTION_EVENT_ACTION_POINTER_DOWN |
1719 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1720 AINPUT_SOURCE_TOUCHSCREEN)
1721 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
1722 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
1723 .x(100)
1724 .y(100))
1725 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER)
1726 .x(150)
1727 .y(150))
1728 .build();
1729 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1730 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
1731 InputEventInjectionSync::WAIT_FOR_RESULT))
1732 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1733
1734 window->consumeMotionPointerDown(1 /* pointerIndex */);
1735 wallpaperWindow->consumeMotionPointerDown(1 /* pointerIndex */, ADISPLAY_ID_DEFAULT,
1736 expectedWallpaperFlags);
1737 window->assertNoEvents();
1738 wallpaperWindow->assertNoEvents();
1739}
1740
1741/**
1742 * Two windows: a window on the left and window on the right.
1743 * A third window, wallpaper, is behind both windows, and spans both top windows.
1744 * The first touch down goes to the left window. A second pointer touches down on the right window.
1745 * The touch is split, so both left and right windows should receive ACTION_DOWN.
1746 * The wallpaper will get the full event, so it should receive ACTION_DOWN followed by
1747 * ACTION_POINTER_DOWN(1).
1748 */
1749TEST_F(InputDispatcherTest, TwoWindows_SplitWallpaperTouch) {
1750 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1751 sp<FakeWindowHandle> leftWindow =
1752 new FakeWindowHandle(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
1753 leftWindow->setFrame(Rect(0, 0, 200, 200));
1754 leftWindow->setFlags(WindowInfo::Flag::SPLIT_TOUCH | WindowInfo::Flag::NOT_TOUCH_MODAL);
1755 leftWindow->setHasWallpaper(true);
1756
1757 sp<FakeWindowHandle> rightWindow =
1758 new FakeWindowHandle(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
1759 rightWindow->setFrame(Rect(200, 0, 400, 200));
1760 rightWindow->setFlags(WindowInfo::Flag::SPLIT_TOUCH | WindowInfo::Flag::NOT_TOUCH_MODAL);
1761 rightWindow->setHasWallpaper(true);
1762
1763 sp<FakeWindowHandle> wallpaperWindow =
1764 new FakeWindowHandle(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
1765 wallpaperWindow->setFrame(Rect(0, 0, 400, 200));
1766 wallpaperWindow->setType(WindowInfo::Type::WALLPAPER);
1767 constexpr int expectedWallpaperFlags =
1768 AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED;
1769
1770 mDispatcher->setInputWindows(
1771 {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}});
1772
1773 // Touch down on left window
1774 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1775 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1776 {100, 100}))
1777 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1778
1779 // Both foreground window and its wallpaper should receive the touch down
1780 leftWindow->consumeMotionDown();
1781 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1782
1783 // Second finger down on the right window
1784 const MotionEvent secondFingerDownEvent =
1785 MotionEventBuilder(AMOTION_EVENT_ACTION_POINTER_DOWN |
1786 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1787 AINPUT_SOURCE_TOUCHSCREEN)
1788 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
1789 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
1790 .x(100)
1791 .y(100))
1792 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER)
1793 .x(300)
1794 .y(100))
1795 .build();
1796 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1797 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
1798 InputEventInjectionSync::WAIT_FOR_RESULT))
1799 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1800
1801 leftWindow->consumeMotionMove();
1802 // Since the touch is split, right window gets ACTION_DOWN
1803 rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1804 wallpaperWindow->consumeMotionPointerDown(1 /* pointerIndex */, ADISPLAY_ID_DEFAULT,
1805 expectedWallpaperFlags);
1806
1807 // Now, leftWindow, which received the first finger, disappears.
1808 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {rightWindow, wallpaperWindow}}});
1809 leftWindow->consumeMotionCancel();
1810 // Since a "parent" window of the wallpaper is gone, wallpaper should receive cancel, too.
1811 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1812
1813 // The pointer that's still down on the right window moves, and goes to the right window only.
1814 // As far as the dispatcher's concerned though, both pointers are still present.
1815 const MotionEvent secondFingerMoveEvent =
1816 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
1817 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
1818 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
1819 .x(100)
1820 .y(100))
1821 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER)
1822 .x(310)
1823 .y(110))
1824 .build();
1825 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1826 injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT,
1827 InputEventInjectionSync::WAIT_FOR_RESULT));
1828 rightWindow->consumeMotionMove();
1829
1830 leftWindow->assertNoEvents();
1831 rightWindow->assertNoEvents();
1832 wallpaperWindow->assertNoEvents();
1833}
1834
Garfield Tandf26e862020-07-01 20:18:19 -07001835TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) {
Chris Yea209fde2020-07-22 13:54:51 -07001836 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07001837 sp<FakeWindowHandle> windowLeft =
1838 new FakeWindowHandle(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
1839 windowLeft->setFrame(Rect(0, 0, 600, 800));
chaviw3277faf2021-05-19 16:45:23 -05001840 windowLeft->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tandf26e862020-07-01 20:18:19 -07001841 sp<FakeWindowHandle> windowRight =
1842 new FakeWindowHandle(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
1843 windowRight->setFrame(Rect(600, 0, 1200, 800));
chaviw3277faf2021-05-19 16:45:23 -05001844 windowRight->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tandf26e862020-07-01 20:18:19 -07001845
1846 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1847
1848 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
1849
1850 // Start cursor position in right window so that we can move the cursor to left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001851 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001852 injectMotionEvent(mDispatcher,
1853 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
1854 AINPUT_SOURCE_MOUSE)
1855 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1856 .x(900)
1857 .y(400))
1858 .build()));
1859 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1860 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1861 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE,
1862 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1863
1864 // Move cursor into left window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001865 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001866 injectMotionEvent(mDispatcher,
1867 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
1868 AINPUT_SOURCE_MOUSE)
1869 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1870 .x(300)
1871 .y(400))
1872 .build()));
1873 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT,
1874 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1875 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1876 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1877 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE,
1878 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1879
1880 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001881 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001882 injectMotionEvent(mDispatcher,
1883 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
1884 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1885 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1886 .x(300)
1887 .y(400))
1888 .build()));
1889 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1890
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001891 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001892 injectMotionEvent(mDispatcher,
1893 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
1894 AINPUT_SOURCE_MOUSE)
1895 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1896 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1897 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1898 .x(300)
1899 .y(400))
1900 .build()));
1901 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_PRESS,
1902 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1903
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001904 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001905 injectMotionEvent(mDispatcher,
1906 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
1907 AINPUT_SOURCE_MOUSE)
1908 .buttonState(0)
1909 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1910 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1911 .x(300)
1912 .y(400))
1913 .build()));
1914 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_RELEASE,
1915 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1916
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001917 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001918 injectMotionEvent(mDispatcher,
1919 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
1920 .buttonState(0)
1921 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1922 .x(300)
1923 .y(400))
1924 .build()));
1925 windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT);
1926
1927 // Move mouse cursor back to right window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001928 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001929 injectMotionEvent(mDispatcher,
1930 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
1931 AINPUT_SOURCE_MOUSE)
1932 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1933 .x(900)
1934 .y(400))
1935 .build()));
1936 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT,
1937 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1938 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1939 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1940 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE,
1941 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1942}
1943
1944// This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected
1945// directly in this test.
1946TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) {
Chris Yea209fde2020-07-22 13:54:51 -07001947 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07001948 sp<FakeWindowHandle> window =
1949 new FakeWindowHandle(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
1950 window->setFrame(Rect(0, 0, 1200, 800));
chaviw3277faf2021-05-19 16:45:23 -05001951 window->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tandf26e862020-07-01 20:18:19 -07001952
1953 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1954
1955 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1956
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001957 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001958 injectMotionEvent(mDispatcher,
1959 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
1960 AINPUT_SOURCE_MOUSE)
1961 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1962 .x(300)
1963 .y(400))
1964 .build()));
1965 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1966 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1967
1968 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001969 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001970 injectMotionEvent(mDispatcher,
1971 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
1972 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1973 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1974 .x(300)
1975 .y(400))
1976 .build()));
1977 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1978
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001979 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001980 injectMotionEvent(mDispatcher,
1981 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
1982 AINPUT_SOURCE_MOUSE)
1983 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1984 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1985 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1986 .x(300)
1987 .y(400))
1988 .build()));
1989 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_PRESS,
1990 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1991
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001992 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001993 injectMotionEvent(mDispatcher,
1994 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
1995 AINPUT_SOURCE_MOUSE)
1996 .buttonState(0)
1997 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1998 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1999 .x(300)
2000 .y(400))
2001 .build()));
2002 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_RELEASE,
2003 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
2004
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002005 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002006 injectMotionEvent(mDispatcher,
2007 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
2008 .buttonState(0)
2009 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2010 .x(300)
2011 .y(400))
2012 .build()));
2013 window->consumeMotionUp(ADISPLAY_ID_DEFAULT);
2014
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002015 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002016 injectMotionEvent(mDispatcher,
2017 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT,
2018 AINPUT_SOURCE_MOUSE)
2019 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
2020 .x(300)
2021 .y(400))
2022 .build()));
2023 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT,
2024 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
2025}
2026
Garfield Tan00f511d2019-06-12 16:55:40 -07002027TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) {
Chris Yea209fde2020-07-22 13:54:51 -07002028 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tan00f511d2019-06-12 16:55:40 -07002029
2030 sp<FakeWindowHandle> windowLeft =
2031 new FakeWindowHandle(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2032 windowLeft->setFrame(Rect(0, 0, 600, 800));
chaviw3277faf2021-05-19 16:45:23 -05002033 windowLeft->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tan00f511d2019-06-12 16:55:40 -07002034 sp<FakeWindowHandle> windowRight =
2035 new FakeWindowHandle(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2036 windowRight->setFrame(Rect(600, 0, 1200, 800));
chaviw3277faf2021-05-19 16:45:23 -05002037 windowRight->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tan00f511d2019-06-12 16:55:40 -07002038
2039 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2040
Arthur Hung72d8dc32020-03-28 00:48:39 +00002041 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
Garfield Tan00f511d2019-06-12 16:55:40 -07002042
2043 // Inject an event with coordinate in the area of right window, with mouse cursor in the area of
2044 // left window. This event should be dispatched to the left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002045 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tan00f511d2019-06-12 16:55:40 -07002046 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07002047 ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400}));
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002048 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07002049 windowRight->assertNoEvents();
2050}
2051
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002052TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) {
Chris Yea209fde2020-07-22 13:54:51 -07002053 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002054 sp<FakeWindowHandle> window =
2055 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Vishnu Nair47074b82020-08-14 11:54:47 -07002056 window->setFocusable(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002057
Arthur Hung72d8dc32020-03-28 00:48:39 +00002058 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002059 setFocusedWindow(window);
2060
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002061 window->consumeFocusEvent(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002062
2063 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
2064 mDispatcher->notifyKey(&keyArgs);
2065
2066 // Window should receive key down event.
2067 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2068
2069 // When device reset happens, that key stream should be terminated with FLAG_CANCELED
2070 // on the app side.
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002071 NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002072 mDispatcher->notifyDeviceReset(&args);
2073 window->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
2074 AKEY_EVENT_FLAG_CANCELED);
2075}
2076
2077TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) {
Chris Yea209fde2020-07-22 13:54:51 -07002078 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002079 sp<FakeWindowHandle> window =
2080 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
2081
Arthur Hung72d8dc32020-03-28 00:48:39 +00002082 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002083
2084 NotifyMotionArgs motionArgs =
2085 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2086 ADISPLAY_ID_DEFAULT);
2087 mDispatcher->notifyMotion(&motionArgs);
2088
2089 // Window should receive motion down event.
2090 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2091
2092 // When device reset happens, that motion stream should be terminated with ACTION_CANCEL
2093 // on the app side.
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002094 NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002095 mDispatcher->notifyDeviceReset(&args);
2096 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, ADISPLAY_ID_DEFAULT,
2097 0 /*expectedFlags*/);
2098}
2099
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07002100/**
2101 * Ensure the correct coordinate spaces are used by InputDispatcher.
2102 *
2103 * InputDispatcher works in the display space, so its coordinate system is relative to the display
2104 * panel. Windows get events in the window space, and get raw coordinates in the logical display
2105 * space.
2106 */
2107class InputDispatcherDisplayProjectionTest : public InputDispatcherTest {
2108public:
2109 void SetUp() override {
2110 InputDispatcherTest::SetUp();
2111 mDisplayInfos.clear();
2112 mWindowInfos.clear();
2113 }
2114
2115 void addDisplayInfo(int displayId, const ui::Transform& transform) {
2116 gui::DisplayInfo info;
2117 info.displayId = displayId;
2118 info.transform = transform;
2119 mDisplayInfos.push_back(std::move(info));
2120 mDispatcher->onWindowInfosChanged(mWindowInfos, mDisplayInfos);
2121 }
2122
2123 void addWindow(const sp<WindowInfoHandle>& windowHandle) {
2124 mWindowInfos.push_back(*windowHandle->getInfo());
2125 mDispatcher->onWindowInfosChanged(mWindowInfos, mDisplayInfos);
2126 }
2127
2128 // Set up a test scenario where the display has a scaled projection and there are two windows
2129 // on the display.
2130 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupScaledDisplayScenario() {
2131 // The display has a projection that has a scale factor of 2 and 4 in the x and y directions
2132 // respectively.
2133 ui::Transform displayTransform;
2134 displayTransform.set(2, 0, 0, 4);
2135 addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform);
2136
2137 std::shared_ptr<FakeApplicationHandle> application =
2138 std::make_shared<FakeApplicationHandle>();
2139
2140 // Add two windows to the display. Their frames are represented in the display space.
2141 sp<FakeWindowHandle> firstWindow =
2142 new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT);
2143 firstWindow->addFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
2144 firstWindow->setFrame(Rect(0, 0, 100, 200), displayTransform);
2145 addWindow(firstWindow);
2146
2147 sp<FakeWindowHandle> secondWindow =
2148 new FakeWindowHandle(application, mDispatcher, "Second Window",
2149 ADISPLAY_ID_DEFAULT);
2150 secondWindow->addFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
2151 secondWindow->setFrame(Rect(100, 200, 200, 400), displayTransform);
2152 addWindow(secondWindow);
2153 return {std::move(firstWindow), std::move(secondWindow)};
2154 }
2155
2156private:
2157 std::vector<gui::DisplayInfo> mDisplayInfos;
2158 std::vector<gui::WindowInfo> mWindowInfos;
2159};
2160
2161TEST_F(InputDispatcherDisplayProjectionTest, HitTestsInDisplaySpace) {
2162 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
2163 // Send down to the first window. The point is represented in the display space. The point is
2164 // selected so that if the hit test was done with the transform applied to it, then it would
2165 // end up in the incorrect window.
2166 NotifyMotionArgs downMotionArgs =
2167 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2168 ADISPLAY_ID_DEFAULT, {PointF{75, 55}});
2169 mDispatcher->notifyMotion(&downMotionArgs);
2170
2171 firstWindow->consumeMotionDown();
2172 secondWindow->assertNoEvents();
2173}
2174
2175// Ensure that when a MotionEvent is injected through the InputDispatcher::injectInputEvent() API,
2176// the event should be treated as being in the logical display space.
2177TEST_F(InputDispatcherDisplayProjectionTest, InjectionInLogicalDisplaySpace) {
2178 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
2179 // Send down to the first window. The point is represented in the logical display space. The
2180 // point is selected so that if the hit test was done in logical display space, then it would
2181 // end up in the incorrect window.
2182 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2183 PointF{75 * 2, 55 * 4});
2184
2185 firstWindow->consumeMotionDown();
2186 secondWindow->assertNoEvents();
2187}
2188
Prabir Pradhandaa2f142021-12-10 09:30:08 +00002189// Ensure that when a MotionEvent that has a custom transform is injected, the post-transformed
2190// event should be treated as being in the logical display space.
2191TEST_F(InputDispatcherDisplayProjectionTest, InjectionWithTransformInLogicalDisplaySpace) {
2192 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
2193
2194 const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0};
2195 ui::Transform injectedEventTransform;
2196 injectedEventTransform.set(matrix);
2197 const vec2 expectedPoint{75, 55}; // The injected point in the logical display space.
2198 const vec2 untransformedPoint = injectedEventTransform.inverse().transform(expectedPoint);
2199
2200 MotionEvent event = MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2201 .displayId(ADISPLAY_ID_DEFAULT)
2202 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
2203 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
2204 .x(untransformedPoint.x)
2205 .y(untransformedPoint.y))
2206 .build();
2207 event.transform(matrix);
2208
2209 injectMotionEvent(mDispatcher, event, INJECT_EVENT_TIMEOUT,
2210 InputEventInjectionSync::WAIT_FOR_RESULT);
2211
2212 firstWindow->consumeMotionDown();
2213 secondWindow->assertNoEvents();
2214}
2215
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07002216TEST_F(InputDispatcherDisplayProjectionTest, WindowGetsEventsInCorrectCoordinateSpace) {
2217 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
2218
2219 // Send down to the second window.
2220 NotifyMotionArgs downMotionArgs =
2221 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2222 ADISPLAY_ID_DEFAULT, {PointF{150, 220}});
2223 mDispatcher->notifyMotion(&downMotionArgs);
2224
2225 firstWindow->assertNoEvents();
2226 const MotionEvent* event = secondWindow->consumeMotion();
2227 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, event->getAction());
2228
2229 // Ensure that the events from the "getRaw" API are in logical display coordinates.
2230 EXPECT_EQ(300, event->getRawX(0));
2231 EXPECT_EQ(880, event->getRawY(0));
2232
2233 // Ensure that the x and y values are in the window's coordinate space.
2234 // The left-top of the second window is at (100, 200) in display space, which is (200, 800) in
2235 // the logical display space. This will be the origin of the window space.
2236 EXPECT_EQ(100, event->getX(0));
2237 EXPECT_EQ(80, event->getY(0));
2238}
2239
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002240using TransferFunction = std::function<bool(const std::unique_ptr<InputDispatcher>& dispatcher,
2241 sp<IBinder>, sp<IBinder>)>;
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00002242
2243class TransferTouchFixture : public InputDispatcherTest,
2244 public ::testing::WithParamInterface<TransferFunction> {};
2245
2246TEST_P(TransferTouchFixture, TransferTouch_OnePointer) {
Chris Yea209fde2020-07-22 13:54:51 -07002247 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08002248
2249 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002250 sp<FakeWindowHandle> firstWindow =
2251 new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT);
2252 sp<FakeWindowHandle> secondWindow =
2253 new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08002254
2255 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00002256 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08002257
2258 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002259 NotifyMotionArgs downMotionArgs =
2260 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2261 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08002262 mDispatcher->notifyMotion(&downMotionArgs);
2263 // Only the first window should get the down event
2264 firstWindow->consumeMotionDown();
2265 secondWindow->assertNoEvents();
2266
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00002267 // Transfer touch to the second window
2268 TransferFunction f = GetParam();
2269 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
2270 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08002271 // The first window gets cancel and the second gets down
2272 firstWindow->consumeMotionCancel();
2273 secondWindow->consumeMotionDown();
2274
2275 // Send up event to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002276 NotifyMotionArgs upMotionArgs =
2277 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
2278 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08002279 mDispatcher->notifyMotion(&upMotionArgs);
2280 // The first window gets no events and the second gets up
2281 firstWindow->assertNoEvents();
2282 secondWindow->consumeMotionUp();
2283}
2284
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00002285TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07002286 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08002287
2288 PointF touchPoint = {10, 10};
2289
2290 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002291 sp<FakeWindowHandle> firstWindow =
2292 new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT);
2293 sp<FakeWindowHandle> secondWindow =
2294 new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08002295
2296 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00002297 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08002298
2299 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002300 NotifyMotionArgs downMotionArgs =
2301 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2302 ADISPLAY_ID_DEFAULT, {touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08002303 mDispatcher->notifyMotion(&downMotionArgs);
2304 // Only the first window should get the down event
2305 firstWindow->consumeMotionDown();
2306 secondWindow->assertNoEvents();
2307
2308 // Send pointer down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002309 NotifyMotionArgs pointerDownMotionArgs =
2310 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN |
2311 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2312 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2313 {touchPoint, touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08002314 mDispatcher->notifyMotion(&pointerDownMotionArgs);
2315 // Only the first window should get the pointer down event
2316 firstWindow->consumeMotionPointerDown(1);
2317 secondWindow->assertNoEvents();
2318
2319 // Transfer touch focus to the second window
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00002320 TransferFunction f = GetParam();
2321 bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
2322 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08002323 // The first window gets cancel and the second gets down and pointer down
2324 firstWindow->consumeMotionCancel();
2325 secondWindow->consumeMotionDown();
2326 secondWindow->consumeMotionPointerDown(1);
2327
2328 // Send pointer up to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002329 NotifyMotionArgs pointerUpMotionArgs =
2330 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP |
2331 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2332 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2333 {touchPoint, touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08002334 mDispatcher->notifyMotion(&pointerUpMotionArgs);
2335 // The first window gets nothing and the second gets pointer up
2336 firstWindow->assertNoEvents();
2337 secondWindow->consumeMotionPointerUp(1);
2338
2339 // Send up event to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002340 NotifyMotionArgs upMotionArgs =
2341 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
2342 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08002343 mDispatcher->notifyMotion(&upMotionArgs);
2344 // The first window gets nothing and the second gets up
2345 firstWindow->assertNoEvents();
2346 secondWindow->consumeMotionUp();
2347}
2348
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00002349// For the cases of single pointer touch and two pointers non-split touch, the api's
2350// 'transferTouch' and 'transferTouchFocus' are equivalent in behaviour. They only differ
2351// for the case where there are multiple pointers split across several windows.
2352INSTANTIATE_TEST_SUITE_P(TransferFunctionTests, TransferTouchFixture,
2353 ::testing::Values(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002354 [&](const std::unique_ptr<InputDispatcher>& dispatcher,
2355 sp<IBinder> /*ignored*/, sp<IBinder> destChannelToken) {
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00002356 return dispatcher->transferTouch(destChannelToken);
2357 },
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002358 [&](const std::unique_ptr<InputDispatcher>& dispatcher,
2359 sp<IBinder> from, sp<IBinder> to) {
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00002360 return dispatcher->transferTouchFocus(from, to,
2361 false /*isDragAndDrop*/);
2362 }));
2363
Svet Ganov5d3bc372020-01-26 23:11:07 -08002364TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07002365 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08002366
2367 // Create a non touch modal window that supports split touch
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002368 sp<FakeWindowHandle> firstWindow =
2369 new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08002370 firstWindow->setFrame(Rect(0, 0, 600, 400));
chaviw3277faf2021-05-19 16:45:23 -05002371 firstWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH);
Svet Ganov5d3bc372020-01-26 23:11:07 -08002372
2373 // Create a non touch modal window that supports split touch
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002374 sp<FakeWindowHandle> secondWindow =
2375 new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08002376 secondWindow->setFrame(Rect(0, 400, 600, 800));
chaviw3277faf2021-05-19 16:45:23 -05002377 secondWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH);
Svet Ganov5d3bc372020-01-26 23:11:07 -08002378
2379 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00002380 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08002381
2382 PointF pointInFirst = {300, 200};
2383 PointF pointInSecond = {300, 600};
2384
2385 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002386 NotifyMotionArgs firstDownMotionArgs =
2387 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2388 ADISPLAY_ID_DEFAULT, {pointInFirst});
Svet Ganov5d3bc372020-01-26 23:11:07 -08002389 mDispatcher->notifyMotion(&firstDownMotionArgs);
2390 // Only the first window should get the down event
2391 firstWindow->consumeMotionDown();
2392 secondWindow->assertNoEvents();
2393
2394 // Send down to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002395 NotifyMotionArgs secondDownMotionArgs =
2396 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN |
2397 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2398 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2399 {pointInFirst, pointInSecond});
Svet Ganov5d3bc372020-01-26 23:11:07 -08002400 mDispatcher->notifyMotion(&secondDownMotionArgs);
2401 // The first window gets a move and the second a down
2402 firstWindow->consumeMotionMove();
2403 secondWindow->consumeMotionDown();
2404
2405 // Transfer touch focus to the second window
2406 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
2407 // The first window gets cancel and the new gets pointer down (it already saw down)
2408 firstWindow->consumeMotionCancel();
2409 secondWindow->consumeMotionPointerDown(1);
2410
2411 // Send pointer up to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002412 NotifyMotionArgs pointerUpMotionArgs =
2413 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP |
2414 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2415 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2416 {pointInFirst, pointInSecond});
Svet Ganov5d3bc372020-01-26 23:11:07 -08002417 mDispatcher->notifyMotion(&pointerUpMotionArgs);
2418 // The first window gets nothing and the second gets pointer up
2419 firstWindow->assertNoEvents();
2420 secondWindow->consumeMotionPointerUp(1);
2421
2422 // Send up event to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002423 NotifyMotionArgs upMotionArgs =
2424 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
2425 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08002426 mDispatcher->notifyMotion(&upMotionArgs);
2427 // The first window gets nothing and the second gets up
2428 firstWindow->assertNoEvents();
2429 secondWindow->consumeMotionUp();
2430}
2431
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00002432// Same as TransferTouchFocus_TwoPointersSplitTouch, but using 'transferTouch' api.
2433// Unlike 'transferTouchFocus', calling 'transferTouch' when there are two windows receiving
2434// touch is not supported, so the touch should continue on those windows and the transferred-to
2435// window should get nothing.
2436TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) {
2437 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2438
2439 // Create a non touch modal window that supports split touch
2440 sp<FakeWindowHandle> firstWindow =
2441 new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT);
2442 firstWindow->setFrame(Rect(0, 0, 600, 400));
chaviw3277faf2021-05-19 16:45:23 -05002443 firstWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00002444
2445 // Create a non touch modal window that supports split touch
2446 sp<FakeWindowHandle> secondWindow =
2447 new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT);
2448 secondWindow->setFrame(Rect(0, 400, 600, 800));
chaviw3277faf2021-05-19 16:45:23 -05002449 secondWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00002450
2451 // Add the windows to the dispatcher
2452 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
2453
2454 PointF pointInFirst = {300, 200};
2455 PointF pointInSecond = {300, 600};
2456
2457 // Send down to the first window
2458 NotifyMotionArgs firstDownMotionArgs =
2459 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2460 ADISPLAY_ID_DEFAULT, {pointInFirst});
2461 mDispatcher->notifyMotion(&firstDownMotionArgs);
2462 // Only the first window should get the down event
2463 firstWindow->consumeMotionDown();
2464 secondWindow->assertNoEvents();
2465
2466 // Send down to the second window
2467 NotifyMotionArgs secondDownMotionArgs =
2468 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN |
2469 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2470 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2471 {pointInFirst, pointInSecond});
2472 mDispatcher->notifyMotion(&secondDownMotionArgs);
2473 // The first window gets a move and the second a down
2474 firstWindow->consumeMotionMove();
2475 secondWindow->consumeMotionDown();
2476
2477 // Transfer touch focus to the second window
2478 const bool transferred = mDispatcher->transferTouch(secondWindow->getToken());
2479 // The 'transferTouch' call should not succeed, because there are 2 touched windows
2480 ASSERT_FALSE(transferred);
2481 firstWindow->assertNoEvents();
2482 secondWindow->assertNoEvents();
2483
2484 // The rest of the dispatch should proceed as normal
2485 // Send pointer up to the second window
2486 NotifyMotionArgs pointerUpMotionArgs =
2487 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP |
2488 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2489 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2490 {pointInFirst, pointInSecond});
2491 mDispatcher->notifyMotion(&pointerUpMotionArgs);
2492 // The first window gets MOVE and the second gets pointer up
2493 firstWindow->consumeMotionMove();
2494 secondWindow->consumeMotionUp();
2495
2496 // Send up event to the first window
2497 NotifyMotionArgs upMotionArgs =
2498 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
2499 ADISPLAY_ID_DEFAULT);
2500 mDispatcher->notifyMotion(&upMotionArgs);
2501 // The first window gets nothing and the second gets up
2502 firstWindow->consumeMotionUp();
2503 secondWindow->assertNoEvents();
2504}
2505
Arthur Hungabbb9d82021-09-01 14:52:30 +00002506// This case will create two windows and one mirrored window on the default display and mirror
2507// two windows on the second display. It will test if 'transferTouchFocus' works fine if we put
2508// the windows info of second display before default display.
2509TEST_F(InputDispatcherTest, TransferTouchFocus_CloneSurface) {
2510 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2511 sp<FakeWindowHandle> firstWindowInPrimary =
2512 new FakeWindowHandle(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT);
2513 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
2514 firstWindowInPrimary->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
2515 sp<FakeWindowHandle> secondWindowInPrimary =
2516 new FakeWindowHandle(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
2517 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
2518 secondWindowInPrimary->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
2519
2520 sp<FakeWindowHandle> mirrorWindowInPrimary =
2521 firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT);
2522 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
2523 mirrorWindowInPrimary->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
2524
2525 sp<FakeWindowHandle> firstWindowInSecondary =
2526 firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
2527 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
2528 firstWindowInSecondary->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
2529
2530 sp<FakeWindowHandle> secondWindowInSecondary =
2531 secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
2532 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
2533 secondWindowInPrimary->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
2534
2535 // Update window info, let it find window handle of second display first.
2536 mDispatcher->setInputWindows(
2537 {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}},
2538 {ADISPLAY_ID_DEFAULT,
2539 {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}});
2540
2541 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2542 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2543 {50, 50}))
2544 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2545
2546 // Window should receive motion event.
2547 firstWindowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2548
2549 // Transfer touch focus
2550 ASSERT_TRUE(mDispatcher->transferTouchFocus(firstWindowInPrimary->getToken(),
2551 secondWindowInPrimary->getToken()));
2552 // The first window gets cancel.
2553 firstWindowInPrimary->consumeMotionCancel();
2554 secondWindowInPrimary->consumeMotionDown();
2555
2556 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2557 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
2558 ADISPLAY_ID_DEFAULT, {150, 50}))
2559 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2560 firstWindowInPrimary->assertNoEvents();
2561 secondWindowInPrimary->consumeMotionMove();
2562
2563 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2564 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2565 {150, 50}))
2566 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2567 firstWindowInPrimary->assertNoEvents();
2568 secondWindowInPrimary->consumeMotionUp();
2569}
2570
2571// Same as TransferTouchFocus_CloneSurface, but this touch on the secondary display and use
2572// 'transferTouch' api.
2573TEST_F(InputDispatcherTest, TransferTouch_CloneSurface) {
2574 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2575 sp<FakeWindowHandle> firstWindowInPrimary =
2576 new FakeWindowHandle(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT);
2577 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
2578 firstWindowInPrimary->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
2579 sp<FakeWindowHandle> secondWindowInPrimary =
2580 new FakeWindowHandle(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
2581 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
2582 secondWindowInPrimary->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
2583
2584 sp<FakeWindowHandle> mirrorWindowInPrimary =
2585 firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT);
2586 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
2587 mirrorWindowInPrimary->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
2588
2589 sp<FakeWindowHandle> firstWindowInSecondary =
2590 firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
2591 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
2592 firstWindowInSecondary->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
2593
2594 sp<FakeWindowHandle> secondWindowInSecondary =
2595 secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
2596 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
2597 secondWindowInPrimary->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
2598
2599 // Update window info, let it find window handle of second display first.
2600 mDispatcher->setInputWindows(
2601 {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}},
2602 {ADISPLAY_ID_DEFAULT,
2603 {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}});
2604
2605 // Touch on second display.
2606 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2607 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {50, 50}))
2608 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2609
2610 // Window should receive motion event.
2611 firstWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID);
2612
2613 // Transfer touch focus
2614 ASSERT_TRUE(mDispatcher->transferTouch(secondWindowInSecondary->getToken()));
2615
2616 // The first window gets cancel.
2617 firstWindowInPrimary->consumeMotionCancel(SECOND_DISPLAY_ID);
2618 secondWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID);
2619
2620 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2621 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
2622 SECOND_DISPLAY_ID, {150, 50}))
2623 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2624 firstWindowInPrimary->assertNoEvents();
2625 secondWindowInPrimary->consumeMotionMove(SECOND_DISPLAY_ID);
2626
2627 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2628 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {150, 50}))
2629 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2630 firstWindowInPrimary->assertNoEvents();
2631 secondWindowInPrimary->consumeMotionUp(SECOND_DISPLAY_ID);
2632}
2633
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002634TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07002635 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002636 sp<FakeWindowHandle> window =
2637 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
2638
Vishnu Nair47074b82020-08-14 11:54:47 -07002639 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002640 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002641 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002642
2643 window->consumeFocusEvent(true);
2644
2645 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
2646 mDispatcher->notifyKey(&keyArgs);
2647
2648 // Window should receive key down event.
2649 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2650}
2651
2652TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07002653 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002654 sp<FakeWindowHandle> window =
2655 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
2656
Arthur Hung72d8dc32020-03-28 00:48:39 +00002657 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002658
2659 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
2660 mDispatcher->notifyKey(&keyArgs);
2661 mDispatcher->waitForIdle();
2662
2663 window->assertNoEvents();
2664}
2665
2666// If a window is touchable, but does not have focus, it should receive motion events, but not keys
2667TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) {
Chris Yea209fde2020-07-22 13:54:51 -07002668 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002669 sp<FakeWindowHandle> window =
2670 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
2671
Arthur Hung72d8dc32020-03-28 00:48:39 +00002672 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002673
2674 // Send key
2675 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
2676 mDispatcher->notifyKey(&keyArgs);
2677 // Send motion
2678 NotifyMotionArgs motionArgs =
2679 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2680 ADISPLAY_ID_DEFAULT);
2681 mDispatcher->notifyMotion(&motionArgs);
2682
2683 // Window should receive only the motion event
2684 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2685 window->assertNoEvents(); // Key event or focus event will not be received
2686}
2687
arthurhungea3f4fc2020-12-21 23:18:53 +08002688TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) {
2689 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2690
2691 // Create first non touch modal window that supports split touch
2692 sp<FakeWindowHandle> firstWindow =
2693 new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT);
2694 firstWindow->setFrame(Rect(0, 0, 600, 400));
chaviw3277faf2021-05-19 16:45:23 -05002695 firstWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH);
arthurhungea3f4fc2020-12-21 23:18:53 +08002696
2697 // Create second non touch modal window that supports split touch
2698 sp<FakeWindowHandle> secondWindow =
2699 new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT);
2700 secondWindow->setFrame(Rect(0, 400, 600, 800));
chaviw3277faf2021-05-19 16:45:23 -05002701 secondWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH);
arthurhungea3f4fc2020-12-21 23:18:53 +08002702
2703 // Add the windows to the dispatcher
2704 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
2705
2706 PointF pointInFirst = {300, 200};
2707 PointF pointInSecond = {300, 600};
2708
2709 // Send down to the first window
2710 NotifyMotionArgs firstDownMotionArgs =
2711 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2712 ADISPLAY_ID_DEFAULT, {pointInFirst});
2713 mDispatcher->notifyMotion(&firstDownMotionArgs);
2714 // Only the first window should get the down event
2715 firstWindow->consumeMotionDown();
2716 secondWindow->assertNoEvents();
2717
2718 // Send down to the second window
2719 NotifyMotionArgs secondDownMotionArgs =
2720 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN |
2721 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2722 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2723 {pointInFirst, pointInSecond});
2724 mDispatcher->notifyMotion(&secondDownMotionArgs);
2725 // The first window gets a move and the second a down
2726 firstWindow->consumeMotionMove();
2727 secondWindow->consumeMotionDown();
2728
2729 // Send pointer cancel to the second window
2730 NotifyMotionArgs pointerUpMotionArgs =
2731 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP |
2732 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2733 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2734 {pointInFirst, pointInSecond});
2735 pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED;
2736 mDispatcher->notifyMotion(&pointerUpMotionArgs);
2737 // The first window gets move and the second gets cancel.
2738 firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
2739 secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
2740
2741 // Send up event.
2742 NotifyMotionArgs upMotionArgs =
2743 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
2744 ADISPLAY_ID_DEFAULT);
2745 mDispatcher->notifyMotion(&upMotionArgs);
2746 // The first window gets up and the second gets nothing.
2747 firstWindow->consumeMotionUp();
2748 secondWindow->assertNoEvents();
2749}
2750
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00002751TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) {
2752 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2753
2754 sp<FakeWindowHandle> window =
2755 new FakeWindowHandle(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2756 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2757 std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline;
2758 graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2;
2759 graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3;
2760
2761 window->sendTimeline(1 /*inputEventId*/, graphicsTimeline);
2762 window->assertNoEvents();
2763 mDispatcher->waitForIdle();
2764}
2765
chaviwd1c23182019-12-20 18:44:56 -08002766class FakeMonitorReceiver {
Michael Wright3a240c42019-12-10 20:53:41 +00002767public:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002768 FakeMonitorReceiver(const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08002769 int32_t displayId) {
Garfield Tan15601662020-09-22 15:32:38 -07002770 base::Result<std::unique_ptr<InputChannel>> channel =
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08002771 dispatcher->createInputMonitor(displayId, false /*isGestureMonitor*/, name,
2772 MONITOR_PID);
Garfield Tan15601662020-09-22 15:32:38 -07002773 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
Michael Wright3a240c42019-12-10 20:53:41 +00002774 }
2775
chaviwd1c23182019-12-20 18:44:56 -08002776 sp<IBinder> getToken() { return mInputReceiver->getToken(); }
2777
2778 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
2779 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN,
2780 expectedDisplayId, expectedFlags);
2781 }
2782
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002783 std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); }
2784
2785 void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); }
2786
chaviwd1c23182019-12-20 18:44:56 -08002787 void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
2788 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN,
2789 expectedDisplayId, expectedFlags);
2790 }
2791
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002792 void consumeMotionMove(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
2793 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE,
2794 expectedDisplayId, expectedFlags);
2795 }
2796
chaviwd1c23182019-12-20 18:44:56 -08002797 void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
2798 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP,
2799 expectedDisplayId, expectedFlags);
2800 }
2801
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002802 void consumeMotionCancel(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
2803 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL,
2804 expectedDisplayId, expectedFlags);
2805 }
2806
Arthur Hungfbfa5722021-11-16 02:45:54 +00002807 void consumeMotionPointerDown(int32_t pointerIdx) {
2808 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
2809 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
2810 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, ADISPLAY_ID_DEFAULT,
2811 0 /*expectedFlags*/);
2812 }
2813
Evan Rosky84f07f02021-04-16 10:42:42 -07002814 MotionEvent* consumeMotion() {
2815 InputEvent* event = mInputReceiver->consume();
2816 if (!event) {
2817 ADD_FAILURE() << "No event was produced";
2818 return nullptr;
2819 }
2820 if (event->getType() != AINPUT_EVENT_TYPE_MOTION) {
2821 ADD_FAILURE() << "Received event of type " << event->getType() << " instead of motion";
2822 return nullptr;
2823 }
2824 return static_cast<MotionEvent*>(event);
2825 }
2826
chaviwd1c23182019-12-20 18:44:56 -08002827 void assertNoEvents() { mInputReceiver->assertNoEvents(); }
2828
2829private:
2830 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Michael Wright3a240c42019-12-10 20:53:41 +00002831};
2832
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08002833using InputDispatcherMonitorTest = InputDispatcherTest;
2834
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002835/**
2836 * Two entities that receive touch: A window, and a global monitor.
2837 * The touch goes to the window, and then the window disappears.
2838 * The monitor does not get cancel right away. But if more events come in, the touch gets canceled
2839 * for the monitor, as well.
2840 * 1. foregroundWindow
2841 * 2. monitor <-- global monitor (doesn't observe z order, receives all events)
2842 */
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08002843TEST_F(InputDispatcherMonitorTest, MonitorTouchIsCanceledWhenForegroundWindowDisappears) {
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002844 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2845 sp<FakeWindowHandle> window =
2846 new FakeWindowHandle(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
2847
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08002848 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002849
2850 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2851 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2852 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2853 {100, 200}))
2854 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2855
2856 // Both the foreground window and the global monitor should receive the touch down
2857 window->consumeMotionDown();
2858 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
2859
2860 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2861 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
2862 ADISPLAY_ID_DEFAULT, {110, 200}))
2863 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2864
2865 window->consumeMotionMove();
2866 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
2867
2868 // Now the foreground window goes away
2869 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
2870 window->consumeMotionCancel();
2871 monitor.assertNoEvents(); // Global monitor does not get a cancel yet
2872
2873 // If more events come in, there will be no more foreground window to send them to. This will
2874 // cause a cancel for the monitor, as well.
2875 ASSERT_EQ(InputEventInjectionResult::FAILED,
2876 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
2877 ADISPLAY_ID_DEFAULT, {120, 200}))
2878 << "Injection should fail because the window was removed";
2879 window->assertNoEvents();
2880 // Global monitor now gets the cancel
2881 monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
2882}
2883
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08002884TEST_F(InputDispatcherMonitorTest, ReceivesMotionEvents) {
Chris Yea209fde2020-07-22 13:54:51 -07002885 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Michael Wright3a240c42019-12-10 20:53:41 +00002886 sp<FakeWindowHandle> window =
2887 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002888 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00002889
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08002890 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00002891
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002892 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00002893 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002894 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00002895 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08002896 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00002897}
2898
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08002899TEST_F(InputDispatcherMonitorTest, MonitorCannotPilferPointers) {
2900 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00002901
Chris Yea209fde2020-07-22 13:54:51 -07002902 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Michael Wright3a240c42019-12-10 20:53:41 +00002903 sp<FakeWindowHandle> window =
2904 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002905 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00002906
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002907 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00002908 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002909 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
chaviwd1c23182019-12-20 18:44:56 -08002910 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08002911 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00002912
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08002913 // Pilfer pointers from the monitor.
2914 // This should not do anything and the window should continue to receive events.
2915 EXPECT_NE(OK, mDispatcher->pilferPointers(monitor.getToken()));
Michael Wright3a240c42019-12-10 20:53:41 +00002916
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002917 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08002918 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
2919 ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002920 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08002921
2922 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
2923 window->consumeMotionMove(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00002924}
2925
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08002926TEST_F(InputDispatcherMonitorTest, NoWindowTransform) {
Evan Rosky84f07f02021-04-16 10:42:42 -07002927 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2928 sp<FakeWindowHandle> window =
2929 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
2930 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2931 window->setWindowOffset(20, 40);
2932 window->setWindowTransform(0, 1, -1, 0);
2933
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08002934 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Evan Rosky84f07f02021-04-16 10:42:42 -07002935
2936 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2937 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
2938 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2939 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2940 MotionEvent* event = monitor.consumeMotion();
2941 // Even though window has transform, gesture monitor must not.
2942 ASSERT_EQ(ui::Transform(), event->getTransform());
2943}
2944
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08002945TEST_F(InputDispatcherMonitorTest, InjectionFailsWithNoWindow) {
Arthur Hungb3307ee2021-10-14 10:57:37 +00002946 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08002947 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Arthur Hungb3307ee2021-10-14 10:57:37 +00002948
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08002949 ASSERT_EQ(InputEventInjectionResult::FAILED,
Arthur Hungb3307ee2021-10-14 10:57:37 +00002950 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08002951 << "Injection should fail if there is a monitor, but no touchable window";
2952 monitor.assertNoEvents();
Arthur Hungb3307ee2021-10-14 10:57:37 +00002953}
2954
chaviw81e2bb92019-12-18 15:03:51 -08002955TEST_F(InputDispatcherTest, TestMoveEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07002956 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
chaviw81e2bb92019-12-18 15:03:51 -08002957 sp<FakeWindowHandle> window =
2958 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
2959
Arthur Hung72d8dc32020-03-28 00:48:39 +00002960 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
chaviw81e2bb92019-12-18 15:03:51 -08002961
2962 NotifyMotionArgs motionArgs =
2963 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2964 ADISPLAY_ID_DEFAULT);
2965
2966 mDispatcher->notifyMotion(&motionArgs);
2967 // Window should receive motion down event.
2968 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2969
2970 motionArgs.action = AMOTION_EVENT_ACTION_MOVE;
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002971 motionArgs.id += 1;
chaviw81e2bb92019-12-18 15:03:51 -08002972 motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
2973 motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
2974 motionArgs.pointerCoords[0].getX() - 10);
2975
2976 mDispatcher->notifyMotion(&motionArgs);
2977 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT,
2978 0 /*expectedFlags*/);
2979}
2980
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002981/**
2982 * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to
2983 * the device default right away. In the test scenario, we check both the default value,
2984 * and the action of enabling / disabling.
2985 */
2986TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) {
Chris Yea209fde2020-07-22 13:54:51 -07002987 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002988 sp<FakeWindowHandle> window =
2989 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
2990
2991 // Set focused application.
2992 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07002993 window->setFocusable(true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002994
2995 SCOPED_TRACE("Check default value of touch mode");
Arthur Hung72d8dc32020-03-28 00:48:39 +00002996 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002997 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002998 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
2999
3000 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07003001 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00003002 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003003 window->consumeFocusEvent(false /*hasFocus*/, true /*inTouchMode*/);
3004
3005 SCOPED_TRACE("Disable touch mode");
3006 mDispatcher->setInTouchMode(false);
Antonio Kantekf16f2832021-09-28 04:39:20 +00003007 window->consumeTouchModeEvent(false);
Vishnu Nair47074b82020-08-14 11:54:47 -07003008 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00003009 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003010 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003011 window->consumeFocusEvent(true /*hasFocus*/, false /*inTouchMode*/);
3012
3013 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07003014 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00003015 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003016 window->consumeFocusEvent(false /*hasFocus*/, false /*inTouchMode*/);
3017
3018 SCOPED_TRACE("Enable touch mode again");
3019 mDispatcher->setInTouchMode(true);
Antonio Kantekf16f2832021-09-28 04:39:20 +00003020 window->consumeTouchModeEvent(true);
Vishnu Nair47074b82020-08-14 11:54:47 -07003021 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00003022 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003023 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003024 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
3025
3026 window->assertNoEvents();
3027}
3028
Gang Wange9087892020-01-07 12:17:14 -05003029TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07003030 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Gang Wange9087892020-01-07 12:17:14 -05003031 sp<FakeWindowHandle> window =
3032 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
3033
3034 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07003035 window->setFocusable(true);
Gang Wange9087892020-01-07 12:17:14 -05003036
Arthur Hung72d8dc32020-03-28 00:48:39 +00003037 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003038 setFocusedWindow(window);
3039
Gang Wange9087892020-01-07 12:17:14 -05003040 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
3041
3042 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
3043 mDispatcher->notifyKey(&keyArgs);
3044
3045 InputEvent* event = window->consume();
3046 ASSERT_NE(event, nullptr);
3047
3048 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
3049 ASSERT_NE(verified, nullptr);
3050 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY);
3051
3052 ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos);
3053 ASSERT_EQ(keyArgs.deviceId, verified->deviceId);
3054 ASSERT_EQ(keyArgs.source, verified->source);
3055 ASSERT_EQ(keyArgs.displayId, verified->displayId);
3056
3057 const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified);
3058
3059 ASSERT_EQ(keyArgs.action, verifiedKey.action);
Gang Wange9087892020-01-07 12:17:14 -05003060 ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08003061 ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos);
Gang Wange9087892020-01-07 12:17:14 -05003062 ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode);
3063 ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode);
3064 ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState);
3065 ASSERT_EQ(0, verifiedKey.repeatCount);
3066}
3067
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08003068TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07003069 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08003070 sp<FakeWindowHandle> window =
3071 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
3072
3073 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3074
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07003075 ui::Transform transform;
3076 transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
3077
3078 gui::DisplayInfo displayInfo;
3079 displayInfo.displayId = ADISPLAY_ID_DEFAULT;
3080 displayInfo.transform = transform;
3081
3082 mDispatcher->onWindowInfosChanged({*window->getInfo()}, {displayInfo});
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08003083
3084 NotifyMotionArgs motionArgs =
3085 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3086 ADISPLAY_ID_DEFAULT);
3087 mDispatcher->notifyMotion(&motionArgs);
3088
3089 InputEvent* event = window->consume();
3090 ASSERT_NE(event, nullptr);
3091
3092 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
3093 ASSERT_NE(verified, nullptr);
3094 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION);
3095
3096 EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos);
3097 EXPECT_EQ(motionArgs.deviceId, verified->deviceId);
3098 EXPECT_EQ(motionArgs.source, verified->source);
3099 EXPECT_EQ(motionArgs.displayId, verified->displayId);
3100
3101 const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified);
3102
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07003103 const vec2 rawXY =
3104 MotionEvent::calculateTransformedXY(motionArgs.source, transform,
3105 motionArgs.pointerCoords[0].getXYValue());
3106 EXPECT_EQ(rawXY.x, verifiedMotion.rawX);
3107 EXPECT_EQ(rawXY.y, verifiedMotion.rawY);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08003108 EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08003109 EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08003110 EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08003111 EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState);
3112 EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState);
3113}
3114
chaviw09c8d2d2020-08-24 15:48:26 -07003115/**
3116 * Ensure that separate calls to sign the same data are generating the same key.
3117 * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance
3118 * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky
3119 * tests.
3120 */
3121TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) {
3122 KeyEvent event = getTestKeyEvent();
3123 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
3124
3125 std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent);
3126 std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent);
3127 ASSERT_EQ(hmac1, hmac2);
3128}
3129
3130/**
3131 * Ensure that changes in VerifiedKeyEvent produce a different hmac.
3132 */
3133TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) {
3134 KeyEvent event = getTestKeyEvent();
3135 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
3136 std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent);
3137
3138 verifiedEvent.deviceId += 1;
3139 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
3140
3141 verifiedEvent.source += 1;
3142 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
3143
3144 verifiedEvent.eventTimeNanos += 1;
3145 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
3146
3147 verifiedEvent.displayId += 1;
3148 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
3149
3150 verifiedEvent.action += 1;
3151 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
3152
3153 verifiedEvent.downTimeNanos += 1;
3154 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
3155
3156 verifiedEvent.flags += 1;
3157 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
3158
3159 verifiedEvent.keyCode += 1;
3160 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
3161
3162 verifiedEvent.scanCode += 1;
3163 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
3164
3165 verifiedEvent.metaState += 1;
3166 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
3167
3168 verifiedEvent.repeatCount += 1;
3169 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
3170}
3171
Vishnu Nair958da932020-08-21 17:12:37 -07003172TEST_F(InputDispatcherTest, SetFocusedWindow) {
3173 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3174 sp<FakeWindowHandle> windowTop =
3175 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
3176 sp<FakeWindowHandle> windowSecond =
3177 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
3178 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3179
3180 // Top window is also focusable but is not granted focus.
3181 windowTop->setFocusable(true);
3182 windowSecond->setFocusable(true);
3183 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
3184 setFocusedWindow(windowSecond);
3185
3186 windowSecond->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003187 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
3188 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07003189
3190 // Focused window should receive event.
3191 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
3192 windowTop->assertNoEvents();
3193}
3194
3195TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) {
3196 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3197 sp<FakeWindowHandle> window =
3198 new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
3199 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3200
3201 window->setFocusable(true);
3202 // Release channel for window is no longer valid.
3203 window->releaseChannel();
3204 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3205 setFocusedWindow(window);
3206
3207 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003208 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
3209 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07003210
3211 // window channel is invalid, so it should not receive any input event.
3212 window->assertNoEvents();
3213}
3214
3215TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) {
3216 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3217 sp<FakeWindowHandle> window =
3218 new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
3219 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3220
3221 // Window is not focusable.
3222 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3223 setFocusedWindow(window);
3224
3225 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003226 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
3227 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07003228
3229 // window is invalid, so it should not receive any input event.
3230 window->assertNoEvents();
3231}
3232
3233TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) {
3234 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3235 sp<FakeWindowHandle> windowTop =
3236 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
3237 sp<FakeWindowHandle> windowSecond =
3238 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
3239 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3240
3241 windowTop->setFocusable(true);
3242 windowSecond->setFocusable(true);
3243 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
3244 setFocusedWindow(windowTop);
3245 windowTop->consumeFocusEvent(true);
3246
3247 setFocusedWindow(windowSecond, windowTop);
3248 windowSecond->consumeFocusEvent(true);
3249 windowTop->consumeFocusEvent(false);
3250
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003251 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
3252 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07003253
3254 // Focused window should receive event.
3255 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
3256}
3257
3258TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestFocusTokenNotFocused) {
3259 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3260 sp<FakeWindowHandle> windowTop =
3261 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
3262 sp<FakeWindowHandle> windowSecond =
3263 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
3264 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3265
3266 windowTop->setFocusable(true);
3267 windowSecond->setFocusable(true);
3268 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
3269 setFocusedWindow(windowSecond, windowTop);
3270
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003271 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
3272 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07003273
3274 // Event should be dropped.
3275 windowTop->assertNoEvents();
3276 windowSecond->assertNoEvents();
3277}
3278
3279TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) {
3280 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3281 sp<FakeWindowHandle> window =
3282 new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
3283 sp<FakeWindowHandle> previousFocusedWindow =
3284 new FakeWindowHandle(application, mDispatcher, "previousFocusedWindow",
3285 ADISPLAY_ID_DEFAULT);
3286 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3287
3288 window->setFocusable(true);
3289 previousFocusedWindow->setFocusable(true);
3290 window->setVisible(false);
3291 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}});
3292 setFocusedWindow(previousFocusedWindow);
3293 previousFocusedWindow->consumeFocusEvent(true);
3294
3295 // Requesting focus on invisible window takes focus from currently focused window.
3296 setFocusedWindow(window);
3297 previousFocusedWindow->consumeFocusEvent(false);
3298
3299 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003300 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Vishnu Nair958da932020-08-21 17:12:37 -07003301 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003302 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07003303
3304 // Window does not get focus event or key down.
3305 window->assertNoEvents();
3306
3307 // Window becomes visible.
3308 window->setVisible(true);
3309 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3310
3311 // Window receives focus event.
3312 window->consumeFocusEvent(true);
3313 // Focused window receives key down.
3314 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3315}
3316
Vishnu Nair599f1412021-06-21 10:39:58 -07003317TEST_F(InputDispatcherTest, DisplayRemoved) {
3318 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3319 sp<FakeWindowHandle> window =
3320 new FakeWindowHandle(application, mDispatcher, "window", ADISPLAY_ID_DEFAULT);
3321 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3322
3323 // window is granted focus.
3324 window->setFocusable(true);
3325 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3326 setFocusedWindow(window);
3327 window->consumeFocusEvent(true);
3328
3329 // When a display is removed window loses focus.
3330 mDispatcher->displayRemoved(ADISPLAY_ID_DEFAULT);
3331 window->consumeFocusEvent(false);
3332}
3333
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003334/**
3335 * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY,
3336 * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top
3337 * of the 'slipperyEnterWindow'.
3338 *
3339 * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such
3340 * a way so that the touched location is no longer covered by the top window.
3341 *
3342 * Next, inject a MOVE event. Because the top window already moved earlier, this event is now
3343 * positioned over the bottom (slipperyEnterWindow) only. And because the top window had
3344 * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive
3345 * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting
3346 * with ACTION_DOWN).
3347 * Thus, the touch has been transferred from the top window into the bottom window, because the top
3348 * window moved itself away from the touched location and had Flag::SLIPPERY.
3349 *
3350 * Even though the top window moved away from the touched location, it is still obscuring the bottom
3351 * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_
3352 * OBSCURED should be set for the MotionEvent that reaches the bottom window.
3353 *
3354 * In this test, we ensure that the event received by the bottom window has
3355 * FLAG_WINDOW_IS_PARTIALLY_OBSCURED.
3356 */
3357TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) {
3358 constexpr int32_t SLIPPERY_PID = INJECTOR_PID + 1;
3359 constexpr int32_t SLIPPERY_UID = INJECTOR_UID + 1;
3360
3361 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3362 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3363
3364 sp<FakeWindowHandle> slipperyExitWindow =
3365 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
chaviw3277faf2021-05-19 16:45:23 -05003366 slipperyExitWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SLIPPERY);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003367 // Make sure this one overlaps the bottom window
3368 slipperyExitWindow->setFrame(Rect(25, 25, 75, 75));
3369 // Change the owner uid/pid of the window so that it is considered to be occluding the bottom
3370 // one. Windows with the same owner are not considered to be occluding each other.
3371 slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID);
3372
3373 sp<FakeWindowHandle> slipperyEnterWindow =
3374 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
3375 slipperyExitWindow->setFrame(Rect(0, 0, 100, 100));
3376
3377 mDispatcher->setInputWindows(
3378 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
3379
3380 // Use notifyMotion instead of injecting to avoid dealing with injection permissions
3381 NotifyMotionArgs args = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3382 ADISPLAY_ID_DEFAULT, {{50, 50}});
3383 mDispatcher->notifyMotion(&args);
3384 slipperyExitWindow->consumeMotionDown();
3385 slipperyExitWindow->setFrame(Rect(70, 70, 100, 100));
3386 mDispatcher->setInputWindows(
3387 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
3388
3389 args = generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
3390 ADISPLAY_ID_DEFAULT, {{51, 51}});
3391 mDispatcher->notifyMotion(&args);
3392
3393 slipperyExitWindow->consumeMotionCancel();
3394
3395 slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT,
3396 AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
3397}
3398
Garfield Tan1c7bc862020-01-28 13:24:04 -08003399class InputDispatcherKeyRepeatTest : public InputDispatcherTest {
3400protected:
3401 static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms
3402 static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms
3403
Chris Yea209fde2020-07-22 13:54:51 -07003404 std::shared_ptr<FakeApplicationHandle> mApp;
Garfield Tan1c7bc862020-01-28 13:24:04 -08003405 sp<FakeWindowHandle> mWindow;
3406
3407 virtual void SetUp() override {
3408 mFakePolicy = new FakeInputDispatcherPolicy();
3409 mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003410 mDispatcher = std::make_unique<InputDispatcher>(mFakePolicy);
Garfield Tan1c7bc862020-01-28 13:24:04 -08003411 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
3412 ASSERT_EQ(OK, mDispatcher->start());
3413
3414 setUpWindow();
3415 }
3416
3417 void setUpWindow() {
Chris Yea209fde2020-07-22 13:54:51 -07003418 mApp = std::make_shared<FakeApplicationHandle>();
Garfield Tan1c7bc862020-01-28 13:24:04 -08003419 mWindow = new FakeWindowHandle(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
3420
Vishnu Nair47074b82020-08-14 11:54:47 -07003421 mWindow->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00003422 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003423 setFocusedWindow(mWindow);
Garfield Tan1c7bc862020-01-28 13:24:04 -08003424 mWindow->consumeFocusEvent(true);
3425 }
3426
Chris Ye2ad95392020-09-01 13:44:44 -07003427 void sendAndConsumeKeyDown(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08003428 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07003429 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08003430 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event
3431 mDispatcher->notifyKey(&keyArgs);
3432
3433 // Window should receive key down event.
3434 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3435 }
3436
3437 void expectKeyRepeatOnce(int32_t repeatCount) {
3438 SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount));
3439 InputEvent* repeatEvent = mWindow->consume();
3440 ASSERT_NE(nullptr, repeatEvent);
3441
3442 uint32_t eventType = repeatEvent->getType();
3443 ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, eventType);
3444
3445 KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent);
3446 uint32_t eventAction = repeatKeyEvent->getAction();
3447 EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction);
3448 EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount());
3449 }
3450
Chris Ye2ad95392020-09-01 13:44:44 -07003451 void sendAndConsumeKeyUp(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08003452 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07003453 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08003454 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event
3455 mDispatcher->notifyKey(&keyArgs);
3456
3457 // Window should receive key down event.
3458 mWindow->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
3459 0 /*expectedFlags*/);
3460 }
3461};
3462
3463TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) {
Chris Ye2ad95392020-09-01 13:44:44 -07003464 sendAndConsumeKeyDown(1 /* deviceId */);
3465 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
3466 expectKeyRepeatOnce(repeatCount);
3467 }
3468}
3469
3470TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) {
3471 sendAndConsumeKeyDown(1 /* deviceId */);
3472 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
3473 expectKeyRepeatOnce(repeatCount);
3474 }
3475 sendAndConsumeKeyDown(2 /* deviceId */);
3476 /* repeatCount will start from 1 for deviceId 2 */
Garfield Tan1c7bc862020-01-28 13:24:04 -08003477 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
3478 expectKeyRepeatOnce(repeatCount);
3479 }
3480}
3481
3482TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) {
Chris Ye2ad95392020-09-01 13:44:44 -07003483 sendAndConsumeKeyDown(1 /* deviceId */);
Garfield Tan1c7bc862020-01-28 13:24:04 -08003484 expectKeyRepeatOnce(1 /*repeatCount*/);
Chris Ye2ad95392020-09-01 13:44:44 -07003485 sendAndConsumeKeyUp(1 /* deviceId */);
3486 mWindow->assertNoEvents();
3487}
3488
3489TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) {
3490 sendAndConsumeKeyDown(1 /* deviceId */);
3491 expectKeyRepeatOnce(1 /*repeatCount*/);
3492 sendAndConsumeKeyDown(2 /* deviceId */);
3493 expectKeyRepeatOnce(1 /*repeatCount*/);
3494 // Stale key up from device 1.
3495 sendAndConsumeKeyUp(1 /* deviceId */);
3496 // Device 2 is still down, keep repeating
3497 expectKeyRepeatOnce(2 /*repeatCount*/);
3498 expectKeyRepeatOnce(3 /*repeatCount*/);
3499 // Device 2 key up
3500 sendAndConsumeKeyUp(2 /* deviceId */);
3501 mWindow->assertNoEvents();
3502}
3503
3504TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) {
3505 sendAndConsumeKeyDown(1 /* deviceId */);
3506 expectKeyRepeatOnce(1 /*repeatCount*/);
3507 sendAndConsumeKeyDown(2 /* deviceId */);
3508 expectKeyRepeatOnce(1 /*repeatCount*/);
3509 // Device 2 which holds the key repeating goes up, expect the repeating to stop.
3510 sendAndConsumeKeyUp(2 /* deviceId */);
3511 // Device 1 still holds key down, but the repeating was already stopped
Garfield Tan1c7bc862020-01-28 13:24:04 -08003512 mWindow->assertNoEvents();
3513}
3514
liushenxiang42232912021-05-21 20:24:09 +08003515TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) {
3516 sendAndConsumeKeyDown(DEVICE_ID);
3517 expectKeyRepeatOnce(1 /*repeatCount*/);
3518 NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID);
3519 mDispatcher->notifyDeviceReset(&args);
3520 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT,
3521 AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS);
3522 mWindow->assertNoEvents();
3523}
3524
Garfield Tan1c7bc862020-01-28 13:24:04 -08003525TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) {
Chris Ye2ad95392020-09-01 13:44:44 -07003526 sendAndConsumeKeyDown(1 /* deviceId */);
Garfield Tan1c7bc862020-01-28 13:24:04 -08003527 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
3528 InputEvent* repeatEvent = mWindow->consume();
3529 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
3530 EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER,
3531 IdGenerator::getSource(repeatEvent->getId()));
3532 }
3533}
3534
3535TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) {
Chris Ye2ad95392020-09-01 13:44:44 -07003536 sendAndConsumeKeyDown(1 /* deviceId */);
Garfield Tan1c7bc862020-01-28 13:24:04 -08003537
3538 std::unordered_set<int32_t> idSet;
3539 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
3540 InputEvent* repeatEvent = mWindow->consume();
3541 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
3542 int32_t id = repeatEvent->getId();
3543 EXPECT_EQ(idSet.end(), idSet.find(id));
3544 idSet.insert(id);
3545 }
3546}
3547
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003548/* Test InputDispatcher for MultiDisplay */
3549class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest {
3550public:
Prabir Pradhan3608aad2019-10-02 17:08:26 -07003551 virtual void SetUp() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003552 InputDispatcherTest::SetUp();
Arthur Hungb92218b2018-08-14 12:00:21 +08003553
Chris Yea209fde2020-07-22 13:54:51 -07003554 application1 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003555 windowInPrimary =
3556 new FakeWindowHandle(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08003557
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003558 // Set focus window for primary display, but focused display would be second one.
3559 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1);
Vishnu Nair47074b82020-08-14 11:54:47 -07003560 windowInPrimary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00003561 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003562 setFocusedWindow(windowInPrimary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003563 windowInPrimary->consumeFocusEvent(true);
Arthur Hungb92218b2018-08-14 12:00:21 +08003564
Chris Yea209fde2020-07-22 13:54:51 -07003565 application2 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003566 windowInSecondary =
3567 new FakeWindowHandle(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003568 // Set focus to second display window.
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003569 // Set focus display to second one.
3570 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
3571 // Set focus window for second display.
3572 mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2);
Vishnu Nair47074b82020-08-14 11:54:47 -07003573 windowInSecondary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00003574 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003575 setFocusedWindow(windowInSecondary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003576 windowInSecondary->consumeFocusEvent(true);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003577 }
3578
Prabir Pradhan3608aad2019-10-02 17:08:26 -07003579 virtual void TearDown() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003580 InputDispatcherTest::TearDown();
3581
Chris Yea209fde2020-07-22 13:54:51 -07003582 application1.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003583 windowInPrimary.clear();
Chris Yea209fde2020-07-22 13:54:51 -07003584 application2.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003585 windowInSecondary.clear();
3586 }
3587
3588protected:
Chris Yea209fde2020-07-22 13:54:51 -07003589 std::shared_ptr<FakeApplicationHandle> application1;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003590 sp<FakeWindowHandle> windowInPrimary;
Chris Yea209fde2020-07-22 13:54:51 -07003591 std::shared_ptr<FakeApplicationHandle> application2;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003592 sp<FakeWindowHandle> windowInSecondary;
3593};
3594
3595TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) {
3596 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003597 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3598 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
3599 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08003600 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08003601 windowInSecondary->assertNoEvents();
3602
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003603 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003604 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3605 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
3606 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08003607 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08003608 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +08003609}
3610
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003611TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) {
Tiger Huang721e26f2018-07-24 22:26:19 +08003612 // Test inject a key down with display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003613 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3614 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003615 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08003616 windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Tiger Huang721e26f2018-07-24 22:26:19 +08003617 windowInSecondary->assertNoEvents();
3618
3619 // Test inject a key down without display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003620 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003621 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08003622 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08003623 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hungb92218b2018-08-14 12:00:21 +08003624
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08003625 // Remove all windows in secondary display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00003626 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}});
Arthur Hungb92218b2018-08-14 12:00:21 +08003627
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003628 // Old focus should receive a cancel event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08003629 windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE,
3630 AKEY_EVENT_FLAG_CANCELED);
Arthur Hungb92218b2018-08-14 12:00:21 +08003631
3632 // Test inject a key down, should timeout because of no target window.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003633 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003634 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Arthur Hungb92218b2018-08-14 12:00:21 +08003635 windowInPrimary->assertNoEvents();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003636 windowInSecondary->consumeFocusEvent(false);
Arthur Hungb92218b2018-08-14 12:00:21 +08003637 windowInSecondary->assertNoEvents();
3638}
3639
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003640// Test per-display input monitors for motion event.
3641TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) {
chaviwd1c23182019-12-20 18:44:56 -08003642 FakeMonitorReceiver monitorInPrimary =
3643 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
3644 FakeMonitorReceiver monitorInSecondary =
3645 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003646
3647 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003648 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3649 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
3650 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08003651 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08003652 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003653 windowInSecondary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08003654 monitorInSecondary.assertNoEvents();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003655
3656 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003657 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3658 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
3659 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003660 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08003661 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08003662 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
chaviwd1c23182019-12-20 18:44:56 -08003663 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003664
3665 // Test inject a non-pointer motion event.
3666 // If specific a display, it will dispatch to the focused window of particular display,
3667 // or it will dispatch to the focused window of focused display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003668 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3669 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE))
3670 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003671 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08003672 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08003673 windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08003674 monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003675}
3676
3677// Test per-display input monitors for key event.
3678TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) {
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003679 // Input monitor per display.
chaviwd1c23182019-12-20 18:44:56 -08003680 FakeMonitorReceiver monitorInPrimary =
3681 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
3682 FakeMonitorReceiver monitorInSecondary =
3683 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003684
3685 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003686 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
3687 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003688 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08003689 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08003690 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08003691 monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003692}
3693
Vishnu Nair958da932020-08-21 17:12:37 -07003694TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) {
3695 sp<FakeWindowHandle> secondWindowInPrimary =
3696 new FakeWindowHandle(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
3697 secondWindowInPrimary->setFocusable(true);
3698 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}});
3699 setFocusedWindow(secondWindowInPrimary);
3700 windowInPrimary->consumeFocusEvent(false);
3701 secondWindowInPrimary->consumeFocusEvent(true);
3702
3703 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003704 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
3705 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07003706 windowInPrimary->assertNoEvents();
3707 windowInSecondary->assertNoEvents();
3708 secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3709}
3710
Arthur Hungdfd528e2021-12-08 13:23:04 +00003711TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CancelTouch_MultiDisplay) {
3712 FakeMonitorReceiver monitorInPrimary =
3713 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
3714 FakeMonitorReceiver monitorInSecondary =
3715 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
3716
3717 // Test touch down on primary display.
3718 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3719 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
3720 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3721 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3722 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
3723
3724 // Test touch down on second display.
3725 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3726 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
3727 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3728 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
3729 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
3730
3731 // Trigger cancel touch.
3732 mDispatcher->cancelCurrentTouch();
3733 windowInPrimary->consumeMotionCancel(ADISPLAY_ID_DEFAULT);
3734 monitorInPrimary.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
3735 windowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID);
3736 monitorInSecondary.consumeMotionCancel(SECOND_DISPLAY_ID);
3737
3738 // Test inject a move motion event, no window/monitor should receive the event.
3739 ASSERT_EQ(InputEventInjectionResult::FAILED,
3740 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
3741 ADISPLAY_ID_DEFAULT, {110, 200}))
3742 << "Inject motion event should return InputEventInjectionResult::FAILED";
3743 windowInPrimary->assertNoEvents();
3744 monitorInPrimary.assertNoEvents();
3745
3746 ASSERT_EQ(InputEventInjectionResult::FAILED,
3747 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
3748 SECOND_DISPLAY_ID, {110, 200}))
3749 << "Inject motion event should return InputEventInjectionResult::FAILED";
3750 windowInSecondary->assertNoEvents();
3751 monitorInSecondary.assertNoEvents();
3752}
3753
Jackal Guof9696682018-10-05 12:23:23 +08003754class InputFilterTest : public InputDispatcherTest {
3755protected:
Prabir Pradhan81420cc2021-09-06 10:28:50 -07003756 void testNotifyMotion(int32_t displayId, bool expectToBeFiltered,
3757 const ui::Transform& transform = ui::Transform()) {
Jackal Guof9696682018-10-05 12:23:23 +08003758 NotifyMotionArgs motionArgs;
3759
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003760 motionArgs =
3761 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Jackal Guof9696682018-10-05 12:23:23 +08003762 mDispatcher->notifyMotion(&motionArgs);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003763 motionArgs =
3764 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Jackal Guof9696682018-10-05 12:23:23 +08003765 mDispatcher->notifyMotion(&motionArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003766 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08003767 if (expectToBeFiltered) {
Prabir Pradhan81420cc2021-09-06 10:28:50 -07003768 const auto xy = transform.transform(motionArgs.pointerCoords->getXYValue());
3769 mFakePolicy->assertFilterInputEventWasCalled(motionArgs, xy);
Jackal Guof9696682018-10-05 12:23:23 +08003770 } else {
3771 mFakePolicy->assertFilterInputEventWasNotCalled();
3772 }
3773 }
3774
3775 void testNotifyKey(bool expectToBeFiltered) {
3776 NotifyKeyArgs keyArgs;
3777
3778 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
3779 mDispatcher->notifyKey(&keyArgs);
3780 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP);
3781 mDispatcher->notifyKey(&keyArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003782 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08003783
3784 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08003785 mFakePolicy->assertFilterInputEventWasCalled(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08003786 } else {
3787 mFakePolicy->assertFilterInputEventWasNotCalled();
3788 }
3789 }
3790};
3791
3792// Test InputFilter for MotionEvent
3793TEST_F(InputFilterTest, MotionEvent_InputFilter) {
3794 // Since the InputFilter is disabled by default, check if touch events aren't filtered.
3795 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
3796 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
3797
3798 // Enable InputFilter
3799 mDispatcher->setInputFilterEnabled(true);
3800 // Test touch on both primary and second display, and check if both events are filtered.
3801 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true);
3802 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true);
3803
3804 // Disable InputFilter
3805 mDispatcher->setInputFilterEnabled(false);
3806 // Test touch on both primary and second display, and check if both events aren't filtered.
3807 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
3808 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
3809}
3810
3811// Test InputFilter for KeyEvent
3812TEST_F(InputFilterTest, KeyEvent_InputFilter) {
3813 // Since the InputFilter is disabled by default, check if key event aren't filtered.
3814 testNotifyKey(/*expectToBeFiltered*/ false);
3815
3816 // Enable InputFilter
3817 mDispatcher->setInputFilterEnabled(true);
3818 // Send a key event, and check if it is filtered.
3819 testNotifyKey(/*expectToBeFiltered*/ true);
3820
3821 // Disable InputFilter
3822 mDispatcher->setInputFilterEnabled(false);
3823 // Send a key event, and check if it isn't filtered.
3824 testNotifyKey(/*expectToBeFiltered*/ false);
3825}
3826
Prabir Pradhan81420cc2021-09-06 10:28:50 -07003827// Ensure that MotionEvents sent to the InputFilter through InputListener are converted to the
3828// logical display coordinate space.
3829TEST_F(InputFilterTest, MotionEvent_UsesLogicalDisplayCoordinates_notifyMotion) {
3830 ui::Transform firstDisplayTransform;
3831 firstDisplayTransform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
3832 ui::Transform secondDisplayTransform;
3833 secondDisplayTransform.set({-6.6, -5.5, -4.4, -3.3, -2.2, -1.1, 0, 0, 1});
3834
3835 std::vector<gui::DisplayInfo> displayInfos(2);
3836 displayInfos[0].displayId = ADISPLAY_ID_DEFAULT;
3837 displayInfos[0].transform = firstDisplayTransform;
3838 displayInfos[1].displayId = SECOND_DISPLAY_ID;
3839 displayInfos[1].transform = secondDisplayTransform;
3840
3841 mDispatcher->onWindowInfosChanged({}, displayInfos);
3842
3843 // Enable InputFilter
3844 mDispatcher->setInputFilterEnabled(true);
3845
3846 // Ensure the correct transforms are used for the displays.
3847 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true, firstDisplayTransform);
3848 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true, secondDisplayTransform);
3849}
3850
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00003851class InputFilterInjectionPolicyTest : public InputDispatcherTest {
3852protected:
3853 virtual void SetUp() override {
3854 InputDispatcherTest::SetUp();
3855
3856 /**
3857 * We don't need to enable input filter to test the injected event policy, but we enabled it
3858 * here to make the tests more realistic, since this policy only matters when inputfilter is
3859 * on.
3860 */
3861 mDispatcher->setInputFilterEnabled(true);
3862
3863 std::shared_ptr<InputApplicationHandle> application =
3864 std::make_shared<FakeApplicationHandle>();
3865 mWindow =
3866 new FakeWindowHandle(application, mDispatcher, "Test Window", ADISPLAY_ID_DEFAULT);
3867
3868 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3869 mWindow->setFocusable(true);
3870 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3871 setFocusedWindow(mWindow);
3872 mWindow->consumeFocusEvent(true);
3873 }
3874
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00003875 void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
3876 int32_t flags) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00003877 KeyEvent event;
3878
3879 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
3880 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD,
3881 ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A,
3882 KEY_A, AMETA_NONE, 0 /*repeatCount*/, eventTime, eventTime);
3883 const int32_t additionalPolicyFlags =
3884 POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT;
3885 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3886 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
3887 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
3888 policyFlags | additionalPolicyFlags));
3889
3890 InputEvent* received = mWindow->consume();
3891 ASSERT_NE(nullptr, received);
3892 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00003893 ASSERT_EQ(received->getType(), AINPUT_EVENT_TYPE_KEY);
3894 KeyEvent& keyEvent = static_cast<KeyEvent&>(*received);
3895 ASSERT_EQ(flags, keyEvent.getFlags());
3896 }
3897
3898 void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
3899 int32_t flags) {
3900 MotionEvent event;
3901 PointerProperties pointerProperties[1];
3902 PointerCoords pointerCoords[1];
3903 pointerProperties[0].clear();
3904 pointerProperties[0].id = 0;
3905 pointerCoords[0].clear();
3906 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300);
3907 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400);
3908
3909 ui::Transform identityTransform;
3910 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
3911 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN,
3912 DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0,
3913 AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE,
3914 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -07003915 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, eventTime,
Evan Rosky09576692021-07-01 12:22:09 -07003916 eventTime,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00003917 /*pointerCount*/ 1, pointerProperties, pointerCoords);
3918
3919 const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER;
3920 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3921 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
3922 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
3923 policyFlags | additionalPolicyFlags));
3924
3925 InputEvent* received = mWindow->consume();
3926 ASSERT_NE(nullptr, received);
3927 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
3928 ASSERT_EQ(received->getType(), AINPUT_EVENT_TYPE_MOTION);
3929 MotionEvent& motionEvent = static_cast<MotionEvent&>(*received);
3930 ASSERT_EQ(flags, motionEvent.getFlags());
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00003931 }
3932
3933private:
3934 sp<FakeWindowHandle> mWindow;
3935};
3936
3937TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) {
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00003938 // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input
3939 // filter. Without it, the event will no different from a regularly injected event, and the
3940 // injected device id will be overwritten.
3941 testInjectedKey(POLICY_FLAG_FILTERED, 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/,
3942 0 /*flags*/);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00003943}
3944
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00003945TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00003946 testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00003947 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/,
3948 AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
3949}
3950
3951TEST_F(InputFilterInjectionPolicyTest,
3952 MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
3953 testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
3954 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/,
3955 AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00003956}
3957
3958TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) {
3959 testInjectedKey(0 /*policyFlags*/, 3 /*injectedDeviceId*/,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00003960 VIRTUAL_KEYBOARD_ID /*resolvedDeviceId*/, 0 /*flags*/);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00003961}
3962
chaviwfd6d3512019-03-25 13:23:49 -07003963class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest {
Prabir Pradhan3608aad2019-10-02 17:08:26 -07003964 virtual void SetUp() override {
chaviwfd6d3512019-03-25 13:23:49 -07003965 InputDispatcherTest::SetUp();
3966
Chris Yea209fde2020-07-22 13:54:51 -07003967 std::shared_ptr<FakeApplicationHandle> application =
3968 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003969 mUnfocusedWindow =
3970 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07003971 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
3972 // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this
3973 // window.
chaviw3277faf2021-05-19 16:45:23 -05003974 mUnfocusedWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
chaviwfd6d3512019-03-25 13:23:49 -07003975
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08003976 mFocusedWindow =
3977 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
3978 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
chaviw3277faf2021-05-19 16:45:23 -05003979 mFocusedWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
chaviwfd6d3512019-03-25 13:23:49 -07003980
3981 // Set focused application.
3982 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07003983 mFocusedWindow->setFocusable(true);
chaviwfd6d3512019-03-25 13:23:49 -07003984
3985 // Expect one focus window exist in display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00003986 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003987 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003988 mFocusedWindow->consumeFocusEvent(true);
chaviwfd6d3512019-03-25 13:23:49 -07003989 }
3990
Prabir Pradhan3608aad2019-10-02 17:08:26 -07003991 virtual void TearDown() override {
chaviwfd6d3512019-03-25 13:23:49 -07003992 InputDispatcherTest::TearDown();
3993
3994 mUnfocusedWindow.clear();
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08003995 mFocusedWindow.clear();
chaviwfd6d3512019-03-25 13:23:49 -07003996 }
3997
3998protected:
3999 sp<FakeWindowHandle> mUnfocusedWindow;
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08004000 sp<FakeWindowHandle> mFocusedWindow;
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07004001 static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60};
chaviwfd6d3512019-03-25 13:23:49 -07004002};
4003
4004// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
4005// DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received
4006// the onPointerDownOutsideFocus callback.
4007TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004008 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07004009 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4010 {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004011 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07004012 mUnfocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07004013
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004014 ASSERT_TRUE(mDispatcher->waitForIdle());
chaviwfd6d3512019-03-25 13:23:49 -07004015 mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken());
4016}
4017
4018// Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action
4019// DOWN on the window that doesn't have focus. Ensure no window received the
4020// onPointerDownOutsideFocus callback.
4021TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004022 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07004023 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004024 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07004025 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07004026
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004027 ASSERT_TRUE(mDispatcher->waitForIdle());
4028 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07004029}
4030
4031// Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't
4032// have focus. Ensure no window received the onPointerDownOutsideFocus callback.
4033TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08004034 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4035 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004036 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07004037 mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07004038
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004039 ASSERT_TRUE(mDispatcher->waitForIdle());
4040 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07004041}
4042
4043// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
4044// DOWN on the window that already has focus. Ensure no window received the
4045// onPointerDownOutsideFocus callback.
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004046TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004047 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08004048 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07004049 FOCUSED_WINDOW_TOUCH_POINT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004050 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07004051 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07004052
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004053 ASSERT_TRUE(mDispatcher->waitForIdle());
4054 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07004055}
4056
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08004057// Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag
4058// NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback.
4059TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) {
4060 const MotionEvent event =
4061 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
4062 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
4063 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(20).y(20))
4064 .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE)
4065 .build();
4066 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(mDispatcher, event))
4067 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4068 mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
4069
4070 ASSERT_TRUE(mDispatcher->waitForIdle());
4071 mFakePolicy->assertOnPointerDownWasNotCalled();
4072 // Ensure that the unfocused window did not receive any FOCUS events.
4073 mUnfocusedWindow->assertNoEvents();
4074}
4075
chaviwaf87b3e2019-10-01 16:59:28 -07004076// These tests ensures we can send touch events to a single client when there are multiple input
4077// windows that point to the same client token.
4078class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest {
4079 virtual void SetUp() override {
4080 InputDispatcherTest::SetUp();
4081
Chris Yea209fde2020-07-22 13:54:51 -07004082 std::shared_ptr<FakeApplicationHandle> application =
4083 std::make_shared<FakeApplicationHandle>();
chaviwaf87b3e2019-10-01 16:59:28 -07004084 mWindow1 = new FakeWindowHandle(application, mDispatcher, "Fake Window 1",
4085 ADISPLAY_ID_DEFAULT);
4086 // Adding FLAG_NOT_TOUCH_MODAL otherwise all taps will go to the top most window.
4087 // We also need FLAG_SPLIT_TOUCH or we won't be able to get touches for both windows.
chaviw3277faf2021-05-19 16:45:23 -05004088 mWindow1->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH);
chaviwaf87b3e2019-10-01 16:59:28 -07004089 mWindow1->setFrame(Rect(0, 0, 100, 100));
4090
4091 mWindow2 = new FakeWindowHandle(application, mDispatcher, "Fake Window 2",
4092 ADISPLAY_ID_DEFAULT, mWindow1->getToken());
chaviw3277faf2021-05-19 16:45:23 -05004093 mWindow2->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH);
chaviwaf87b3e2019-10-01 16:59:28 -07004094 mWindow2->setFrame(Rect(100, 100, 200, 200));
4095
Arthur Hung72d8dc32020-03-28 00:48:39 +00004096 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
chaviwaf87b3e2019-10-01 16:59:28 -07004097 }
4098
4099protected:
4100 sp<FakeWindowHandle> mWindow1;
4101 sp<FakeWindowHandle> mWindow2;
4102
4103 // Helper function to convert the point from screen coordinates into the window's space
chaviw3277faf2021-05-19 16:45:23 -05004104 static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) {
chaviw1ff3d1e2020-07-01 15:53:47 -07004105 vec2 vals = windowInfo->transform.transform(point.x, point.y);
4106 return {vals.x, vals.y};
chaviwaf87b3e2019-10-01 16:59:28 -07004107 }
4108
4109 void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction,
4110 const std::vector<PointF>& points) {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004111 const std::string name = window->getName();
chaviwaf87b3e2019-10-01 16:59:28 -07004112 InputEvent* event = window->consume();
4113
4114 ASSERT_NE(nullptr, event) << name.c_str()
4115 << ": consumer should have returned non-NULL event.";
4116
4117 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType())
4118 << name.c_str() << "expected " << inputEventTypeToString(AINPUT_EVENT_TYPE_MOTION)
4119 << " event, got " << inputEventTypeToString(event->getType()) << " event";
4120
4121 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004122 assertMotionAction(expectedAction, motionEvent.getAction());
chaviwaf87b3e2019-10-01 16:59:28 -07004123
4124 for (size_t i = 0; i < points.size(); i++) {
4125 float expectedX = points[i].x;
4126 float expectedY = points[i].y;
4127
4128 EXPECT_EQ(expectedX, motionEvent.getX(i))
4129 << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str()
4130 << ", got " << motionEvent.getX(i);
4131 EXPECT_EQ(expectedY, motionEvent.getY(i))
4132 << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str()
4133 << ", got " << motionEvent.getY(i);
4134 }
4135 }
chaviw9eaa22c2020-07-01 16:21:27 -07004136
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08004137 void touchAndAssertPositions(int32_t action, const std::vector<PointF>& touchedPoints,
chaviw9eaa22c2020-07-01 16:21:27 -07004138 std::vector<PointF> expectedPoints) {
4139 NotifyMotionArgs motionArgs = generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN,
4140 ADISPLAY_ID_DEFAULT, touchedPoints);
4141 mDispatcher->notifyMotion(&motionArgs);
4142
4143 // Always consume from window1 since it's the window that has the InputReceiver
4144 consumeMotionEvent(mWindow1, action, expectedPoints);
4145 }
chaviwaf87b3e2019-10-01 16:59:28 -07004146};
4147
4148TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) {
4149 // Touch Window 1
4150 PointF touchedPoint = {10, 10};
4151 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07004152 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07004153
4154 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07004155 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07004156
4157 // Touch Window 2
4158 touchedPoint = {150, 150};
4159 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07004160 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07004161}
4162
chaviw9eaa22c2020-07-01 16:21:27 -07004163TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) {
4164 // Set scale value for window2
chaviwaf87b3e2019-10-01 16:59:28 -07004165 mWindow2->setWindowScale(0.5f, 0.5f);
4166
4167 // Touch Window 1
4168 PointF touchedPoint = {10, 10};
4169 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07004170 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07004171 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07004172 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07004173
4174 // Touch Window 2
4175 touchedPoint = {150, 150};
4176 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07004177 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
4178 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07004179
chaviw9eaa22c2020-07-01 16:21:27 -07004180 // Update the transform so rotation is set
4181 mWindow2->setWindowTransform(0, -1, 1, 0);
4182 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
4183 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07004184}
4185
chaviw9eaa22c2020-07-01 16:21:27 -07004186TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004187 mWindow2->setWindowScale(0.5f, 0.5f);
4188
4189 // Touch Window 1
4190 std::vector<PointF> touchedPoints = {PointF{10, 10}};
4191 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07004192 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004193
4194 // Touch Window 2
4195 int32_t actionPointerDown =
4196 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
chaviw9eaa22c2020-07-01 16:21:27 -07004197 touchedPoints.push_back(PointF{150, 150});
4198 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
4199 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004200
chaviw9eaa22c2020-07-01 16:21:27 -07004201 // Release Window 2
4202 int32_t actionPointerUp =
4203 AMOTION_EVENT_ACTION_POINTER_UP + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
4204 touchAndAssertPositions(actionPointerUp, touchedPoints, expectedPoints);
4205 expectedPoints.pop_back();
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004206
chaviw9eaa22c2020-07-01 16:21:27 -07004207 // Update the transform so rotation is set for Window 2
4208 mWindow2->setWindowTransform(0, -1, 1, 0);
4209 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
4210 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004211}
4212
chaviw9eaa22c2020-07-01 16:21:27 -07004213TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004214 mWindow2->setWindowScale(0.5f, 0.5f);
4215
4216 // Touch Window 1
4217 std::vector<PointF> touchedPoints = {PointF{10, 10}};
4218 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07004219 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004220
4221 // Touch Window 2
4222 int32_t actionPointerDown =
4223 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
chaviw9eaa22c2020-07-01 16:21:27 -07004224 touchedPoints.push_back(PointF{150, 150});
4225 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004226
chaviw9eaa22c2020-07-01 16:21:27 -07004227 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004228
4229 // Move both windows
4230 touchedPoints = {{20, 20}, {175, 175}};
4231 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
4232 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
4233
chaviw9eaa22c2020-07-01 16:21:27 -07004234 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004235
chaviw9eaa22c2020-07-01 16:21:27 -07004236 // Release Window 2
4237 int32_t actionPointerUp =
4238 AMOTION_EVENT_ACTION_POINTER_UP + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
4239 touchAndAssertPositions(actionPointerUp, touchedPoints, expectedPoints);
4240 expectedPoints.pop_back();
4241
4242 // Touch Window 2
4243 mWindow2->setWindowTransform(0, -1, 1, 0);
4244 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
4245 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
4246
4247 // Move both windows
4248 touchedPoints = {{20, 20}, {175, 175}};
4249 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
4250 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
4251
4252 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004253}
4254
4255TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) {
4256 mWindow1->setWindowScale(0.5f, 0.5f);
4257
4258 // Touch Window 1
4259 std::vector<PointF> touchedPoints = {PointF{10, 10}};
4260 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07004261 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004262
4263 // Touch Window 2
4264 int32_t actionPointerDown =
4265 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
chaviw9eaa22c2020-07-01 16:21:27 -07004266 touchedPoints.push_back(PointF{150, 150});
4267 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004268
chaviw9eaa22c2020-07-01 16:21:27 -07004269 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004270
4271 // Move both windows
4272 touchedPoints = {{20, 20}, {175, 175}};
4273 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
4274 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
4275
chaviw9eaa22c2020-07-01 16:21:27 -07004276 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00004277}
4278
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004279class InputDispatcherSingleWindowAnr : public InputDispatcherTest {
4280 virtual void SetUp() override {
4281 InputDispatcherTest::SetUp();
4282
Chris Yea209fde2020-07-22 13:54:51 -07004283 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004284 mApplication->setDispatchingTimeout(20ms);
4285 mWindow =
4286 new FakeWindowHandle(mApplication, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
4287 mWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05004288 mWindow->setDispatchingTimeout(30ms);
Vishnu Nair47074b82020-08-14 11:54:47 -07004289 mWindow->setFocusable(true);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004290 // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this
4291 // window.
chaviw3277faf2021-05-19 16:45:23 -05004292 mWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004293
4294 // Set focused application.
4295 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
4296
4297 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004298 setFocusedWindow(mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004299 mWindow->consumeFocusEvent(true);
4300 }
4301
4302 virtual void TearDown() override {
4303 InputDispatcherTest::TearDown();
4304 mWindow.clear();
4305 }
4306
4307protected:
Chris Yea209fde2020-07-22 13:54:51 -07004308 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004309 sp<FakeWindowHandle> mWindow;
4310 static constexpr PointF WINDOW_LOCATION = {20, 20};
4311
4312 void tapOnWindow() {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004313 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004314 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4315 WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004316 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004317 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4318 WINDOW_LOCATION));
4319 }
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004320
4321 sp<FakeWindowHandle> addSpyWindow() {
4322 sp<FakeWindowHandle> spy =
4323 new FakeWindowHandle(mApplication, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
4324 spy->setTrustedOverlay(true);
4325 spy->setFocusable(false);
4326 spy->setInputFeatures(WindowInfo::Feature::SPY);
4327 spy->setDispatchingTimeout(30ms);
4328 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, mWindow}}});
4329 return spy;
4330 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004331};
4332
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004333// Send a tap and respond, which should not cause an ANR.
4334TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) {
4335 tapOnWindow();
4336 mWindow->consumeMotionDown();
4337 mWindow->consumeMotionUp();
4338 ASSERT_TRUE(mDispatcher->waitForIdle());
4339 mFakePolicy->assertNotifyAnrWasNotCalled();
4340}
4341
4342// Send a regular key and respond, which should not cause an ANR.
4343TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08004344 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004345 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
4346 ASSERT_TRUE(mDispatcher->waitForIdle());
4347 mFakePolicy->assertNotifyAnrWasNotCalled();
4348}
4349
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05004350TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) {
4351 mWindow->setFocusable(false);
4352 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
4353 mWindow->consumeFocusEvent(false);
4354
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004355 InputEventInjectionResult result =
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05004356 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08004357 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/,
4358 false /* allowKeyRepeat */);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004359 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05004360 // Key will not go to window because we have no focused window.
4361 // The 'no focused window' ANR timer should start instead.
4362
4363 // Now, the focused application goes away.
4364 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr);
4365 // The key should get dropped and there should be no ANR.
4366
4367 ASSERT_TRUE(mDispatcher->waitForIdle());
4368 mFakePolicy->assertNotifyAnrWasNotCalled();
4369}
4370
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004371// Send an event to the app and have the app not respond right away.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004372// When ANR is raised, policy will tell the dispatcher to cancel the events for that window.
4373// So InputDispatcher will enqueue ACTION_CANCEL event as well.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004374TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004375 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004376 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4377 WINDOW_LOCATION));
4378
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004379 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN
4380 ASSERT_TRUE(sequenceNum);
4381 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004382 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004383
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004384 mWindow->finishEvent(*sequenceNum);
4385 mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL,
4386 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004387 ASSERT_TRUE(mDispatcher->waitForIdle());
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004388 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004389}
4390
4391// Send a key to the app and have the app not respond right away.
4392TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) {
4393 // Inject a key, and don't respond - expect that ANR is called.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08004394 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004395 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent();
4396 ASSERT_TRUE(sequenceNum);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004397 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004398 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004399 ASSERT_TRUE(mDispatcher->waitForIdle());
4400}
4401
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004402// We have a focused application, but no focused window
4403TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) {
Vishnu Nair47074b82020-08-14 11:54:47 -07004404 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004405 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
4406 mWindow->consumeFocusEvent(false);
4407
4408 // taps on the window work as normal
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004409 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004410 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4411 WINDOW_LOCATION));
4412 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
4413 mDispatcher->waitForIdle();
4414 mFakePolicy->assertNotifyAnrWasNotCalled();
4415
4416 // Once a focused event arrives, we get an ANR for this application
4417 // We specify the injection timeout to be smaller than the application timeout, to ensure that
4418 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004419 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004420 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08004421 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, false /* allowKeyRepeat */);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004422 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004423 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004424 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004425 ASSERT_TRUE(mDispatcher->waitForIdle());
4426}
4427
4428// We have a focused application, but no focused window
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004429// Make sure that we don't notify policy twice about the same ANR.
4430TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) {
Vishnu Nair47074b82020-08-14 11:54:47 -07004431 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004432 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
4433 mWindow->consumeFocusEvent(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004434
4435 // Once a focused event arrives, we get an ANR for this application
4436 // We specify the injection timeout to be smaller than the application timeout, to ensure that
4437 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004438 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004439 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08004440 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, false /* allowKeyRepeat */);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004441 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004442 const std::chrono::duration appTimeout =
4443 mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004444 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004445
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004446 std::this_thread::sleep_for(appTimeout);
4447 // ANR should not be raised again. It is up to policy to do that if it desires.
4448 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004449
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004450 // If we now get a focused window, the ANR should stop, but the policy handles that via
4451 // 'notifyFocusChanged' callback. This is implemented in the policy so we can't test it here.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004452 ASSERT_TRUE(mDispatcher->waitForIdle());
4453}
4454
4455// We have a focused application, but no focused window
4456TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) {
Vishnu Nair47074b82020-08-14 11:54:47 -07004457 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004458 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
4459 mWindow->consumeFocusEvent(false);
4460
4461 // Once a focused event arrives, we get an ANR for this application
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004462 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004463 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004464 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
4465 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004466
4467 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004468 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004469
4470 // Future focused events get dropped right away
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004471 ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004472 ASSERT_TRUE(mDispatcher->waitForIdle());
4473 mWindow->assertNoEvents();
4474}
4475
4476/**
4477 * Ensure that the implementation is valid. Since we are using multiset to keep track of the
4478 * ANR timeouts, we are allowing entries with identical timestamps in the same connection.
4479 * If we process 1 of the events, but ANR on the second event with the same timestamp,
4480 * the ANR mechanism should still work.
4481 *
4482 * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the
4483 * DOWN event, while not responding on the second one.
4484 */
4485TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) {
4486 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
4487 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4488 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
4489 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
4490 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004491 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004492
4493 // Now send ACTION_UP, with identical timestamp
4494 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4495 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
4496 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
4497 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004498 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004499
4500 // We have now sent down and up. Let's consume first event and then ANR on the second.
4501 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4502 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004503 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004504}
4505
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004506// A spy window can receive an ANR
4507TEST_F(InputDispatcherSingleWindowAnr, SpyWindowAnr) {
4508 sp<FakeWindowHandle> spy = addSpyWindow();
4509
4510 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4511 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4512 WINDOW_LOCATION));
4513 mWindow->consumeMotionDown();
4514
4515 std::optional<uint32_t> sequenceNum = spy->receiveEvent(); // ACTION_DOWN
4516 ASSERT_TRUE(sequenceNum);
4517 const std::chrono::duration timeout = spy->getDispatchingTimeout(DISPATCHING_TIMEOUT);
4518 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, spy->getToken());
4519
4520 spy->finishEvent(*sequenceNum);
4521 spy->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, ADISPLAY_ID_DEFAULT,
4522 0 /*flags*/);
4523 ASSERT_TRUE(mDispatcher->waitForIdle());
4524 mFakePolicy->assertNotifyWindowResponsiveWasCalled(spy->getToken());
4525}
4526
4527// If an app is not responding to a key event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004528// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004529TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnKey) {
4530 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004531
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004532 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4533 injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004534 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004535 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004536
4537 // Stuck on the ACTION_UP
4538 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004539 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004540
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004541 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004542 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004543 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4544 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004545
4546 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion
4547 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004548 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004549 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004550 spy->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004551}
4552
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004553// If an app is not responding to a motion event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004554// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004555TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnMotion) {
4556 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004557
4558 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004559 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4560 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004561
4562 mWindow->consumeMotionDown();
4563 // Stuck on the ACTION_UP
4564 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004565 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004566
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004567 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004568 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004569 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4570 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004571
4572 mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion
4573 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004574 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004575 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004576 spy->assertNoEvents();
4577}
4578
4579TEST_F(InputDispatcherSingleWindowAnr, UnresponsiveMonitorAnr) {
4580 mDispatcher->setMonitorDispatchingTimeoutForTest(30ms);
4581
4582 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
4583
4584 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4585 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4586 WINDOW_LOCATION));
4587
4588 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4589 const std::optional<uint32_t> consumeSeq = monitor.receiveEvent();
4590 ASSERT_TRUE(consumeSeq);
4591
4592 mFakePolicy->assertNotifyMonitorUnresponsiveWasCalled(30ms);
4593
4594 monitor.finishEvent(*consumeSeq);
4595 monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
4596
4597 ASSERT_TRUE(mDispatcher->waitForIdle());
4598 mFakePolicy->assertNotifyMonitorResponsiveWasCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004599}
4600
4601// If a window is unresponsive, then you get anr. if the window later catches up and starts to
4602// process events, you don't get an anr. When the window later becomes unresponsive again, you
4603// get an ANR again.
4604// 1. tap -> block on ACTION_UP -> receive ANR
4605// 2. consume all pending events (= queue becomes healthy again)
4606// 3. tap again -> block on ACTION_UP again -> receive ANR second time
4607TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) {
4608 tapOnWindow();
4609
4610 mWindow->consumeMotionDown();
4611 // Block on ACTION_UP
4612 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004613 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004614 mWindow->consumeMotionUp(); // Now the connection should be healthy again
4615 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004616 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004617 mWindow->assertNoEvents();
4618
4619 tapOnWindow();
4620 mWindow->consumeMotionDown();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004621 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004622 mWindow->consumeMotionUp();
4623
4624 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004625 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004626 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004627 mWindow->assertNoEvents();
4628}
4629
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004630// If a connection remains unresponsive for a while, make sure policy is only notified once about
4631// it.
4632TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004633 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004634 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4635 WINDOW_LOCATION));
4636
4637 const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004638 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004639 std::this_thread::sleep_for(windowTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004640 // 'notifyConnectionUnresponsive' should only be called once per connection
4641 mFakePolicy->assertNotifyAnrWasNotCalled();
4642 // When the ANR happened, dispatcher should abort the current event stream via ACTION_CANCEL
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004643 mWindow->consumeMotionDown();
4644 mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL,
4645 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
4646 mWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004647 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004648 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004649 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004650}
4651
4652/**
4653 * If a window is processing a motion event, and then a key event comes in, the key event should
4654 * not to to the focused window until the motion is processed.
4655 *
4656 * Warning!!!
4657 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
4658 * and the injection timeout that we specify when injecting the key.
4659 * We must have the injection timeout (10ms) be smaller than
4660 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
4661 *
4662 * If that value changes, this test should also change.
4663 */
4664TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) {
4665 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
4666 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
4667
4668 tapOnWindow();
4669 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
4670 ASSERT_TRUE(downSequenceNum);
4671 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
4672 ASSERT_TRUE(upSequenceNum);
4673 // Don't finish the events yet, and send a key
4674 // Injection will "succeed" because we will eventually give up and send the key to the focused
4675 // window even if motions are still being processed. But because the injection timeout is short,
4676 // we will receive INJECTION_TIMED_OUT as the result.
4677
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004678 InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004679 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004680 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
4681 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004682 // Key will not be sent to the window, yet, because the window is still processing events
4683 // and the key remains pending, waiting for the touch events to be processed
4684 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
4685 ASSERT_FALSE(keySequenceNum);
4686
4687 std::this_thread::sleep_for(500ms);
4688 // if we wait long enough though, dispatcher will give up, and still send the key
4689 // to the focused window, even though we have not yet finished the motion event
4690 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4691 mWindow->finishEvent(*downSequenceNum);
4692 mWindow->finishEvent(*upSequenceNum);
4693}
4694
4695/**
4696 * If a window is processing a motion event, and then a key event comes in, the key event should
4697 * not go to the focused window until the motion is processed.
4698 * If then a new motion comes in, then the pending key event should be going to the currently
4699 * focused window right away.
4700 */
4701TEST_F(InputDispatcherSingleWindowAnr,
4702 PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) {
4703 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
4704 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
4705
4706 tapOnWindow();
4707 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
4708 ASSERT_TRUE(downSequenceNum);
4709 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
4710 ASSERT_TRUE(upSequenceNum);
4711 // Don't finish the events yet, and send a key
4712 // Injection is async, so it will succeed
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004713 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004714 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004715 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004716 // At this point, key is still pending, and should not be sent to the application yet.
4717 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
4718 ASSERT_FALSE(keySequenceNum);
4719
4720 // Now tap down again. It should cause the pending key to go to the focused window right away.
4721 tapOnWindow();
4722 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd
4723 // the other events yet. We can finish events in any order.
4724 mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN
4725 mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP
4726 mWindow->consumeMotionDown();
4727 mWindow->consumeMotionUp();
4728 mWindow->assertNoEvents();
4729}
4730
4731class InputDispatcherMultiWindowAnr : public InputDispatcherTest {
4732 virtual void SetUp() override {
4733 InputDispatcherTest::SetUp();
4734
Chris Yea209fde2020-07-22 13:54:51 -07004735 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004736 mApplication->setDispatchingTimeout(10ms);
4737 mUnfocusedWindow =
4738 new FakeWindowHandle(mApplication, mDispatcher, "Unfocused", ADISPLAY_ID_DEFAULT);
4739 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
4740 // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this
4741 // window.
4742 // Adding FLAG_WATCH_OUTSIDE_TOUCH to receive ACTION_OUTSIDE when another window is tapped
chaviw3277faf2021-05-19 16:45:23 -05004743 mUnfocusedWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL |
4744 WindowInfo::Flag::WATCH_OUTSIDE_TOUCH |
4745 WindowInfo::Flag::SPLIT_TOUCH);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004746
4747 mFocusedWindow =
4748 new FakeWindowHandle(mApplication, mDispatcher, "Focused", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05004749 mFocusedWindow->setDispatchingTimeout(30ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004750 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
chaviw3277faf2021-05-19 16:45:23 -05004751 mFocusedWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004752
4753 // Set focused application.
4754 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
Vishnu Nair47074b82020-08-14 11:54:47 -07004755 mFocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004756
4757 // Expect one focus window exist in display.
4758 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004759 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004760 mFocusedWindow->consumeFocusEvent(true);
4761 }
4762
4763 virtual void TearDown() override {
4764 InputDispatcherTest::TearDown();
4765
4766 mUnfocusedWindow.clear();
4767 mFocusedWindow.clear();
4768 }
4769
4770protected:
Chris Yea209fde2020-07-22 13:54:51 -07004771 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004772 sp<FakeWindowHandle> mUnfocusedWindow;
4773 sp<FakeWindowHandle> mFocusedWindow;
4774 static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20};
4775 static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75};
4776 static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40};
4777
4778 void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); }
4779
4780 void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); }
4781
4782private:
4783 void tap(const PointF& location) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004784 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004785 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4786 location));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004787 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004788 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4789 location));
4790 }
4791};
4792
4793// If we have 2 windows that are both unresponsive, the one with the shortest timeout
4794// should be ANR'd first.
4795TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004796 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004797 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4798 FOCUSED_WINDOW_LOCATION))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004799 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004800 mFocusedWindow->consumeMotionDown();
4801 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
4802 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
4803 // We consumed all events, so no ANR
4804 ASSERT_TRUE(mDispatcher->waitForIdle());
4805 mFakePolicy->assertNotifyAnrWasNotCalled();
4806
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004807 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004808 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4809 FOCUSED_WINDOW_LOCATION));
4810 std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent();
4811 ASSERT_TRUE(unfocusedSequenceNum);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004812
4813 const std::chrono::duration timeout =
4814 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004815 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004816 // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event
4817 // sequence to make it consistent
4818 mFocusedWindow->consumeMotionCancel();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004819 mUnfocusedWindow->finishEvent(*unfocusedSequenceNum);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004820 mFocusedWindow->consumeMotionDown();
4821 // This cancel is generated because the connection was unresponsive
4822 mFocusedWindow->consumeMotionCancel();
4823 mFocusedWindow->assertNoEvents();
4824 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004825 ASSERT_TRUE(mDispatcher->waitForIdle());
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004826 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004827 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004828}
4829
4830// If we have 2 windows with identical timeouts that are both unresponsive,
4831// it doesn't matter which order they should have ANR.
4832// But we should receive ANR for both.
4833TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) {
4834 // Set the timeout for unfocused window to match the focused window
4835 mUnfocusedWindow->setDispatchingTimeout(10ms);
4836 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
4837
4838 tapOnFocusedWindow();
4839 // we should have ACTION_DOWN/ACTION_UP on focused window and ACTION_OUTSIDE on unfocused window
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004840 sp<IBinder> anrConnectionToken1 = mFakePolicy->getUnresponsiveWindowToken(10ms);
4841 sp<IBinder> anrConnectionToken2 = mFakePolicy->getUnresponsiveWindowToken(0ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004842
4843 // We don't know which window will ANR first. But both of them should happen eventually.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004844 ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 ||
4845 mFocusedWindow->getToken() == anrConnectionToken2);
4846 ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 ||
4847 mUnfocusedWindow->getToken() == anrConnectionToken2);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004848
4849 ASSERT_TRUE(mDispatcher->waitForIdle());
4850 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004851
4852 mFocusedWindow->consumeMotionDown();
4853 mFocusedWindow->consumeMotionUp();
4854 mUnfocusedWindow->consumeMotionOutside();
4855
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004856 sp<IBinder> responsiveToken1 = mFakePolicy->getResponsiveWindowToken();
4857 sp<IBinder> responsiveToken2 = mFakePolicy->getResponsiveWindowToken();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004858
4859 // Both applications should be marked as responsive, in any order
4860 ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 ||
4861 mFocusedWindow->getToken() == responsiveToken2);
4862 ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 ||
4863 mUnfocusedWindow->getToken() == responsiveToken2);
4864 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004865}
4866
4867// If a window is already not responding, the second tap on the same window should be ignored.
4868// We should also log an error to account for the dropped event (not tested here).
4869// At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events.
4870TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) {
4871 tapOnFocusedWindow();
4872 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
4873 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
4874 // Receive the events, but don't respond
4875 std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN
4876 ASSERT_TRUE(downEventSequenceNum);
4877 std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP
4878 ASSERT_TRUE(upEventSequenceNum);
4879 const std::chrono::duration timeout =
4880 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004881 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004882
4883 // Tap once again
4884 // We cannot use "tapOnFocusedWindow" because it asserts the injection result to be success
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004885 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004886 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4887 FOCUSED_WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004888 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004889 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4890 FOCUSED_WINDOW_LOCATION));
4891 // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a
4892 // valid touch target
4893 mUnfocusedWindow->assertNoEvents();
4894
4895 // Consume the first tap
4896 mFocusedWindow->finishEvent(*downEventSequenceNum);
4897 mFocusedWindow->finishEvent(*upEventSequenceNum);
4898 ASSERT_TRUE(mDispatcher->waitForIdle());
4899 // The second tap did not go to the focused window
4900 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004901 // Since all events are finished, connection should be deemed healthy again
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004902 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004903 mFakePolicy->assertNotifyAnrWasNotCalled();
4904}
4905
4906// If you tap outside of all windows, there will not be ANR
4907TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004908 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004909 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4910 LOCATION_OUTSIDE_ALL_WINDOWS));
4911 ASSERT_TRUE(mDispatcher->waitForIdle());
4912 mFakePolicy->assertNotifyAnrWasNotCalled();
4913}
4914
4915// Since the focused window is paused, tapping on it should not produce any events
4916TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) {
4917 mFocusedWindow->setPaused(true);
4918 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
4919
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004920 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004921 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4922 FOCUSED_WINDOW_LOCATION));
4923
4924 std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT));
4925 ASSERT_TRUE(mDispatcher->waitForIdle());
4926 // Should not ANR because the window is paused, and touches shouldn't go to it
4927 mFakePolicy->assertNotifyAnrWasNotCalled();
4928
4929 mFocusedWindow->assertNoEvents();
4930 mUnfocusedWindow->assertNoEvents();
4931}
4932
4933/**
4934 * If a window is processing a motion event, and then a key event comes in, the key event should
4935 * not to to the focused window until the motion is processed.
4936 * If a different window becomes focused at this time, the key should go to that window instead.
4937 *
4938 * Warning!!!
4939 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
4940 * and the injection timeout that we specify when injecting the key.
4941 * We must have the injection timeout (10ms) be smaller than
4942 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
4943 *
4944 * If that value changes, this test should also change.
4945 */
4946TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) {
4947 // Set a long ANR timeout to prevent it from triggering
4948 mFocusedWindow->setDispatchingTimeout(2s);
4949 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
4950
4951 tapOnUnfocusedWindow();
4952 std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent();
4953 ASSERT_TRUE(downSequenceNum);
4954 std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent();
4955 ASSERT_TRUE(upSequenceNum);
4956 // Don't finish the events yet, and send a key
4957 // Injection will succeed because we will eventually give up and send the key to the focused
4958 // window even if motions are still being processed.
4959
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004960 InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004961 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004962 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/);
4963 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004964 // Key will not be sent to the window, yet, because the window is still processing events
4965 // and the key remains pending, waiting for the touch events to be processed
4966 std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent();
4967 ASSERT_FALSE(keySequenceNum);
4968
4969 // Switch the focus to the "unfocused" window that we tapped. Expect the key to go there
Vishnu Nair47074b82020-08-14 11:54:47 -07004970 mFocusedWindow->setFocusable(false);
4971 mUnfocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004972 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004973 setFocusedWindow(mUnfocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004974
4975 // Focus events should precede the key events
4976 mUnfocusedWindow->consumeFocusEvent(true);
4977 mFocusedWindow->consumeFocusEvent(false);
4978
4979 // Finish the tap events, which should unblock dispatcher
4980 mUnfocusedWindow->finishEvent(*downSequenceNum);
4981 mUnfocusedWindow->finishEvent(*upSequenceNum);
4982
4983 // Now that all queues are cleared and no backlog in the connections, the key event
4984 // can finally go to the newly focused "mUnfocusedWindow".
4985 mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4986 mFocusedWindow->assertNoEvents();
4987 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004988 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004989}
4990
4991// When the touch stream is split across 2 windows, and one of them does not respond,
4992// then ANR should be raised and the touch should be canceled for the unresponsive window.
4993// The other window should not be affected by that.
4994TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) {
4995 // Touch Window 1
4996 NotifyMotionArgs motionArgs =
4997 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4998 ADISPLAY_ID_DEFAULT, {FOCUSED_WINDOW_LOCATION});
4999 mDispatcher->notifyMotion(&motionArgs);
5000 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
5001 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
5002
5003 // Touch Window 2
5004 int32_t actionPointerDown =
5005 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
5006
5007 motionArgs =
5008 generateMotionArgs(actionPointerDown, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5009 {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION});
5010 mDispatcher->notifyMotion(&motionArgs);
5011
5012 const std::chrono::duration timeout =
5013 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00005014 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005015
5016 mUnfocusedWindow->consumeMotionDown();
5017 mFocusedWindow->consumeMotionDown();
5018 // Focused window may or may not receive ACTION_MOVE
5019 // But it should definitely receive ACTION_CANCEL due to the ANR
5020 InputEvent* event;
5021 std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event);
5022 ASSERT_TRUE(moveOrCancelSequenceNum);
5023 mFocusedWindow->finishEvent(*moveOrCancelSequenceNum);
5024 ASSERT_NE(nullptr, event);
5025 ASSERT_EQ(event->getType(), AINPUT_EVENT_TYPE_MOTION);
5026 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
5027 if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) {
5028 mFocusedWindow->consumeMotionCancel();
5029 } else {
5030 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction());
5031 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005032 ASSERT_TRUE(mDispatcher->waitForIdle());
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00005033 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005034
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005035 mUnfocusedWindow->assertNoEvents();
5036 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05005037 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005038}
5039
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05005040/**
5041 * If we have no focused window, and a key comes in, we start the ANR timer.
5042 * The focused application should add a focused window before the timer runs out to prevent ANR.
5043 *
5044 * If the user touches another application during this time, the key should be dropped.
5045 * Next, if a new focused window comes in, without toggling the focused application,
5046 * then no ANR should occur.
5047 *
5048 * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication',
5049 * but in some cases the policy may not update the focused application.
5050 */
5051TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) {
5052 std::shared_ptr<FakeApplicationHandle> focusedApplication =
5053 std::make_shared<FakeApplicationHandle>();
5054 focusedApplication->setDispatchingTimeout(60ms);
5055 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication);
5056 // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused.
5057 mFocusedWindow->setFocusable(false);
5058
5059 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
5060 mFocusedWindow->consumeFocusEvent(false);
5061
5062 // Send a key. The ANR timer should start because there is no focused window.
5063 // 'focusedApplication' will get blamed if this timer completes.
5064 // Key will not be sent anywhere because we have no focused window. It will remain pending.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005065 InputEventInjectionResult result =
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05005066 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005067 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/,
5068 false /* allowKeyRepeat */);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005069 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05005070
5071 // Wait until dispatcher starts the "no focused window" timer. If we don't wait here,
5072 // then the injected touches won't cause the focused event to get dropped.
5073 // The dispatcher only checks for whether the queue should be pruned upon queueing.
5074 // If we inject the touch right away and the ANR timer hasn't started, the touch event would
5075 // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'.
5076 // For this test, it means that the key would get delivered to the window once it becomes
5077 // focused.
5078 std::this_thread::sleep_for(10ms);
5079
5080 // Touch unfocused window. This should force the pending key to get dropped.
5081 NotifyMotionArgs motionArgs =
5082 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5083 ADISPLAY_ID_DEFAULT, {UNFOCUSED_WINDOW_LOCATION});
5084 mDispatcher->notifyMotion(&motionArgs);
5085
5086 // We do not consume the motion right away, because that would require dispatcher to first
5087 // process (== drop) the key event, and by that time, ANR will be raised.
5088 // Set the focused window first.
5089 mFocusedWindow->setFocusable(true);
5090 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
5091 setFocusedWindow(mFocusedWindow);
5092 mFocusedWindow->consumeFocusEvent(true);
5093 // We do not call "setFocusedApplication" here, even though the newly focused window belongs
5094 // to another application. This could be a bug / behaviour in the policy.
5095
5096 mUnfocusedWindow->consumeMotionDown();
5097
5098 ASSERT_TRUE(mDispatcher->waitForIdle());
5099 // Should not ANR because we actually have a focused window. It was just added too slowly.
5100 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled());
5101}
5102
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05005103// These tests ensure we cannot send touch events to a window that's positioned behind a window
5104// that has feature NO_INPUT_CHANNEL.
5105// Layout:
5106// Top (closest to user)
5107// mNoInputWindow (above all windows)
5108// mBottomWindow
5109// Bottom (furthest from user)
5110class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest {
5111 virtual void SetUp() override {
5112 InputDispatcherTest::SetUp();
5113
5114 mApplication = std::make_shared<FakeApplicationHandle>();
5115 mNoInputWindow = new FakeWindowHandle(mApplication, mDispatcher,
5116 "Window without input channel", ADISPLAY_ID_DEFAULT,
5117 std::make_optional<sp<IBinder>>(nullptr) /*token*/);
5118
chaviw3277faf2021-05-19 16:45:23 -05005119 mNoInputWindow->setInputFeatures(WindowInfo::Feature::NO_INPUT_CHANNEL);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05005120 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
5121 // It's perfectly valid for this window to not have an associated input channel
5122
5123 mBottomWindow = new FakeWindowHandle(mApplication, mDispatcher, "Bottom window",
5124 ADISPLAY_ID_DEFAULT);
5125 mBottomWindow->setFrame(Rect(0, 0, 100, 100));
5126
5127 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
5128 }
5129
5130protected:
5131 std::shared_ptr<FakeApplicationHandle> mApplication;
5132 sp<FakeWindowHandle> mNoInputWindow;
5133 sp<FakeWindowHandle> mBottomWindow;
5134};
5135
5136TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) {
5137 PointF touchedPoint = {10, 10};
5138
5139 NotifyMotionArgs motionArgs =
5140 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5141 ADISPLAY_ID_DEFAULT, {touchedPoint});
5142 mDispatcher->notifyMotion(&motionArgs);
5143
5144 mNoInputWindow->assertNoEvents();
5145 // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have
5146 // an input channel, it is not marked as FLAG_NOT_TOUCHABLE,
5147 // and therefore should prevent mBottomWindow from receiving touches
5148 mBottomWindow->assertNoEvents();
5149}
5150
5151/**
5152 * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel,
5153 * ensure that this window does not receive any touches, and blocks touches to windows underneath.
5154 */
5155TEST_F(InputDispatcherMultiWindowOcclusionTests,
5156 NoInputChannelFeature_DropsTouchesWithValidChannel) {
5157 mNoInputWindow = new FakeWindowHandle(mApplication, mDispatcher,
5158 "Window with input channel and NO_INPUT_CHANNEL",
5159 ADISPLAY_ID_DEFAULT);
5160
chaviw3277faf2021-05-19 16:45:23 -05005161 mNoInputWindow->setInputFeatures(WindowInfo::Feature::NO_INPUT_CHANNEL);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05005162 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
5163 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
5164
5165 PointF touchedPoint = {10, 10};
5166
5167 NotifyMotionArgs motionArgs =
5168 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5169 ADISPLAY_ID_DEFAULT, {touchedPoint});
5170 mDispatcher->notifyMotion(&motionArgs);
5171
5172 mNoInputWindow->assertNoEvents();
5173 mBottomWindow->assertNoEvents();
5174}
5175
Vishnu Nair958da932020-08-21 17:12:37 -07005176class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest {
5177protected:
5178 std::shared_ptr<FakeApplicationHandle> mApp;
5179 sp<FakeWindowHandle> mWindow;
5180 sp<FakeWindowHandle> mMirror;
5181
5182 virtual void SetUp() override {
5183 InputDispatcherTest::SetUp();
5184 mApp = std::make_shared<FakeApplicationHandle>();
5185 mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
5186 mMirror = new FakeWindowHandle(mApp, mDispatcher, "TestWindowMirror", ADISPLAY_ID_DEFAULT,
5187 mWindow->getToken());
5188 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
5189 mWindow->setFocusable(true);
5190 mMirror->setFocusable(true);
5191 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
5192 }
5193};
5194
5195TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) {
5196 // Request focus on a mirrored window
5197 setFocusedWindow(mMirror);
5198
5199 // window gets focused
5200 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005201 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5202 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005203 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
5204}
5205
5206// A focused & mirrored window remains focused only if the window and its mirror are both
5207// focusable.
5208TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) {
5209 setFocusedWindow(mMirror);
5210
5211 // window gets focused
5212 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005213 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5214 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005215 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005216 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
5217 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005218 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
5219
5220 mMirror->setFocusable(false);
5221 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
5222
5223 // window loses focus since one of the windows associated with the token in not focusable
5224 mWindow->consumeFocusEvent(false);
5225
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005226 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
5227 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07005228 mWindow->assertNoEvents();
5229}
5230
5231// A focused & mirrored window remains focused until the window and its mirror both become
5232// invisible.
5233TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) {
5234 setFocusedWindow(mMirror);
5235
5236 // window gets focused
5237 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005238 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5239 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005240 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005241 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
5242 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005243 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
5244
5245 mMirror->setVisible(false);
5246 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
5247
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005248 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5249 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005250 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005251 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
5252 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005253 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
5254
5255 mWindow->setVisible(false);
5256 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
5257
5258 // window loses focus only after all windows associated with the token become invisible.
5259 mWindow->consumeFocusEvent(false);
5260
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005261 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
5262 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07005263 mWindow->assertNoEvents();
5264}
5265
5266// A focused & mirrored window remains focused until both windows are removed.
5267TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) {
5268 setFocusedWindow(mMirror);
5269
5270 // window gets focused
5271 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005272 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5273 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005274 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005275 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
5276 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005277 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
5278
5279 // single window is removed but the window token remains focused
5280 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}});
5281
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005282 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5283 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005284 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005285 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
5286 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005287 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
5288
5289 // Both windows are removed
5290 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
5291 mWindow->consumeFocusEvent(false);
5292
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005293 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
5294 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07005295 mWindow->assertNoEvents();
5296}
5297
5298// Focus request can be pending until one window becomes visible.
5299TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) {
5300 // Request focus on an invisible mirror.
5301 mWindow->setVisible(false);
5302 mMirror->setVisible(false);
5303 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
5304 setFocusedWindow(mMirror);
5305
5306 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005307 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Vishnu Nair958da932020-08-21 17:12:37 -07005308 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005309 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07005310
5311 mMirror->setVisible(true);
5312 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
5313
5314 // window gets focused
5315 mWindow->consumeFocusEvent(true);
5316 // window gets the pending key event
5317 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5318}
Prabir Pradhan99987712020-11-10 18:43:05 -08005319
5320class InputDispatcherPointerCaptureTests : public InputDispatcherTest {
5321protected:
5322 std::shared_ptr<FakeApplicationHandle> mApp;
5323 sp<FakeWindowHandle> mWindow;
5324 sp<FakeWindowHandle> mSecondWindow;
5325
5326 void SetUp() override {
5327 InputDispatcherTest::SetUp();
5328 mApp = std::make_shared<FakeApplicationHandle>();
5329 mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
5330 mWindow->setFocusable(true);
5331 mSecondWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
5332 mSecondWindow->setFocusable(true);
5333
5334 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
5335 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}});
5336
5337 setFocusedWindow(mWindow);
5338 mWindow->consumeFocusEvent(true);
5339 }
5340
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00005341 void notifyPointerCaptureChanged(const PointerCaptureRequest& request) {
5342 const NotifyPointerCaptureChangedArgs args = generatePointerCaptureChangedArgs(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08005343 mDispatcher->notifyPointerCaptureChanged(&args);
5344 }
5345
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00005346 PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window,
5347 bool enabled) {
Prabir Pradhan99987712020-11-10 18:43:05 -08005348 mDispatcher->requestPointerCapture(window->getToken(), enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00005349 auto request = mFakePolicy->assertSetPointerCaptureCalled(enabled);
5350 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08005351 window->consumeCaptureEvent(enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00005352 return request;
Prabir Pradhan99987712020-11-10 18:43:05 -08005353 }
5354};
5355
5356TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) {
5357 // Ensure that capture cannot be obtained for unfocused windows.
5358 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
5359 mFakePolicy->assertSetPointerCaptureNotCalled();
5360 mSecondWindow->assertNoEvents();
5361
5362 // Ensure that capture can be enabled from the focus window.
5363 requestAndVerifyPointerCapture(mWindow, true);
5364
5365 // Ensure that capture cannot be disabled from a window that does not have capture.
5366 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false);
5367 mFakePolicy->assertSetPointerCaptureNotCalled();
5368
5369 // Ensure that capture can be disabled from the window with capture.
5370 requestAndVerifyPointerCapture(mWindow, false);
5371}
5372
5373TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00005374 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08005375
5376 setFocusedWindow(mSecondWindow);
5377
5378 // Ensure that the capture disabled event was sent first.
5379 mWindow->consumeCaptureEvent(false);
5380 mWindow->consumeFocusEvent(false);
5381 mSecondWindow->consumeFocusEvent(true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00005382 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08005383
5384 // Ensure that additional state changes from InputReader are not sent to the window.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00005385 notifyPointerCaptureChanged({});
5386 notifyPointerCaptureChanged(request);
5387 notifyPointerCaptureChanged({});
Prabir Pradhan99987712020-11-10 18:43:05 -08005388 mWindow->assertNoEvents();
5389 mSecondWindow->assertNoEvents();
5390 mFakePolicy->assertSetPointerCaptureNotCalled();
5391}
5392
5393TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00005394 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08005395
5396 // InputReader unexpectedly disables and enables pointer capture.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00005397 notifyPointerCaptureChanged({});
5398 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08005399
5400 // Ensure that Pointer Capture is disabled.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00005401 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08005402 mWindow->consumeCaptureEvent(false);
5403 mWindow->assertNoEvents();
5404}
5405
Prabir Pradhan167e6d92021-02-04 16:18:17 -08005406TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) {
5407 requestAndVerifyPointerCapture(mWindow, true);
5408
5409 // The first window loses focus.
5410 setFocusedWindow(mSecondWindow);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00005411 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08005412 mWindow->consumeCaptureEvent(false);
5413
5414 // Request Pointer Capture from the second window before the notification from InputReader
5415 // arrives.
5416 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00005417 auto request = mFakePolicy->assertSetPointerCaptureCalled(true);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08005418
5419 // InputReader notifies Pointer Capture was disabled (because of the focus change).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00005420 notifyPointerCaptureChanged({});
Prabir Pradhan167e6d92021-02-04 16:18:17 -08005421
5422 // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00005423 notifyPointerCaptureChanged(request);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08005424
5425 mSecondWindow->consumeFocusEvent(true);
5426 mSecondWindow->consumeCaptureEvent(true);
5427}
5428
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00005429TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) {
5430 // App repeatedly enables and disables capture.
5431 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
5432 auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
5433 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
5434 mFakePolicy->assertSetPointerCaptureCalled(false);
5435 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
5436 auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
5437
5438 // InputReader notifies that PointerCapture has been enabled for the first request. Since the
5439 // first request is now stale, this should do nothing.
5440 notifyPointerCaptureChanged(firstRequest);
5441 mWindow->assertNoEvents();
5442
5443 // InputReader notifies that the second request was enabled.
5444 notifyPointerCaptureChanged(secondRequest);
5445 mWindow->consumeCaptureEvent(true);
5446}
5447
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00005448class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest {
5449protected:
5450 constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8;
Bernardo Rufino7393d172021-02-26 13:56:11 +00005451
5452 constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9;
5453 static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY);
5454
5455 constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7;
5456 static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
5457
5458 // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold
5459 constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5;
5460 static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
5461 static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) <
5462 MAXIMUM_OBSCURING_OPACITY);
5463
Bernardo Rufino6d52e542021-02-15 18:38:10 +00005464 static const int32_t TOUCHED_APP_UID = 10001;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00005465 static const int32_t APP_B_UID = 10002;
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00005466 static const int32_t APP_C_UID = 10003;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00005467
5468 sp<FakeWindowHandle> mTouchWindow;
5469
5470 virtual void SetUp() override {
5471 InputDispatcherTest::SetUp();
Bernardo Rufino6d52e542021-02-15 18:38:10 +00005472 mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched");
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00005473 mDispatcher->setBlockUntrustedTouchesMode(android::os::BlockUntrustedTouchesMode::BLOCK);
5474 mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY);
5475 }
5476
5477 virtual void TearDown() override {
5478 InputDispatcherTest::TearDown();
5479 mTouchWindow.clear();
5480 }
5481
chaviw3277faf2021-05-19 16:45:23 -05005482 sp<FakeWindowHandle> getOccludingWindow(int32_t uid, std::string name, TouchOcclusionMode mode,
5483 float alpha = 1.0f) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00005484 sp<FakeWindowHandle> window = getWindow(uid, name);
chaviw3277faf2021-05-19 16:45:23 -05005485 window->setFlags(WindowInfo::Flag::NOT_TOUCHABLE);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00005486 window->setTouchOcclusionMode(mode);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00005487 window->setAlpha(alpha);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00005488 return window;
5489 }
5490
5491 sp<FakeWindowHandle> getWindow(int32_t uid, std::string name) {
5492 std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>();
5493 sp<FakeWindowHandle> window =
5494 new FakeWindowHandle(app, mDispatcher, name, ADISPLAY_ID_DEFAULT);
5495 // Generate an arbitrary PID based on the UID
5496 window->setOwnerInfo(1777 + (uid % 10000), uid);
5497 return window;
5498 }
5499
5500 void touch(const std::vector<PointF>& points = {PointF{100, 200}}) {
5501 NotifyMotionArgs args =
5502 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5503 ADISPLAY_ID_DEFAULT, points);
5504 mDispatcher->notifyMotion(&args);
5505 }
5506};
5507
5508TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00005509 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00005510 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00005511 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00005512
5513 touch();
5514
5515 mTouchWindow->assertNoEvents();
5516}
5517
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00005518TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufino7393d172021-02-26 13:56:11 +00005519 WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) {
5520 const sp<FakeWindowHandle>& w =
5521 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f);
5522 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
5523
5524 touch();
5525
5526 mTouchWindow->assertNoEvents();
5527}
5528
5529TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00005530 WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) {
5531 const sp<FakeWindowHandle>& w =
5532 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
5533 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
5534
5535 touch();
5536
5537 w->assertNoEvents();
5538}
5539
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00005540TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00005541 const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW);
5542 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00005543
5544 touch();
5545
5546 mTouchWindow->consumeAnyMotionDown();
5547}
5548
5549TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00005550 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00005551 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00005552 w->setFrame(Rect(0, 0, 50, 50));
5553 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00005554
5555 touch({PointF{100, 100}});
5556
5557 mTouchWindow->consumeAnyMotionDown();
5558}
5559
5560TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00005561 const sp<FakeWindowHandle>& w =
Bernardo Rufino6d52e542021-02-15 18:38:10 +00005562 getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00005563 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
5564
5565 touch();
5566
5567 mTouchWindow->consumeAnyMotionDown();
5568}
5569
5570TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) {
5571 const sp<FakeWindowHandle>& w =
5572 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
5573 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00005574
5575 touch();
5576
5577 mTouchWindow->consumeAnyMotionDown();
5578}
5579
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00005580TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) {
5581 const sp<FakeWindowHandle>& w =
5582 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
5583 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
5584
5585 touch();
5586
5587 w->assertNoEvents();
5588}
5589
5590/**
5591 * This is important to make sure apps can't indirectly learn the position of touches (outside vs
5592 * inside) while letting them pass-through. Note that even though touch passes through the occluding
5593 * window, the occluding window will still receive ACTION_OUTSIDE event.
5594 */
5595TEST_F(InputDispatcherUntrustedTouchesTest,
5596 WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) {
5597 const sp<FakeWindowHandle>& w =
5598 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
chaviw3277faf2021-05-19 16:45:23 -05005599 w->addFlags(WindowInfo::Flag::WATCH_OUTSIDE_TOUCH);
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00005600 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
5601
5602 touch();
5603
5604 w->consumeMotionOutside();
5605}
5606
5607TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) {
5608 const sp<FakeWindowHandle>& w =
5609 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
chaviw3277faf2021-05-19 16:45:23 -05005610 w->addFlags(WindowInfo::Flag::WATCH_OUTSIDE_TOUCH);
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00005611 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
5612
5613 touch();
5614
5615 InputEvent* event = w->consume();
5616 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType());
5617 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
5618 EXPECT_EQ(0.0f, motionEvent.getRawPointerCoords(0)->getX());
5619 EXPECT_EQ(0.0f, motionEvent.getRawPointerCoords(0)->getY());
5620}
5621
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00005622TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00005623 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00005624 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
5625 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00005626 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
5627
5628 touch();
5629
5630 mTouchWindow->consumeAnyMotionDown();
5631}
5632
5633TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) {
5634 const sp<FakeWindowHandle>& w =
5635 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
5636 MAXIMUM_OBSCURING_OPACITY);
5637 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00005638
5639 touch();
5640
5641 mTouchWindow->consumeAnyMotionDown();
5642}
5643
5644TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00005645 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00005646 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
5647 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00005648 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
5649
5650 touch();
5651
5652 mTouchWindow->assertNoEvents();
5653}
5654
5655TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) {
5656 // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91
5657 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00005658 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
5659 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00005660 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00005661 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
5662 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00005663 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
5664
5665 touch();
5666
5667 mTouchWindow->assertNoEvents();
5668}
5669
5670TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) {
5671 // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75
5672 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00005673 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
5674 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00005675 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00005676 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
5677 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00005678 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
5679
5680 touch();
5681
5682 mTouchWindow->consumeAnyMotionDown();
5683}
5684
5685TEST_F(InputDispatcherUntrustedTouchesTest,
5686 WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) {
5687 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00005688 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
5689 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00005690 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00005691 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
5692 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00005693 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
5694
5695 touch();
5696
5697 mTouchWindow->consumeAnyMotionDown();
5698}
5699
5700TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) {
5701 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00005702 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
5703 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00005704 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00005705 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
5706 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00005707 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00005708
5709 touch();
5710
5711 mTouchWindow->assertNoEvents();
5712}
5713
Bernardo Rufino6d52e542021-02-15 18:38:10 +00005714TEST_F(InputDispatcherUntrustedTouchesTest,
5715 WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) {
5716 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00005717 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
5718 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00005719 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00005720 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
5721 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00005722 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
5723
5724 touch();
5725
5726 mTouchWindow->assertNoEvents();
5727}
5728
5729TEST_F(InputDispatcherUntrustedTouchesTest,
5730 WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) {
5731 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00005732 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
5733 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00005734 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00005735 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
5736 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00005737 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
5738
5739 touch();
5740
5741 mTouchWindow->consumeAnyMotionDown();
5742}
5743
5744TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) {
5745 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00005746 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
5747 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00005748 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
5749
5750 touch();
5751
5752 mTouchWindow->consumeAnyMotionDown();
5753}
5754
5755TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) {
5756 const sp<FakeWindowHandle>& w =
5757 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED);
5758 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
5759
5760 touch();
5761
5762 mTouchWindow->consumeAnyMotionDown();
5763}
5764
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00005765TEST_F(InputDispatcherUntrustedTouchesTest,
5766 OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) {
5767 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
5768 const sp<FakeWindowHandle>& w =
5769 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f);
5770 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
5771
5772 touch();
5773
5774 mTouchWindow->assertNoEvents();
5775}
5776
5777TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) {
5778 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
5779 const sp<FakeWindowHandle>& w =
5780 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f);
5781 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
5782
5783 touch();
5784
5785 mTouchWindow->consumeAnyMotionDown();
5786}
5787
5788TEST_F(InputDispatcherUntrustedTouchesTest,
5789 OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) {
5790 mDispatcher->setMaximumObscuringOpacityForTouch(1.0f);
5791 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00005792 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
5793 OPACITY_ABOVE_THRESHOLD);
5794 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
5795
5796 touch();
5797
5798 mTouchWindow->consumeAnyMotionDown();
5799}
5800
5801TEST_F(InputDispatcherUntrustedTouchesTest,
5802 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) {
5803 const sp<FakeWindowHandle>& w1 =
5804 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
5805 OPACITY_BELOW_THRESHOLD);
5806 const sp<FakeWindowHandle>& w2 =
5807 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
5808 OPACITY_BELOW_THRESHOLD);
5809 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
5810
5811 touch();
5812
5813 mTouchWindow->assertNoEvents();
5814}
5815
5816/**
5817 * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the
5818 * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold
5819 * (which alone would result in allowing touches) does not affect the blocking behavior.
5820 */
5821TEST_F(InputDispatcherUntrustedTouchesTest,
5822 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) {
5823 const sp<FakeWindowHandle>& wB =
5824 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
5825 OPACITY_BELOW_THRESHOLD);
5826 const sp<FakeWindowHandle>& wC =
5827 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
5828 OPACITY_BELOW_THRESHOLD);
5829 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
5830
5831 touch();
5832
5833 mTouchWindow->assertNoEvents();
5834}
5835
5836/**
5837 * This test is testing that a window from a different UID but with same application token doesn't
5838 * block the touch. Apps can share the application token for close UI collaboration for example.
5839 */
5840TEST_F(InputDispatcherUntrustedTouchesTest,
5841 WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) {
5842 const sp<FakeWindowHandle>& w =
5843 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
5844 w->setApplicationToken(mTouchWindow->getApplicationToken());
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00005845 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
5846
5847 touch();
5848
5849 mTouchWindow->consumeAnyMotionDown();
5850}
5851
arthurhungb89ccb02020-12-30 16:19:01 +08005852class InputDispatcherDragTests : public InputDispatcherTest {
5853protected:
5854 std::shared_ptr<FakeApplicationHandle> mApp;
5855 sp<FakeWindowHandle> mWindow;
5856 sp<FakeWindowHandle> mSecondWindow;
5857 sp<FakeWindowHandle> mDragWindow;
5858
5859 void SetUp() override {
5860 InputDispatcherTest::SetUp();
5861 mApp = std::make_shared<FakeApplicationHandle>();
5862 mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
5863 mWindow->setFrame(Rect(0, 0, 100, 100));
chaviw3277faf2021-05-19 16:45:23 -05005864 mWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
arthurhungb89ccb02020-12-30 16:19:01 +08005865
5866 mSecondWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
5867 mSecondWindow->setFrame(Rect(100, 0, 200, 100));
chaviw3277faf2021-05-19 16:45:23 -05005868 mSecondWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
arthurhungb89ccb02020-12-30 16:19:01 +08005869
5870 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
5871 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}});
5872 }
5873
5874 // Start performing drag, we will create a drag window and transfer touch to it.
5875 void performDrag() {
5876 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5877 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5878 {50, 50}))
5879 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5880
5881 // Window should receive motion event.
5882 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5883
5884 // The drag window covers the entire display
5885 mDragWindow = new FakeWindowHandle(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT);
5886 mDispatcher->setInputWindows(
5887 {{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}});
5888
5889 // Transfer touch focus to the drag window
5890 mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(),
5891 true /* isDragDrop */);
5892 mWindow->consumeMotionCancel();
5893 mDragWindow->consumeMotionDown();
5894 }
arthurhung6d4bed92021-03-17 11:59:33 +08005895
5896 // Start performing drag, we will create a drag window and transfer touch to it.
5897 void performStylusDrag() {
5898 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5899 injectMotionEvent(mDispatcher,
5900 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
5901 AINPUT_SOURCE_STYLUS)
5902 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
5903 .pointer(PointerBuilder(0,
5904 AMOTION_EVENT_TOOL_TYPE_STYLUS)
5905 .x(50)
5906 .y(50))
5907 .build()));
5908 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5909
5910 // The drag window covers the entire display
5911 mDragWindow = new FakeWindowHandle(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT);
5912 mDispatcher->setInputWindows(
5913 {{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}});
5914
5915 // Transfer touch focus to the drag window
5916 mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(),
5917 true /* isDragDrop */);
5918 mWindow->consumeMotionCancel();
5919 mDragWindow->consumeMotionDown();
5920 }
arthurhungb89ccb02020-12-30 16:19:01 +08005921};
5922
5923TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) {
5924 performDrag();
5925
5926 // Move on window.
5927 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5928 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5929 ADISPLAY_ID_DEFAULT, {50, 50}))
5930 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5931 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5932 mWindow->consumeDragEvent(false, 50, 50);
5933 mSecondWindow->assertNoEvents();
5934
5935 // Move to another window.
5936 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5937 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5938 ADISPLAY_ID_DEFAULT, {150, 50}))
5939 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5940 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5941 mWindow->consumeDragEvent(true, 150, 50);
5942 mSecondWindow->consumeDragEvent(false, 50, 50);
5943
5944 // Move back to original window.
5945 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5946 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5947 ADISPLAY_ID_DEFAULT, {50, 50}))
5948 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5949 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5950 mWindow->consumeDragEvent(false, 50, 50);
5951 mSecondWindow->consumeDragEvent(true, -50, 50);
5952
5953 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5954 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {50, 50}))
5955 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5956 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
5957 mWindow->assertNoEvents();
5958 mSecondWindow->assertNoEvents();
5959}
5960
arthurhungf452d0b2021-01-06 00:19:52 +08005961TEST_F(InputDispatcherDragTests, DragAndDrop) {
5962 performDrag();
5963
5964 // Move on window.
5965 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5966 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5967 ADISPLAY_ID_DEFAULT, {50, 50}))
5968 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5969 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5970 mWindow->consumeDragEvent(false, 50, 50);
5971 mSecondWindow->assertNoEvents();
5972
5973 // Move to another window.
5974 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5975 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5976 ADISPLAY_ID_DEFAULT, {150, 50}))
5977 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5978 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5979 mWindow->consumeDragEvent(true, 150, 50);
5980 mSecondWindow->consumeDragEvent(false, 50, 50);
5981
5982 // drop to another window.
5983 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5984 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5985 {150, 50}))
5986 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5987 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
5988 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
5989 mWindow->assertNoEvents();
5990 mSecondWindow->assertNoEvents();
5991}
5992
arthurhung6d4bed92021-03-17 11:59:33 +08005993TEST_F(InputDispatcherDragTests, StylusDragAndDrop) {
5994 performStylusDrag();
5995
5996 // Move on window and keep button pressed.
5997 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5998 injectMotionEvent(mDispatcher,
5999 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
6000 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
6001 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS)
6002 .x(50)
6003 .y(50))
6004 .build()))
6005 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6006 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
6007 mWindow->consumeDragEvent(false, 50, 50);
6008 mSecondWindow->assertNoEvents();
6009
6010 // Move to another window and release button, expect to drop item.
6011 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6012 injectMotionEvent(mDispatcher,
6013 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
6014 .buttonState(0)
6015 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS)
6016 .x(150)
6017 .y(50))
6018 .build()))
6019 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6020 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
6021 mWindow->assertNoEvents();
6022 mSecondWindow->assertNoEvents();
6023 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
6024
6025 // nothing to the window.
6026 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6027 injectMotionEvent(mDispatcher,
6028 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS)
6029 .buttonState(0)
6030 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS)
6031 .x(150)
6032 .y(50))
6033 .build()))
6034 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6035 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
6036 mWindow->assertNoEvents();
6037 mSecondWindow->assertNoEvents();
6038}
6039
Arthur Hung6d0571e2021-04-09 20:18:16 +08006040TEST_F(InputDispatcherDragTests, DragAndDrop_InvalidWindow) {
6041 performDrag();
6042
6043 // Set second window invisible.
6044 mSecondWindow->setVisible(false);
6045 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}});
6046
6047 // Move on window.
6048 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6049 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6050 ADISPLAY_ID_DEFAULT, {50, 50}))
6051 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6052 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
6053 mWindow->consumeDragEvent(false, 50, 50);
6054 mSecondWindow->assertNoEvents();
6055
6056 // Move to another window.
6057 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6058 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6059 ADISPLAY_ID_DEFAULT, {150, 50}))
6060 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6061 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
6062 mWindow->consumeDragEvent(true, 150, 50);
6063 mSecondWindow->assertNoEvents();
6064
6065 // drop to another window.
6066 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6067 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6068 {150, 50}))
6069 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6070 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
6071 mFakePolicy->assertDropTargetEquals(nullptr);
6072 mWindow->assertNoEvents();
6073 mSecondWindow->assertNoEvents();
6074}
6075
Vishnu Nair062a8672021-09-03 16:07:44 -07006076class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {};
6077
6078TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) {
6079 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6080 sp<FakeWindowHandle> window =
6081 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
6082 window->setInputFeatures(WindowInfo::Feature::DROP_INPUT);
6083 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
6084 window->setFocusable(true);
6085 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
6086 setFocusedWindow(window);
6087 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
6088
6089 // With the flag set, window should not get any input
6090 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
6091 mDispatcher->notifyKey(&keyArgs);
6092 window->assertNoEvents();
6093
6094 NotifyMotionArgs motionArgs =
6095 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6096 ADISPLAY_ID_DEFAULT);
6097 mDispatcher->notifyMotion(&motionArgs);
6098 window->assertNoEvents();
6099
6100 // With the flag cleared, the window should get input
6101 window->setInputFeatures({});
6102 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
6103
6104 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
6105 mDispatcher->notifyKey(&keyArgs);
6106 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
6107
6108 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6109 ADISPLAY_ID_DEFAULT);
6110 mDispatcher->notifyMotion(&motionArgs);
6111 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6112 window->assertNoEvents();
6113}
6114
6115TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) {
6116 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
6117 std::make_shared<FakeApplicationHandle>();
6118 sp<FakeWindowHandle> obscuringWindow =
6119 new FakeWindowHandle(obscuringApplication, mDispatcher, "obscuringWindow",
6120 ADISPLAY_ID_DEFAULT);
6121 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
6122 obscuringWindow->setOwnerInfo(111, 111);
6123 obscuringWindow->setFlags(WindowInfo::Flag::NOT_TOUCHABLE);
6124 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6125 sp<FakeWindowHandle> window =
6126 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
6127 window->setInputFeatures(WindowInfo::Feature::DROP_INPUT_IF_OBSCURED);
6128 window->setOwnerInfo(222, 222);
6129 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
6130 window->setFocusable(true);
6131 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
6132 setFocusedWindow(window);
6133 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
6134
6135 // With the flag set, window should not get any input
6136 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
6137 mDispatcher->notifyKey(&keyArgs);
6138 window->assertNoEvents();
6139
6140 NotifyMotionArgs motionArgs =
6141 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6142 ADISPLAY_ID_DEFAULT);
6143 mDispatcher->notifyMotion(&motionArgs);
6144 window->assertNoEvents();
6145
6146 // With the flag cleared, the window should get input
6147 window->setInputFeatures({});
6148 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
6149
6150 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
6151 mDispatcher->notifyKey(&keyArgs);
6152 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
6153
6154 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6155 ADISPLAY_ID_DEFAULT);
6156 mDispatcher->notifyMotion(&motionArgs);
6157 window->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
6158 window->assertNoEvents();
6159}
6160
6161TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) {
6162 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
6163 std::make_shared<FakeApplicationHandle>();
6164 sp<FakeWindowHandle> obscuringWindow =
6165 new FakeWindowHandle(obscuringApplication, mDispatcher, "obscuringWindow",
6166 ADISPLAY_ID_DEFAULT);
6167 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
6168 obscuringWindow->setOwnerInfo(111, 111);
6169 obscuringWindow->setFlags(WindowInfo::Flag::NOT_TOUCHABLE);
6170 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6171 sp<FakeWindowHandle> window =
6172 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
6173 window->setInputFeatures(WindowInfo::Feature::DROP_INPUT_IF_OBSCURED);
6174 window->setOwnerInfo(222, 222);
6175 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
6176 window->setFocusable(true);
6177 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
6178 setFocusedWindow(window);
6179 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
6180
6181 // With the flag set, window should not get any input
6182 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
6183 mDispatcher->notifyKey(&keyArgs);
6184 window->assertNoEvents();
6185
6186 NotifyMotionArgs motionArgs =
6187 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6188 ADISPLAY_ID_DEFAULT);
6189 mDispatcher->notifyMotion(&motionArgs);
6190 window->assertNoEvents();
6191
6192 // When the window is no longer obscured because it went on top, it should get input
6193 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, obscuringWindow}}});
6194
6195 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
6196 mDispatcher->notifyKey(&keyArgs);
6197 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
6198
6199 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6200 ADISPLAY_ID_DEFAULT);
6201 mDispatcher->notifyMotion(&motionArgs);
6202 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6203 window->assertNoEvents();
6204}
6205
Antonio Kantekf16f2832021-09-28 04:39:20 +00006206class InputDispatcherTouchModeChangedTests : public InputDispatcherTest {
6207protected:
6208 std::shared_ptr<FakeApplicationHandle> mApp;
6209 sp<FakeWindowHandle> mWindow;
6210 sp<FakeWindowHandle> mSecondWindow;
6211
6212 void SetUp() override {
6213 InputDispatcherTest::SetUp();
6214
6215 mApp = std::make_shared<FakeApplicationHandle>();
6216 mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
6217 mWindow->setFocusable(true);
6218 mSecondWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
6219 mSecondWindow->setFocusable(true);
6220
6221 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
6222 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}});
6223
6224 setFocusedWindow(mWindow);
6225 mWindow->consumeFocusEvent(true);
6226 }
6227
6228 void changeAndVerifyTouchMode(bool inTouchMode) {
6229 mDispatcher->setInTouchMode(inTouchMode);
6230 mWindow->consumeTouchModeEvent(inTouchMode);
6231 mSecondWindow->consumeTouchModeEvent(inTouchMode);
6232 }
6233};
6234
6235TEST_F(InputDispatcherTouchModeChangedTests, ChangeTouchModeOnFocusedWindow) {
6236 changeAndVerifyTouchMode(!InputDispatcher::kDefaultInTouchMode);
6237}
6238
6239TEST_F(InputDispatcherTouchModeChangedTests, EventIsNotGeneratedIfNotChangingTouchMode) {
6240 mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode);
6241 mWindow->assertNoEvents();
6242 mSecondWindow->assertNoEvents();
6243}
6244
Prabir Pradhan07e05b62021-11-19 03:57:24 -08006245class InputDispatcherSpyWindowTest : public InputDispatcherTest {
6246public:
6247 sp<FakeWindowHandle> createSpy(const Flags<WindowInfo::Flag> flags) {
6248 std::shared_ptr<FakeApplicationHandle> application =
6249 std::make_shared<FakeApplicationHandle>();
6250 std::string name = "Fake Spy ";
6251 name += std::to_string(mSpyCount++);
6252 sp<FakeWindowHandle> spy =
6253 new FakeWindowHandle(application, mDispatcher, name.c_str(), ADISPLAY_ID_DEFAULT);
6254 spy->setInputFeatures(WindowInfo::Feature::SPY);
Prabir Pradhan5c85e052021-12-22 02:27:12 -08006255 spy->setTrustedOverlay(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08006256 spy->addFlags(flags);
6257 return spy;
6258 }
6259
6260 sp<FakeWindowHandle> createForeground() {
6261 std::shared_ptr<FakeApplicationHandle> application =
6262 std::make_shared<FakeApplicationHandle>();
6263 sp<FakeWindowHandle> window =
6264 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
6265 window->addFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006266 window->setFocusable(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08006267 return window;
6268 }
6269
6270private:
6271 int mSpyCount{0};
6272};
6273
6274/**
Prabir Pradhan5c85e052021-12-22 02:27:12 -08006275 * Adding a spy window that is not a trusted overlay causes Dispatcher to abort.
6276 */
6277TEST_F(InputDispatcherSpyWindowTest, UntrustedSpy_AbortsDispatcher) {
6278 auto spy = createSpy(WindowInfo::Flag::NOT_TOUCH_MODAL);
6279 spy->setTrustedOverlay(false);
6280 ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}}),
6281 ".* not a trusted overlay");
6282}
6283
6284/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08006285 * Input injection into a display with a spy window but no foreground windows should succeed.
6286 */
6287TEST_F(InputDispatcherSpyWindowTest, NoForegroundWindow) {
6288 auto spy = createSpy(WindowInfo::Flag::NOT_TOUCH_MODAL);
6289 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}});
6290
6291 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6292 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
6293 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6294 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6295}
6296
6297/**
6298 * Verify the order in which different input windows receive events. The touched foreground window
6299 * (if there is one) should always receive the event first. When there are multiple spy windows, the
6300 * spy windows will receive the event according to their Z-order, where the top-most spy window will
6301 * receive events before ones belows it.
6302 *
6303 * Here, we set up a scenario with four windows in the following Z order from the top:
6304 * spy1, spy2, window, spy3.
6305 * We then inject an event and verify that the foreground "window" receives it first, followed by
6306 * "spy1" and "spy2". The "spy3" does not receive the event because it is underneath the foreground
6307 * window.
6308 */
6309TEST_F(InputDispatcherSpyWindowTest, ReceivesInputInOrder) {
6310 auto window = createForeground();
6311 auto spy1 = createSpy(WindowInfo::Flag::NOT_TOUCH_MODAL);
6312 auto spy2 = createSpy(WindowInfo::Flag::NOT_TOUCH_MODAL);
6313 auto spy3 = createSpy(WindowInfo::Flag::NOT_TOUCH_MODAL);
6314 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window, spy3}}});
6315 const std::vector<sp<FakeWindowHandle>> channels{spy1, spy2, window, spy3};
6316 const size_t numChannels = channels.size();
6317
6318 base::unique_fd epollFd(epoll_create1(0 /*flags*/));
6319 if (!epollFd.ok()) {
6320 FAIL() << "Failed to create epoll fd";
6321 }
6322
6323 for (size_t i = 0; i < numChannels; i++) {
6324 struct epoll_event event = {.events = EPOLLIN, .data.u64 = i};
6325 if (epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, channels[i]->getChannelFd(), &event) < 0) {
6326 FAIL() << "Failed to add fd to epoll";
6327 }
6328 }
6329
6330 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6331 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
6332 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6333
6334 std::vector<size_t> eventOrder;
6335 std::vector<struct epoll_event> events(numChannels);
6336 for (;;) {
6337 const int nFds = epoll_wait(epollFd.get(), events.data(), static_cast<int>(numChannels),
6338 (100ms).count());
6339 if (nFds < 0) {
6340 FAIL() << "Failed to call epoll_wait";
6341 }
6342 if (nFds == 0) {
6343 break; // epoll_wait timed out
6344 }
6345 for (int i = 0; i < nFds; i++) {
6346 ASSERT_EQ(EPOLLIN, events[i].events);
6347 eventOrder.push_back(events[i].data.u64);
6348 channels[i]->consumeMotionDown();
6349 }
6350 }
6351
6352 // Verify the order in which the events were received.
6353 EXPECT_EQ(3u, eventOrder.size());
6354 EXPECT_EQ(2u, eventOrder[0]); // index 2: window
6355 EXPECT_EQ(0u, eventOrder[1]); // index 0: spy1
6356 EXPECT_EQ(1u, eventOrder[2]); // index 1: spy2
6357}
6358
6359/**
6360 * A spy window using the NOT_TOUCHABLE flag does not receive events.
6361 */
6362TEST_F(InputDispatcherSpyWindowTest, NotTouchable) {
6363 auto window = createForeground();
6364 auto spy = createSpy(WindowInfo::Flag::NOT_TOUCHABLE);
6365 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
6366
6367 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6368 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
6369 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6370 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6371 spy->assertNoEvents();
6372}
6373
6374/**
6375 * A spy window will only receive gestures that originate within its touchable region. Gestures that
6376 * have their ACTION_DOWN outside of the touchable region of the spy window will not be dispatched
6377 * to the window.
6378 */
6379TEST_F(InputDispatcherSpyWindowTest, TouchableRegion) {
6380 auto window = createForeground();
6381 auto spy = createSpy(WindowInfo::Flag::NOT_TOUCH_MODAL);
6382 spy->setTouchableRegion(Region{{0, 0, 20, 20}});
6383 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
6384
6385 // Inject an event outside the spy window's touchable region.
6386 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6387 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
6388 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6389 window->consumeMotionDown();
6390 spy->assertNoEvents();
6391 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6392 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
6393 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6394 window->consumeMotionUp();
6395 spy->assertNoEvents();
6396
6397 // Inject an event inside the spy window's touchable region.
6398 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6399 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6400 {5, 10}))
6401 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6402 window->consumeMotionDown();
6403 spy->consumeMotionDown();
6404}
6405
6406/**
6407 * A spy window that is a modal window will receive gestures outside of its frame and touchable
6408 * region.
6409 */
6410TEST_F(InputDispatcherSpyWindowTest, ModalWindow) {
6411 auto window = createForeground();
6412 auto spy = createSpy(static_cast<WindowInfo::Flag>(0));
6413 // This spy window does not have the NOT_TOUCH_MODAL flag set.
6414 spy->setFrame(Rect{0, 0, 20, 20});
6415 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
6416
6417 // Inject an event outside the spy window's frame and touchable region.
6418 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6419 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
6420 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6421 window->consumeMotionDown();
6422 spy->consumeMotionDown();
6423}
6424
6425/**
6426 * A spy window can listen for touches outside its touchable region using the WATCH_OUTSIDE_TOUCHES
6427 * flag.
6428 */
6429TEST_F(InputDispatcherSpyWindowTest, WatchOutsideTouches) {
6430 auto window = createForeground();
6431 auto spy = createSpy(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::WATCH_OUTSIDE_TOUCH);
6432 spy->setFrame(Rect{0, 0, 20, 20});
6433 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
6434
6435 // Inject an event outside the spy window's frame and touchable region.
6436 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6437 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
6438 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6439 window->consumeMotionDown();
6440 spy->consumeMotionOutside();
6441}
6442
6443/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08006444 * A spy window can pilfer pointers. When this happens, touch gestures that are currently sent to
6445 * any other windows - including other spy windows - will also be cancelled.
6446 */
6447TEST_F(InputDispatcherSpyWindowTest, PilferPointers) {
6448 auto window = createForeground();
6449 auto spy1 = createSpy(WindowInfo::Flag::NOT_TOUCH_MODAL);
6450 auto spy2 = createSpy(WindowInfo::Flag::NOT_TOUCH_MODAL);
6451 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window}}});
6452
6453 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6454 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
6455 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6456 window->consumeMotionDown();
6457 spy1->consumeMotionDown();
6458 spy2->consumeMotionDown();
6459
6460 // Pilfer pointers from the second spy window.
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006461 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy2->getToken()));
Prabir Pradhan07e05b62021-11-19 03:57:24 -08006462 spy2->assertNoEvents();
6463 spy1->consumeMotionCancel();
6464 window->consumeMotionCancel();
6465
6466 // The rest of the gesture should only be sent to the second spy window.
6467 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6468 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6469 ADISPLAY_ID_DEFAULT))
6470 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6471 spy2->consumeMotionMove();
6472 spy1->assertNoEvents();
6473 window->assertNoEvents();
6474}
6475
6476/**
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006477 * A spy window can pilfer pointers for a gesture even after the foreground window has been removed
6478 * in the middle of the gesture.
6479 */
6480TEST_F(InputDispatcherSpyWindowTest, CanPilferAfterWindowIsRemovedMidStream) {
6481 auto window = createForeground();
6482 auto spy = createSpy(WindowInfo::Flag::NOT_TOUCH_MODAL);
6483 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
6484
6485 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6486 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
6487 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6488 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6489 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6490
6491 window->releaseChannel();
6492
6493 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
6494
6495 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6496 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
6497 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6498 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
6499}
6500
6501/**
6502 * After a spy window pilfers pointers, new pointers that go down should not go to any foreground
6503 * windows.
6504 */
6505TEST_F(InputDispatcherSpyWindowTest, NoSplitAfterPilfer) {
6506 // Create a touch modal spy that spies on the entire display.
6507 auto spy = createSpy(static_cast<WindowInfo::Flag>(0));
6508
6509 // Create a non touch modal window that supports split touch.
6510 auto window = createForeground();
6511 window->setFrame(Rect(0, 0, 100, 100));
6512 window->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH);
6513
6514 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
6515
6516 // First finger down, no window touched.
6517 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6518 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6519 {100, 200}))
6520 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6521 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6522 window->assertNoEvents();
6523
6524 // Spy window pilfer the pointers.
6525 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
6526
6527 // Second finger down on window, the window should not receive touch down.
6528 const MotionEvent secondFingerDownEvent =
6529 MotionEventBuilder(AMOTION_EVENT_ACTION_POINTER_DOWN |
6530 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6531 AINPUT_SOURCE_TOUCHSCREEN)
6532 .displayId(ADISPLAY_ID_DEFAULT)
6533 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
6534 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
6535 .x(100)
6536 .y(200))
6537 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
6538 .build();
6539 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6540 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
6541 InputEventInjectionSync::WAIT_FOR_RESULT))
6542 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6543
6544 window->assertNoEvents();
6545 // Since we no longer allow splitting, the spy will not receive new pointers.
6546 // TODO(b/217376964): Add a way for the pilfering window to receive the rest of the gesture.
6547 spy->consumeMotionMove();
6548}
6549
6550/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08006551 * Even when a spy window spans over multiple foreground windows, the spy should receive all
6552 * pointers that are down within its bounds.
6553 */
6554TEST_F(InputDispatcherSpyWindowTest, ReceivesMultiplePointers) {
6555 auto windowLeft = createForeground();
6556 windowLeft->setFrame({0, 0, 100, 200});
6557 auto windowRight = createForeground();
6558 windowRight->setFrame({100, 0, 200, 200});
6559 auto spy = createSpy(WindowInfo::Flag::NOT_TOUCH_MODAL);
6560 spy->setFrame({0, 0, 200, 200});
6561 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, windowLeft, windowRight}}});
6562
6563 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6564 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6565 {50, 50}))
6566 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6567 windowLeft->consumeMotionDown();
6568 spy->consumeMotionDown();
6569
6570 const MotionEvent secondFingerDownEvent =
6571 MotionEventBuilder(AMOTION_EVENT_ACTION_POINTER_DOWN |
6572 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6573 AINPUT_SOURCE_TOUCHSCREEN)
6574 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
6575 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
6576 .pointer(
6577 PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50))
6578 .build();
6579 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6580 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
6581 InputEventInjectionSync::WAIT_FOR_RESULT))
6582 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6583 windowRight->consumeMotionDown();
6584 spy->consumeMotionPointerDown(1 /*pointerIndex*/);
6585}
6586
6587/**
6588 * When the first pointer lands outside the spy window and the second pointer lands inside it, the
6589 * the spy should receive the second pointer with ACTION_DOWN.
6590 */
6591TEST_F(InputDispatcherSpyWindowTest, ReceivesSecondPointerAsDown) {
6592 auto window = createForeground();
6593 window->setFrame({0, 0, 200, 200});
6594 auto spyRight = createSpy(WindowInfo::Flag::NOT_TOUCH_MODAL);
6595 spyRight->setFrame({100, 0, 200, 200});
6596 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyRight, window}}});
6597
6598 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6599 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6600 {50, 50}))
6601 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6602 window->consumeMotionDown();
6603 spyRight->assertNoEvents();
6604
6605 const MotionEvent secondFingerDownEvent =
6606 MotionEventBuilder(AMOTION_EVENT_ACTION_POINTER_DOWN |
6607 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6608 AINPUT_SOURCE_TOUCHSCREEN)
6609 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
6610 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
6611 .pointer(
6612 PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50))
6613 .build();
6614 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6615 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
6616 InputEventInjectionSync::WAIT_FOR_RESULT))
6617 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6618 window->consumeMotionPointerDown(1 /*pointerIndex*/);
6619 spyRight->consumeMotionDown();
6620}
6621
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006622/**
6623 * The spy window should not be able to affect whether or not touches are split. Only the foreground
6624 * windows should be allowed to control split touch.
6625 */
6626TEST_F(InputDispatcherSpyWindowTest, SplitIfNoForegroundWindowTouched) {
6627 // Create a touch modal spy that spies on the entire display.
6628 // This spy window does not set the SPLIT_TOUCH flag. However, we still expect to split touches
6629 // because a foreground window has not disabled splitting.
6630 auto spy = createSpy(static_cast<WindowInfo::Flag>(0));
6631
6632 // Create a non touch modal window that supports split touch.
6633 auto window = createForeground();
6634 window->setFrame(Rect(0, 0, 100, 100));
6635 window->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH);
6636
6637 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
6638
6639 // First finger down, no window touched.
6640 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6641 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6642 {100, 200}))
6643 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6644 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6645 window->assertNoEvents();
6646
6647 // Second finger down on window, the window should receive touch down.
6648 const MotionEvent secondFingerDownEvent =
6649 MotionEventBuilder(AMOTION_EVENT_ACTION_POINTER_DOWN |
6650 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6651 AINPUT_SOURCE_TOUCHSCREEN)
6652 .displayId(ADISPLAY_ID_DEFAULT)
6653 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
6654 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
6655 .x(100)
6656 .y(200))
6657 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
6658 .build();
6659 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6660 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
6661 InputEventInjectionSync::WAIT_FOR_RESULT))
6662 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6663
6664 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6665 spy->consumeMotionPointerDown(1 /* pointerIndex */);
6666}
6667
6668/**
6669 * A spy window will usually be implemented as an un-focusable window. Verify that these windows
6670 * do not receive key events.
6671 */
6672TEST_F(InputDispatcherSpyWindowTest, UnfocusableSpyDoesNotReceiveKeyEvents) {
6673 auto spy = createSpy(static_cast<WindowInfo::Flag>(0));
6674 spy->setFocusable(false);
6675
6676 auto window = createForeground();
6677 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
6678 setFocusedWindow(window);
6679 window->consumeFocusEvent(true);
6680
6681 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
6682 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
6683 window->consumeKeyDown(ADISPLAY_ID_NONE);
6684
6685 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
6686 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
6687 window->consumeKeyUp(ADISPLAY_ID_NONE);
6688
6689 spy->assertNoEvents();
6690}
6691
Prabir Pradhand65552b2021-10-07 11:23:50 -07006692class InputDispatcherStylusInterceptorTest : public InputDispatcherTest {
6693public:
6694 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupStylusOverlayScenario() {
6695 std::shared_ptr<FakeApplicationHandle> overlayApplication =
6696 std::make_shared<FakeApplicationHandle>();
6697 sp<FakeWindowHandle> overlay =
6698 new FakeWindowHandle(overlayApplication, mDispatcher, "Stylus interceptor window",
6699 ADISPLAY_ID_DEFAULT);
6700 overlay->setFocusable(false);
6701 overlay->setOwnerInfo(111, 111);
6702 overlay->setFlags(WindowInfo::Flag::NOT_TOUCHABLE | WindowInfo::Flag::SPLIT_TOUCH);
6703 overlay->setInputFeatures(WindowInfo::Feature::INTERCEPTS_STYLUS);
6704 overlay->setTrustedOverlay(true);
6705
6706 std::shared_ptr<FakeApplicationHandle> application =
6707 std::make_shared<FakeApplicationHandle>();
6708 sp<FakeWindowHandle> window =
6709 new FakeWindowHandle(application, mDispatcher, "Application window",
6710 ADISPLAY_ID_DEFAULT);
6711 window->setFocusable(true);
6712 window->setOwnerInfo(222, 222);
6713 window->setFlags(WindowInfo::Flag::SPLIT_TOUCH);
6714
6715 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
6716 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
6717 setFocusedWindow(window);
6718 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
6719 return {std::move(overlay), std::move(window)};
6720 }
6721
6722 void sendFingerEvent(int32_t action) {
6723 NotifyMotionArgs motionArgs =
6724 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
6725 ADISPLAY_ID_DEFAULT, {PointF{20, 20}});
6726 mDispatcher->notifyMotion(&motionArgs);
6727 }
6728
6729 void sendStylusEvent(int32_t action) {
6730 NotifyMotionArgs motionArgs =
6731 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
6732 ADISPLAY_ID_DEFAULT, {PointF{30, 40}});
6733 motionArgs.pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS;
6734 mDispatcher->notifyMotion(&motionArgs);
6735 }
6736};
6737
6738TEST_F(InputDispatcherStylusInterceptorTest, UntrustedOverlay_AbortsDispatcher) {
6739 auto [overlay, window] = setupStylusOverlayScenario();
6740 overlay->setTrustedOverlay(false);
6741 // Configuring an untrusted overlay as a stylus interceptor should cause Dispatcher to abort.
6742 ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}),
6743 ".* not a trusted overlay");
6744}
6745
6746TEST_F(InputDispatcherStylusInterceptorTest, ConsmesOnlyStylusEvents) {
6747 auto [overlay, window] = setupStylusOverlayScenario();
6748 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
6749
6750 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
6751 overlay->consumeMotionDown();
6752 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
6753 overlay->consumeMotionUp();
6754
6755 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
6756 window->consumeMotionDown();
6757 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
6758 window->consumeMotionUp();
6759
6760 overlay->assertNoEvents();
6761 window->assertNoEvents();
6762}
6763
6764TEST_F(InputDispatcherStylusInterceptorTest, SpyWindowStylusInterceptor) {
6765 auto [overlay, window] = setupStylusOverlayScenario();
6766 overlay->setInputFeatures(overlay->getInfo()->inputFeatures | WindowInfo::Feature::SPY);
6767 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
6768
6769 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
6770 overlay->consumeMotionDown();
6771 window->consumeMotionDown();
6772 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
6773 overlay->consumeMotionUp();
6774 window->consumeMotionUp();
6775
6776 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
6777 window->consumeMotionDown();
6778 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
6779 window->consumeMotionUp();
6780
6781 overlay->assertNoEvents();
6782 window->assertNoEvents();
6783}
6784
Garfield Tane84e6f92019-08-29 17:28:41 -07006785} // namespace android::inputdispatcher