blob: ee4d3b8e7994df6a20bfb7c788aea7a8804ba8bd [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
Garfield Tan1c7bc862020-01-28 13:24:04 -080019#include <android-base/stringprintf.h>
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -070020#include <android-base/thread_annotations.h>
Robert Carr803535b2018-08-02 16:38:15 -070021#include <binder/Binder.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080022#include <gtest/gtest.h>
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100023#include <input/Input.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080024#include <linux/input.h>
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100025
Garfield Tan1c7bc862020-01-28 13:24:04 -080026#include <cinttypes>
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -070027#include <thread>
Garfield Tan1c7bc862020-01-28 13:24:04 -080028#include <unordered_set>
chaviwd1c23182019-12-20 18:44:56 -080029#include <vector>
Michael Wrightd02c5b62014-02-10 15:10:22 -080030
Garfield Tan1c7bc862020-01-28 13:24:04 -080031using android::base::StringPrintf;
chaviw3277faf2021-05-19 16:45:23 -050032using android::gui::FocusRequest;
33using android::gui::TouchOcclusionMode;
34using android::gui::WindowInfo;
35using android::gui::WindowInfoHandle;
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080036using android::os::InputEventInjectionResult;
37using android::os::InputEventInjectionSync;
Michael Wright44753b12020-07-08 13:48:11 +010038using namespace android::flag_operators;
Garfield Tan1c7bc862020-01-28 13:24:04 -080039
Garfield Tane84e6f92019-08-29 17:28:41 -070040namespace android::inputdispatcher {
Michael Wrightd02c5b62014-02-10 15:10:22 -080041
42// An arbitrary time value.
43static const nsecs_t ARBITRARY_TIME = 1234;
44
45// An arbitrary device id.
46static const int32_t DEVICE_ID = 1;
47
Jeff Brownf086ddb2014-02-11 14:28:48 -080048// An arbitrary display id.
Arthur Hungabbb9d82021-09-01 14:52:30 +000049static constexpr int32_t DISPLAY_ID = ADISPLAY_ID_DEFAULT;
50static constexpr int32_t SECOND_DISPLAY_ID = 1;
Jeff Brownf086ddb2014-02-11 14:28:48 -080051
Michael Wrightd02c5b62014-02-10 15:10:22 -080052// An arbitrary injector pid / uid pair that has permission to inject events.
53static const int32_t INJECTOR_PID = 999;
54static const int32_t INJECTOR_UID = 1001;
55
Siarhei Vishniakou58cfc602020-12-14 23:21:30 +000056// An arbitrary pid of the gesture monitor window
57static constexpr int32_t MONITOR_PID = 2001;
58
chaviwd1c23182019-12-20 18:44:56 -080059struct PointF {
60 float x;
61 float y;
62};
Michael Wrightd02c5b62014-02-10 15:10:22 -080063
Gang Wang342c9272020-01-13 13:15:04 -050064/**
65 * Return a DOWN key event with KEYCODE_A.
66 */
67static KeyEvent getTestKeyEvent() {
68 KeyEvent event;
69
Garfield Tanfbe732e2020-01-24 11:26:14 -080070 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
71 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
72 ARBITRARY_TIME, ARBITRARY_TIME);
Gang Wang342c9272020-01-13 13:15:04 -050073 return event;
74}
75
Michael Wrightd02c5b62014-02-10 15:10:22 -080076// --- FakeInputDispatcherPolicy ---
77
78class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface {
79 InputDispatcherConfiguration mConfig;
80
81protected:
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100082 virtual ~FakeInputDispatcherPolicy() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -080083
84public:
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100085 FakeInputDispatcherPolicy() {}
Jackal Guof9696682018-10-05 12:23:23 +080086
Siarhei Vishniakou8935a802019-11-15 16:41:44 -080087 void assertFilterInputEventWasCalled(const NotifyKeyArgs& args) {
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -080088 assertFilterInputEventWasCalled(AINPUT_EVENT_TYPE_KEY, args.eventTime, args.action,
89 args.displayId);
Jackal Guof9696682018-10-05 12:23:23 +080090 }
91
Siarhei Vishniakou8935a802019-11-15 16:41:44 -080092 void assertFilterInputEventWasCalled(const NotifyMotionArgs& args) {
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -080093 assertFilterInputEventWasCalled(AINPUT_EVENT_TYPE_MOTION, args.eventTime, args.action,
94 args.displayId);
Jackal Guof9696682018-10-05 12:23:23 +080095 }
96
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -070097 void assertFilterInputEventWasNotCalled() {
98 std::scoped_lock lock(mLock);
99 ASSERT_EQ(nullptr, mFilteredEvent);
100 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800101
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800102 void assertNotifyConfigurationChangedWasCalled(nsecs_t when) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700103 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800104 ASSERT_TRUE(mConfigurationChangedTime)
105 << "Timed out waiting for configuration changed call";
106 ASSERT_EQ(*mConfigurationChangedTime, when);
107 mConfigurationChangedTime = std::nullopt;
108 }
109
110 void assertNotifySwitchWasCalled(const NotifySwitchArgs& args) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700111 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800112 ASSERT_TRUE(mLastNotifySwitch);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800113 // We do not check id because it is not exposed to the policy
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800114 EXPECT_EQ(args.eventTime, mLastNotifySwitch->eventTime);
115 EXPECT_EQ(args.policyFlags, mLastNotifySwitch->policyFlags);
116 EXPECT_EQ(args.switchValues, mLastNotifySwitch->switchValues);
117 EXPECT_EQ(args.switchMask, mLastNotifySwitch->switchMask);
118 mLastNotifySwitch = std::nullopt;
119 }
120
chaviwfd6d3512019-03-25 13:23:49 -0700121 void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700122 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800123 ASSERT_EQ(touchedToken, mOnPointerDownToken);
124 mOnPointerDownToken.clear();
125 }
126
127 void assertOnPointerDownWasNotCalled() {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700128 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800129 ASSERT_TRUE(mOnPointerDownToken == nullptr)
130 << "Expected onPointerDownOutsideFocus to not have been called";
chaviwfd6d3512019-03-25 13:23:49 -0700131 }
132
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700133 // This function must be called soon after the expected ANR timer starts,
134 // because we are also checking how much time has passed.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500135 void assertNotifyNoFocusedWindowAnrWasCalled(
Chris Yea209fde2020-07-22 13:54:51 -0700136 std::chrono::nanoseconds timeout,
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500137 const std::shared_ptr<InputApplicationHandle>& expectedApplication) {
138 std::shared_ptr<InputApplicationHandle> application;
139 { // acquire lock
140 std::unique_lock lock(mLock);
141 android::base::ScopedLockAssertion assumeLocked(mLock);
142 ASSERT_NO_FATAL_FAILURE(
143 application = getAnrTokenLockedInterruptible(timeout, mAnrApplications, lock));
144 } // release lock
145 ASSERT_EQ(expectedApplication, application);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700146 }
147
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000148 void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout,
149 const sp<IBinder>& expectedConnectionToken) {
150 sp<IBinder> connectionToken = getUnresponsiveWindowToken(timeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500151 ASSERT_EQ(expectedConnectionToken, connectionToken);
152 }
153
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000154 void assertNotifyWindowResponsiveWasCalled(const sp<IBinder>& expectedConnectionToken) {
155 sp<IBinder> connectionToken = getResponsiveWindowToken();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500156 ASSERT_EQ(expectedConnectionToken, connectionToken);
157 }
158
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000159 void assertNotifyMonitorUnresponsiveWasCalled(std::chrono::nanoseconds timeout) {
160 int32_t pid = getUnresponsiveMonitorPid(timeout);
161 ASSERT_EQ(MONITOR_PID, pid);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500162 }
163
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000164 void assertNotifyMonitorResponsiveWasCalled() {
165 int32_t pid = getResponsiveMonitorPid();
166 ASSERT_EQ(MONITOR_PID, pid);
167 }
168
169 sp<IBinder> getUnresponsiveWindowToken(std::chrono::nanoseconds timeout) {
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500170 std::unique_lock lock(mLock);
171 android::base::ScopedLockAssertion assumeLocked(mLock);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000172 return getAnrTokenLockedInterruptible(timeout, mAnrWindowTokens, lock);
173 }
174
175 sp<IBinder> getResponsiveWindowToken() {
176 std::unique_lock lock(mLock);
177 android::base::ScopedLockAssertion assumeLocked(mLock);
178 return getAnrTokenLockedInterruptible(0s, mResponsiveWindowTokens, lock);
179 }
180
181 int32_t getUnresponsiveMonitorPid(std::chrono::nanoseconds timeout) {
182 std::unique_lock lock(mLock);
183 android::base::ScopedLockAssertion assumeLocked(mLock);
184 return getAnrTokenLockedInterruptible(timeout, mAnrMonitorPids, lock);
185 }
186
187 int32_t getResponsiveMonitorPid() {
188 std::unique_lock lock(mLock);
189 android::base::ScopedLockAssertion assumeLocked(mLock);
190 return getAnrTokenLockedInterruptible(0s, mResponsiveMonitorPids, lock);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500191 }
192
193 // All three ANR-related callbacks behave the same way, so we use this generic function to wait
194 // for a specific container to become non-empty. When the container is non-empty, return the
195 // first entry from the container and erase it.
196 template <class T>
197 T getAnrTokenLockedInterruptible(std::chrono::nanoseconds timeout, std::queue<T>& storage,
198 std::unique_lock<std::mutex>& lock) REQUIRES(mLock) {
199 const std::chrono::time_point start = std::chrono::steady_clock::now();
200 std::chrono::duration timeToWait = timeout + 100ms; // provide some slack
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700201
202 // If there is an ANR, Dispatcher won't be idle because there are still events
203 // in the waitQueue that we need to check on. So we can't wait for dispatcher to be idle
204 // before checking if ANR was called.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500205 // Since dispatcher is not guaranteed to call notifyNoFocusedWindowAnr right away, we need
206 // to provide it some time to act. 100ms seems reasonable.
207 mNotifyAnr.wait_for(lock, timeToWait,
208 [&storage]() REQUIRES(mLock) { return !storage.empty(); });
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700209 const std::chrono::duration waited = std::chrono::steady_clock::now() - start;
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500210 if (storage.empty()) {
211 ADD_FAILURE() << "Did not receive the ANR callback";
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000212 return {};
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700213 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700214 // Ensure that the ANR didn't get raised too early. We can't be too strict here because
215 // the dispatcher started counting before this function was called
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700216 if (std::chrono::abs(timeout - waited) > 100ms) {
217 ADD_FAILURE() << "ANR was raised too early or too late. Expected "
218 << std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count()
219 << "ms, but waited "
220 << std::chrono::duration_cast<std::chrono::milliseconds>(waited).count()
221 << "ms instead";
222 }
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500223 T token = storage.front();
224 storage.pop();
225 return token;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700226 }
227
228 void assertNotifyAnrWasNotCalled() {
229 std::scoped_lock lock(mLock);
230 ASSERT_TRUE(mAnrApplications.empty());
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000231 ASSERT_TRUE(mAnrWindowTokens.empty());
232 ASSERT_TRUE(mAnrMonitorPids.empty());
233 ASSERT_TRUE(mResponsiveWindowTokens.empty())
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500234 << "ANR was not called, but please also consume the 'connection is responsive' "
235 "signal";
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000236 ASSERT_TRUE(mResponsiveMonitorPids.empty())
237 << "Monitor ANR was not called, but please also consume the 'monitor is responsive'"
238 " signal";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700239 }
240
Garfield Tan1c7bc862020-01-28 13:24:04 -0800241 void setKeyRepeatConfiguration(nsecs_t timeout, nsecs_t delay) {
242 mConfig.keyRepeatTimeout = timeout;
243 mConfig.keyRepeatDelay = delay;
244 }
245
Prabir Pradhanac483a62021-08-06 14:01:18 +0000246 PointerCaptureRequest assertSetPointerCaptureCalled(bool enabled) {
Prabir Pradhan99987712020-11-10 18:43:05 -0800247 std::unique_lock lock(mLock);
248 base::ScopedLockAssertion assumeLocked(mLock);
249
250 if (!mPointerCaptureChangedCondition.wait_for(lock, 100ms,
251 [this, enabled]() REQUIRES(mLock) {
Prabir Pradhanac483a62021-08-06 14:01:18 +0000252 return mPointerCaptureRequest->enable ==
Prabir Pradhan99987712020-11-10 18:43:05 -0800253 enabled;
254 })) {
Prabir Pradhanac483a62021-08-06 14:01:18 +0000255 ADD_FAILURE() << "Timed out waiting for setPointerCapture(" << enabled
256 << ") to be called.";
257 return {};
Prabir Pradhan99987712020-11-10 18:43:05 -0800258 }
Prabir Pradhanac483a62021-08-06 14:01:18 +0000259 auto request = *mPointerCaptureRequest;
260 mPointerCaptureRequest.reset();
261 return request;
Prabir Pradhan99987712020-11-10 18:43:05 -0800262 }
263
264 void assertSetPointerCaptureNotCalled() {
265 std::unique_lock lock(mLock);
266 base::ScopedLockAssertion assumeLocked(mLock);
267
268 if (mPointerCaptureChangedCondition.wait_for(lock, 100ms) != std::cv_status::timeout) {
Prabir Pradhanac483a62021-08-06 14:01:18 +0000269 FAIL() << "Expected setPointerCapture(request) to not be called, but was called. "
Prabir Pradhan99987712020-11-10 18:43:05 -0800270 "enabled = "
Prabir Pradhanac483a62021-08-06 14:01:18 +0000271 << std::to_string(mPointerCaptureRequest->enable);
Prabir Pradhan99987712020-11-10 18:43:05 -0800272 }
Prabir Pradhanac483a62021-08-06 14:01:18 +0000273 mPointerCaptureRequest.reset();
Prabir Pradhan99987712020-11-10 18:43:05 -0800274 }
275
arthurhungf452d0b2021-01-06 00:19:52 +0800276 void assertDropTargetEquals(const sp<IBinder>& targetToken) {
277 std::scoped_lock lock(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800278 ASSERT_TRUE(mNotifyDropWindowWasCalled);
arthurhungf452d0b2021-01-06 00:19:52 +0800279 ASSERT_EQ(targetToken, mDropTargetWindowToken);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800280 mNotifyDropWindowWasCalled = false;
arthurhungf452d0b2021-01-06 00:19:52 +0800281 }
282
Michael Wrightd02c5b62014-02-10 15:10:22 -0800283private:
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700284 std::mutex mLock;
285 std::unique_ptr<InputEvent> mFilteredEvent GUARDED_BY(mLock);
286 std::optional<nsecs_t> mConfigurationChangedTime GUARDED_BY(mLock);
287 sp<IBinder> mOnPointerDownToken GUARDED_BY(mLock);
288 std::optional<NotifySwitchArgs> mLastNotifySwitch GUARDED_BY(mLock);
Jackal Guof9696682018-10-05 12:23:23 +0800289
Prabir Pradhan99987712020-11-10 18:43:05 -0800290 std::condition_variable mPointerCaptureChangedCondition;
Prabir Pradhanac483a62021-08-06 14:01:18 +0000291
292 std::optional<PointerCaptureRequest> mPointerCaptureRequest GUARDED_BY(mLock);
Prabir Pradhan99987712020-11-10 18:43:05 -0800293
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700294 // ANR handling
Chris Yea209fde2020-07-22 13:54:51 -0700295 std::queue<std::shared_ptr<InputApplicationHandle>> mAnrApplications GUARDED_BY(mLock);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000296 std::queue<sp<IBinder>> mAnrWindowTokens GUARDED_BY(mLock);
297 std::queue<sp<IBinder>> mResponsiveWindowTokens GUARDED_BY(mLock);
298 std::queue<int32_t> mAnrMonitorPids GUARDED_BY(mLock);
299 std::queue<int32_t> mResponsiveMonitorPids GUARDED_BY(mLock);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700300 std::condition_variable mNotifyAnr;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700301
arthurhungf452d0b2021-01-06 00:19:52 +0800302 sp<IBinder> mDropTargetWindowToken GUARDED_BY(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800303 bool mNotifyDropWindowWasCalled GUARDED_BY(mLock) = false;
arthurhungf452d0b2021-01-06 00:19:52 +0800304
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600305 void notifyConfigurationChanged(nsecs_t when) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700306 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800307 mConfigurationChangedTime = when;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800308 }
309
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000310 void notifyWindowUnresponsive(const sp<IBinder>& connectionToken, const std::string&) override {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700311 std::scoped_lock lock(mLock);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000312 mAnrWindowTokens.push(connectionToken);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700313 mNotifyAnr.notify_all();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500314 }
315
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000316 void notifyMonitorUnresponsive(int32_t pid, const std::string&) override {
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500317 std::scoped_lock lock(mLock);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000318 mAnrMonitorPids.push(pid);
319 mNotifyAnr.notify_all();
320 }
321
322 void notifyWindowResponsive(const sp<IBinder>& connectionToken) override {
323 std::scoped_lock lock(mLock);
324 mResponsiveWindowTokens.push(connectionToken);
325 mNotifyAnr.notify_all();
326 }
327
328 void notifyMonitorResponsive(int32_t pid) override {
329 std::scoped_lock lock(mLock);
330 mResponsiveMonitorPids.push(pid);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500331 mNotifyAnr.notify_all();
332 }
333
334 void notifyNoFocusedWindowAnr(
335 const std::shared_ptr<InputApplicationHandle>& applicationHandle) override {
336 std::scoped_lock lock(mLock);
337 mAnrApplications.push(applicationHandle);
338 mNotifyAnr.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800339 }
340
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600341 void notifyInputChannelBroken(const sp<IBinder>&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800342
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600343 void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {}
Robert Carr740167f2018-10-11 19:03:41 -0700344
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600345 void notifyUntrustedTouch(const std::string& obscuringPackage) override {}
Chris Yef59a2f42020-10-16 12:55:26 -0700346 void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType,
347 InputDeviceSensorAccuracy accuracy, nsecs_t timestamp,
348 const std::vector<float>& values) override {}
349
350 void notifySensorAccuracy(int deviceId, InputDeviceSensorType sensorType,
351 InputDeviceSensorAccuracy accuracy) override {}
Bernardo Rufino2e1f6512020-10-08 13:42:07 +0000352
Chris Yefb552902021-02-03 17:18:37 -0800353 void notifyVibratorState(int32_t deviceId, bool isOn) override {}
354
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600355 void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800356 *outConfig = mConfig;
357 }
358
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600359 bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700360 std::scoped_lock lock(mLock);
Jackal Guof9696682018-10-05 12:23:23 +0800361 switch (inputEvent->getType()) {
362 case AINPUT_EVENT_TYPE_KEY: {
363 const KeyEvent* keyEvent = static_cast<const KeyEvent*>(inputEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800364 mFilteredEvent = std::make_unique<KeyEvent>(*keyEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800365 break;
366 }
367
368 case AINPUT_EVENT_TYPE_MOTION: {
369 const MotionEvent* motionEvent = static_cast<const MotionEvent*>(inputEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800370 mFilteredEvent = std::make_unique<MotionEvent>(*motionEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800371 break;
372 }
373 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800374 return true;
375 }
376
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600377 void interceptKeyBeforeQueueing(const KeyEvent*, uint32_t&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800378
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600379 void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800380
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600381 nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent*, uint32_t) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800382 return 0;
383 }
384
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600385 bool dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent*, uint32_t, KeyEvent*) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800386 return false;
387 }
388
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600389 void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask,
390 uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700391 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800392 /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is
393 * essentially a passthrough for notifySwitch.
394 */
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800395 mLastNotifySwitch = NotifySwitchArgs(1 /*id*/, when, policyFlags, switchValues, switchMask);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800396 }
397
Sean Stoutb4e0a592021-02-23 07:34:53 -0800398 void pokeUserActivity(nsecs_t, int32_t, int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800399
Prabir Pradhan93f342c2021-03-11 15:05:30 -0800400 bool checkInjectEventsPermissionNonReentrant(int32_t pid, int32_t uid) override {
401 return pid == INJECTOR_PID && uid == INJECTOR_UID;
402 }
Jackal Guof9696682018-10-05 12:23:23 +0800403
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600404 void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700405 std::scoped_lock lock(mLock);
chaviwfd6d3512019-03-25 13:23:49 -0700406 mOnPointerDownToken = newToken;
407 }
408
Prabir Pradhanac483a62021-08-06 14:01:18 +0000409 void setPointerCapture(const PointerCaptureRequest& request) override {
Prabir Pradhan99987712020-11-10 18:43:05 -0800410 std::scoped_lock lock(mLock);
Prabir Pradhanac483a62021-08-06 14:01:18 +0000411 mPointerCaptureRequest = {request};
Prabir Pradhan99987712020-11-10 18:43:05 -0800412 mPointerCaptureChangedCondition.notify_all();
413 }
414
arthurhungf452d0b2021-01-06 00:19:52 +0800415 void notifyDropWindow(const sp<IBinder>& token, float x, float y) override {
416 std::scoped_lock lock(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800417 mNotifyDropWindowWasCalled = true;
arthurhungf452d0b2021-01-06 00:19:52 +0800418 mDropTargetWindowToken = token;
419 }
420
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -0800421 void assertFilterInputEventWasCalled(int type, nsecs_t eventTime, int32_t action,
422 int32_t displayId) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700423 std::scoped_lock lock(mLock);
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -0800424 ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called.";
425 ASSERT_EQ(mFilteredEvent->getType(), type);
426
427 if (type == AINPUT_EVENT_TYPE_KEY) {
428 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*mFilteredEvent);
429 EXPECT_EQ(keyEvent.getEventTime(), eventTime);
430 EXPECT_EQ(keyEvent.getAction(), action);
431 EXPECT_EQ(keyEvent.getDisplayId(), displayId);
432 } else if (type == AINPUT_EVENT_TYPE_MOTION) {
433 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*mFilteredEvent);
434 EXPECT_EQ(motionEvent.getEventTime(), eventTime);
435 EXPECT_EQ(motionEvent.getAction(), action);
436 EXPECT_EQ(motionEvent.getDisplayId(), displayId);
437 } else {
438 FAIL() << "Unknown type: " << type;
439 }
440
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800441 mFilteredEvent = nullptr;
Jackal Guof9696682018-10-05 12:23:23 +0800442 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800443};
444
Michael Wrightd02c5b62014-02-10 15:10:22 -0800445// --- InputDispatcherTest ---
446
447class InputDispatcherTest : public testing::Test {
448protected:
449 sp<FakeInputDispatcherPolicy> mFakePolicy;
450 sp<InputDispatcher> mDispatcher;
451
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000452 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800453 mFakePolicy = new FakeInputDispatcherPolicy();
454 mDispatcher = new InputDispatcher(mFakePolicy);
Arthur Hungb92218b2018-08-14 12:00:21 +0800455 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000456 // Start InputDispatcher thread
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700457 ASSERT_EQ(OK, mDispatcher->start());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800458 }
459
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000460 void TearDown() override {
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700461 ASSERT_EQ(OK, mDispatcher->stop());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800462 mFakePolicy.clear();
463 mDispatcher.clear();
464 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700465
466 /**
467 * Used for debugging when writing the test
468 */
469 void dumpDispatcherState() {
470 std::string dump;
471 mDispatcher->dump(dump);
472 std::stringstream ss(dump);
473 std::string to;
474
475 while (std::getline(ss, to, '\n')) {
476 ALOGE("%s", to.c_str());
477 }
478 }
Vishnu Nair958da932020-08-21 17:12:37 -0700479
chaviw3277faf2021-05-19 16:45:23 -0500480 void setFocusedWindow(const sp<WindowInfoHandle>& window,
481 const sp<WindowInfoHandle>& focusedWindow = nullptr) {
Vishnu Nair958da932020-08-21 17:12:37 -0700482 FocusRequest request;
483 request.token = window->getToken();
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +0000484 request.windowName = window->getName();
Vishnu Nair958da932020-08-21 17:12:37 -0700485 if (focusedWindow) {
486 request.focusedToken = focusedWindow->getToken();
487 }
488 request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
489 request.displayId = window->getInfo()->displayId;
490 mDispatcher->setFocusedWindow(request);
491 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800492};
493
Michael Wrightd02c5b62014-02-10 15:10:22 -0800494TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) {
495 KeyEvent event;
496
497 // Rejects undefined key actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800498 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
499 INVALID_HMAC,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600500 /*action*/ -1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME,
501 ARBITRARY_TIME);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800502 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700503 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800504 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800505 << "Should reject key events with undefined action.";
506
507 // Rejects ACTION_MULTIPLE since it is not supported despite being defined in the API.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800508 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
509 INVALID_HMAC, AKEY_EVENT_ACTION_MULTIPLE, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600510 ARBITRARY_TIME, ARBITRARY_TIME);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800511 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700512 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800513 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800514 << "Should reject key events with ACTION_MULTIPLE.";
515}
516
517TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) {
518 MotionEvent event;
519 PointerProperties pointerProperties[MAX_POINTERS + 1];
520 PointerCoords pointerCoords[MAX_POINTERS + 1];
521 for (int i = 0; i <= MAX_POINTERS; i++) {
522 pointerProperties[i].clear();
523 pointerProperties[i].id = i;
524 pointerCoords[i].clear();
525 }
526
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800527 // Some constants commonly used below
528 constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN;
529 constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE;
530 constexpr int32_t metaState = AMETA_NONE;
531 constexpr MotionClassification classification = MotionClassification::NONE;
532
chaviw9eaa22c2020-07-01 16:21:27 -0700533 ui::Transform identityTransform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800534 // Rejects undefined motion actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800535 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
chaviw9eaa22c2020-07-01 16:21:27 -0700536 /*action*/ -1, 0, 0, edgeFlags, metaState, 0, classification,
537 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Evan Rosky09576692021-07-01 12:22:09 -0700538 AMOTION_EVENT_INVALID_CURSOR_POSITION, ui::Transform::ROT_0,
539 INVALID_DISPLAY_SIZE, INVALID_DISPLAY_SIZE, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700540 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800541 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700542 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800543 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800544 << "Should reject motion events with undefined action.";
545
546 // Rejects pointer down with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800547 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700548 AMOTION_EVENT_ACTION_POINTER_DOWN |
549 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700550 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
551 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Evan Rosky09576692021-07-01 12:22:09 -0700552 ui::Transform::ROT_0, INVALID_DISPLAY_SIZE, INVALID_DISPLAY_SIZE,
553 ARBITRARY_TIME, ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500554 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800555 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700556 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800557 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800558 << "Should reject motion events with pointer down index too large.";
559
Garfield Tanfbe732e2020-01-24 11:26:14 -0800560 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700561 AMOTION_EVENT_ACTION_POINTER_DOWN |
562 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700563 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
564 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Evan Rosky09576692021-07-01 12:22:09 -0700565 ui::Transform::ROT_0, INVALID_DISPLAY_SIZE, INVALID_DISPLAY_SIZE,
566 ARBITRARY_TIME, ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500567 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800568 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700569 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800570 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800571 << "Should reject motion events with pointer down index too small.";
572
573 // Rejects pointer up with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800574 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700575 AMOTION_EVENT_ACTION_POINTER_UP |
576 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700577 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
578 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Evan Rosky09576692021-07-01 12:22:09 -0700579 ui::Transform::ROT_0, INVALID_DISPLAY_SIZE, INVALID_DISPLAY_SIZE,
580 ARBITRARY_TIME, ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500581 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800582 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700583 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800584 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800585 << "Should reject motion events with pointer up index too large.";
586
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_UP |
589 (~0U << 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,
Evan Rosky09576692021-07-01 12:22:09 -0700592 ui::Transform::ROT_0, INVALID_DISPLAY_SIZE, INVALID_DISPLAY_SIZE,
593 ARBITRARY_TIME, ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500594 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800595 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700596 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800597 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800598 << "Should reject motion events with pointer up index too small.";
599
600 // Rejects motion events with invalid number of pointers.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800601 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
602 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700603 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Evan Rosky09576692021-07-01 12:22:09 -0700604 AMOTION_EVENT_INVALID_CURSOR_POSITION, ui::Transform::ROT_0,
605 INVALID_DISPLAY_SIZE, INVALID_DISPLAY_SIZE, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700606 /*pointerCount*/ 0, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800607 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700608 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800609 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800610 << "Should reject motion events with 0 pointers.";
611
Garfield Tanfbe732e2020-01-24 11:26:14 -0800612 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
613 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700614 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Evan Rosky09576692021-07-01 12:22:09 -0700615 AMOTION_EVENT_INVALID_CURSOR_POSITION, ui::Transform::ROT_0,
616 INVALID_DISPLAY_SIZE, INVALID_DISPLAY_SIZE, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700617 /*pointerCount*/ MAX_POINTERS + 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800618 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700619 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800620 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800621 << "Should reject motion events with more than MAX_POINTERS pointers.";
622
623 // Rejects motion events with invalid pointer ids.
624 pointerProperties[0].id = -1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800625 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
626 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700627 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Evan Rosky09576692021-07-01 12:22:09 -0700628 AMOTION_EVENT_INVALID_CURSOR_POSITION, ui::Transform::ROT_0,
629 INVALID_DISPLAY_SIZE, INVALID_DISPLAY_SIZE, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700630 /*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 ids less than 0.";
635
636 pointerProperties[0].id = MAX_POINTER_ID + 1;
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,
Evan Rosky09576692021-07-01 12:22:09 -0700640 AMOTION_EVENT_INVALID_CURSOR_POSITION, ui::Transform::ROT_0,
641 INVALID_DISPLAY_SIZE, INVALID_DISPLAY_SIZE, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700642 /*pointerCount*/ 1, 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 pointer ids greater than MAX_POINTER_ID.";
647
648 // Rejects motion events with duplicate pointer ids.
649 pointerProperties[0].id = 1;
650 pointerProperties[1].id = 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800651 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
652 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700653 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Evan Rosky09576692021-07-01 12:22:09 -0700654 AMOTION_EVENT_INVALID_CURSOR_POSITION, ui::Transform::ROT_0,
655 INVALID_DISPLAY_SIZE, INVALID_DISPLAY_SIZE, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700656 /*pointerCount*/ 2, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800657 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700658 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800659 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800660 << "Should reject motion events with duplicate pointer ids.";
661}
662
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800663/* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */
664
665TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) {
666 constexpr nsecs_t eventTime = 20;
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800667 NotifyConfigurationChangedArgs args(10 /*id*/, eventTime);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800668 mDispatcher->notifyConfigurationChanged(&args);
669 ASSERT_TRUE(mDispatcher->waitForIdle());
670
671 mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime);
672}
673
674TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) {
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800675 NotifySwitchArgs args(10 /*id*/, 20 /*eventTime*/, 0 /*policyFlags*/, 1 /*switchValues*/,
676 2 /*switchMask*/);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800677 mDispatcher->notifySwitch(&args);
678
679 // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener
680 args.policyFlags |= POLICY_FLAG_TRUSTED;
681 mFakePolicy->assertNotifySwitchWasCalled(args);
682}
683
Arthur Hungb92218b2018-08-14 12:00:21 +0800684// --- InputDispatcherTest SetInputWindowTest ---
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700685static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms;
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700686static constexpr std::chrono::nanoseconds DISPATCHING_TIMEOUT = 5s;
Arthur Hungb92218b2018-08-14 12:00:21 +0800687
688class FakeApplicationHandle : public InputApplicationHandle {
689public:
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700690 FakeApplicationHandle() {
691 mInfo.name = "Fake Application";
692 mInfo.token = new BBinder();
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500693 mInfo.dispatchingTimeoutMillis =
694 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700695 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800696 virtual ~FakeApplicationHandle() {}
697
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000698 virtual bool updateInfo() override { return true; }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700699
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500700 void setDispatchingTimeout(std::chrono::milliseconds timeout) {
701 mInfo.dispatchingTimeoutMillis = timeout.count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700702 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800703};
704
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800705class FakeInputReceiver {
Arthur Hungb92218b2018-08-14 12:00:21 +0800706public:
Garfield Tan15601662020-09-22 15:32:38 -0700707 explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name)
chaviwd1c23182019-12-20 18:44:56 -0800708 : mName(name) {
Garfield Tan15601662020-09-22 15:32:38 -0700709 mConsumer = std::make_unique<InputConsumer>(std::move(clientChannel));
chaviwd1c23182019-12-20 18:44:56 -0800710 }
711
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800712 InputEvent* consume() {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700713 InputEvent* event;
714 std::optional<uint32_t> consumeSeq = receiveEvent(&event);
715 if (!consumeSeq) {
716 return nullptr;
717 }
718 finishEvent(*consumeSeq);
719 return event;
720 }
721
722 /**
723 * Receive an event without acknowledging it.
724 * Return the sequence number that could later be used to send finished signal.
725 */
726 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Arthur Hungb92218b2018-08-14 12:00:21 +0800727 uint32_t consumeSeq;
728 InputEvent* event;
Arthur Hungb92218b2018-08-14 12:00:21 +0800729
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800730 std::chrono::time_point start = std::chrono::steady_clock::now();
731 status_t status = WOULD_BLOCK;
732 while (status == WOULD_BLOCK) {
chaviw81e2bb92019-12-18 15:03:51 -0800733 status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq,
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800734 &event);
735 std::chrono::duration elapsed = std::chrono::steady_clock::now() - start;
736 if (elapsed > 100ms) {
737 break;
738 }
739 }
740
741 if (status == WOULD_BLOCK) {
742 // Just means there's no event available.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700743 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800744 }
745
746 if (status != OK) {
747 ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK.";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700748 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800749 }
750 if (event == nullptr) {
751 ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700752 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800753 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700754 if (outEvent != nullptr) {
755 *outEvent = event;
756 }
757 return consumeSeq;
758 }
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800759
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700760 /**
761 * To be used together with "receiveEvent" to complete the consumption of an event.
762 */
763 void finishEvent(uint32_t consumeSeq) {
764 const status_t status = mConsumer->sendFinishedSignal(consumeSeq, true);
765 ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK.";
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800766 }
767
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000768 void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) {
769 const status_t status = mConsumer->sendTimeline(inputEventId, timeline);
770 ASSERT_EQ(OK, status);
771 }
772
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000773 void consumeEvent(int32_t expectedEventType, int32_t expectedAction,
774 std::optional<int32_t> expectedDisplayId,
775 std::optional<int32_t> expectedFlags) {
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800776 InputEvent* event = consume();
777
778 ASSERT_NE(nullptr, event) << mName.c_str()
779 << ": consumer should have returned non-NULL event.";
Arthur Hungb92218b2018-08-14 12:00:21 +0800780 ASSERT_EQ(expectedEventType, event->getType())
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700781 << mName.c_str() << " expected " << inputEventTypeToString(expectedEventType)
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800782 << " event, got " << inputEventTypeToString(event->getType()) << " event";
Arthur Hungb92218b2018-08-14 12:00:21 +0800783
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000784 if (expectedDisplayId.has_value()) {
785 EXPECT_EQ(expectedDisplayId, event->getDisplayId());
786 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800787
Tiger Huang8664f8c2018-10-11 19:14:35 +0800788 switch (expectedEventType) {
789 case AINPUT_EVENT_TYPE_KEY: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800790 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event);
791 EXPECT_EQ(expectedAction, keyEvent.getAction());
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000792 if (expectedFlags.has_value()) {
793 EXPECT_EQ(expectedFlags.value(), keyEvent.getFlags());
794 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800795 break;
796 }
797 case AINPUT_EVENT_TYPE_MOTION: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800798 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
799 EXPECT_EQ(expectedAction, motionEvent.getAction());
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000800 if (expectedFlags.has_value()) {
801 EXPECT_EQ(expectedFlags.value(), motionEvent.getFlags());
802 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800803 break;
804 }
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100805 case AINPUT_EVENT_TYPE_FOCUS: {
806 FAIL() << "Use 'consumeFocusEvent' for FOCUS events";
807 }
Prabir Pradhan99987712020-11-10 18:43:05 -0800808 case AINPUT_EVENT_TYPE_CAPTURE: {
809 FAIL() << "Use 'consumeCaptureEvent' for CAPTURE events";
810 }
arthurhungb89ccb02020-12-30 16:19:01 +0800811 case AINPUT_EVENT_TYPE_DRAG: {
812 FAIL() << "Use 'consumeDragEvent' for DRAG events";
813 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800814 default: {
815 FAIL() << mName.c_str() << ": invalid event type: " << expectedEventType;
816 }
817 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800818 }
819
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100820 void consumeFocusEvent(bool hasFocus, bool inTouchMode) {
821 InputEvent* event = consume();
822 ASSERT_NE(nullptr, event) << mName.c_str()
823 << ": consumer should have returned non-NULL event.";
824 ASSERT_EQ(AINPUT_EVENT_TYPE_FOCUS, event->getType())
825 << "Got " << inputEventTypeToString(event->getType())
826 << " event instead of FOCUS event";
827
828 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
829 << mName.c_str() << ": event displayId should always be NONE.";
830
831 FocusEvent* focusEvent = static_cast<FocusEvent*>(event);
832 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
833 EXPECT_EQ(inTouchMode, focusEvent->getInTouchMode());
834 }
835
Prabir Pradhan99987712020-11-10 18:43:05 -0800836 void consumeCaptureEvent(bool hasCapture) {
837 const InputEvent* event = consume();
838 ASSERT_NE(nullptr, event) << mName.c_str()
839 << ": consumer should have returned non-NULL event.";
840 ASSERT_EQ(AINPUT_EVENT_TYPE_CAPTURE, event->getType())
841 << "Got " << inputEventTypeToString(event->getType())
842 << " event instead of CAPTURE event";
843
844 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
845 << mName.c_str() << ": event displayId should always be NONE.";
846
847 const auto& captureEvent = static_cast<const CaptureEvent&>(*event);
848 EXPECT_EQ(hasCapture, captureEvent.getPointerCaptureEnabled());
849 }
850
arthurhungb89ccb02020-12-30 16:19:01 +0800851 void consumeDragEvent(bool isExiting, float x, float y) {
852 const InputEvent* event = consume();
853 ASSERT_NE(nullptr, event) << mName.c_str()
854 << ": consumer should have returned non-NULL event.";
855 ASSERT_EQ(AINPUT_EVENT_TYPE_DRAG, event->getType())
856 << "Got " << inputEventTypeToString(event->getType())
857 << " event instead of DRAG event";
858
859 EXPECT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
860 << mName.c_str() << ": event displayId should always be NONE.";
861
862 const auto& dragEvent = static_cast<const DragEvent&>(*event);
863 EXPECT_EQ(isExiting, dragEvent.isExiting());
864 EXPECT_EQ(x, dragEvent.getX());
865 EXPECT_EQ(y, dragEvent.getY());
866 }
867
chaviwd1c23182019-12-20 18:44:56 -0800868 void assertNoEvents() {
869 InputEvent* event = consume();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700870 if (event == nullptr) {
871 return;
872 }
873 if (event->getType() == AINPUT_EVENT_TYPE_KEY) {
874 KeyEvent& keyEvent = static_cast<KeyEvent&>(*event);
875 ADD_FAILURE() << "Received key event "
876 << KeyEvent::actionToString(keyEvent.getAction());
877 } else if (event->getType() == AINPUT_EVENT_TYPE_MOTION) {
878 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
879 ADD_FAILURE() << "Received motion event "
880 << MotionEvent::actionToString(motionEvent.getAction());
881 } else if (event->getType() == AINPUT_EVENT_TYPE_FOCUS) {
882 FocusEvent& focusEvent = static_cast<FocusEvent&>(*event);
883 ADD_FAILURE() << "Received focus event, hasFocus = "
884 << (focusEvent.getHasFocus() ? "true" : "false");
Prabir Pradhan99987712020-11-10 18:43:05 -0800885 } else if (event->getType() == AINPUT_EVENT_TYPE_CAPTURE) {
886 const auto& captureEvent = static_cast<CaptureEvent&>(*event);
887 ADD_FAILURE() << "Received capture event, pointerCaptureEnabled = "
888 << (captureEvent.getPointerCaptureEnabled() ? "true" : "false");
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700889 }
890 FAIL() << mName.c_str()
891 << ": should not have received any events, so consume() should return NULL";
chaviwd1c23182019-12-20 18:44:56 -0800892 }
893
894 sp<IBinder> getToken() { return mConsumer->getChannel()->getConnectionToken(); }
895
896protected:
897 std::unique_ptr<InputConsumer> mConsumer;
898 PreallocatedInputEventFactory mEventFactory;
899
900 std::string mName;
901};
902
chaviw3277faf2021-05-19 16:45:23 -0500903class FakeWindowHandle : public WindowInfoHandle {
chaviwd1c23182019-12-20 18:44:56 -0800904public:
905 static const int32_t WIDTH = 600;
906 static const int32_t HEIGHT = 800;
chaviwd1c23182019-12-20 18:44:56 -0800907
Chris Yea209fde2020-07-22 13:54:51 -0700908 FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
chaviwd1c23182019-12-20 18:44:56 -0800909 const sp<InputDispatcher>& dispatcher, const std::string name,
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -0500910 int32_t displayId, std::optional<sp<IBinder>> token = std::nullopt)
chaviwd1c23182019-12-20 18:44:56 -0800911 : mName(name) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -0500912 if (token == std::nullopt) {
Garfield Tan15601662020-09-22 15:32:38 -0700913 base::Result<std::unique_ptr<InputChannel>> channel =
914 dispatcher->createInputChannel(name);
915 token = (*channel)->getConnectionToken();
916 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
chaviwd1c23182019-12-20 18:44:56 -0800917 }
918
919 inputApplicationHandle->updateInfo();
920 mInfo.applicationInfo = *inputApplicationHandle->getInfo();
921
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -0500922 mInfo.token = *token;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -0700923 mInfo.id = sId++;
chaviwd1c23182019-12-20 18:44:56 -0800924 mInfo.name = name;
chaviw3277faf2021-05-19 16:45:23 -0500925 mInfo.type = WindowInfo::Type::APPLICATION;
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500926 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000927 mInfo.alpha = 1.0;
chaviwd1c23182019-12-20 18:44:56 -0800928 mInfo.frameLeft = 0;
929 mInfo.frameTop = 0;
930 mInfo.frameRight = WIDTH;
931 mInfo.frameBottom = HEIGHT;
chaviw1ff3d1e2020-07-01 15:53:47 -0700932 mInfo.transform.set(0, 0);
chaviwd1c23182019-12-20 18:44:56 -0800933 mInfo.globalScaleFactor = 1.0;
934 mInfo.touchableRegion.clear();
935 mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT));
936 mInfo.visible = true;
Vishnu Nair47074b82020-08-14 11:54:47 -0700937 mInfo.focusable = false;
chaviwd1c23182019-12-20 18:44:56 -0800938 mInfo.hasWallpaper = false;
939 mInfo.paused = false;
chaviwd1c23182019-12-20 18:44:56 -0800940 mInfo.ownerPid = INJECTOR_PID;
941 mInfo.ownerUid = INJECTOR_UID;
chaviwd1c23182019-12-20 18:44:56 -0800942 mInfo.displayId = displayId;
943 }
944
Arthur Hungabbb9d82021-09-01 14:52:30 +0000945 sp<FakeWindowHandle> clone(
946 const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
947 const sp<InputDispatcher>& dispatcher, int32_t displayId) {
948 sp<FakeWindowHandle> handle =
949 new FakeWindowHandle(inputApplicationHandle, dispatcher, mInfo.name + "(Mirror)",
950 displayId, mInfo.token);
951 return handle;
952 }
953
Vishnu Nair47074b82020-08-14 11:54:47 -0700954 void setFocusable(bool focusable) { mInfo.focusable = focusable; }
chaviwd1c23182019-12-20 18:44:56 -0800955
Vishnu Nair958da932020-08-21 17:12:37 -0700956 void setVisible(bool visible) { mInfo.visible = visible; }
957
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700958 void setDispatchingTimeout(std::chrono::nanoseconds timeout) {
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500959 mInfo.dispatchingTimeout = timeout;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700960 }
961
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700962 void setPaused(bool paused) { mInfo.paused = paused; }
963
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000964 void setAlpha(float alpha) { mInfo.alpha = alpha; }
965
chaviw3277faf2021-05-19 16:45:23 -0500966 void setTouchOcclusionMode(TouchOcclusionMode mode) { mInfo.touchOcclusionMode = mode; }
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000967
Bernardo Rufino7393d172021-02-26 13:56:11 +0000968 void setApplicationToken(sp<IBinder> token) { mInfo.applicationInfo.token = token; }
969
chaviwd1c23182019-12-20 18:44:56 -0800970 void setFrame(const Rect& frame) {
971 mInfo.frameLeft = frame.left;
972 mInfo.frameTop = frame.top;
973 mInfo.frameRight = frame.right;
974 mInfo.frameBottom = frame.bottom;
arthurhungb89ccb02020-12-30 16:19:01 +0800975 mInfo.transform.set(-frame.left, -frame.top);
chaviwd1c23182019-12-20 18:44:56 -0800976 mInfo.touchableRegion.clear();
977 mInfo.addTouchableRegion(frame);
978 }
979
chaviw3277faf2021-05-19 16:45:23 -0500980 void addFlags(Flags<WindowInfo::Flag> flags) { mInfo.flags |= flags; }
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +0000981
chaviw3277faf2021-05-19 16:45:23 -0500982 void setFlags(Flags<WindowInfo::Flag> flags) { mInfo.flags = flags; }
chaviwd1c23182019-12-20 18:44:56 -0800983
chaviw3277faf2021-05-19 16:45:23 -0500984 void setInputFeatures(WindowInfo::Feature features) { mInfo.inputFeatures = features; }
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -0500985
chaviw9eaa22c2020-07-01 16:21:27 -0700986 void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) {
987 mInfo.transform.set(dsdx, dtdx, dtdy, dsdy);
988 }
989
990 void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); }
chaviwaf87b3e2019-10-01 16:59:28 -0700991
yunho.shinf4a80b82020-11-16 21:13:57 +0900992 void setWindowOffset(float offsetX, float offsetY) { mInfo.transform.set(offsetX, offsetY); }
993
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800994 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
995 consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId,
996 expectedFlags);
997 }
998
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700999 void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
1000 consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, expectedDisplayId, expectedFlags);
1001 }
1002
Svet Ganov5d3bc372020-01-26 23:11:07 -08001003 void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001004 int32_t expectedFlags = 0) {
Svet Ganov5d3bc372020-01-26 23:11:07 -08001005 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, expectedDisplayId,
1006 expectedFlags);
1007 }
1008
1009 void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001010 int32_t expectedFlags = 0) {
Svet Ganov5d3bc372020-01-26 23:11:07 -08001011 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, expectedDisplayId,
1012 expectedFlags);
1013 }
1014
1015 void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001016 int32_t expectedFlags = 0) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001017 consumeAnyMotionDown(expectedDisplayId, expectedFlags);
1018 }
1019
1020 void consumeAnyMotionDown(std::optional<int32_t> expectedDisplayId = std::nullopt,
1021 std::optional<int32_t> expectedFlags = std::nullopt) {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001022 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId,
1023 expectedFlags);
1024 }
1025
Svet Ganov5d3bc372020-01-26 23:11:07 -08001026 void consumeMotionPointerDown(int32_t pointerIdx,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001027 int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1028 int32_t expectedFlags = 0) {
1029 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
1030 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001031 consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags);
1032 }
1033
1034 void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001035 int32_t expectedFlags = 0) {
1036 int32_t action = AMOTION_EVENT_ACTION_POINTER_UP |
1037 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001038 consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags);
1039 }
1040
1041 void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001042 int32_t expectedFlags = 0) {
Michael Wright3a240c42019-12-10 20:53:41 +00001043 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId,
1044 expectedFlags);
1045 }
1046
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05001047 void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1048 int32_t expectedFlags = 0) {
1049 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, expectedDisplayId,
1050 expectedFlags);
1051 }
1052
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001053 void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) {
1054 ASSERT_NE(mInputReceiver, nullptr)
1055 << "Cannot consume events from a window with no receiver";
1056 mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode);
1057 }
1058
Prabir Pradhan99987712020-11-10 18:43:05 -08001059 void consumeCaptureEvent(bool hasCapture) {
1060 ASSERT_NE(mInputReceiver, nullptr)
1061 << "Cannot consume events from a window with no receiver";
1062 mInputReceiver->consumeCaptureEvent(hasCapture);
1063 }
1064
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001065 void consumeEvent(int32_t expectedEventType, int32_t expectedAction,
1066 std::optional<int32_t> expectedDisplayId,
1067 std::optional<int32_t> expectedFlags) {
chaviwd1c23182019-12-20 18:44:56 -08001068 ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver";
1069 mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId,
1070 expectedFlags);
1071 }
1072
arthurhungb89ccb02020-12-30 16:19:01 +08001073 void consumeDragEvent(bool isExiting, float x, float y) {
1074 mInputReceiver->consumeDragEvent(isExiting, x, y);
1075 }
1076
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001077 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001078 if (mInputReceiver == nullptr) {
1079 ADD_FAILURE() << "Invalid receive event on window with no receiver";
1080 return std::nullopt;
1081 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001082 return mInputReceiver->receiveEvent(outEvent);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001083 }
1084
1085 void finishEvent(uint32_t sequenceNum) {
1086 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
1087 mInputReceiver->finishEvent(sequenceNum);
1088 }
1089
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00001090 void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) {
1091 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
1092 mInputReceiver->sendTimeline(inputEventId, timeline);
1093 }
1094
chaviwaf87b3e2019-10-01 16:59:28 -07001095 InputEvent* consume() {
1096 if (mInputReceiver == nullptr) {
1097 return nullptr;
1098 }
1099 return mInputReceiver->consume();
1100 }
1101
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00001102 MotionEvent* consumeMotion() {
1103 InputEvent* event = consume();
1104 if (event == nullptr) {
1105 ADD_FAILURE() << "Consume failed : no event";
1106 return nullptr;
1107 }
1108 if (event->getType() != AINPUT_EVENT_TYPE_MOTION) {
1109 ADD_FAILURE() << "Instead of motion event, got "
1110 << inputEventTypeToString(event->getType());
1111 return nullptr;
1112 }
1113 return static_cast<MotionEvent*>(event);
1114 }
1115
Arthur Hungb92218b2018-08-14 12:00:21 +08001116 void assertNoEvents() {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001117 if (mInputReceiver == nullptr &&
chaviw3277faf2021-05-19 16:45:23 -05001118 mInfo.inputFeatures.test(WindowInfo::Feature::NO_INPUT_CHANNEL)) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001119 return; // Can't receive events if the window does not have input channel
1120 }
1121 ASSERT_NE(nullptr, mInputReceiver)
1122 << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL";
chaviwd1c23182019-12-20 18:44:56 -08001123 mInputReceiver->assertNoEvents();
Arthur Hungb92218b2018-08-14 12:00:21 +08001124 }
1125
chaviwaf87b3e2019-10-01 16:59:28 -07001126 sp<IBinder> getToken() { return mInfo.token; }
1127
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001128 const std::string& getName() { return mName; }
1129
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001130 void setOwnerInfo(int32_t ownerPid, int32_t ownerUid) {
1131 mInfo.ownerPid = ownerPid;
1132 mInfo.ownerUid = ownerUid;
1133 }
1134
chaviwd1c23182019-12-20 18:44:56 -08001135private:
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001136 const std::string mName;
chaviwd1c23182019-12-20 18:44:56 -08001137 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001138 static std::atomic<int32_t> sId; // each window gets a unique id, like in surfaceflinger
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001139};
1140
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001141std::atomic<int32_t> FakeWindowHandle::sId{1};
1142
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001143static InputEventInjectionResult injectKey(
1144 const sp<InputDispatcher>& dispatcher, int32_t action, int32_t repeatCount,
1145 int32_t displayId = ADISPLAY_ID_NONE,
1146 InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001147 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
1148 bool allowKeyRepeat = true) {
Arthur Hungb92218b2018-08-14 12:00:21 +08001149 KeyEvent event;
1150 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1151
1152 // Define a valid key down event.
Garfield Tanfbe732e2020-01-24 11:26:14 -08001153 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001154 INVALID_HMAC, action, /* flags */ 0, AKEYCODE_A, KEY_A, AMETA_NONE,
1155 repeatCount, currentTime, currentTime);
Arthur Hungb92218b2018-08-14 12:00:21 +08001156
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001157 int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER;
1158 if (!allowKeyRepeat) {
1159 policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT;
1160 }
Arthur Hungb92218b2018-08-14 12:00:21 +08001161 // Inject event until dispatch out.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001162 return dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, syncMode,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001163 injectionTimeout, policyFlags);
Arthur Hungb92218b2018-08-14 12:00:21 +08001164}
1165
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001166static InputEventInjectionResult injectKeyDown(const sp<InputDispatcher>& dispatcher,
1167 int32_t displayId = ADISPLAY_ID_NONE) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001168 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /* repeatCount */ 0, displayId);
1169}
1170
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001171// Inject a down event that has key repeat disabled. This allows InputDispatcher to idle without
1172// sending a subsequent key up. When key repeat is enabled, the dispatcher cannot idle because it
1173// has to be woken up to process the repeating key.
1174static InputEventInjectionResult injectKeyDownNoRepeat(const sp<InputDispatcher>& dispatcher,
1175 int32_t displayId = ADISPLAY_ID_NONE) {
1176 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /* repeatCount */ 0, displayId,
1177 InputEventInjectionSync::WAIT_FOR_RESULT, INJECT_EVENT_TIMEOUT,
1178 /* allowKeyRepeat */ false);
1179}
1180
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001181static InputEventInjectionResult injectKeyUp(const sp<InputDispatcher>& dispatcher,
1182 int32_t displayId = ADISPLAY_ID_NONE) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001183 return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /* repeatCount */ 0, displayId);
1184}
1185
Garfield Tandf26e862020-07-01 20:18:19 -07001186class PointerBuilder {
1187public:
1188 PointerBuilder(int32_t id, int32_t toolType) {
1189 mProperties.clear();
1190 mProperties.id = id;
1191 mProperties.toolType = toolType;
1192 mCoords.clear();
1193 }
1194
1195 PointerBuilder& x(float x) { return axis(AMOTION_EVENT_AXIS_X, x); }
1196
1197 PointerBuilder& y(float y) { return axis(AMOTION_EVENT_AXIS_Y, y); }
1198
1199 PointerBuilder& axis(int32_t axis, float value) {
1200 mCoords.setAxisValue(axis, value);
1201 return *this;
1202 }
1203
1204 PointerProperties buildProperties() const { return mProperties; }
1205
1206 PointerCoords buildCoords() const { return mCoords; }
1207
1208private:
1209 PointerProperties mProperties;
1210 PointerCoords mCoords;
1211};
1212
1213class MotionEventBuilder {
1214public:
1215 MotionEventBuilder(int32_t action, int32_t source) {
1216 mAction = action;
1217 mSource = source;
1218 mEventTime = systemTime(SYSTEM_TIME_MONOTONIC);
1219 }
1220
1221 MotionEventBuilder& eventTime(nsecs_t eventTime) {
1222 mEventTime = eventTime;
1223 return *this;
1224 }
1225
1226 MotionEventBuilder& displayId(int32_t displayId) {
1227 mDisplayId = displayId;
1228 return *this;
1229 }
1230
1231 MotionEventBuilder& actionButton(int32_t actionButton) {
1232 mActionButton = actionButton;
1233 return *this;
1234 }
1235
arthurhung6d4bed92021-03-17 11:59:33 +08001236 MotionEventBuilder& buttonState(int32_t buttonState) {
1237 mButtonState = buttonState;
Garfield Tandf26e862020-07-01 20:18:19 -07001238 return *this;
1239 }
1240
1241 MotionEventBuilder& rawXCursorPosition(float rawXCursorPosition) {
1242 mRawXCursorPosition = rawXCursorPosition;
1243 return *this;
1244 }
1245
1246 MotionEventBuilder& rawYCursorPosition(float rawYCursorPosition) {
1247 mRawYCursorPosition = rawYCursorPosition;
1248 return *this;
1249 }
1250
1251 MotionEventBuilder& pointer(PointerBuilder pointer) {
1252 mPointers.push_back(pointer);
1253 return *this;
1254 }
1255
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08001256 MotionEventBuilder& addFlag(uint32_t flags) {
1257 mFlags |= flags;
1258 return *this;
1259 }
1260
Garfield Tandf26e862020-07-01 20:18:19 -07001261 MotionEvent build() {
1262 std::vector<PointerProperties> pointerProperties;
1263 std::vector<PointerCoords> pointerCoords;
1264 for (const PointerBuilder& pointer : mPointers) {
1265 pointerProperties.push_back(pointer.buildProperties());
1266 pointerCoords.push_back(pointer.buildCoords());
1267 }
1268
1269 // Set mouse cursor position for the most common cases to avoid boilerplate.
1270 if (mSource == AINPUT_SOURCE_MOUSE &&
1271 !MotionEvent::isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition) &&
1272 mPointers.size() == 1) {
1273 mRawXCursorPosition = pointerCoords[0].getX();
1274 mRawYCursorPosition = pointerCoords[0].getY();
1275 }
1276
1277 MotionEvent event;
chaviw9eaa22c2020-07-01 16:21:27 -07001278 ui::Transform identityTransform;
Garfield Tandf26e862020-07-01 20:18:19 -07001279 event.initialize(InputEvent::nextId(), DEVICE_ID, mSource, mDisplayId, INVALID_HMAC,
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08001280 mAction, mActionButton, mFlags, /* edgeFlags */ 0, AMETA_NONE,
chaviw9eaa22c2020-07-01 16:21:27 -07001281 mButtonState, MotionClassification::NONE, identityTransform,
1282 /* xPrecision */ 0, /* yPrecision */ 0, mRawXCursorPosition,
Evan Rosky09576692021-07-01 12:22:09 -07001283 mRawYCursorPosition, mDisplayOrientation, mDisplayWidth, mDisplayHeight,
1284 mEventTime, mEventTime, mPointers.size(), pointerProperties.data(),
1285 pointerCoords.data());
Garfield Tandf26e862020-07-01 20:18:19 -07001286
1287 return event;
1288 }
1289
1290private:
1291 int32_t mAction;
1292 int32_t mSource;
1293 nsecs_t mEventTime;
1294 int32_t mDisplayId{ADISPLAY_ID_DEFAULT};
1295 int32_t mActionButton{0};
1296 int32_t mButtonState{0};
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08001297 int32_t mFlags{0};
Garfield Tandf26e862020-07-01 20:18:19 -07001298 float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
1299 float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
Evan Rosky09576692021-07-01 12:22:09 -07001300 uint32_t mDisplayOrientation{ui::Transform::ROT_0};
chaviw3277faf2021-05-19 16:45:23 -05001301 int32_t mDisplayWidth{INVALID_DISPLAY_SIZE};
1302 int32_t mDisplayHeight{INVALID_DISPLAY_SIZE};
Garfield Tandf26e862020-07-01 20:18:19 -07001303
1304 std::vector<PointerBuilder> mPointers;
1305};
1306
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001307static InputEventInjectionResult injectMotionEvent(
Garfield Tandf26e862020-07-01 20:18:19 -07001308 const sp<InputDispatcher>& dispatcher, const MotionEvent& event,
1309 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001310 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT) {
Garfield Tandf26e862020-07-01 20:18:19 -07001311 return dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, injectionMode,
1312 injectionTimeout,
1313 POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
1314}
1315
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001316static InputEventInjectionResult injectMotionEvent(
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001317 const sp<InputDispatcher>& dispatcher, int32_t action, int32_t source, int32_t displayId,
1318 const PointF& position,
1319 const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001320 AMOTION_EVENT_INVALID_CURSOR_POSITION},
1321 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001322 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001323 nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC)) {
Garfield Tandf26e862020-07-01 20:18:19 -07001324 MotionEvent event = MotionEventBuilder(action, source)
1325 .displayId(displayId)
1326 .eventTime(eventTime)
1327 .rawXCursorPosition(cursorPosition.x)
1328 .rawYCursorPosition(cursorPosition.y)
1329 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
1330 .x(position.x)
1331 .y(position.y))
1332 .build();
Arthur Hungb92218b2018-08-14 12:00:21 +08001333
1334 // Inject event until dispatch out.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001335 return injectMotionEvent(dispatcher, event, injectionTimeout, injectionMode);
Arthur Hungb92218b2018-08-14 12:00:21 +08001336}
1337
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001338static InputEventInjectionResult injectMotionDown(const sp<InputDispatcher>& dispatcher,
1339 int32_t source, int32_t displayId,
1340 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001341 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location);
Garfield Tan00f511d2019-06-12 16:55:40 -07001342}
1343
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001344static InputEventInjectionResult injectMotionUp(const sp<InputDispatcher>& dispatcher,
1345 int32_t source, int32_t displayId,
1346 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001347 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location);
Michael Wright3a240c42019-12-10 20:53:41 +00001348}
1349
Jackal Guof9696682018-10-05 12:23:23 +08001350static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) {
1351 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1352 // Define a valid key event.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001353 NotifyKeyArgs args(/* id */ 0, currentTime, 0 /*readTime*/, DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
1354 displayId, POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0, AKEYCODE_A,
1355 KEY_A, AMETA_NONE, currentTime);
Jackal Guof9696682018-10-05 12:23:23 +08001356
1357 return args;
1358}
1359
chaviwd1c23182019-12-20 18:44:56 -08001360static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId,
1361 const std::vector<PointF>& points) {
1362 size_t pointerCount = points.size();
chaviwaf87b3e2019-10-01 16:59:28 -07001363 if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) {
1364 EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer";
1365 }
1366
chaviwd1c23182019-12-20 18:44:56 -08001367 PointerProperties pointerProperties[pointerCount];
1368 PointerCoords pointerCoords[pointerCount];
Jackal Guof9696682018-10-05 12:23:23 +08001369
chaviwd1c23182019-12-20 18:44:56 -08001370 for (size_t i = 0; i < pointerCount; i++) {
1371 pointerProperties[i].clear();
1372 pointerProperties[i].id = i;
1373 pointerProperties[i].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jackal Guof9696682018-10-05 12:23:23 +08001374
chaviwd1c23182019-12-20 18:44:56 -08001375 pointerCoords[i].clear();
1376 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x);
1377 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y);
1378 }
Jackal Guof9696682018-10-05 12:23:23 +08001379
1380 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1381 // Define a valid motion event.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001382 NotifyMotionArgs args(/* id */ 0, currentTime, 0 /*readTime*/, DEVICE_ID, source, displayId,
Garfield Tan00f511d2019-06-12 16:55:40 -07001383 POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0,
1384 AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
chaviwd1c23182019-12-20 18:44:56 -08001385 AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties,
1386 pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0,
Garfield Tan00f511d2019-06-12 16:55:40 -07001387 AMOTION_EVENT_INVALID_CURSOR_POSITION,
1388 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {});
Jackal Guof9696682018-10-05 12:23:23 +08001389
1390 return args;
1391}
1392
chaviwd1c23182019-12-20 18:44:56 -08001393static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) {
1394 return generateMotionArgs(action, source, displayId, {PointF{100, 200}});
1395}
1396
Prabir Pradhanac483a62021-08-06 14:01:18 +00001397static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs(
1398 const PointerCaptureRequest& request) {
1399 return NotifyPointerCaptureChangedArgs(/* id */ 0, systemTime(SYSTEM_TIME_MONOTONIC), request);
Prabir Pradhan99987712020-11-10 18:43:05 -08001400}
1401
Arthur Hungb92218b2018-08-14 12:00:21 +08001402TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001403 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001404 sp<FakeWindowHandle> window =
1405 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001406
Arthur Hung72d8dc32020-03-28 00:48:39 +00001407 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001408 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1409 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1410 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001411
1412 // Window should receive motion event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001413 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001414}
1415
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001416/**
1417 * Calling setInputWindows once with FLAG_NOT_TOUCH_MODAL should not cause any issues.
1418 * To ensure that window receives only events that were directly inside of it, add
1419 * FLAG_NOT_TOUCH_MODAL. This will enforce using the touchableRegion of the input
1420 * when finding touched windows.
1421 * This test serves as a sanity check for the next test, where setInputWindows is
1422 * called twice.
1423 */
1424TEST_F(InputDispatcherTest, SetInputWindowOnce_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001425 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001426 sp<FakeWindowHandle> window =
1427 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1428 window->setFrame(Rect(0, 0, 100, 100));
chaviw3277faf2021-05-19 16:45:23 -05001429 window->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001430
1431 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001432 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001433 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1434 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001435 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001436
1437 // Window should receive motion event.
1438 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1439}
1440
1441/**
1442 * Calling setInputWindows twice, with the same info, should not cause any issues.
1443 * To ensure that window receives only events that were directly inside of it, add
1444 * FLAG_NOT_TOUCH_MODAL. This will enforce using the touchableRegion of the input
1445 * when finding touched windows.
1446 */
1447TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001448 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001449 sp<FakeWindowHandle> window =
1450 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1451 window->setFrame(Rect(0, 0, 100, 100));
chaviw3277faf2021-05-19 16:45:23 -05001452 window->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001453
1454 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1455 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001456 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001457 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1458 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001459 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001460
1461 // Window should receive motion event.
1462 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1463}
1464
Arthur Hungb92218b2018-08-14 12:00:21 +08001465// The foreground window should receive the first touch down event.
1466TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001467 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001468 sp<FakeWindowHandle> windowTop =
1469 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
1470 sp<FakeWindowHandle> windowSecond =
1471 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001472
Arthur Hung72d8dc32020-03-28 00:48:39 +00001473 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001474 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1475 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1476 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001477
1478 // Top window should receive the touch down event. Second window should not receive anything.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001479 windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001480 windowSecond->assertNoEvents();
1481}
1482
Garfield Tandf26e862020-07-01 20:18:19 -07001483TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) {
Chris Yea209fde2020-07-22 13:54:51 -07001484 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07001485 sp<FakeWindowHandle> windowLeft =
1486 new FakeWindowHandle(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
1487 windowLeft->setFrame(Rect(0, 0, 600, 800));
chaviw3277faf2021-05-19 16:45:23 -05001488 windowLeft->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tandf26e862020-07-01 20:18:19 -07001489 sp<FakeWindowHandle> windowRight =
1490 new FakeWindowHandle(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
1491 windowRight->setFrame(Rect(600, 0, 1200, 800));
chaviw3277faf2021-05-19 16:45:23 -05001492 windowRight->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tandf26e862020-07-01 20:18:19 -07001493
1494 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1495
1496 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
1497
1498 // Start cursor position in right window so that we can move the cursor to left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001499 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001500 injectMotionEvent(mDispatcher,
1501 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
1502 AINPUT_SOURCE_MOUSE)
1503 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1504 .x(900)
1505 .y(400))
1506 .build()));
1507 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1508 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1509 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE,
1510 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1511
1512 // Move cursor into left window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001513 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001514 injectMotionEvent(mDispatcher,
1515 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
1516 AINPUT_SOURCE_MOUSE)
1517 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1518 .x(300)
1519 .y(400))
1520 .build()));
1521 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT,
1522 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1523 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1524 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1525 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE,
1526 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1527
1528 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001529 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001530 injectMotionEvent(mDispatcher,
1531 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
1532 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1533 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1534 .x(300)
1535 .y(400))
1536 .build()));
1537 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1538
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001539 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001540 injectMotionEvent(mDispatcher,
1541 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
1542 AINPUT_SOURCE_MOUSE)
1543 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1544 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1545 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1546 .x(300)
1547 .y(400))
1548 .build()));
1549 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_PRESS,
1550 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1551
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001552 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001553 injectMotionEvent(mDispatcher,
1554 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
1555 AINPUT_SOURCE_MOUSE)
1556 .buttonState(0)
1557 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1558 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1559 .x(300)
1560 .y(400))
1561 .build()));
1562 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_RELEASE,
1563 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1564
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001565 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001566 injectMotionEvent(mDispatcher,
1567 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
1568 .buttonState(0)
1569 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1570 .x(300)
1571 .y(400))
1572 .build()));
1573 windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT);
1574
1575 // Move mouse cursor back to right window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001576 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001577 injectMotionEvent(mDispatcher,
1578 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
1579 AINPUT_SOURCE_MOUSE)
1580 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1581 .x(900)
1582 .y(400))
1583 .build()));
1584 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT,
1585 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1586 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1587 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1588 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE,
1589 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1590}
1591
1592// This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected
1593// directly in this test.
1594TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) {
Chris Yea209fde2020-07-22 13:54:51 -07001595 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07001596 sp<FakeWindowHandle> window =
1597 new FakeWindowHandle(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
1598 window->setFrame(Rect(0, 0, 1200, 800));
chaviw3277faf2021-05-19 16:45:23 -05001599 window->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tandf26e862020-07-01 20:18:19 -07001600
1601 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1602
1603 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1604
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001605 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001606 injectMotionEvent(mDispatcher,
1607 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
1608 AINPUT_SOURCE_MOUSE)
1609 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1610 .x(300)
1611 .y(400))
1612 .build()));
1613 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1614 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1615
1616 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001617 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001618 injectMotionEvent(mDispatcher,
1619 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
1620 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1621 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1622 .x(300)
1623 .y(400))
1624 .build()));
1625 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1626
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001627 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001628 injectMotionEvent(mDispatcher,
1629 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
1630 AINPUT_SOURCE_MOUSE)
1631 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1632 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1633 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1634 .x(300)
1635 .y(400))
1636 .build()));
1637 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_PRESS,
1638 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1639
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001640 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001641 injectMotionEvent(mDispatcher,
1642 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
1643 AINPUT_SOURCE_MOUSE)
1644 .buttonState(0)
1645 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1646 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1647 .x(300)
1648 .y(400))
1649 .build()));
1650 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_RELEASE,
1651 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1652
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001653 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001654 injectMotionEvent(mDispatcher,
1655 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
1656 .buttonState(0)
1657 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1658 .x(300)
1659 .y(400))
1660 .build()));
1661 window->consumeMotionUp(ADISPLAY_ID_DEFAULT);
1662
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001663 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001664 injectMotionEvent(mDispatcher,
1665 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT,
1666 AINPUT_SOURCE_MOUSE)
1667 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1668 .x(300)
1669 .y(400))
1670 .build()));
1671 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT,
1672 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1673}
1674
Garfield Tan00f511d2019-06-12 16:55:40 -07001675TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) {
Chris Yea209fde2020-07-22 13:54:51 -07001676 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tan00f511d2019-06-12 16:55:40 -07001677
1678 sp<FakeWindowHandle> windowLeft =
1679 new FakeWindowHandle(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
1680 windowLeft->setFrame(Rect(0, 0, 600, 800));
chaviw3277faf2021-05-19 16:45:23 -05001681 windowLeft->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tan00f511d2019-06-12 16:55:40 -07001682 sp<FakeWindowHandle> windowRight =
1683 new FakeWindowHandle(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
1684 windowRight->setFrame(Rect(600, 0, 1200, 800));
chaviw3277faf2021-05-19 16:45:23 -05001685 windowRight->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tan00f511d2019-06-12 16:55:40 -07001686
1687 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1688
Arthur Hung72d8dc32020-03-28 00:48:39 +00001689 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
Garfield Tan00f511d2019-06-12 16:55:40 -07001690
1691 // Inject an event with coordinate in the area of right window, with mouse cursor in the area of
1692 // left window. This event should be dispatched to the left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001693 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tan00f511d2019-06-12 16:55:40 -07001694 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001695 ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400}));
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001696 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07001697 windowRight->assertNoEvents();
1698}
1699
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001700TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) {
Chris Yea209fde2020-07-22 13:54:51 -07001701 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001702 sp<FakeWindowHandle> window =
1703 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Vishnu Nair47074b82020-08-14 11:54:47 -07001704 window->setFocusable(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001705
Arthur Hung72d8dc32020-03-28 00:48:39 +00001706 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07001707 setFocusedWindow(window);
1708
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001709 window->consumeFocusEvent(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001710
1711 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
1712 mDispatcher->notifyKey(&keyArgs);
1713
1714 // Window should receive key down event.
1715 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
1716
1717 // When device reset happens, that key stream should be terminated with FLAG_CANCELED
1718 // on the app side.
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001719 NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001720 mDispatcher->notifyDeviceReset(&args);
1721 window->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
1722 AKEY_EVENT_FLAG_CANCELED);
1723}
1724
1725TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) {
Chris Yea209fde2020-07-22 13:54:51 -07001726 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001727 sp<FakeWindowHandle> window =
1728 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1729
Arthur Hung72d8dc32020-03-28 00:48:39 +00001730 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001731
1732 NotifyMotionArgs motionArgs =
1733 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1734 ADISPLAY_ID_DEFAULT);
1735 mDispatcher->notifyMotion(&motionArgs);
1736
1737 // Window should receive motion down event.
1738 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1739
1740 // When device reset happens, that motion stream should be terminated with ACTION_CANCEL
1741 // on the app side.
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001742 NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001743 mDispatcher->notifyDeviceReset(&args);
1744 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, ADISPLAY_ID_DEFAULT,
1745 0 /*expectedFlags*/);
1746}
1747
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00001748using TransferFunction =
1749 std::function<bool(sp<InputDispatcher> dispatcher, sp<IBinder>, sp<IBinder>)>;
1750
1751class TransferTouchFixture : public InputDispatcherTest,
1752 public ::testing::WithParamInterface<TransferFunction> {};
1753
1754TEST_P(TransferTouchFixture, TransferTouch_OnePointer) {
Chris Yea209fde2020-07-22 13:54:51 -07001755 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08001756
1757 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001758 sp<FakeWindowHandle> firstWindow =
1759 new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT);
1760 sp<FakeWindowHandle> secondWindow =
1761 new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001762
1763 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00001764 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001765
1766 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001767 NotifyMotionArgs downMotionArgs =
1768 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1769 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001770 mDispatcher->notifyMotion(&downMotionArgs);
1771 // Only the first window should get the down event
1772 firstWindow->consumeMotionDown();
1773 secondWindow->assertNoEvents();
1774
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00001775 // Transfer touch to the second window
1776 TransferFunction f = GetParam();
1777 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
1778 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001779 // The first window gets cancel and the second gets down
1780 firstWindow->consumeMotionCancel();
1781 secondWindow->consumeMotionDown();
1782
1783 // Send up event to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001784 NotifyMotionArgs upMotionArgs =
1785 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
1786 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001787 mDispatcher->notifyMotion(&upMotionArgs);
1788 // The first window gets no events and the second gets up
1789 firstWindow->assertNoEvents();
1790 secondWindow->consumeMotionUp();
1791}
1792
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00001793TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001794 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08001795
1796 PointF touchPoint = {10, 10};
1797
1798 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001799 sp<FakeWindowHandle> firstWindow =
1800 new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT);
1801 sp<FakeWindowHandle> secondWindow =
1802 new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001803
1804 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00001805 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001806
1807 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001808 NotifyMotionArgs downMotionArgs =
1809 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1810 ADISPLAY_ID_DEFAULT, {touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001811 mDispatcher->notifyMotion(&downMotionArgs);
1812 // Only the first window should get the down event
1813 firstWindow->consumeMotionDown();
1814 secondWindow->assertNoEvents();
1815
1816 // Send pointer down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001817 NotifyMotionArgs pointerDownMotionArgs =
1818 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN |
1819 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1820 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1821 {touchPoint, touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001822 mDispatcher->notifyMotion(&pointerDownMotionArgs);
1823 // Only the first window should get the pointer down event
1824 firstWindow->consumeMotionPointerDown(1);
1825 secondWindow->assertNoEvents();
1826
1827 // Transfer touch focus to the second window
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00001828 TransferFunction f = GetParam();
1829 bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
1830 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001831 // The first window gets cancel and the second gets down and pointer down
1832 firstWindow->consumeMotionCancel();
1833 secondWindow->consumeMotionDown();
1834 secondWindow->consumeMotionPointerDown(1);
1835
1836 // Send pointer up to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001837 NotifyMotionArgs pointerUpMotionArgs =
1838 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP |
1839 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1840 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1841 {touchPoint, touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001842 mDispatcher->notifyMotion(&pointerUpMotionArgs);
1843 // The first window gets nothing and the second gets pointer up
1844 firstWindow->assertNoEvents();
1845 secondWindow->consumeMotionPointerUp(1);
1846
1847 // Send up event to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001848 NotifyMotionArgs upMotionArgs =
1849 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
1850 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001851 mDispatcher->notifyMotion(&upMotionArgs);
1852 // The first window gets nothing and the second gets up
1853 firstWindow->assertNoEvents();
1854 secondWindow->consumeMotionUp();
1855}
1856
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00001857// For the cases of single pointer touch and two pointers non-split touch, the api's
1858// 'transferTouch' and 'transferTouchFocus' are equivalent in behaviour. They only differ
1859// for the case where there are multiple pointers split across several windows.
1860INSTANTIATE_TEST_SUITE_P(TransferFunctionTests, TransferTouchFixture,
1861 ::testing::Values(
1862 [&](sp<InputDispatcher> dispatcher, sp<IBinder> /*ignored*/,
1863 sp<IBinder> destChannelToken) {
1864 return dispatcher->transferTouch(destChannelToken);
1865 },
1866 [&](sp<InputDispatcher> dispatcher, sp<IBinder> from,
1867 sp<IBinder> to) {
1868 return dispatcher->transferTouchFocus(from, to,
1869 false /*isDragAndDrop*/);
1870 }));
1871
Svet Ganov5d3bc372020-01-26 23:11:07 -08001872TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001873 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08001874
1875 // Create a non touch modal window that supports split touch
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001876 sp<FakeWindowHandle> firstWindow =
1877 new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001878 firstWindow->setFrame(Rect(0, 0, 600, 400));
chaviw3277faf2021-05-19 16:45:23 -05001879 firstWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001880
1881 // Create a non touch modal window that supports split touch
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001882 sp<FakeWindowHandle> secondWindow =
1883 new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001884 secondWindow->setFrame(Rect(0, 400, 600, 800));
chaviw3277faf2021-05-19 16:45:23 -05001885 secondWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001886
1887 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00001888 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001889
1890 PointF pointInFirst = {300, 200};
1891 PointF pointInSecond = {300, 600};
1892
1893 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001894 NotifyMotionArgs firstDownMotionArgs =
1895 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1896 ADISPLAY_ID_DEFAULT, {pointInFirst});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001897 mDispatcher->notifyMotion(&firstDownMotionArgs);
1898 // Only the first window should get the down event
1899 firstWindow->consumeMotionDown();
1900 secondWindow->assertNoEvents();
1901
1902 // Send down to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001903 NotifyMotionArgs secondDownMotionArgs =
1904 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN |
1905 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1906 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1907 {pointInFirst, pointInSecond});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001908 mDispatcher->notifyMotion(&secondDownMotionArgs);
1909 // The first window gets a move and the second a down
1910 firstWindow->consumeMotionMove();
1911 secondWindow->consumeMotionDown();
1912
1913 // Transfer touch focus to the second window
1914 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
1915 // The first window gets cancel and the new gets pointer down (it already saw down)
1916 firstWindow->consumeMotionCancel();
1917 secondWindow->consumeMotionPointerDown(1);
1918
1919 // Send pointer up to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001920 NotifyMotionArgs pointerUpMotionArgs =
1921 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP |
1922 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1923 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1924 {pointInFirst, pointInSecond});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001925 mDispatcher->notifyMotion(&pointerUpMotionArgs);
1926 // The first window gets nothing and the second gets pointer up
1927 firstWindow->assertNoEvents();
1928 secondWindow->consumeMotionPointerUp(1);
1929
1930 // Send up event to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001931 NotifyMotionArgs upMotionArgs =
1932 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
1933 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001934 mDispatcher->notifyMotion(&upMotionArgs);
1935 // The first window gets nothing and the second gets up
1936 firstWindow->assertNoEvents();
1937 secondWindow->consumeMotionUp();
1938}
1939
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00001940// Same as TransferTouchFocus_TwoPointersSplitTouch, but using 'transferTouch' api.
1941// Unlike 'transferTouchFocus', calling 'transferTouch' when there are two windows receiving
1942// touch is not supported, so the touch should continue on those windows and the transferred-to
1943// window should get nothing.
1944TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) {
1945 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1946
1947 // Create a non touch modal window that supports split touch
1948 sp<FakeWindowHandle> firstWindow =
1949 new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT);
1950 firstWindow->setFrame(Rect(0, 0, 600, 400));
chaviw3277faf2021-05-19 16:45:23 -05001951 firstWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00001952
1953 // Create a non touch modal window that supports split touch
1954 sp<FakeWindowHandle> secondWindow =
1955 new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT);
1956 secondWindow->setFrame(Rect(0, 400, 600, 800));
chaviw3277faf2021-05-19 16:45:23 -05001957 secondWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00001958
1959 // Add the windows to the dispatcher
1960 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
1961
1962 PointF pointInFirst = {300, 200};
1963 PointF pointInSecond = {300, 600};
1964
1965 // Send down to the first window
1966 NotifyMotionArgs firstDownMotionArgs =
1967 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1968 ADISPLAY_ID_DEFAULT, {pointInFirst});
1969 mDispatcher->notifyMotion(&firstDownMotionArgs);
1970 // Only the first window should get the down event
1971 firstWindow->consumeMotionDown();
1972 secondWindow->assertNoEvents();
1973
1974 // Send down to the second window
1975 NotifyMotionArgs secondDownMotionArgs =
1976 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN |
1977 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1978 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1979 {pointInFirst, pointInSecond});
1980 mDispatcher->notifyMotion(&secondDownMotionArgs);
1981 // The first window gets a move and the second a down
1982 firstWindow->consumeMotionMove();
1983 secondWindow->consumeMotionDown();
1984
1985 // Transfer touch focus to the second window
1986 const bool transferred = mDispatcher->transferTouch(secondWindow->getToken());
1987 // The 'transferTouch' call should not succeed, because there are 2 touched windows
1988 ASSERT_FALSE(transferred);
1989 firstWindow->assertNoEvents();
1990 secondWindow->assertNoEvents();
1991
1992 // The rest of the dispatch should proceed as normal
1993 // Send pointer up to the second window
1994 NotifyMotionArgs pointerUpMotionArgs =
1995 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP |
1996 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1997 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1998 {pointInFirst, pointInSecond});
1999 mDispatcher->notifyMotion(&pointerUpMotionArgs);
2000 // The first window gets MOVE and the second gets pointer up
2001 firstWindow->consumeMotionMove();
2002 secondWindow->consumeMotionUp();
2003
2004 // Send up event to the first window
2005 NotifyMotionArgs upMotionArgs =
2006 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
2007 ADISPLAY_ID_DEFAULT);
2008 mDispatcher->notifyMotion(&upMotionArgs);
2009 // The first window gets nothing and the second gets up
2010 firstWindow->consumeMotionUp();
2011 secondWindow->assertNoEvents();
2012}
2013
Arthur Hungabbb9d82021-09-01 14:52:30 +00002014// This case will create two windows and one mirrored window on the default display and mirror
2015// two windows on the second display. It will test if 'transferTouchFocus' works fine if we put
2016// the windows info of second display before default display.
2017TEST_F(InputDispatcherTest, TransferTouchFocus_CloneSurface) {
2018 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2019 sp<FakeWindowHandle> firstWindowInPrimary =
2020 new FakeWindowHandle(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT);
2021 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
2022 firstWindowInPrimary->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
2023 sp<FakeWindowHandle> secondWindowInPrimary =
2024 new FakeWindowHandle(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
2025 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
2026 secondWindowInPrimary->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
2027
2028 sp<FakeWindowHandle> mirrorWindowInPrimary =
2029 firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT);
2030 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
2031 mirrorWindowInPrimary->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
2032
2033 sp<FakeWindowHandle> firstWindowInSecondary =
2034 firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
2035 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
2036 firstWindowInSecondary->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
2037
2038 sp<FakeWindowHandle> secondWindowInSecondary =
2039 secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
2040 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
2041 secondWindowInPrimary->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
2042
2043 // Update window info, let it find window handle of second display first.
2044 mDispatcher->setInputWindows(
2045 {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}},
2046 {ADISPLAY_ID_DEFAULT,
2047 {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}});
2048
2049 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2050 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2051 {50, 50}))
2052 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2053
2054 // Window should receive motion event.
2055 firstWindowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2056
2057 // Transfer touch focus
2058 ASSERT_TRUE(mDispatcher->transferTouchFocus(firstWindowInPrimary->getToken(),
2059 secondWindowInPrimary->getToken()));
2060 // The first window gets cancel.
2061 firstWindowInPrimary->consumeMotionCancel();
2062 secondWindowInPrimary->consumeMotionDown();
2063
2064 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2065 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
2066 ADISPLAY_ID_DEFAULT, {150, 50}))
2067 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2068 firstWindowInPrimary->assertNoEvents();
2069 secondWindowInPrimary->consumeMotionMove();
2070
2071 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2072 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2073 {150, 50}))
2074 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2075 firstWindowInPrimary->assertNoEvents();
2076 secondWindowInPrimary->consumeMotionUp();
2077}
2078
2079// Same as TransferTouchFocus_CloneSurface, but this touch on the secondary display and use
2080// 'transferTouch' api.
2081TEST_F(InputDispatcherTest, TransferTouch_CloneSurface) {
2082 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2083 sp<FakeWindowHandle> firstWindowInPrimary =
2084 new FakeWindowHandle(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT);
2085 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
2086 firstWindowInPrimary->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
2087 sp<FakeWindowHandle> secondWindowInPrimary =
2088 new FakeWindowHandle(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
2089 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
2090 secondWindowInPrimary->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
2091
2092 sp<FakeWindowHandle> mirrorWindowInPrimary =
2093 firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT);
2094 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
2095 mirrorWindowInPrimary->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
2096
2097 sp<FakeWindowHandle> firstWindowInSecondary =
2098 firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
2099 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
2100 firstWindowInSecondary->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
2101
2102 sp<FakeWindowHandle> secondWindowInSecondary =
2103 secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
2104 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
2105 secondWindowInPrimary->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
2106
2107 // Update window info, let it find window handle of second display first.
2108 mDispatcher->setInputWindows(
2109 {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}},
2110 {ADISPLAY_ID_DEFAULT,
2111 {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}});
2112
2113 // Touch on second display.
2114 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2115 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {50, 50}))
2116 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2117
2118 // Window should receive motion event.
2119 firstWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID);
2120
2121 // Transfer touch focus
2122 ASSERT_TRUE(mDispatcher->transferTouch(secondWindowInSecondary->getToken()));
2123
2124 // The first window gets cancel.
2125 firstWindowInPrimary->consumeMotionCancel(SECOND_DISPLAY_ID);
2126 secondWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID);
2127
2128 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2129 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
2130 SECOND_DISPLAY_ID, {150, 50}))
2131 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2132 firstWindowInPrimary->assertNoEvents();
2133 secondWindowInPrimary->consumeMotionMove(SECOND_DISPLAY_ID);
2134
2135 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2136 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {150, 50}))
2137 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2138 firstWindowInPrimary->assertNoEvents();
2139 secondWindowInPrimary->consumeMotionUp(SECOND_DISPLAY_ID);
2140}
2141
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002142TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07002143 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002144 sp<FakeWindowHandle> window =
2145 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
2146
Vishnu Nair47074b82020-08-14 11:54:47 -07002147 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002148 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002149 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002150
2151 window->consumeFocusEvent(true);
2152
2153 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
2154 mDispatcher->notifyKey(&keyArgs);
2155
2156 // Window should receive key down event.
2157 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2158}
2159
2160TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07002161 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002162 sp<FakeWindowHandle> window =
2163 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
2164
Arthur Hung72d8dc32020-03-28 00:48:39 +00002165 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002166
2167 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
2168 mDispatcher->notifyKey(&keyArgs);
2169 mDispatcher->waitForIdle();
2170
2171 window->assertNoEvents();
2172}
2173
2174// If a window is touchable, but does not have focus, it should receive motion events, but not keys
2175TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) {
Chris Yea209fde2020-07-22 13:54:51 -07002176 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002177 sp<FakeWindowHandle> window =
2178 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
2179
Arthur Hung72d8dc32020-03-28 00:48:39 +00002180 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002181
2182 // Send key
2183 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
2184 mDispatcher->notifyKey(&keyArgs);
2185 // Send motion
2186 NotifyMotionArgs motionArgs =
2187 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2188 ADISPLAY_ID_DEFAULT);
2189 mDispatcher->notifyMotion(&motionArgs);
2190
2191 // Window should receive only the motion event
2192 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2193 window->assertNoEvents(); // Key event or focus event will not be received
2194}
2195
arthurhungea3f4fc2020-12-21 23:18:53 +08002196TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) {
2197 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2198
2199 // Create first non touch modal window that supports split touch
2200 sp<FakeWindowHandle> firstWindow =
2201 new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT);
2202 firstWindow->setFrame(Rect(0, 0, 600, 400));
chaviw3277faf2021-05-19 16:45:23 -05002203 firstWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH);
arthurhungea3f4fc2020-12-21 23:18:53 +08002204
2205 // Create second non touch modal window that supports split touch
2206 sp<FakeWindowHandle> secondWindow =
2207 new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT);
2208 secondWindow->setFrame(Rect(0, 400, 600, 800));
chaviw3277faf2021-05-19 16:45:23 -05002209 secondWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH);
arthurhungea3f4fc2020-12-21 23:18:53 +08002210
2211 // Add the windows to the dispatcher
2212 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
2213
2214 PointF pointInFirst = {300, 200};
2215 PointF pointInSecond = {300, 600};
2216
2217 // Send down to the first window
2218 NotifyMotionArgs firstDownMotionArgs =
2219 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2220 ADISPLAY_ID_DEFAULT, {pointInFirst});
2221 mDispatcher->notifyMotion(&firstDownMotionArgs);
2222 // Only the first window should get the down event
2223 firstWindow->consumeMotionDown();
2224 secondWindow->assertNoEvents();
2225
2226 // Send down to the second window
2227 NotifyMotionArgs secondDownMotionArgs =
2228 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN |
2229 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2230 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2231 {pointInFirst, pointInSecond});
2232 mDispatcher->notifyMotion(&secondDownMotionArgs);
2233 // The first window gets a move and the second a down
2234 firstWindow->consumeMotionMove();
2235 secondWindow->consumeMotionDown();
2236
2237 // Send pointer cancel to the second window
2238 NotifyMotionArgs pointerUpMotionArgs =
2239 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP |
2240 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2241 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2242 {pointInFirst, pointInSecond});
2243 pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED;
2244 mDispatcher->notifyMotion(&pointerUpMotionArgs);
2245 // The first window gets move and the second gets cancel.
2246 firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
2247 secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
2248
2249 // Send up event.
2250 NotifyMotionArgs upMotionArgs =
2251 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
2252 ADISPLAY_ID_DEFAULT);
2253 mDispatcher->notifyMotion(&upMotionArgs);
2254 // The first window gets up and the second gets nothing.
2255 firstWindow->consumeMotionUp();
2256 secondWindow->assertNoEvents();
2257}
2258
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00002259TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) {
2260 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2261
2262 sp<FakeWindowHandle> window =
2263 new FakeWindowHandle(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2264 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2265 std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline;
2266 graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2;
2267 graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3;
2268
2269 window->sendTimeline(1 /*inputEventId*/, graphicsTimeline);
2270 window->assertNoEvents();
2271 mDispatcher->waitForIdle();
2272}
2273
chaviwd1c23182019-12-20 18:44:56 -08002274class FakeMonitorReceiver {
Michael Wright3a240c42019-12-10 20:53:41 +00002275public:
2276 FakeMonitorReceiver(const sp<InputDispatcher>& dispatcher, const std::string name,
chaviwd1c23182019-12-20 18:44:56 -08002277 int32_t displayId, bool isGestureMonitor = false) {
Garfield Tan15601662020-09-22 15:32:38 -07002278 base::Result<std::unique_ptr<InputChannel>> channel =
Siarhei Vishniakou58cfc602020-12-14 23:21:30 +00002279 dispatcher->createInputMonitor(displayId, isGestureMonitor, name, MONITOR_PID);
Garfield Tan15601662020-09-22 15:32:38 -07002280 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
Michael Wright3a240c42019-12-10 20:53:41 +00002281 }
2282
chaviwd1c23182019-12-20 18:44:56 -08002283 sp<IBinder> getToken() { return mInputReceiver->getToken(); }
2284
2285 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
2286 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN,
2287 expectedDisplayId, expectedFlags);
2288 }
2289
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002290 std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); }
2291
2292 void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); }
2293
chaviwd1c23182019-12-20 18:44:56 -08002294 void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
2295 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN,
2296 expectedDisplayId, expectedFlags);
2297 }
2298
2299 void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
2300 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP,
2301 expectedDisplayId, expectedFlags);
2302 }
2303
Arthur Hung71625472021-11-16 02:45:54 +00002304 void consumeMotionCancel(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
2305 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL,
2306 expectedDisplayId, expectedFlags);
2307 }
2308
2309 void consumeMotionPointerDown(int32_t pointerIdx) {
2310 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
2311 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
2312 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, ADISPLAY_ID_DEFAULT,
2313 0 /*expectedFlags*/);
2314 }
2315
Evan Rosky84f07f02021-04-16 10:42:42 -07002316 MotionEvent* consumeMotion() {
2317 InputEvent* event = mInputReceiver->consume();
2318 if (!event) {
2319 ADD_FAILURE() << "No event was produced";
2320 return nullptr;
2321 }
2322 if (event->getType() != AINPUT_EVENT_TYPE_MOTION) {
2323 ADD_FAILURE() << "Received event of type " << event->getType() << " instead of motion";
2324 return nullptr;
2325 }
2326 return static_cast<MotionEvent*>(event);
2327 }
2328
chaviwd1c23182019-12-20 18:44:56 -08002329 void assertNoEvents() { mInputReceiver->assertNoEvents(); }
2330
2331private:
2332 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Michael Wright3a240c42019-12-10 20:53:41 +00002333};
2334
2335// Tests for gesture monitors
2336TEST_F(InputDispatcherTest, GestureMonitor_ReceivesMotionEvents) {
Chris Yea209fde2020-07-22 13:54:51 -07002337 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Michael Wright3a240c42019-12-10 20:53:41 +00002338 sp<FakeWindowHandle> window =
2339 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002340 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00002341
chaviwd1c23182019-12-20 18:44:56 -08002342 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT,
2343 true /*isGestureMonitor*/);
Michael Wright3a240c42019-12-10 20:53:41 +00002344
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002345 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00002346 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002347 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00002348 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08002349 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00002350}
2351
2352TEST_F(InputDispatcherTest, GestureMonitor_DoesNotReceiveKeyEvents) {
Chris Yea209fde2020-07-22 13:54:51 -07002353 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Michael Wright3a240c42019-12-10 20:53:41 +00002354 sp<FakeWindowHandle> window =
2355 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
2356
2357 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07002358 window->setFocusable(true);
Michael Wright3a240c42019-12-10 20:53:41 +00002359
Arthur Hung72d8dc32020-03-28 00:48:39 +00002360 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002361 setFocusedWindow(window);
2362
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002363 window->consumeFocusEvent(true);
Michael Wright3a240c42019-12-10 20:53:41 +00002364
chaviwd1c23182019-12-20 18:44:56 -08002365 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT,
2366 true /*isGestureMonitor*/);
Michael Wright3a240c42019-12-10 20:53:41 +00002367
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002368 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
2369 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00002370 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08002371 monitor.assertNoEvents();
Michael Wright3a240c42019-12-10 20:53:41 +00002372}
2373
2374TEST_F(InputDispatcherTest, GestureMonitor_CanPilferAfterWindowIsRemovedMidStream) {
Chris Yea209fde2020-07-22 13:54:51 -07002375 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Michael Wright3a240c42019-12-10 20:53:41 +00002376 sp<FakeWindowHandle> window =
2377 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002378 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00002379
chaviwd1c23182019-12-20 18:44:56 -08002380 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT,
2381 true /*isGestureMonitor*/);
Michael Wright3a240c42019-12-10 20:53:41 +00002382
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002383 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00002384 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002385 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00002386 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08002387 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00002388
2389 window->releaseChannel();
2390
chaviwd1c23182019-12-20 18:44:56 -08002391 mDispatcher->pilferPointers(monitor.getToken());
Michael Wright3a240c42019-12-10 20:53:41 +00002392
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002393 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00002394 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002395 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
chaviwd1c23182019-12-20 18:44:56 -08002396 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00002397}
2398
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002399TEST_F(InputDispatcherTest, UnresponsiveGestureMonitor_GetsAnr) {
2400 FakeMonitorReceiver monitor =
2401 FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT,
2402 true /*isGestureMonitor*/);
2403
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002404 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002405 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
2406 std::optional<uint32_t> consumeSeq = monitor.receiveEvent();
2407 ASSERT_TRUE(consumeSeq);
2408
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00002409 mFakePolicy->assertNotifyMonitorUnresponsiveWasCalled(DISPATCHING_TIMEOUT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002410 monitor.finishEvent(*consumeSeq);
2411 ASSERT_TRUE(mDispatcher->waitForIdle());
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00002412 mFakePolicy->assertNotifyMonitorResponsiveWasCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002413}
2414
Evan Rosky84f07f02021-04-16 10:42:42 -07002415// Tests for gesture monitors
2416TEST_F(InputDispatcherTest, GestureMonitor_NoWindowTransform) {
2417 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2418 sp<FakeWindowHandle> window =
2419 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
2420 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2421 window->setWindowOffset(20, 40);
2422 window->setWindowTransform(0, 1, -1, 0);
2423
2424 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT,
2425 true /*isGestureMonitor*/);
2426
2427 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2428 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
2429 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2430 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2431 MotionEvent* event = monitor.consumeMotion();
2432 // Even though window has transform, gesture monitor must not.
2433 ASSERT_EQ(ui::Transform(), event->getTransform());
2434}
2435
chaviw81e2bb92019-12-18 15:03:51 -08002436TEST_F(InputDispatcherTest, TestMoveEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07002437 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
chaviw81e2bb92019-12-18 15:03:51 -08002438 sp<FakeWindowHandle> window =
2439 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
2440
Arthur Hung72d8dc32020-03-28 00:48:39 +00002441 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
chaviw81e2bb92019-12-18 15:03:51 -08002442
2443 NotifyMotionArgs motionArgs =
2444 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2445 ADISPLAY_ID_DEFAULT);
2446
2447 mDispatcher->notifyMotion(&motionArgs);
2448 // Window should receive motion down event.
2449 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2450
2451 motionArgs.action = AMOTION_EVENT_ACTION_MOVE;
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002452 motionArgs.id += 1;
chaviw81e2bb92019-12-18 15:03:51 -08002453 motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
2454 motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
2455 motionArgs.pointerCoords[0].getX() - 10);
2456
2457 mDispatcher->notifyMotion(&motionArgs);
2458 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT,
2459 0 /*expectedFlags*/);
2460}
2461
Arthur Hung71625472021-11-16 02:45:54 +00002462TEST_F(InputDispatcherTest, GestureMonitor_SplitIfNoWindowTouched) {
2463 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT,
2464 true /*isGestureMonitor*/);
2465
2466 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2467 // Create a non touch modal window that supports split touch
2468 sp<FakeWindowHandle> window =
2469 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
2470 window->setFrame(Rect(0, 0, 100, 100));
2471 window->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH);
2472 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2473
2474 // First finger down, no window touched.
2475 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2476 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2477 {100, 200}))
2478 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2479 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
2480 window->assertNoEvents();
2481
2482 // Second finger down on window, the window should receive touch down.
2483 const MotionEvent secondFingerDownEvent =
2484 MotionEventBuilder(AMOTION_EVENT_ACTION_POINTER_DOWN |
2485 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2486 AINPUT_SOURCE_TOUCHSCREEN)
2487 .displayId(ADISPLAY_ID_DEFAULT)
2488 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
2489 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
2490 .x(100)
2491 .y(200))
2492 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
2493 .build();
2494 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2495 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
2496 InputEventInjectionSync::WAIT_FOR_RESULT))
2497 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2498
2499 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2500 monitor.consumeMotionPointerDown(1 /* pointerIndex */);
2501}
2502
2503TEST_F(InputDispatcherTest, GestureMonitor_NoSplitAfterPilfer) {
2504 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT,
2505 true /*isGestureMonitor*/);
2506
2507 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2508 // Create a non touch modal window that supports split touch
2509 sp<FakeWindowHandle> window =
2510 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
2511 window->setFrame(Rect(0, 0, 100, 100));
2512 window->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH);
2513 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2514
2515 // First finger down, no window touched.
2516 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2517 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2518 {100, 200}))
2519 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2520 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
2521 window->assertNoEvents();
2522
2523 // Gesture monitor pilfer the pointers.
2524 mDispatcher->pilferPointers(monitor.getToken());
2525
2526 // Second finger down on window, the window should not receive touch down.
2527 const MotionEvent secondFingerDownEvent =
2528 MotionEventBuilder(AMOTION_EVENT_ACTION_POINTER_DOWN |
2529 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2530 AINPUT_SOURCE_TOUCHSCREEN)
2531 .displayId(ADISPLAY_ID_DEFAULT)
2532 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
2533 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
2534 .x(100)
2535 .y(200))
2536 .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50))
2537 .build();
2538 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2539 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
2540 InputEventInjectionSync::WAIT_FOR_RESULT))
2541 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2542
2543 window->assertNoEvents();
2544 monitor.consumeMotionPointerDown(1 /* pointerIndex */);
2545}
2546
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002547/**
2548 * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to
2549 * the device default right away. In the test scenario, we check both the default value,
2550 * and the action of enabling / disabling.
2551 */
2552TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) {
Chris Yea209fde2020-07-22 13:54:51 -07002553 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002554 sp<FakeWindowHandle> window =
2555 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
2556
2557 // Set focused application.
2558 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07002559 window->setFocusable(true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002560
2561 SCOPED_TRACE("Check default value of touch mode");
Arthur Hung72d8dc32020-03-28 00:48:39 +00002562 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002563 setFocusedWindow(window);
2564
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002565 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
2566
2567 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07002568 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002569 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002570 window->consumeFocusEvent(false /*hasFocus*/, true /*inTouchMode*/);
2571
2572 SCOPED_TRACE("Disable touch mode");
2573 mDispatcher->setInTouchMode(false);
Vishnu Nair47074b82020-08-14 11:54:47 -07002574 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002575 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002576 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002577 window->consumeFocusEvent(true /*hasFocus*/, false /*inTouchMode*/);
2578
2579 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07002580 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002581 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002582 window->consumeFocusEvent(false /*hasFocus*/, false /*inTouchMode*/);
2583
2584 SCOPED_TRACE("Enable touch mode again");
2585 mDispatcher->setInTouchMode(true);
Vishnu Nair47074b82020-08-14 11:54:47 -07002586 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002587 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002588 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002589 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
2590
2591 window->assertNoEvents();
2592}
2593
Gang Wange9087892020-01-07 12:17:14 -05002594TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07002595 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Gang Wange9087892020-01-07 12:17:14 -05002596 sp<FakeWindowHandle> window =
2597 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
2598
2599 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07002600 window->setFocusable(true);
Gang Wange9087892020-01-07 12:17:14 -05002601
Arthur Hung72d8dc32020-03-28 00:48:39 +00002602 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002603 setFocusedWindow(window);
2604
Gang Wange9087892020-01-07 12:17:14 -05002605 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
2606
2607 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
2608 mDispatcher->notifyKey(&keyArgs);
2609
2610 InputEvent* event = window->consume();
2611 ASSERT_NE(event, nullptr);
2612
2613 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
2614 ASSERT_NE(verified, nullptr);
2615 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY);
2616
2617 ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos);
2618 ASSERT_EQ(keyArgs.deviceId, verified->deviceId);
2619 ASSERT_EQ(keyArgs.source, verified->source);
2620 ASSERT_EQ(keyArgs.displayId, verified->displayId);
2621
2622 const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified);
2623
2624 ASSERT_EQ(keyArgs.action, verifiedKey.action);
2625 ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos);
Gang Wange9087892020-01-07 12:17:14 -05002626 ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags);
2627 ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode);
2628 ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode);
2629 ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState);
2630 ASSERT_EQ(0, verifiedKey.repeatCount);
2631}
2632
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08002633TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07002634 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08002635 sp<FakeWindowHandle> window =
2636 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
2637
2638 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2639
Arthur Hung72d8dc32020-03-28 00:48:39 +00002640 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08002641
2642 NotifyMotionArgs motionArgs =
2643 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2644 ADISPLAY_ID_DEFAULT);
2645 mDispatcher->notifyMotion(&motionArgs);
2646
2647 InputEvent* event = window->consume();
2648 ASSERT_NE(event, nullptr);
2649
2650 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
2651 ASSERT_NE(verified, nullptr);
2652 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION);
2653
2654 EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos);
2655 EXPECT_EQ(motionArgs.deviceId, verified->deviceId);
2656 EXPECT_EQ(motionArgs.source, verified->source);
2657 EXPECT_EQ(motionArgs.displayId, verified->displayId);
2658
2659 const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified);
2660
2661 EXPECT_EQ(motionArgs.pointerCoords[0].getX(), verifiedMotion.rawX);
2662 EXPECT_EQ(motionArgs.pointerCoords[0].getY(), verifiedMotion.rawY);
2663 EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked);
2664 EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos);
2665 EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags);
2666 EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState);
2667 EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState);
2668}
2669
Prabir Pradhan664834b2021-05-20 16:00:42 -07002670TEST_F(InputDispatcherTest, NonPointerMotionEvent_JoystickAndTouchpadNotTransformed) {
yunho.shinf4a80b82020-11-16 21:13:57 +09002671 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2672 sp<FakeWindowHandle> window =
2673 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
2674 const std::string name = window->getName();
2675
2676 // Window gets transformed by offset values.
2677 window->setWindowOffset(500.0f, 500.0f);
2678
2679 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2680 window->setFocusable(true);
2681
2682 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2683
2684 // First, we set focused window so that focusedWindowHandle is not null.
2685 setFocusedWindow(window);
2686
2687 // Second, we consume focus event if it is right or wrong according to onFocusChangedLocked.
2688 window->consumeFocusEvent(true);
2689
Prabir Pradhan664834b2021-05-20 16:00:42 -07002690 constexpr const std::array nonTransformedSources = {std::pair(AINPUT_SOURCE_TOUCHPAD,
2691 AMOTION_EVENT_ACTION_DOWN),
2692 std::pair(AINPUT_SOURCE_JOYSTICK,
2693 AMOTION_EVENT_ACTION_MOVE)};
2694 for (const auto& [source, action] : nonTransformedSources) {
2695 const NotifyMotionArgs motionArgs = generateMotionArgs(action, source, ADISPLAY_ID_DEFAULT);
Prabir Pradhanbd527712021-03-09 19:17:09 -08002696 mDispatcher->notifyMotion(&motionArgs);
yunho.shinf4a80b82020-11-16 21:13:57 +09002697
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00002698 MotionEvent* event = window->consumeMotion();
Prabir Pradhanbd527712021-03-09 19:17:09 -08002699 ASSERT_NE(event, nullptr);
yunho.shinf4a80b82020-11-16 21:13:57 +09002700
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00002701 const MotionEvent& motionEvent = *event;
Prabir Pradhan664834b2021-05-20 16:00:42 -07002702 EXPECT_EQ(action, motionEvent.getAction());
Prabir Pradhanbd527712021-03-09 19:17:09 -08002703 EXPECT_EQ(motionArgs.pointerCount, motionEvent.getPointerCount());
yunho.shinf4a80b82020-11-16 21:13:57 +09002704
Prabir Pradhanbd527712021-03-09 19:17:09 -08002705 float expectedX = motionArgs.pointerCoords[0].getX();
2706 float expectedY = motionArgs.pointerCoords[0].getY();
yunho.shinf4a80b82020-11-16 21:13:57 +09002707
Prabir Pradhanbd527712021-03-09 19:17:09 -08002708 // Ensure the axis values from the final motion event are not transformed.
2709 EXPECT_EQ(expectedX, motionEvent.getX(0))
2710 << "expected " << expectedX << " for x coord of " << name.c_str() << ", got "
2711 << motionEvent.getX(0);
2712 EXPECT_EQ(expectedY, motionEvent.getY(0))
2713 << "expected " << expectedY << " for y coord of " << name.c_str() << ", got "
2714 << motionEvent.getY(0);
2715 // Ensure the raw and transformed axis values for the motion event are the same.
2716 EXPECT_EQ(motionEvent.getRawX(0), motionEvent.getX(0))
2717 << "expected raw and transformed X-axis values to be equal";
2718 EXPECT_EQ(motionEvent.getRawY(0), motionEvent.getY(0))
2719 << "expected raw and transformed Y-axis values to be equal";
2720 }
yunho.shinf4a80b82020-11-16 21:13:57 +09002721}
2722
chaviw09c8d2d2020-08-24 15:48:26 -07002723/**
2724 * Ensure that separate calls to sign the same data are generating the same key.
2725 * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance
2726 * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky
2727 * tests.
2728 */
2729TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) {
2730 KeyEvent event = getTestKeyEvent();
2731 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
2732
2733 std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent);
2734 std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent);
2735 ASSERT_EQ(hmac1, hmac2);
2736}
2737
2738/**
2739 * Ensure that changes in VerifiedKeyEvent produce a different hmac.
2740 */
2741TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) {
2742 KeyEvent event = getTestKeyEvent();
2743 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
2744 std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent);
2745
2746 verifiedEvent.deviceId += 1;
2747 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2748
2749 verifiedEvent.source += 1;
2750 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2751
2752 verifiedEvent.eventTimeNanos += 1;
2753 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2754
2755 verifiedEvent.displayId += 1;
2756 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2757
2758 verifiedEvent.action += 1;
2759 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2760
2761 verifiedEvent.downTimeNanos += 1;
2762 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2763
2764 verifiedEvent.flags += 1;
2765 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2766
2767 verifiedEvent.keyCode += 1;
2768 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2769
2770 verifiedEvent.scanCode += 1;
2771 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2772
2773 verifiedEvent.metaState += 1;
2774 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2775
2776 verifiedEvent.repeatCount += 1;
2777 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2778}
2779
Vishnu Nair958da932020-08-21 17:12:37 -07002780TEST_F(InputDispatcherTest, SetFocusedWindow) {
2781 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2782 sp<FakeWindowHandle> windowTop =
2783 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
2784 sp<FakeWindowHandle> windowSecond =
2785 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
2786 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2787
2788 // Top window is also focusable but is not granted focus.
2789 windowTop->setFocusable(true);
2790 windowSecond->setFocusable(true);
2791 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
2792 setFocusedWindow(windowSecond);
2793
2794 windowSecond->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002795 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
2796 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07002797
2798 // Focused window should receive event.
2799 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
2800 windowTop->assertNoEvents();
2801}
2802
2803TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) {
2804 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2805 sp<FakeWindowHandle> window =
2806 new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
2807 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2808
2809 window->setFocusable(true);
2810 // Release channel for window is no longer valid.
2811 window->releaseChannel();
2812 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2813 setFocusedWindow(window);
2814
2815 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002816 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
2817 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07002818
2819 // window channel is invalid, so it should not receive any input event.
2820 window->assertNoEvents();
2821}
2822
2823TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) {
2824 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2825 sp<FakeWindowHandle> window =
2826 new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
2827 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2828
2829 // Window is not focusable.
2830 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2831 setFocusedWindow(window);
2832
2833 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002834 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
2835 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07002836
2837 // window is invalid, so it should not receive any input event.
2838 window->assertNoEvents();
2839}
2840
2841TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) {
2842 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2843 sp<FakeWindowHandle> windowTop =
2844 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
2845 sp<FakeWindowHandle> windowSecond =
2846 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
2847 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2848
2849 windowTop->setFocusable(true);
2850 windowSecond->setFocusable(true);
2851 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
2852 setFocusedWindow(windowTop);
2853 windowTop->consumeFocusEvent(true);
2854
2855 setFocusedWindow(windowSecond, windowTop);
2856 windowSecond->consumeFocusEvent(true);
2857 windowTop->consumeFocusEvent(false);
2858
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002859 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
2860 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07002861
2862 // Focused window should receive event.
2863 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
2864}
2865
2866TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestFocusTokenNotFocused) {
2867 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2868 sp<FakeWindowHandle> windowTop =
2869 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
2870 sp<FakeWindowHandle> windowSecond =
2871 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
2872 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2873
2874 windowTop->setFocusable(true);
2875 windowSecond->setFocusable(true);
2876 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
2877 setFocusedWindow(windowSecond, windowTop);
2878
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002879 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
2880 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07002881
2882 // Event should be dropped.
2883 windowTop->assertNoEvents();
2884 windowSecond->assertNoEvents();
2885}
2886
2887TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) {
2888 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2889 sp<FakeWindowHandle> window =
2890 new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
2891 sp<FakeWindowHandle> previousFocusedWindow =
2892 new FakeWindowHandle(application, mDispatcher, "previousFocusedWindow",
2893 ADISPLAY_ID_DEFAULT);
2894 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2895
2896 window->setFocusable(true);
2897 previousFocusedWindow->setFocusable(true);
2898 window->setVisible(false);
2899 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}});
2900 setFocusedWindow(previousFocusedWindow);
2901 previousFocusedWindow->consumeFocusEvent(true);
2902
2903 // Requesting focus on invisible window takes focus from currently focused window.
2904 setFocusedWindow(window);
2905 previousFocusedWindow->consumeFocusEvent(false);
2906
2907 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002908 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Vishnu Nair958da932020-08-21 17:12:37 -07002909 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002910 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07002911
2912 // Window does not get focus event or key down.
2913 window->assertNoEvents();
2914
2915 // Window becomes visible.
2916 window->setVisible(true);
2917 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2918
2919 // Window receives focus event.
2920 window->consumeFocusEvent(true);
2921 // Focused window receives key down.
2922 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2923}
2924
Vishnu Nair599f1412021-06-21 10:39:58 -07002925TEST_F(InputDispatcherTest, DisplayRemoved) {
2926 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2927 sp<FakeWindowHandle> window =
2928 new FakeWindowHandle(application, mDispatcher, "window", ADISPLAY_ID_DEFAULT);
2929 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2930
2931 // window is granted focus.
2932 window->setFocusable(true);
2933 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2934 setFocusedWindow(window);
2935 window->consumeFocusEvent(true);
2936
2937 // When a display is removed window loses focus.
2938 mDispatcher->displayRemoved(ADISPLAY_ID_DEFAULT);
2939 window->consumeFocusEvent(false);
2940}
2941
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002942/**
2943 * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY,
2944 * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top
2945 * of the 'slipperyEnterWindow'.
2946 *
2947 * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such
2948 * a way so that the touched location is no longer covered by the top window.
2949 *
2950 * Next, inject a MOVE event. Because the top window already moved earlier, this event is now
2951 * positioned over the bottom (slipperyEnterWindow) only. And because the top window had
2952 * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive
2953 * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting
2954 * with ACTION_DOWN).
2955 * Thus, the touch has been transferred from the top window into the bottom window, because the top
2956 * window moved itself away from the touched location and had Flag::SLIPPERY.
2957 *
2958 * Even though the top window moved away from the touched location, it is still obscuring the bottom
2959 * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_
2960 * OBSCURED should be set for the MotionEvent that reaches the bottom window.
2961 *
2962 * In this test, we ensure that the event received by the bottom window has
2963 * FLAG_WINDOW_IS_PARTIALLY_OBSCURED.
2964 */
2965TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) {
2966 constexpr int32_t SLIPPERY_PID = INJECTOR_PID + 1;
2967 constexpr int32_t SLIPPERY_UID = INJECTOR_UID + 1;
2968
2969 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2970 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2971
2972 sp<FakeWindowHandle> slipperyExitWindow =
2973 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
chaviw3277faf2021-05-19 16:45:23 -05002974 slipperyExitWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SLIPPERY);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002975 // Make sure this one overlaps the bottom window
2976 slipperyExitWindow->setFrame(Rect(25, 25, 75, 75));
2977 // Change the owner uid/pid of the window so that it is considered to be occluding the bottom
2978 // one. Windows with the same owner are not considered to be occluding each other.
2979 slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID);
2980
2981 sp<FakeWindowHandle> slipperyEnterWindow =
2982 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
2983 slipperyExitWindow->setFrame(Rect(0, 0, 100, 100));
2984
2985 mDispatcher->setInputWindows(
2986 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
2987
2988 // Use notifyMotion instead of injecting to avoid dealing with injection permissions
2989 NotifyMotionArgs args = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2990 ADISPLAY_ID_DEFAULT, {{50, 50}});
2991 mDispatcher->notifyMotion(&args);
2992 slipperyExitWindow->consumeMotionDown();
2993 slipperyExitWindow->setFrame(Rect(70, 70, 100, 100));
2994 mDispatcher->setInputWindows(
2995 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
2996
2997 args = generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
2998 ADISPLAY_ID_DEFAULT, {{51, 51}});
2999 mDispatcher->notifyMotion(&args);
3000
3001 slipperyExitWindow->consumeMotionCancel();
3002
3003 slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT,
3004 AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
3005}
3006
Garfield Tan1c7bc862020-01-28 13:24:04 -08003007class InputDispatcherKeyRepeatTest : public InputDispatcherTest {
3008protected:
3009 static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms
3010 static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms
3011
Chris Yea209fde2020-07-22 13:54:51 -07003012 std::shared_ptr<FakeApplicationHandle> mApp;
Garfield Tan1c7bc862020-01-28 13:24:04 -08003013 sp<FakeWindowHandle> mWindow;
3014
3015 virtual void SetUp() override {
3016 mFakePolicy = new FakeInputDispatcherPolicy();
3017 mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY);
3018 mDispatcher = new InputDispatcher(mFakePolicy);
3019 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
3020 ASSERT_EQ(OK, mDispatcher->start());
3021
3022 setUpWindow();
3023 }
3024
3025 void setUpWindow() {
Chris Yea209fde2020-07-22 13:54:51 -07003026 mApp = std::make_shared<FakeApplicationHandle>();
Garfield Tan1c7bc862020-01-28 13:24:04 -08003027 mWindow = new FakeWindowHandle(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
3028
Vishnu Nair47074b82020-08-14 11:54:47 -07003029 mWindow->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00003030 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003031 setFocusedWindow(mWindow);
Garfield Tan1c7bc862020-01-28 13:24:04 -08003032 mWindow->consumeFocusEvent(true);
3033 }
3034
Chris Ye2ad95392020-09-01 13:44:44 -07003035 void sendAndConsumeKeyDown(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08003036 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07003037 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08003038 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event
3039 mDispatcher->notifyKey(&keyArgs);
3040
3041 // Window should receive key down event.
3042 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3043 }
3044
3045 void expectKeyRepeatOnce(int32_t repeatCount) {
3046 SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount));
3047 InputEvent* repeatEvent = mWindow->consume();
3048 ASSERT_NE(nullptr, repeatEvent);
3049
3050 uint32_t eventType = repeatEvent->getType();
3051 ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, eventType);
3052
3053 KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent);
3054 uint32_t eventAction = repeatKeyEvent->getAction();
3055 EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction);
3056 EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount());
3057 }
3058
Chris Ye2ad95392020-09-01 13:44:44 -07003059 void sendAndConsumeKeyUp(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08003060 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07003061 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08003062 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event
3063 mDispatcher->notifyKey(&keyArgs);
3064
3065 // Window should receive key down event.
3066 mWindow->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
3067 0 /*expectedFlags*/);
3068 }
3069};
3070
3071TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) {
Chris Ye2ad95392020-09-01 13:44:44 -07003072 sendAndConsumeKeyDown(1 /* deviceId */);
3073 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
3074 expectKeyRepeatOnce(repeatCount);
3075 }
3076}
3077
3078TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) {
3079 sendAndConsumeKeyDown(1 /* deviceId */);
3080 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
3081 expectKeyRepeatOnce(repeatCount);
3082 }
3083 sendAndConsumeKeyDown(2 /* deviceId */);
3084 /* repeatCount will start from 1 for deviceId 2 */
Garfield Tan1c7bc862020-01-28 13:24:04 -08003085 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
3086 expectKeyRepeatOnce(repeatCount);
3087 }
3088}
3089
3090TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) {
Chris Ye2ad95392020-09-01 13:44:44 -07003091 sendAndConsumeKeyDown(1 /* deviceId */);
Garfield Tan1c7bc862020-01-28 13:24:04 -08003092 expectKeyRepeatOnce(1 /*repeatCount*/);
Chris Ye2ad95392020-09-01 13:44:44 -07003093 sendAndConsumeKeyUp(1 /* deviceId */);
3094 mWindow->assertNoEvents();
3095}
3096
3097TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) {
3098 sendAndConsumeKeyDown(1 /* deviceId */);
3099 expectKeyRepeatOnce(1 /*repeatCount*/);
3100 sendAndConsumeKeyDown(2 /* deviceId */);
3101 expectKeyRepeatOnce(1 /*repeatCount*/);
3102 // Stale key up from device 1.
3103 sendAndConsumeKeyUp(1 /* deviceId */);
3104 // Device 2 is still down, keep repeating
3105 expectKeyRepeatOnce(2 /*repeatCount*/);
3106 expectKeyRepeatOnce(3 /*repeatCount*/);
3107 // Device 2 key up
3108 sendAndConsumeKeyUp(2 /* deviceId */);
3109 mWindow->assertNoEvents();
3110}
3111
3112TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) {
3113 sendAndConsumeKeyDown(1 /* deviceId */);
3114 expectKeyRepeatOnce(1 /*repeatCount*/);
3115 sendAndConsumeKeyDown(2 /* deviceId */);
3116 expectKeyRepeatOnce(1 /*repeatCount*/);
3117 // Device 2 which holds the key repeating goes up, expect the repeating to stop.
3118 sendAndConsumeKeyUp(2 /* deviceId */);
3119 // Device 1 still holds key down, but the repeating was already stopped
Garfield Tan1c7bc862020-01-28 13:24:04 -08003120 mWindow->assertNoEvents();
3121}
3122
liushenxiang42232912021-05-21 20:24:09 +08003123TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) {
3124 sendAndConsumeKeyDown(DEVICE_ID);
3125 expectKeyRepeatOnce(1 /*repeatCount*/);
3126 NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID);
3127 mDispatcher->notifyDeviceReset(&args);
3128 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT,
3129 AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS);
3130 mWindow->assertNoEvents();
3131}
3132
Garfield Tan1c7bc862020-01-28 13:24:04 -08003133TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) {
Chris Ye2ad95392020-09-01 13:44:44 -07003134 sendAndConsumeKeyDown(1 /* deviceId */);
Garfield Tan1c7bc862020-01-28 13:24:04 -08003135 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
3136 InputEvent* repeatEvent = mWindow->consume();
3137 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
3138 EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER,
3139 IdGenerator::getSource(repeatEvent->getId()));
3140 }
3141}
3142
3143TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) {
Chris Ye2ad95392020-09-01 13:44:44 -07003144 sendAndConsumeKeyDown(1 /* deviceId */);
Garfield Tan1c7bc862020-01-28 13:24:04 -08003145
3146 std::unordered_set<int32_t> idSet;
3147 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
3148 InputEvent* repeatEvent = mWindow->consume();
3149 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
3150 int32_t id = repeatEvent->getId();
3151 EXPECT_EQ(idSet.end(), idSet.find(id));
3152 idSet.insert(id);
3153 }
3154}
3155
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003156/* Test InputDispatcher for MultiDisplay */
3157class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest {
3158public:
Prabir Pradhan3608aad2019-10-02 17:08:26 -07003159 virtual void SetUp() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003160 InputDispatcherTest::SetUp();
Arthur Hungb92218b2018-08-14 12:00:21 +08003161
Chris Yea209fde2020-07-22 13:54:51 -07003162 application1 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003163 windowInPrimary =
3164 new FakeWindowHandle(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08003165
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003166 // Set focus window for primary display, but focused display would be second one.
3167 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1);
Vishnu Nair47074b82020-08-14 11:54:47 -07003168 windowInPrimary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00003169 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003170 setFocusedWindow(windowInPrimary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003171 windowInPrimary->consumeFocusEvent(true);
Arthur Hungb92218b2018-08-14 12:00:21 +08003172
Chris Yea209fde2020-07-22 13:54:51 -07003173 application2 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003174 windowInSecondary =
3175 new FakeWindowHandle(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003176 // Set focus to second display window.
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003177 // Set focus display to second one.
3178 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
3179 // Set focus window for second display.
3180 mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2);
Vishnu Nair47074b82020-08-14 11:54:47 -07003181 windowInSecondary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00003182 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003183 setFocusedWindow(windowInSecondary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003184 windowInSecondary->consumeFocusEvent(true);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003185 }
3186
Prabir Pradhan3608aad2019-10-02 17:08:26 -07003187 virtual void TearDown() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003188 InputDispatcherTest::TearDown();
3189
Chris Yea209fde2020-07-22 13:54:51 -07003190 application1.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003191 windowInPrimary.clear();
Chris Yea209fde2020-07-22 13:54:51 -07003192 application2.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003193 windowInSecondary.clear();
3194 }
3195
3196protected:
Chris Yea209fde2020-07-22 13:54:51 -07003197 std::shared_ptr<FakeApplicationHandle> application1;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003198 sp<FakeWindowHandle> windowInPrimary;
Chris Yea209fde2020-07-22 13:54:51 -07003199 std::shared_ptr<FakeApplicationHandle> application2;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003200 sp<FakeWindowHandle> windowInSecondary;
3201};
3202
3203TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) {
3204 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003205 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3206 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
3207 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08003208 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08003209 windowInSecondary->assertNoEvents();
3210
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003211 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003212 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3213 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
3214 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08003215 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08003216 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +08003217}
3218
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003219TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) {
Tiger Huang721e26f2018-07-24 22:26:19 +08003220 // Test inject a key down with display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003221 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3222 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003223 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08003224 windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Tiger Huang721e26f2018-07-24 22:26:19 +08003225 windowInSecondary->assertNoEvents();
3226
3227 // Test inject a key down without display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003228 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003229 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08003230 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08003231 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hungb92218b2018-08-14 12:00:21 +08003232
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08003233 // Remove all windows in secondary display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00003234 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}});
Arthur Hungb92218b2018-08-14 12:00:21 +08003235
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003236 // Old focus should receive a cancel event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08003237 windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE,
3238 AKEY_EVENT_FLAG_CANCELED);
Arthur Hungb92218b2018-08-14 12:00:21 +08003239
3240 // Test inject a key down, should timeout because of no target window.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003241 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003242 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Arthur Hungb92218b2018-08-14 12:00:21 +08003243 windowInPrimary->assertNoEvents();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003244 windowInSecondary->consumeFocusEvent(false);
Arthur Hungb92218b2018-08-14 12:00:21 +08003245 windowInSecondary->assertNoEvents();
3246}
3247
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003248// Test per-display input monitors for motion event.
3249TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) {
chaviwd1c23182019-12-20 18:44:56 -08003250 FakeMonitorReceiver monitorInPrimary =
3251 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
3252 FakeMonitorReceiver monitorInSecondary =
3253 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003254
3255 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003256 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3257 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
3258 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08003259 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08003260 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003261 windowInSecondary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08003262 monitorInSecondary.assertNoEvents();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003263
3264 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003265 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3266 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
3267 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003268 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08003269 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08003270 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
chaviwd1c23182019-12-20 18:44:56 -08003271 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003272
3273 // Test inject a non-pointer motion event.
3274 // If specific a display, it will dispatch to the focused window of particular display,
3275 // or it will dispatch to the focused window of focused display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003276 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3277 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE))
3278 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003279 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08003280 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08003281 windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08003282 monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003283}
3284
3285// Test per-display input monitors for key event.
3286TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) {
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003287 // Input monitor per display.
chaviwd1c23182019-12-20 18:44:56 -08003288 FakeMonitorReceiver monitorInPrimary =
3289 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
3290 FakeMonitorReceiver monitorInSecondary =
3291 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003292
3293 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003294 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
3295 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003296 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08003297 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08003298 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08003299 monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003300}
3301
Vishnu Nair958da932020-08-21 17:12:37 -07003302TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) {
3303 sp<FakeWindowHandle> secondWindowInPrimary =
3304 new FakeWindowHandle(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
3305 secondWindowInPrimary->setFocusable(true);
3306 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}});
3307 setFocusedWindow(secondWindowInPrimary);
3308 windowInPrimary->consumeFocusEvent(false);
3309 secondWindowInPrimary->consumeFocusEvent(true);
3310
3311 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003312 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
3313 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07003314 windowInPrimary->assertNoEvents();
3315 windowInSecondary->assertNoEvents();
3316 secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3317}
3318
Jackal Guof9696682018-10-05 12:23:23 +08003319class InputFilterTest : public InputDispatcherTest {
3320protected:
Jackal Guof9696682018-10-05 12:23:23 +08003321 void testNotifyMotion(int32_t displayId, bool expectToBeFiltered) {
3322 NotifyMotionArgs motionArgs;
3323
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003324 motionArgs =
3325 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Jackal Guof9696682018-10-05 12:23:23 +08003326 mDispatcher->notifyMotion(&motionArgs);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003327 motionArgs =
3328 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Jackal Guof9696682018-10-05 12:23:23 +08003329 mDispatcher->notifyMotion(&motionArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003330 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08003331 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08003332 mFakePolicy->assertFilterInputEventWasCalled(motionArgs);
Jackal Guof9696682018-10-05 12:23:23 +08003333 } else {
3334 mFakePolicy->assertFilterInputEventWasNotCalled();
3335 }
3336 }
3337
3338 void testNotifyKey(bool expectToBeFiltered) {
3339 NotifyKeyArgs keyArgs;
3340
3341 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
3342 mDispatcher->notifyKey(&keyArgs);
3343 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP);
3344 mDispatcher->notifyKey(&keyArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003345 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08003346
3347 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08003348 mFakePolicy->assertFilterInputEventWasCalled(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08003349 } else {
3350 mFakePolicy->assertFilterInputEventWasNotCalled();
3351 }
3352 }
3353};
3354
3355// Test InputFilter for MotionEvent
3356TEST_F(InputFilterTest, MotionEvent_InputFilter) {
3357 // Since the InputFilter is disabled by default, check if touch events aren't filtered.
3358 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
3359 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
3360
3361 // Enable InputFilter
3362 mDispatcher->setInputFilterEnabled(true);
3363 // Test touch on both primary and second display, and check if both events are filtered.
3364 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true);
3365 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true);
3366
3367 // Disable InputFilter
3368 mDispatcher->setInputFilterEnabled(false);
3369 // Test touch on both primary and second display, and check if both events aren't filtered.
3370 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
3371 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
3372}
3373
3374// Test InputFilter for KeyEvent
3375TEST_F(InputFilterTest, KeyEvent_InputFilter) {
3376 // Since the InputFilter is disabled by default, check if key event aren't filtered.
3377 testNotifyKey(/*expectToBeFiltered*/ false);
3378
3379 // Enable InputFilter
3380 mDispatcher->setInputFilterEnabled(true);
3381 // Send a key event, and check if it is filtered.
3382 testNotifyKey(/*expectToBeFiltered*/ true);
3383
3384 // Disable InputFilter
3385 mDispatcher->setInputFilterEnabled(false);
3386 // Send a key event, and check if it isn't filtered.
3387 testNotifyKey(/*expectToBeFiltered*/ false);
3388}
3389
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00003390class InputFilterInjectionPolicyTest : public InputDispatcherTest {
3391protected:
3392 virtual void SetUp() override {
3393 InputDispatcherTest::SetUp();
3394
3395 /**
3396 * We don't need to enable input filter to test the injected event policy, but we enabled it
3397 * here to make the tests more realistic, since this policy only matters when inputfilter is
3398 * on.
3399 */
3400 mDispatcher->setInputFilterEnabled(true);
3401
3402 std::shared_ptr<InputApplicationHandle> application =
3403 std::make_shared<FakeApplicationHandle>();
3404 mWindow =
3405 new FakeWindowHandle(application, mDispatcher, "Test Window", ADISPLAY_ID_DEFAULT);
3406
3407 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3408 mWindow->setFocusable(true);
3409 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3410 setFocusedWindow(mWindow);
3411 mWindow->consumeFocusEvent(true);
3412 }
3413
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00003414 void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
3415 int32_t flags) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00003416 KeyEvent event;
3417
3418 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
3419 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD,
3420 ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A,
3421 KEY_A, AMETA_NONE, 0 /*repeatCount*/, eventTime, eventTime);
3422 const int32_t additionalPolicyFlags =
3423 POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT;
3424 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3425 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
3426 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
3427 policyFlags | additionalPolicyFlags));
3428
3429 InputEvent* received = mWindow->consume();
3430 ASSERT_NE(nullptr, received);
3431 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00003432 ASSERT_EQ(received->getType(), AINPUT_EVENT_TYPE_KEY);
3433 KeyEvent& keyEvent = static_cast<KeyEvent&>(*received);
3434 ASSERT_EQ(flags, keyEvent.getFlags());
3435 }
3436
3437 void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
3438 int32_t flags) {
3439 MotionEvent event;
3440 PointerProperties pointerProperties[1];
3441 PointerCoords pointerCoords[1];
3442 pointerProperties[0].clear();
3443 pointerProperties[0].id = 0;
3444 pointerCoords[0].clear();
3445 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300);
3446 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400);
3447
3448 ui::Transform identityTransform;
3449 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
3450 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN,
3451 DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0,
3452 AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE,
3453 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Evan Rosky09576692021-07-01 12:22:09 -07003454 AMOTION_EVENT_INVALID_CURSOR_POSITION, ui::Transform::ROT_0,
3455 0 /*INVALID_DISPLAY_SIZE*/, 0 /*INVALID_DISPLAY_SIZE*/, eventTime,
3456 eventTime,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00003457 /*pointerCount*/ 1, pointerProperties, pointerCoords);
3458
3459 const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER;
3460 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3461 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
3462 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
3463 policyFlags | additionalPolicyFlags));
3464
3465 InputEvent* received = mWindow->consume();
3466 ASSERT_NE(nullptr, received);
3467 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
3468 ASSERT_EQ(received->getType(), AINPUT_EVENT_TYPE_MOTION);
3469 MotionEvent& motionEvent = static_cast<MotionEvent&>(*received);
3470 ASSERT_EQ(flags, motionEvent.getFlags());
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00003471 }
3472
3473private:
3474 sp<FakeWindowHandle> mWindow;
3475};
3476
3477TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) {
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00003478 // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input
3479 // filter. Without it, the event will no different from a regularly injected event, and the
3480 // injected device id will be overwritten.
3481 testInjectedKey(POLICY_FLAG_FILTERED, 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/,
3482 0 /*flags*/);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00003483}
3484
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00003485TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00003486 testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00003487 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/,
3488 AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
3489}
3490
3491TEST_F(InputFilterInjectionPolicyTest,
3492 MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
3493 testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
3494 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/,
3495 AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00003496}
3497
3498TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) {
3499 testInjectedKey(0 /*policyFlags*/, 3 /*injectedDeviceId*/,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00003500 VIRTUAL_KEYBOARD_ID /*resolvedDeviceId*/, 0 /*flags*/);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00003501}
3502
chaviwfd6d3512019-03-25 13:23:49 -07003503class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest {
Prabir Pradhan3608aad2019-10-02 17:08:26 -07003504 virtual void SetUp() override {
chaviwfd6d3512019-03-25 13:23:49 -07003505 InputDispatcherTest::SetUp();
3506
Chris Yea209fde2020-07-22 13:54:51 -07003507 std::shared_ptr<FakeApplicationHandle> application =
3508 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003509 mUnfocusedWindow =
3510 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07003511 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
3512 // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this
3513 // window.
chaviw3277faf2021-05-19 16:45:23 -05003514 mUnfocusedWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
chaviwfd6d3512019-03-25 13:23:49 -07003515
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08003516 mFocusedWindow =
3517 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
3518 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
chaviw3277faf2021-05-19 16:45:23 -05003519 mFocusedWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
chaviwfd6d3512019-03-25 13:23:49 -07003520
3521 // Set focused application.
3522 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07003523 mFocusedWindow->setFocusable(true);
chaviwfd6d3512019-03-25 13:23:49 -07003524
3525 // Expect one focus window exist in display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00003526 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003527 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003528 mFocusedWindow->consumeFocusEvent(true);
chaviwfd6d3512019-03-25 13:23:49 -07003529 }
3530
Prabir Pradhan3608aad2019-10-02 17:08:26 -07003531 virtual void TearDown() override {
chaviwfd6d3512019-03-25 13:23:49 -07003532 InputDispatcherTest::TearDown();
3533
3534 mUnfocusedWindow.clear();
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08003535 mFocusedWindow.clear();
chaviwfd6d3512019-03-25 13:23:49 -07003536 }
3537
3538protected:
3539 sp<FakeWindowHandle> mUnfocusedWindow;
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08003540 sp<FakeWindowHandle> mFocusedWindow;
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07003541 static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60};
chaviwfd6d3512019-03-25 13:23:49 -07003542};
3543
3544// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
3545// DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received
3546// the onPointerDownOutsideFocus callback.
3547TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003548 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07003549 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3550 {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003551 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07003552 mUnfocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07003553
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003554 ASSERT_TRUE(mDispatcher->waitForIdle());
chaviwfd6d3512019-03-25 13:23:49 -07003555 mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken());
3556}
3557
3558// Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action
3559// DOWN on the window that doesn't have focus. Ensure no window received the
3560// onPointerDownOutsideFocus callback.
3561TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003562 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07003563 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003564 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07003565 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07003566
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003567 ASSERT_TRUE(mDispatcher->waitForIdle());
3568 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07003569}
3570
3571// Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't
3572// have focus. Ensure no window received the onPointerDownOutsideFocus callback.
3573TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003574 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3575 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003576 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07003577 mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07003578
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003579 ASSERT_TRUE(mDispatcher->waitForIdle());
3580 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07003581}
3582
3583// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
3584// DOWN on the window that already has focus. Ensure no window received the
3585// onPointerDownOutsideFocus callback.
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003586TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003587 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08003588 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07003589 FOCUSED_WINDOW_TOUCH_POINT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003590 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07003591 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07003592
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003593 ASSERT_TRUE(mDispatcher->waitForIdle());
3594 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07003595}
3596
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08003597// Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag
3598// NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback.
3599TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) {
3600 const MotionEvent event =
3601 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3602 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
3603 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(20).y(20))
3604 .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE)
3605 .build();
3606 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(mDispatcher, event))
3607 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3608 mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
3609
3610 ASSERT_TRUE(mDispatcher->waitForIdle());
3611 mFakePolicy->assertOnPointerDownWasNotCalled();
3612 // Ensure that the unfocused window did not receive any FOCUS events.
3613 mUnfocusedWindow->assertNoEvents();
3614}
3615
chaviwaf87b3e2019-10-01 16:59:28 -07003616// These tests ensures we can send touch events to a single client when there are multiple input
3617// windows that point to the same client token.
3618class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest {
3619 virtual void SetUp() override {
3620 InputDispatcherTest::SetUp();
3621
Chris Yea209fde2020-07-22 13:54:51 -07003622 std::shared_ptr<FakeApplicationHandle> application =
3623 std::make_shared<FakeApplicationHandle>();
chaviwaf87b3e2019-10-01 16:59:28 -07003624 mWindow1 = new FakeWindowHandle(application, mDispatcher, "Fake Window 1",
3625 ADISPLAY_ID_DEFAULT);
3626 // Adding FLAG_NOT_TOUCH_MODAL otherwise all taps will go to the top most window.
3627 // We also need FLAG_SPLIT_TOUCH or we won't be able to get touches for both windows.
chaviw3277faf2021-05-19 16:45:23 -05003628 mWindow1->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH);
chaviwaf87b3e2019-10-01 16:59:28 -07003629 mWindow1->setFrame(Rect(0, 0, 100, 100));
3630
3631 mWindow2 = new FakeWindowHandle(application, mDispatcher, "Fake Window 2",
3632 ADISPLAY_ID_DEFAULT, mWindow1->getToken());
chaviw3277faf2021-05-19 16:45:23 -05003633 mWindow2->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH);
chaviwaf87b3e2019-10-01 16:59:28 -07003634 mWindow2->setFrame(Rect(100, 100, 200, 200));
3635
Arthur Hung72d8dc32020-03-28 00:48:39 +00003636 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
chaviwaf87b3e2019-10-01 16:59:28 -07003637 }
3638
3639protected:
3640 sp<FakeWindowHandle> mWindow1;
3641 sp<FakeWindowHandle> mWindow2;
3642
3643 // Helper function to convert the point from screen coordinates into the window's space
chaviw3277faf2021-05-19 16:45:23 -05003644 static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) {
chaviw1ff3d1e2020-07-01 15:53:47 -07003645 vec2 vals = windowInfo->transform.transform(point.x, point.y);
3646 return {vals.x, vals.y};
chaviwaf87b3e2019-10-01 16:59:28 -07003647 }
3648
3649 void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction,
3650 const std::vector<PointF>& points) {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003651 const std::string name = window->getName();
chaviwaf87b3e2019-10-01 16:59:28 -07003652 InputEvent* event = window->consume();
3653
3654 ASSERT_NE(nullptr, event) << name.c_str()
3655 << ": consumer should have returned non-NULL event.";
3656
3657 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType())
3658 << name.c_str() << "expected " << inputEventTypeToString(AINPUT_EVENT_TYPE_MOTION)
3659 << " event, got " << inputEventTypeToString(event->getType()) << " event";
3660
3661 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
3662 EXPECT_EQ(expectedAction, motionEvent.getAction());
3663
3664 for (size_t i = 0; i < points.size(); i++) {
3665 float expectedX = points[i].x;
3666 float expectedY = points[i].y;
3667
3668 EXPECT_EQ(expectedX, motionEvent.getX(i))
3669 << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str()
3670 << ", got " << motionEvent.getX(i);
3671 EXPECT_EQ(expectedY, motionEvent.getY(i))
3672 << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str()
3673 << ", got " << motionEvent.getY(i);
3674 }
3675 }
chaviw9eaa22c2020-07-01 16:21:27 -07003676
3677 void touchAndAssertPositions(int32_t action, std::vector<PointF> touchedPoints,
3678 std::vector<PointF> expectedPoints) {
3679 NotifyMotionArgs motionArgs = generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN,
3680 ADISPLAY_ID_DEFAULT, touchedPoints);
3681 mDispatcher->notifyMotion(&motionArgs);
3682
3683 // Always consume from window1 since it's the window that has the InputReceiver
3684 consumeMotionEvent(mWindow1, action, expectedPoints);
3685 }
chaviwaf87b3e2019-10-01 16:59:28 -07003686};
3687
3688TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) {
3689 // Touch Window 1
3690 PointF touchedPoint = {10, 10};
3691 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07003692 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003693
3694 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07003695 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003696
3697 // Touch Window 2
3698 touchedPoint = {150, 150};
3699 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07003700 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003701}
3702
chaviw9eaa22c2020-07-01 16:21:27 -07003703TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) {
3704 // Set scale value for window2
chaviwaf87b3e2019-10-01 16:59:28 -07003705 mWindow2->setWindowScale(0.5f, 0.5f);
3706
3707 // Touch Window 1
3708 PointF touchedPoint = {10, 10};
3709 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07003710 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003711 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07003712 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003713
3714 // Touch Window 2
3715 touchedPoint = {150, 150};
3716 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07003717 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
3718 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003719
chaviw9eaa22c2020-07-01 16:21:27 -07003720 // Update the transform so rotation is set
3721 mWindow2->setWindowTransform(0, -1, 1, 0);
3722 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
3723 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003724}
3725
chaviw9eaa22c2020-07-01 16:21:27 -07003726TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003727 mWindow2->setWindowScale(0.5f, 0.5f);
3728
3729 // Touch Window 1
3730 std::vector<PointF> touchedPoints = {PointF{10, 10}};
3731 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07003732 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003733
3734 // Touch Window 2
3735 int32_t actionPointerDown =
3736 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
chaviw9eaa22c2020-07-01 16:21:27 -07003737 touchedPoints.push_back(PointF{150, 150});
3738 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
3739 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003740
chaviw9eaa22c2020-07-01 16:21:27 -07003741 // Release Window 2
3742 int32_t actionPointerUp =
3743 AMOTION_EVENT_ACTION_POINTER_UP + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
3744 touchAndAssertPositions(actionPointerUp, touchedPoints, expectedPoints);
3745 expectedPoints.pop_back();
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003746
chaviw9eaa22c2020-07-01 16:21:27 -07003747 // Update the transform so rotation is set for Window 2
3748 mWindow2->setWindowTransform(0, -1, 1, 0);
3749 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
3750 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003751}
3752
chaviw9eaa22c2020-07-01 16:21:27 -07003753TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003754 mWindow2->setWindowScale(0.5f, 0.5f);
3755
3756 // Touch Window 1
3757 std::vector<PointF> touchedPoints = {PointF{10, 10}};
3758 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07003759 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003760
3761 // Touch Window 2
3762 int32_t actionPointerDown =
3763 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
chaviw9eaa22c2020-07-01 16:21:27 -07003764 touchedPoints.push_back(PointF{150, 150});
3765 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003766
chaviw9eaa22c2020-07-01 16:21:27 -07003767 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003768
3769 // Move both windows
3770 touchedPoints = {{20, 20}, {175, 175}};
3771 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
3772 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
3773
chaviw9eaa22c2020-07-01 16:21:27 -07003774 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003775
chaviw9eaa22c2020-07-01 16:21:27 -07003776 // Release Window 2
3777 int32_t actionPointerUp =
3778 AMOTION_EVENT_ACTION_POINTER_UP + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
3779 touchAndAssertPositions(actionPointerUp, touchedPoints, expectedPoints);
3780 expectedPoints.pop_back();
3781
3782 // Touch Window 2
3783 mWindow2->setWindowTransform(0, -1, 1, 0);
3784 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
3785 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
3786
3787 // Move both windows
3788 touchedPoints = {{20, 20}, {175, 175}};
3789 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
3790 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
3791
3792 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003793}
3794
3795TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) {
3796 mWindow1->setWindowScale(0.5f, 0.5f);
3797
3798 // Touch Window 1
3799 std::vector<PointF> touchedPoints = {PointF{10, 10}};
3800 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07003801 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003802
3803 // Touch Window 2
3804 int32_t actionPointerDown =
3805 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
chaviw9eaa22c2020-07-01 16:21:27 -07003806 touchedPoints.push_back(PointF{150, 150});
3807 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003808
chaviw9eaa22c2020-07-01 16:21:27 -07003809 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003810
3811 // Move both windows
3812 touchedPoints = {{20, 20}, {175, 175}};
3813 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
3814 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
3815
chaviw9eaa22c2020-07-01 16:21:27 -07003816 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003817}
3818
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003819class InputDispatcherSingleWindowAnr : public InputDispatcherTest {
3820 virtual void SetUp() override {
3821 InputDispatcherTest::SetUp();
3822
Chris Yea209fde2020-07-22 13:54:51 -07003823 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003824 mApplication->setDispatchingTimeout(20ms);
3825 mWindow =
3826 new FakeWindowHandle(mApplication, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
3827 mWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05003828 mWindow->setDispatchingTimeout(30ms);
Vishnu Nair47074b82020-08-14 11:54:47 -07003829 mWindow->setFocusable(true);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003830 // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this
3831 // window.
chaviw3277faf2021-05-19 16:45:23 -05003832 mWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003833
3834 // Set focused application.
3835 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
3836
3837 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003838 setFocusedWindow(mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003839 mWindow->consumeFocusEvent(true);
3840 }
3841
3842 virtual void TearDown() override {
3843 InputDispatcherTest::TearDown();
3844 mWindow.clear();
3845 }
3846
3847protected:
Chris Yea209fde2020-07-22 13:54:51 -07003848 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003849 sp<FakeWindowHandle> mWindow;
3850 static constexpr PointF WINDOW_LOCATION = {20, 20};
3851
3852 void tapOnWindow() {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003853 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003854 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3855 WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003856 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003857 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3858 WINDOW_LOCATION));
3859 }
3860};
3861
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003862// Send a tap and respond, which should not cause an ANR.
3863TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) {
3864 tapOnWindow();
3865 mWindow->consumeMotionDown();
3866 mWindow->consumeMotionUp();
3867 ASSERT_TRUE(mDispatcher->waitForIdle());
3868 mFakePolicy->assertNotifyAnrWasNotCalled();
3869}
3870
3871// Send a regular key and respond, which should not cause an ANR.
3872TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003873 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003874 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
3875 ASSERT_TRUE(mDispatcher->waitForIdle());
3876 mFakePolicy->assertNotifyAnrWasNotCalled();
3877}
3878
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05003879TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) {
3880 mWindow->setFocusable(false);
3881 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3882 mWindow->consumeFocusEvent(false);
3883
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003884 InputEventInjectionResult result =
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05003885 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003886 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/,
3887 false /* allowKeyRepeat */);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003888 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05003889 // Key will not go to window because we have no focused window.
3890 // The 'no focused window' ANR timer should start instead.
3891
3892 // Now, the focused application goes away.
3893 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr);
3894 // The key should get dropped and there should be no ANR.
3895
3896 ASSERT_TRUE(mDispatcher->waitForIdle());
3897 mFakePolicy->assertNotifyAnrWasNotCalled();
3898}
3899
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003900// Send an event to the app and have the app not respond right away.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003901// When ANR is raised, policy will tell the dispatcher to cancel the events for that window.
3902// So InputDispatcher will enqueue ACTION_CANCEL event as well.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003903TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003904 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003905 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3906 WINDOW_LOCATION));
3907
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003908 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN
3909 ASSERT_TRUE(sequenceNum);
3910 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003911 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003912
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003913 mWindow->finishEvent(*sequenceNum);
3914 mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL,
3915 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003916 ASSERT_TRUE(mDispatcher->waitForIdle());
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003917 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003918}
3919
3920// Send a key to the app and have the app not respond right away.
3921TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) {
3922 // Inject a key, and don't respond - expect that ANR is called.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003923 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003924 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent();
3925 ASSERT_TRUE(sequenceNum);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003926 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003927 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003928 ASSERT_TRUE(mDispatcher->waitForIdle());
3929}
3930
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003931// We have a focused application, but no focused window
3932TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) {
Vishnu Nair47074b82020-08-14 11:54:47 -07003933 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003934 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3935 mWindow->consumeFocusEvent(false);
3936
3937 // taps on the window work as normal
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003938 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003939 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3940 WINDOW_LOCATION));
3941 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
3942 mDispatcher->waitForIdle();
3943 mFakePolicy->assertNotifyAnrWasNotCalled();
3944
3945 // Once a focused event arrives, we get an ANR for this application
3946 // We specify the injection timeout to be smaller than the application timeout, to ensure that
3947 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003948 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003949 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003950 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, false /* allowKeyRepeat */);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003951 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003952 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003953 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003954 ASSERT_TRUE(mDispatcher->waitForIdle());
3955}
3956
3957// We have a focused application, but no focused window
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003958// Make sure that we don't notify policy twice about the same ANR.
3959TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) {
Vishnu Nair47074b82020-08-14 11:54:47 -07003960 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003961 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3962 mWindow->consumeFocusEvent(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003963
3964 // Once a focused event arrives, we get an ANR for this application
3965 // We specify the injection timeout to be smaller than the application timeout, to ensure that
3966 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003967 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003968 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003969 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, false /* allowKeyRepeat */);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003970 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003971 const std::chrono::duration appTimeout =
3972 mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003973 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003974
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003975 std::this_thread::sleep_for(appTimeout);
3976 // ANR should not be raised again. It is up to policy to do that if it desires.
3977 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003978
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003979 // If we now get a focused window, the ANR should stop, but the policy handles that via
3980 // 'notifyFocusChanged' callback. This is implemented in the policy so we can't test it here.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003981 ASSERT_TRUE(mDispatcher->waitForIdle());
3982}
3983
3984// We have a focused application, but no focused window
3985TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) {
Vishnu Nair47074b82020-08-14 11:54:47 -07003986 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003987 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3988 mWindow->consumeFocusEvent(false);
3989
3990 // Once a focused event arrives, we get an ANR for this application
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003991 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003992 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003993 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
3994 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003995
3996 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003997 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003998
3999 // Future focused events get dropped right away
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004000 ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004001 ASSERT_TRUE(mDispatcher->waitForIdle());
4002 mWindow->assertNoEvents();
4003}
4004
4005/**
4006 * Ensure that the implementation is valid. Since we are using multiset to keep track of the
4007 * ANR timeouts, we are allowing entries with identical timestamps in the same connection.
4008 * If we process 1 of the events, but ANR on the second event with the same timestamp,
4009 * the ANR mechanism should still work.
4010 *
4011 * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the
4012 * DOWN event, while not responding on the second one.
4013 */
4014TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) {
4015 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
4016 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4017 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
4018 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
4019 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004020 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004021
4022 // Now send ACTION_UP, with identical timestamp
4023 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4024 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
4025 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
4026 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004027 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004028
4029 // We have now sent down and up. Let's consume first event and then ANR on the second.
4030 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4031 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004032 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004033}
4034
4035// If an app is not responding to a key event, gesture monitors should continue to receive
4036// new motion events
4037TEST_F(InputDispatcherSingleWindowAnr, GestureMonitors_ReceiveEventsDuringAppAnrOnKey) {
4038 FakeMonitorReceiver monitor =
4039 FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT,
4040 true /*isGestureMonitor*/);
4041
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004042 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4043 injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004044 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004045 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004046
4047 // Stuck on the ACTION_UP
4048 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004049 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004050
4051 // New tap will go to the gesture monitor, but not to the window
4052 tapOnWindow();
4053 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
4054 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
4055
4056 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion
4057 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004058 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004059 mWindow->assertNoEvents();
4060 monitor.assertNoEvents();
4061}
4062
4063// If an app is not responding to a motion event, gesture monitors should continue to receive
4064// new motion events
4065TEST_F(InputDispatcherSingleWindowAnr, GestureMonitors_ReceiveEventsDuringAppAnrOnMotion) {
4066 FakeMonitorReceiver monitor =
4067 FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT,
4068 true /*isGestureMonitor*/);
4069
4070 tapOnWindow();
4071 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
4072 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
4073
4074 mWindow->consumeMotionDown();
4075 // Stuck on the ACTION_UP
4076 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004077 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004078
4079 // New tap will go to the gesture monitor, but not to the window
4080 tapOnWindow();
4081 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
4082 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
4083
4084 mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion
4085 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004086 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004087 mWindow->assertNoEvents();
4088 monitor.assertNoEvents();
4089}
4090
4091// If a window is unresponsive, then you get anr. if the window later catches up and starts to
4092// process events, you don't get an anr. When the window later becomes unresponsive again, you
4093// get an ANR again.
4094// 1. tap -> block on ACTION_UP -> receive ANR
4095// 2. consume all pending events (= queue becomes healthy again)
4096// 3. tap again -> block on ACTION_UP again -> receive ANR second time
4097TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) {
4098 tapOnWindow();
4099
4100 mWindow->consumeMotionDown();
4101 // Block on ACTION_UP
4102 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004103 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004104 mWindow->consumeMotionUp(); // Now the connection should be healthy again
4105 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004106 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004107 mWindow->assertNoEvents();
4108
4109 tapOnWindow();
4110 mWindow->consumeMotionDown();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004111 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004112 mWindow->consumeMotionUp();
4113
4114 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004115 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004116 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004117 mWindow->assertNoEvents();
4118}
4119
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004120// If a connection remains unresponsive for a while, make sure policy is only notified once about
4121// it.
4122TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004123 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004124 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4125 WINDOW_LOCATION));
4126
4127 const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004128 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004129 std::this_thread::sleep_for(windowTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004130 // 'notifyConnectionUnresponsive' should only be called once per connection
4131 mFakePolicy->assertNotifyAnrWasNotCalled();
4132 // When the ANR happened, dispatcher should abort the current event stream via ACTION_CANCEL
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004133 mWindow->consumeMotionDown();
4134 mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL,
4135 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
4136 mWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004137 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004138 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004139 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004140}
4141
4142/**
4143 * If a window is processing a motion event, and then a key event comes in, the key event should
4144 * not to to the focused window until the motion is processed.
4145 *
4146 * Warning!!!
4147 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
4148 * and the injection timeout that we specify when injecting the key.
4149 * We must have the injection timeout (10ms) be smaller than
4150 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
4151 *
4152 * If that value changes, this test should also change.
4153 */
4154TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) {
4155 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
4156 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
4157
4158 tapOnWindow();
4159 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
4160 ASSERT_TRUE(downSequenceNum);
4161 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
4162 ASSERT_TRUE(upSequenceNum);
4163 // Don't finish the events yet, and send a key
4164 // Injection will "succeed" because we will eventually give up and send the key to the focused
4165 // window even if motions are still being processed. But because the injection timeout is short,
4166 // we will receive INJECTION_TIMED_OUT as the result.
4167
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004168 InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004169 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004170 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
4171 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004172 // Key will not be sent to the window, yet, because the window is still processing events
4173 // and the key remains pending, waiting for the touch events to be processed
4174 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
4175 ASSERT_FALSE(keySequenceNum);
4176
4177 std::this_thread::sleep_for(500ms);
4178 // if we wait long enough though, dispatcher will give up, and still send the key
4179 // to the focused window, even though we have not yet finished the motion event
4180 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4181 mWindow->finishEvent(*downSequenceNum);
4182 mWindow->finishEvent(*upSequenceNum);
4183}
4184
4185/**
4186 * If a window is processing a motion event, and then a key event comes in, the key event should
4187 * not go to the focused window until the motion is processed.
4188 * If then a new motion comes in, then the pending key event should be going to the currently
4189 * focused window right away.
4190 */
4191TEST_F(InputDispatcherSingleWindowAnr,
4192 PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) {
4193 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
4194 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
4195
4196 tapOnWindow();
4197 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
4198 ASSERT_TRUE(downSequenceNum);
4199 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
4200 ASSERT_TRUE(upSequenceNum);
4201 // Don't finish the events yet, and send a key
4202 // Injection is async, so it will succeed
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004203 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004204 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004205 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004206 // At this point, key is still pending, and should not be sent to the application yet.
4207 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
4208 ASSERT_FALSE(keySequenceNum);
4209
4210 // Now tap down again. It should cause the pending key to go to the focused window right away.
4211 tapOnWindow();
4212 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd
4213 // the other events yet. We can finish events in any order.
4214 mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN
4215 mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP
4216 mWindow->consumeMotionDown();
4217 mWindow->consumeMotionUp();
4218 mWindow->assertNoEvents();
4219}
4220
4221class InputDispatcherMultiWindowAnr : public InputDispatcherTest {
4222 virtual void SetUp() override {
4223 InputDispatcherTest::SetUp();
4224
Chris Yea209fde2020-07-22 13:54:51 -07004225 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004226 mApplication->setDispatchingTimeout(10ms);
4227 mUnfocusedWindow =
4228 new FakeWindowHandle(mApplication, mDispatcher, "Unfocused", ADISPLAY_ID_DEFAULT);
4229 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
4230 // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this
4231 // window.
4232 // Adding FLAG_WATCH_OUTSIDE_TOUCH to receive ACTION_OUTSIDE when another window is tapped
chaviw3277faf2021-05-19 16:45:23 -05004233 mUnfocusedWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL |
4234 WindowInfo::Flag::WATCH_OUTSIDE_TOUCH |
4235 WindowInfo::Flag::SPLIT_TOUCH);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004236
4237 mFocusedWindow =
4238 new FakeWindowHandle(mApplication, mDispatcher, "Focused", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05004239 mFocusedWindow->setDispatchingTimeout(30ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004240 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
chaviw3277faf2021-05-19 16:45:23 -05004241 mFocusedWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004242
4243 // Set focused application.
4244 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
Vishnu Nair47074b82020-08-14 11:54:47 -07004245 mFocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004246
4247 // Expect one focus window exist in display.
4248 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004249 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004250 mFocusedWindow->consumeFocusEvent(true);
4251 }
4252
4253 virtual void TearDown() override {
4254 InputDispatcherTest::TearDown();
4255
4256 mUnfocusedWindow.clear();
4257 mFocusedWindow.clear();
4258 }
4259
4260protected:
Chris Yea209fde2020-07-22 13:54:51 -07004261 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004262 sp<FakeWindowHandle> mUnfocusedWindow;
4263 sp<FakeWindowHandle> mFocusedWindow;
4264 static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20};
4265 static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75};
4266 static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40};
4267
4268 void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); }
4269
4270 void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); }
4271
4272private:
4273 void tap(const PointF& location) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004274 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004275 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4276 location));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004277 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004278 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4279 location));
4280 }
4281};
4282
4283// If we have 2 windows that are both unresponsive, the one with the shortest timeout
4284// should be ANR'd first.
4285TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004286 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004287 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4288 FOCUSED_WINDOW_LOCATION))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004289 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004290 mFocusedWindow->consumeMotionDown();
4291 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
4292 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
4293 // We consumed all events, so no ANR
4294 ASSERT_TRUE(mDispatcher->waitForIdle());
4295 mFakePolicy->assertNotifyAnrWasNotCalled();
4296
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004297 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004298 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4299 FOCUSED_WINDOW_LOCATION));
4300 std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent();
4301 ASSERT_TRUE(unfocusedSequenceNum);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004302
4303 const std::chrono::duration timeout =
4304 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004305 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004306 // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event
4307 // sequence to make it consistent
4308 mFocusedWindow->consumeMotionCancel();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004309 mUnfocusedWindow->finishEvent(*unfocusedSequenceNum);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004310 mFocusedWindow->consumeMotionDown();
4311 // This cancel is generated because the connection was unresponsive
4312 mFocusedWindow->consumeMotionCancel();
4313 mFocusedWindow->assertNoEvents();
4314 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004315 ASSERT_TRUE(mDispatcher->waitForIdle());
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004316 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004317 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004318}
4319
4320// If we have 2 windows with identical timeouts that are both unresponsive,
4321// it doesn't matter which order they should have ANR.
4322// But we should receive ANR for both.
4323TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) {
4324 // Set the timeout for unfocused window to match the focused window
4325 mUnfocusedWindow->setDispatchingTimeout(10ms);
4326 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
4327
4328 tapOnFocusedWindow();
4329 // we should have ACTION_DOWN/ACTION_UP on focused window and ACTION_OUTSIDE on unfocused window
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004330 sp<IBinder> anrConnectionToken1 = mFakePolicy->getUnresponsiveWindowToken(10ms);
4331 sp<IBinder> anrConnectionToken2 = mFakePolicy->getUnresponsiveWindowToken(0ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004332
4333 // We don't know which window will ANR first. But both of them should happen eventually.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004334 ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 ||
4335 mFocusedWindow->getToken() == anrConnectionToken2);
4336 ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 ||
4337 mUnfocusedWindow->getToken() == anrConnectionToken2);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004338
4339 ASSERT_TRUE(mDispatcher->waitForIdle());
4340 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004341
4342 mFocusedWindow->consumeMotionDown();
4343 mFocusedWindow->consumeMotionUp();
4344 mUnfocusedWindow->consumeMotionOutside();
4345
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004346 sp<IBinder> responsiveToken1 = mFakePolicy->getResponsiveWindowToken();
4347 sp<IBinder> responsiveToken2 = mFakePolicy->getResponsiveWindowToken();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004348
4349 // Both applications should be marked as responsive, in any order
4350 ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 ||
4351 mFocusedWindow->getToken() == responsiveToken2);
4352 ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 ||
4353 mUnfocusedWindow->getToken() == responsiveToken2);
4354 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004355}
4356
4357// If a window is already not responding, the second tap on the same window should be ignored.
4358// We should also log an error to account for the dropped event (not tested here).
4359// At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events.
4360TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) {
4361 tapOnFocusedWindow();
4362 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
4363 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
4364 // Receive the events, but don't respond
4365 std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN
4366 ASSERT_TRUE(downEventSequenceNum);
4367 std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP
4368 ASSERT_TRUE(upEventSequenceNum);
4369 const std::chrono::duration timeout =
4370 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004371 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004372
4373 // Tap once again
4374 // We cannot use "tapOnFocusedWindow" because it asserts the injection result to be success
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004375 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004376 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4377 FOCUSED_WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004378 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004379 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4380 FOCUSED_WINDOW_LOCATION));
4381 // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a
4382 // valid touch target
4383 mUnfocusedWindow->assertNoEvents();
4384
4385 // Consume the first tap
4386 mFocusedWindow->finishEvent(*downEventSequenceNum);
4387 mFocusedWindow->finishEvent(*upEventSequenceNum);
4388 ASSERT_TRUE(mDispatcher->waitForIdle());
4389 // The second tap did not go to the focused window
4390 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004391 // Since all events are finished, connection should be deemed healthy again
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004392 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004393 mFakePolicy->assertNotifyAnrWasNotCalled();
4394}
4395
4396// If you tap outside of all windows, there will not be ANR
4397TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004398 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004399 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4400 LOCATION_OUTSIDE_ALL_WINDOWS));
4401 ASSERT_TRUE(mDispatcher->waitForIdle());
4402 mFakePolicy->assertNotifyAnrWasNotCalled();
4403}
4404
4405// Since the focused window is paused, tapping on it should not produce any events
4406TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) {
4407 mFocusedWindow->setPaused(true);
4408 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
4409
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004410 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004411 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4412 FOCUSED_WINDOW_LOCATION));
4413
4414 std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT));
4415 ASSERT_TRUE(mDispatcher->waitForIdle());
4416 // Should not ANR because the window is paused, and touches shouldn't go to it
4417 mFakePolicy->assertNotifyAnrWasNotCalled();
4418
4419 mFocusedWindow->assertNoEvents();
4420 mUnfocusedWindow->assertNoEvents();
4421}
4422
4423/**
4424 * If a window is processing a motion event, and then a key event comes in, the key event should
4425 * not to to the focused window until the motion is processed.
4426 * If a different window becomes focused at this time, the key should go to that window instead.
4427 *
4428 * Warning!!!
4429 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
4430 * and the injection timeout that we specify when injecting the key.
4431 * We must have the injection timeout (10ms) be smaller than
4432 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
4433 *
4434 * If that value changes, this test should also change.
4435 */
4436TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) {
4437 // Set a long ANR timeout to prevent it from triggering
4438 mFocusedWindow->setDispatchingTimeout(2s);
4439 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
4440
4441 tapOnUnfocusedWindow();
4442 std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent();
4443 ASSERT_TRUE(downSequenceNum);
4444 std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent();
4445 ASSERT_TRUE(upSequenceNum);
4446 // Don't finish the events yet, and send a key
4447 // Injection will succeed because we will eventually give up and send the key to the focused
4448 // window even if motions are still being processed.
4449
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004450 InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004451 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004452 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/);
4453 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004454 // Key will not be sent to the window, yet, because the window is still processing events
4455 // and the key remains pending, waiting for the touch events to be processed
4456 std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent();
4457 ASSERT_FALSE(keySequenceNum);
4458
4459 // Switch the focus to the "unfocused" window that we tapped. Expect the key to go there
Vishnu Nair47074b82020-08-14 11:54:47 -07004460 mFocusedWindow->setFocusable(false);
4461 mUnfocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004462 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004463 setFocusedWindow(mUnfocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004464
4465 // Focus events should precede the key events
4466 mUnfocusedWindow->consumeFocusEvent(true);
4467 mFocusedWindow->consumeFocusEvent(false);
4468
4469 // Finish the tap events, which should unblock dispatcher
4470 mUnfocusedWindow->finishEvent(*downSequenceNum);
4471 mUnfocusedWindow->finishEvent(*upSequenceNum);
4472
4473 // Now that all queues are cleared and no backlog in the connections, the key event
4474 // can finally go to the newly focused "mUnfocusedWindow".
4475 mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4476 mFocusedWindow->assertNoEvents();
4477 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004478 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004479}
4480
4481// When the touch stream is split across 2 windows, and one of them does not respond,
4482// then ANR should be raised and the touch should be canceled for the unresponsive window.
4483// The other window should not be affected by that.
4484TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) {
4485 // Touch Window 1
4486 NotifyMotionArgs motionArgs =
4487 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4488 ADISPLAY_ID_DEFAULT, {FOCUSED_WINDOW_LOCATION});
4489 mDispatcher->notifyMotion(&motionArgs);
4490 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
4491 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
4492
4493 // Touch Window 2
4494 int32_t actionPointerDown =
4495 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
4496
4497 motionArgs =
4498 generateMotionArgs(actionPointerDown, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4499 {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION});
4500 mDispatcher->notifyMotion(&motionArgs);
4501
4502 const std::chrono::duration timeout =
4503 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004504 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004505
4506 mUnfocusedWindow->consumeMotionDown();
4507 mFocusedWindow->consumeMotionDown();
4508 // Focused window may or may not receive ACTION_MOVE
4509 // But it should definitely receive ACTION_CANCEL due to the ANR
4510 InputEvent* event;
4511 std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event);
4512 ASSERT_TRUE(moveOrCancelSequenceNum);
4513 mFocusedWindow->finishEvent(*moveOrCancelSequenceNum);
4514 ASSERT_NE(nullptr, event);
4515 ASSERT_EQ(event->getType(), AINPUT_EVENT_TYPE_MOTION);
4516 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
4517 if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) {
4518 mFocusedWindow->consumeMotionCancel();
4519 } else {
4520 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction());
4521 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004522 ASSERT_TRUE(mDispatcher->waitForIdle());
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004523 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004524
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004525 mUnfocusedWindow->assertNoEvents();
4526 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004527 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004528}
4529
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05004530/**
4531 * If we have no focused window, and a key comes in, we start the ANR timer.
4532 * The focused application should add a focused window before the timer runs out to prevent ANR.
4533 *
4534 * If the user touches another application during this time, the key should be dropped.
4535 * Next, if a new focused window comes in, without toggling the focused application,
4536 * then no ANR should occur.
4537 *
4538 * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication',
4539 * but in some cases the policy may not update the focused application.
4540 */
4541TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) {
4542 std::shared_ptr<FakeApplicationHandle> focusedApplication =
4543 std::make_shared<FakeApplicationHandle>();
4544 focusedApplication->setDispatchingTimeout(60ms);
4545 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication);
4546 // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused.
4547 mFocusedWindow->setFocusable(false);
4548
4549 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
4550 mFocusedWindow->consumeFocusEvent(false);
4551
4552 // Send a key. The ANR timer should start because there is no focused window.
4553 // 'focusedApplication' will get blamed if this timer completes.
4554 // Key will not be sent anywhere because we have no focused window. It will remain pending.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004555 InputEventInjectionResult result =
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05004556 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08004557 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/,
4558 false /* allowKeyRepeat */);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004559 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05004560
4561 // Wait until dispatcher starts the "no focused window" timer. If we don't wait here,
4562 // then the injected touches won't cause the focused event to get dropped.
4563 // The dispatcher only checks for whether the queue should be pruned upon queueing.
4564 // If we inject the touch right away and the ANR timer hasn't started, the touch event would
4565 // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'.
4566 // For this test, it means that the key would get delivered to the window once it becomes
4567 // focused.
4568 std::this_thread::sleep_for(10ms);
4569
4570 // Touch unfocused window. This should force the pending key to get dropped.
4571 NotifyMotionArgs motionArgs =
4572 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4573 ADISPLAY_ID_DEFAULT, {UNFOCUSED_WINDOW_LOCATION});
4574 mDispatcher->notifyMotion(&motionArgs);
4575
4576 // We do not consume the motion right away, because that would require dispatcher to first
4577 // process (== drop) the key event, and by that time, ANR will be raised.
4578 // Set the focused window first.
4579 mFocusedWindow->setFocusable(true);
4580 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
4581 setFocusedWindow(mFocusedWindow);
4582 mFocusedWindow->consumeFocusEvent(true);
4583 // We do not call "setFocusedApplication" here, even though the newly focused window belongs
4584 // to another application. This could be a bug / behaviour in the policy.
4585
4586 mUnfocusedWindow->consumeMotionDown();
4587
4588 ASSERT_TRUE(mDispatcher->waitForIdle());
4589 // Should not ANR because we actually have a focused window. It was just added too slowly.
4590 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled());
4591}
4592
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05004593// These tests ensure we cannot send touch events to a window that's positioned behind a window
4594// that has feature NO_INPUT_CHANNEL.
4595// Layout:
4596// Top (closest to user)
4597// mNoInputWindow (above all windows)
4598// mBottomWindow
4599// Bottom (furthest from user)
4600class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest {
4601 virtual void SetUp() override {
4602 InputDispatcherTest::SetUp();
4603
4604 mApplication = std::make_shared<FakeApplicationHandle>();
4605 mNoInputWindow = new FakeWindowHandle(mApplication, mDispatcher,
4606 "Window without input channel", ADISPLAY_ID_DEFAULT,
4607 std::make_optional<sp<IBinder>>(nullptr) /*token*/);
4608
chaviw3277faf2021-05-19 16:45:23 -05004609 mNoInputWindow->setInputFeatures(WindowInfo::Feature::NO_INPUT_CHANNEL);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05004610 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
4611 // It's perfectly valid for this window to not have an associated input channel
4612
4613 mBottomWindow = new FakeWindowHandle(mApplication, mDispatcher, "Bottom window",
4614 ADISPLAY_ID_DEFAULT);
4615 mBottomWindow->setFrame(Rect(0, 0, 100, 100));
4616
4617 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
4618 }
4619
4620protected:
4621 std::shared_ptr<FakeApplicationHandle> mApplication;
4622 sp<FakeWindowHandle> mNoInputWindow;
4623 sp<FakeWindowHandle> mBottomWindow;
4624};
4625
4626TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) {
4627 PointF touchedPoint = {10, 10};
4628
4629 NotifyMotionArgs motionArgs =
4630 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4631 ADISPLAY_ID_DEFAULT, {touchedPoint});
4632 mDispatcher->notifyMotion(&motionArgs);
4633
4634 mNoInputWindow->assertNoEvents();
4635 // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have
4636 // an input channel, it is not marked as FLAG_NOT_TOUCHABLE,
4637 // and therefore should prevent mBottomWindow from receiving touches
4638 mBottomWindow->assertNoEvents();
4639}
4640
4641/**
4642 * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel,
4643 * ensure that this window does not receive any touches, and blocks touches to windows underneath.
4644 */
4645TEST_F(InputDispatcherMultiWindowOcclusionTests,
4646 NoInputChannelFeature_DropsTouchesWithValidChannel) {
4647 mNoInputWindow = new FakeWindowHandle(mApplication, mDispatcher,
4648 "Window with input channel and NO_INPUT_CHANNEL",
4649 ADISPLAY_ID_DEFAULT);
4650
chaviw3277faf2021-05-19 16:45:23 -05004651 mNoInputWindow->setInputFeatures(WindowInfo::Feature::NO_INPUT_CHANNEL);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05004652 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
4653 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
4654
4655 PointF touchedPoint = {10, 10};
4656
4657 NotifyMotionArgs motionArgs =
4658 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4659 ADISPLAY_ID_DEFAULT, {touchedPoint});
4660 mDispatcher->notifyMotion(&motionArgs);
4661
4662 mNoInputWindow->assertNoEvents();
4663 mBottomWindow->assertNoEvents();
4664}
4665
Vishnu Nair958da932020-08-21 17:12:37 -07004666class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest {
4667protected:
4668 std::shared_ptr<FakeApplicationHandle> mApp;
4669 sp<FakeWindowHandle> mWindow;
4670 sp<FakeWindowHandle> mMirror;
4671
4672 virtual void SetUp() override {
4673 InputDispatcherTest::SetUp();
4674 mApp = std::make_shared<FakeApplicationHandle>();
4675 mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
4676 mMirror = new FakeWindowHandle(mApp, mDispatcher, "TestWindowMirror", ADISPLAY_ID_DEFAULT,
4677 mWindow->getToken());
4678 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
4679 mWindow->setFocusable(true);
4680 mMirror->setFocusable(true);
4681 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4682 }
4683};
4684
4685TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) {
4686 // Request focus on a mirrored window
4687 setFocusedWindow(mMirror);
4688
4689 // window gets focused
4690 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004691 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4692 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004693 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
4694}
4695
4696// A focused & mirrored window remains focused only if the window and its mirror are both
4697// focusable.
4698TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) {
4699 setFocusedWindow(mMirror);
4700
4701 // window gets focused
4702 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004703 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4704 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004705 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004706 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
4707 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004708 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
4709
4710 mMirror->setFocusable(false);
4711 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4712
4713 // window loses focus since one of the windows associated with the token in not focusable
4714 mWindow->consumeFocusEvent(false);
4715
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004716 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
4717 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07004718 mWindow->assertNoEvents();
4719}
4720
4721// A focused & mirrored window remains focused until the window and its mirror both become
4722// invisible.
4723TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) {
4724 setFocusedWindow(mMirror);
4725
4726 // window gets focused
4727 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004728 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4729 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004730 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004731 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
4732 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004733 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
4734
4735 mMirror->setVisible(false);
4736 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4737
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004738 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4739 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004740 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004741 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
4742 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004743 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
4744
4745 mWindow->setVisible(false);
4746 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4747
4748 // window loses focus only after all windows associated with the token become invisible.
4749 mWindow->consumeFocusEvent(false);
4750
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004751 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
4752 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07004753 mWindow->assertNoEvents();
4754}
4755
4756// A focused & mirrored window remains focused until both windows are removed.
4757TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) {
4758 setFocusedWindow(mMirror);
4759
4760 // window gets focused
4761 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004762 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4763 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004764 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004765 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
4766 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004767 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
4768
4769 // single window is removed but the window token remains focused
4770 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}});
4771
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004772 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4773 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004774 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004775 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
4776 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004777 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
4778
4779 // Both windows are removed
4780 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
4781 mWindow->consumeFocusEvent(false);
4782
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004783 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
4784 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07004785 mWindow->assertNoEvents();
4786}
4787
4788// Focus request can be pending until one window becomes visible.
4789TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) {
4790 // Request focus on an invisible mirror.
4791 mWindow->setVisible(false);
4792 mMirror->setVisible(false);
4793 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4794 setFocusedWindow(mMirror);
4795
4796 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004797 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Vishnu Nair958da932020-08-21 17:12:37 -07004798 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004799 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07004800
4801 mMirror->setVisible(true);
4802 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4803
4804 // window gets focused
4805 mWindow->consumeFocusEvent(true);
4806 // window gets the pending key event
4807 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4808}
Prabir Pradhan99987712020-11-10 18:43:05 -08004809
4810class InputDispatcherPointerCaptureTests : public InputDispatcherTest {
4811protected:
4812 std::shared_ptr<FakeApplicationHandle> mApp;
4813 sp<FakeWindowHandle> mWindow;
4814 sp<FakeWindowHandle> mSecondWindow;
4815
4816 void SetUp() override {
4817 InputDispatcherTest::SetUp();
4818 mApp = std::make_shared<FakeApplicationHandle>();
4819 mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
4820 mWindow->setFocusable(true);
4821 mSecondWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
4822 mSecondWindow->setFocusable(true);
4823
4824 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
4825 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}});
4826
4827 setFocusedWindow(mWindow);
4828 mWindow->consumeFocusEvent(true);
4829 }
4830
Prabir Pradhanac483a62021-08-06 14:01:18 +00004831 void notifyPointerCaptureChanged(const PointerCaptureRequest& request) {
4832 const NotifyPointerCaptureChangedArgs args = generatePointerCaptureChangedArgs(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08004833 mDispatcher->notifyPointerCaptureChanged(&args);
4834 }
4835
Prabir Pradhanac483a62021-08-06 14:01:18 +00004836 PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window,
4837 bool enabled) {
Prabir Pradhan99987712020-11-10 18:43:05 -08004838 mDispatcher->requestPointerCapture(window->getToken(), enabled);
Prabir Pradhanac483a62021-08-06 14:01:18 +00004839 auto request = mFakePolicy->assertSetPointerCaptureCalled(enabled);
4840 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08004841 window->consumeCaptureEvent(enabled);
Prabir Pradhanac483a62021-08-06 14:01:18 +00004842 return request;
Prabir Pradhan99987712020-11-10 18:43:05 -08004843 }
4844};
4845
4846TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) {
4847 // Ensure that capture cannot be obtained for unfocused windows.
4848 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
4849 mFakePolicy->assertSetPointerCaptureNotCalled();
4850 mSecondWindow->assertNoEvents();
4851
4852 // Ensure that capture can be enabled from the focus window.
4853 requestAndVerifyPointerCapture(mWindow, true);
4854
4855 // Ensure that capture cannot be disabled from a window that does not have capture.
4856 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false);
4857 mFakePolicy->assertSetPointerCaptureNotCalled();
4858
4859 // Ensure that capture can be disabled from the window with capture.
4860 requestAndVerifyPointerCapture(mWindow, false);
4861}
4862
4863TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) {
Prabir Pradhanac483a62021-08-06 14:01:18 +00004864 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08004865
4866 setFocusedWindow(mSecondWindow);
4867
4868 // Ensure that the capture disabled event was sent first.
4869 mWindow->consumeCaptureEvent(false);
4870 mWindow->consumeFocusEvent(false);
4871 mSecondWindow->consumeFocusEvent(true);
Prabir Pradhanac483a62021-08-06 14:01:18 +00004872 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08004873
4874 // Ensure that additional state changes from InputReader are not sent to the window.
Prabir Pradhanac483a62021-08-06 14:01:18 +00004875 notifyPointerCaptureChanged({});
4876 notifyPointerCaptureChanged(request);
4877 notifyPointerCaptureChanged({});
Prabir Pradhan99987712020-11-10 18:43:05 -08004878 mWindow->assertNoEvents();
4879 mSecondWindow->assertNoEvents();
4880 mFakePolicy->assertSetPointerCaptureNotCalled();
4881}
4882
4883TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) {
Prabir Pradhanac483a62021-08-06 14:01:18 +00004884 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08004885
4886 // InputReader unexpectedly disables and enables pointer capture.
Prabir Pradhanac483a62021-08-06 14:01:18 +00004887 notifyPointerCaptureChanged({});
4888 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08004889
4890 // Ensure that Pointer Capture is disabled.
Prabir Pradhanac483a62021-08-06 14:01:18 +00004891 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08004892 mWindow->consumeCaptureEvent(false);
4893 mWindow->assertNoEvents();
4894}
4895
Prabir Pradhan167e6d92021-02-04 16:18:17 -08004896TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) {
4897 requestAndVerifyPointerCapture(mWindow, true);
4898
4899 // The first window loses focus.
4900 setFocusedWindow(mSecondWindow);
Prabir Pradhanac483a62021-08-06 14:01:18 +00004901 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08004902 mWindow->consumeCaptureEvent(false);
4903
4904 // Request Pointer Capture from the second window before the notification from InputReader
4905 // arrives.
4906 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
Prabir Pradhanac483a62021-08-06 14:01:18 +00004907 auto request = mFakePolicy->assertSetPointerCaptureCalled(true);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08004908
4909 // InputReader notifies Pointer Capture was disabled (because of the focus change).
Prabir Pradhanac483a62021-08-06 14:01:18 +00004910 notifyPointerCaptureChanged({});
Prabir Pradhan167e6d92021-02-04 16:18:17 -08004911
4912 // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request).
Prabir Pradhanac483a62021-08-06 14:01:18 +00004913 notifyPointerCaptureChanged(request);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08004914
4915 mSecondWindow->consumeFocusEvent(true);
4916 mSecondWindow->consumeCaptureEvent(true);
4917}
4918
Prabir Pradhanac483a62021-08-06 14:01:18 +00004919TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) {
4920 // App repeatedly enables and disables capture.
4921 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
4922 auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
4923 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
4924 mFakePolicy->assertSetPointerCaptureCalled(false);
4925 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
4926 auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
4927
4928 // InputReader notifies that PointerCapture has been enabled for the first request. Since the
4929 // first request is now stale, this should do nothing.
4930 notifyPointerCaptureChanged(firstRequest);
4931 mWindow->assertNoEvents();
4932
4933 // InputReader notifies that the second request was enabled.
4934 notifyPointerCaptureChanged(secondRequest);
4935 mWindow->consumeCaptureEvent(true);
4936}
4937
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004938class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest {
4939protected:
4940 constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8;
Bernardo Rufino7393d172021-02-26 13:56:11 +00004941
4942 constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9;
4943 static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY);
4944
4945 constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7;
4946 static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
4947
4948 // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold
4949 constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5;
4950 static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
4951 static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) <
4952 MAXIMUM_OBSCURING_OPACITY);
4953
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004954 static const int32_t TOUCHED_APP_UID = 10001;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004955 static const int32_t APP_B_UID = 10002;
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004956 static const int32_t APP_C_UID = 10003;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004957
4958 sp<FakeWindowHandle> mTouchWindow;
4959
4960 virtual void SetUp() override {
4961 InputDispatcherTest::SetUp();
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004962 mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched");
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004963 mDispatcher->setBlockUntrustedTouchesMode(android::os::BlockUntrustedTouchesMode::BLOCK);
4964 mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY);
4965 }
4966
4967 virtual void TearDown() override {
4968 InputDispatcherTest::TearDown();
4969 mTouchWindow.clear();
4970 }
4971
chaviw3277faf2021-05-19 16:45:23 -05004972 sp<FakeWindowHandle> getOccludingWindow(int32_t uid, std::string name, TouchOcclusionMode mode,
4973 float alpha = 1.0f) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004974 sp<FakeWindowHandle> window = getWindow(uid, name);
chaviw3277faf2021-05-19 16:45:23 -05004975 window->setFlags(WindowInfo::Flag::NOT_TOUCHABLE);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004976 window->setTouchOcclusionMode(mode);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004977 window->setAlpha(alpha);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004978 return window;
4979 }
4980
4981 sp<FakeWindowHandle> getWindow(int32_t uid, std::string name) {
4982 std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>();
4983 sp<FakeWindowHandle> window =
4984 new FakeWindowHandle(app, mDispatcher, name, ADISPLAY_ID_DEFAULT);
4985 // Generate an arbitrary PID based on the UID
4986 window->setOwnerInfo(1777 + (uid % 10000), uid);
4987 return window;
4988 }
4989
4990 void touch(const std::vector<PointF>& points = {PointF{100, 200}}) {
4991 NotifyMotionArgs args =
4992 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4993 ADISPLAY_ID_DEFAULT, points);
4994 mDispatcher->notifyMotion(&args);
4995 }
4996};
4997
4998TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004999 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00005000 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00005001 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00005002
5003 touch();
5004
5005 mTouchWindow->assertNoEvents();
5006}
5007
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00005008TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufino7393d172021-02-26 13:56:11 +00005009 WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) {
5010 const sp<FakeWindowHandle>& w =
5011 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f);
5012 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
5013
5014 touch();
5015
5016 mTouchWindow->assertNoEvents();
5017}
5018
5019TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00005020 WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) {
5021 const sp<FakeWindowHandle>& w =
5022 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
5023 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
5024
5025 touch();
5026
5027 w->assertNoEvents();
5028}
5029
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00005030TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00005031 const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW);
5032 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00005033
5034 touch();
5035
5036 mTouchWindow->consumeAnyMotionDown();
5037}
5038
5039TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00005040 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00005041 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00005042 w->setFrame(Rect(0, 0, 50, 50));
5043 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00005044
5045 touch({PointF{100, 100}});
5046
5047 mTouchWindow->consumeAnyMotionDown();
5048}
5049
5050TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00005051 const sp<FakeWindowHandle>& w =
Bernardo Rufino6d52e542021-02-15 18:38:10 +00005052 getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00005053 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
5054
5055 touch();
5056
5057 mTouchWindow->consumeAnyMotionDown();
5058}
5059
5060TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) {
5061 const sp<FakeWindowHandle>& w =
5062 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
5063 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00005064
5065 touch();
5066
5067 mTouchWindow->consumeAnyMotionDown();
5068}
5069
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00005070TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) {
5071 const sp<FakeWindowHandle>& w =
5072 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
5073 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
5074
5075 touch();
5076
5077 w->assertNoEvents();
5078}
5079
5080/**
5081 * This is important to make sure apps can't indirectly learn the position of touches (outside vs
5082 * inside) while letting them pass-through. Note that even though touch passes through the occluding
5083 * window, the occluding window will still receive ACTION_OUTSIDE event.
5084 */
5085TEST_F(InputDispatcherUntrustedTouchesTest,
5086 WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) {
5087 const sp<FakeWindowHandle>& w =
5088 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
chaviw3277faf2021-05-19 16:45:23 -05005089 w->addFlags(WindowInfo::Flag::WATCH_OUTSIDE_TOUCH);
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00005090 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
5091
5092 touch();
5093
5094 w->consumeMotionOutside();
5095}
5096
5097TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) {
5098 const sp<FakeWindowHandle>& w =
5099 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
chaviw3277faf2021-05-19 16:45:23 -05005100 w->addFlags(WindowInfo::Flag::WATCH_OUTSIDE_TOUCH);
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00005101 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
5102
5103 touch();
5104
5105 InputEvent* event = w->consume();
5106 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType());
5107 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
5108 EXPECT_EQ(0.0f, motionEvent.getRawPointerCoords(0)->getX());
5109 EXPECT_EQ(0.0f, motionEvent.getRawPointerCoords(0)->getY());
5110}
5111
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00005112TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00005113 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00005114 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
5115 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00005116 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
5117
5118 touch();
5119
5120 mTouchWindow->consumeAnyMotionDown();
5121}
5122
5123TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) {
5124 const sp<FakeWindowHandle>& w =
5125 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
5126 MAXIMUM_OBSCURING_OPACITY);
5127 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00005128
5129 touch();
5130
5131 mTouchWindow->consumeAnyMotionDown();
5132}
5133
5134TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00005135 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00005136 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
5137 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00005138 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
5139
5140 touch();
5141
5142 mTouchWindow->assertNoEvents();
5143}
5144
5145TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) {
5146 // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91
5147 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00005148 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
5149 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00005150 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00005151 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
5152 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00005153 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
5154
5155 touch();
5156
5157 mTouchWindow->assertNoEvents();
5158}
5159
5160TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) {
5161 // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75
5162 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00005163 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
5164 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00005165 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00005166 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
5167 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00005168 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
5169
5170 touch();
5171
5172 mTouchWindow->consumeAnyMotionDown();
5173}
5174
5175TEST_F(InputDispatcherUntrustedTouchesTest,
5176 WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) {
5177 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00005178 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
5179 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00005180 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00005181 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
5182 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00005183 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
5184
5185 touch();
5186
5187 mTouchWindow->consumeAnyMotionDown();
5188}
5189
5190TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) {
5191 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00005192 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
5193 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00005194 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00005195 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
5196 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00005197 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00005198
5199 touch();
5200
5201 mTouchWindow->assertNoEvents();
5202}
5203
Bernardo Rufino6d52e542021-02-15 18:38:10 +00005204TEST_F(InputDispatcherUntrustedTouchesTest,
5205 WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) {
5206 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00005207 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
5208 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00005209 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00005210 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
5211 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00005212 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
5213
5214 touch();
5215
5216 mTouchWindow->assertNoEvents();
5217}
5218
5219TEST_F(InputDispatcherUntrustedTouchesTest,
5220 WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) {
5221 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00005222 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
5223 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00005224 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00005225 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
5226 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00005227 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
5228
5229 touch();
5230
5231 mTouchWindow->consumeAnyMotionDown();
5232}
5233
5234TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) {
5235 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00005236 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
5237 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00005238 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
5239
5240 touch();
5241
5242 mTouchWindow->consumeAnyMotionDown();
5243}
5244
5245TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) {
5246 const sp<FakeWindowHandle>& w =
5247 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED);
5248 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
5249
5250 touch();
5251
5252 mTouchWindow->consumeAnyMotionDown();
5253}
5254
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00005255TEST_F(InputDispatcherUntrustedTouchesTest,
5256 OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) {
5257 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
5258 const sp<FakeWindowHandle>& w =
5259 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f);
5260 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
5261
5262 touch();
5263
5264 mTouchWindow->assertNoEvents();
5265}
5266
5267TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) {
5268 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
5269 const sp<FakeWindowHandle>& w =
5270 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f);
5271 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
5272
5273 touch();
5274
5275 mTouchWindow->consumeAnyMotionDown();
5276}
5277
5278TEST_F(InputDispatcherUntrustedTouchesTest,
5279 OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) {
5280 mDispatcher->setMaximumObscuringOpacityForTouch(1.0f);
5281 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00005282 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
5283 OPACITY_ABOVE_THRESHOLD);
5284 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
5285
5286 touch();
5287
5288 mTouchWindow->consumeAnyMotionDown();
5289}
5290
5291TEST_F(InputDispatcherUntrustedTouchesTest,
5292 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) {
5293 const sp<FakeWindowHandle>& w1 =
5294 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
5295 OPACITY_BELOW_THRESHOLD);
5296 const sp<FakeWindowHandle>& w2 =
5297 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
5298 OPACITY_BELOW_THRESHOLD);
5299 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
5300
5301 touch();
5302
5303 mTouchWindow->assertNoEvents();
5304}
5305
5306/**
5307 * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the
5308 * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold
5309 * (which alone would result in allowing touches) does not affect the blocking behavior.
5310 */
5311TEST_F(InputDispatcherUntrustedTouchesTest,
5312 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) {
5313 const sp<FakeWindowHandle>& wB =
5314 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
5315 OPACITY_BELOW_THRESHOLD);
5316 const sp<FakeWindowHandle>& wC =
5317 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
5318 OPACITY_BELOW_THRESHOLD);
5319 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
5320
5321 touch();
5322
5323 mTouchWindow->assertNoEvents();
5324}
5325
5326/**
5327 * This test is testing that a window from a different UID but with same application token doesn't
5328 * block the touch. Apps can share the application token for close UI collaboration for example.
5329 */
5330TEST_F(InputDispatcherUntrustedTouchesTest,
5331 WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) {
5332 const sp<FakeWindowHandle>& w =
5333 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
5334 w->setApplicationToken(mTouchWindow->getApplicationToken());
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00005335 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
5336
5337 touch();
5338
5339 mTouchWindow->consumeAnyMotionDown();
5340}
5341
arthurhungb89ccb02020-12-30 16:19:01 +08005342class InputDispatcherDragTests : public InputDispatcherTest {
5343protected:
5344 std::shared_ptr<FakeApplicationHandle> mApp;
5345 sp<FakeWindowHandle> mWindow;
5346 sp<FakeWindowHandle> mSecondWindow;
5347 sp<FakeWindowHandle> mDragWindow;
5348
5349 void SetUp() override {
5350 InputDispatcherTest::SetUp();
5351 mApp = std::make_shared<FakeApplicationHandle>();
5352 mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
5353 mWindow->setFrame(Rect(0, 0, 100, 100));
chaviw3277faf2021-05-19 16:45:23 -05005354 mWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
arthurhungb89ccb02020-12-30 16:19:01 +08005355
5356 mSecondWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
5357 mSecondWindow->setFrame(Rect(100, 0, 200, 100));
chaviw3277faf2021-05-19 16:45:23 -05005358 mSecondWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
arthurhungb89ccb02020-12-30 16:19:01 +08005359
5360 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
5361 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}});
5362 }
5363
5364 // Start performing drag, we will create a drag window and transfer touch to it.
5365 void performDrag() {
5366 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5367 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5368 {50, 50}))
5369 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5370
5371 // Window should receive motion event.
5372 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5373
5374 // The drag window covers the entire display
5375 mDragWindow = new FakeWindowHandle(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT);
5376 mDispatcher->setInputWindows(
5377 {{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}});
5378
5379 // Transfer touch focus to the drag window
5380 mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(),
5381 true /* isDragDrop */);
5382 mWindow->consumeMotionCancel();
5383 mDragWindow->consumeMotionDown();
5384 }
arthurhung6d4bed92021-03-17 11:59:33 +08005385
5386 // Start performing drag, we will create a drag window and transfer touch to it.
5387 void performStylusDrag() {
5388 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5389 injectMotionEvent(mDispatcher,
5390 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
5391 AINPUT_SOURCE_STYLUS)
5392 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
5393 .pointer(PointerBuilder(0,
5394 AMOTION_EVENT_TOOL_TYPE_STYLUS)
5395 .x(50)
5396 .y(50))
5397 .build()));
5398 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5399
5400 // The drag window covers the entire display
5401 mDragWindow = new FakeWindowHandle(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT);
5402 mDispatcher->setInputWindows(
5403 {{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}});
5404
5405 // Transfer touch focus to the drag window
5406 mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(),
5407 true /* isDragDrop */);
5408 mWindow->consumeMotionCancel();
5409 mDragWindow->consumeMotionDown();
5410 }
arthurhungb89ccb02020-12-30 16:19:01 +08005411};
5412
5413TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) {
5414 performDrag();
5415
5416 // Move on window.
5417 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5418 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5419 ADISPLAY_ID_DEFAULT, {50, 50}))
5420 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5421 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5422 mWindow->consumeDragEvent(false, 50, 50);
5423 mSecondWindow->assertNoEvents();
5424
5425 // Move to another window.
5426 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5427 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5428 ADISPLAY_ID_DEFAULT, {150, 50}))
5429 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5430 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5431 mWindow->consumeDragEvent(true, 150, 50);
5432 mSecondWindow->consumeDragEvent(false, 50, 50);
5433
5434 // Move back to original window.
5435 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5436 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5437 ADISPLAY_ID_DEFAULT, {50, 50}))
5438 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5439 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5440 mWindow->consumeDragEvent(false, 50, 50);
5441 mSecondWindow->consumeDragEvent(true, -50, 50);
5442
5443 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5444 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {50, 50}))
5445 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5446 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
5447 mWindow->assertNoEvents();
5448 mSecondWindow->assertNoEvents();
5449}
5450
arthurhungf452d0b2021-01-06 00:19:52 +08005451TEST_F(InputDispatcherDragTests, DragAndDrop) {
5452 performDrag();
5453
5454 // Move on window.
5455 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5456 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5457 ADISPLAY_ID_DEFAULT, {50, 50}))
5458 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5459 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5460 mWindow->consumeDragEvent(false, 50, 50);
5461 mSecondWindow->assertNoEvents();
5462
5463 // Move to another window.
5464 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5465 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5466 ADISPLAY_ID_DEFAULT, {150, 50}))
5467 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5468 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5469 mWindow->consumeDragEvent(true, 150, 50);
5470 mSecondWindow->consumeDragEvent(false, 50, 50);
5471
5472 // drop to another window.
5473 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5474 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5475 {150, 50}))
5476 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5477 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
5478 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
5479 mWindow->assertNoEvents();
5480 mSecondWindow->assertNoEvents();
5481}
5482
arthurhung6d4bed92021-03-17 11:59:33 +08005483TEST_F(InputDispatcherDragTests, StylusDragAndDrop) {
5484 performStylusDrag();
5485
5486 // Move on window and keep button pressed.
5487 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5488 injectMotionEvent(mDispatcher,
5489 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
5490 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
5491 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS)
5492 .x(50)
5493 .y(50))
5494 .build()))
5495 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5496 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5497 mWindow->consumeDragEvent(false, 50, 50);
5498 mSecondWindow->assertNoEvents();
5499
5500 // Move to another window and release button, expect to drop item.
5501 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5502 injectMotionEvent(mDispatcher,
5503 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
5504 .buttonState(0)
5505 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS)
5506 .x(150)
5507 .y(50))
5508 .build()))
5509 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5510 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5511 mWindow->assertNoEvents();
5512 mSecondWindow->assertNoEvents();
5513 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
5514
5515 // nothing to the window.
5516 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5517 injectMotionEvent(mDispatcher,
5518 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS)
5519 .buttonState(0)
5520 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS)
5521 .x(150)
5522 .y(50))
5523 .build()))
5524 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5525 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
5526 mWindow->assertNoEvents();
5527 mSecondWindow->assertNoEvents();
5528}
5529
Arthur Hung6d0571e2021-04-09 20:18:16 +08005530TEST_F(InputDispatcherDragTests, DragAndDrop_InvalidWindow) {
5531 performDrag();
5532
5533 // Set second window invisible.
5534 mSecondWindow->setVisible(false);
5535 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}});
5536
5537 // Move on window.
5538 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5539 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5540 ADISPLAY_ID_DEFAULT, {50, 50}))
5541 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5542 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5543 mWindow->consumeDragEvent(false, 50, 50);
5544 mSecondWindow->assertNoEvents();
5545
5546 // Move to another window.
5547 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5548 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5549 ADISPLAY_ID_DEFAULT, {150, 50}))
5550 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5551 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5552 mWindow->consumeDragEvent(true, 150, 50);
5553 mSecondWindow->assertNoEvents();
5554
5555 // drop to another window.
5556 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5557 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5558 {150, 50}))
5559 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5560 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
5561 mFakePolicy->assertDropTargetEquals(nullptr);
5562 mWindow->assertNoEvents();
5563 mSecondWindow->assertNoEvents();
5564}
5565
Vishnu Nair41f77b82021-09-03 16:07:44 -07005566class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {};
5567
5568TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) {
5569 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5570 sp<FakeWindowHandle> window =
5571 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
5572 window->setInputFeatures(WindowInfo::Feature::DROP_INPUT);
5573 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5574 window->setFocusable(true);
5575 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5576 setFocusedWindow(window);
5577 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
5578
5579 // With the flag set, window should not get any input
5580 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
5581 mDispatcher->notifyKey(&keyArgs);
5582 window->assertNoEvents();
5583
5584 NotifyMotionArgs motionArgs =
5585 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5586 ADISPLAY_ID_DEFAULT);
5587 mDispatcher->notifyMotion(&motionArgs);
5588 window->assertNoEvents();
5589
5590 // With the flag cleared, the window should get input
5591 window->setInputFeatures({});
5592 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5593
5594 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
5595 mDispatcher->notifyKey(&keyArgs);
5596 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
5597
5598 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5599 ADISPLAY_ID_DEFAULT);
5600 mDispatcher->notifyMotion(&motionArgs);
5601 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5602 window->assertNoEvents();
5603}
5604
5605TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) {
5606 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
5607 std::make_shared<FakeApplicationHandle>();
5608 sp<FakeWindowHandle> obscuringWindow =
5609 new FakeWindowHandle(obscuringApplication, mDispatcher, "obscuringWindow",
5610 ADISPLAY_ID_DEFAULT);
5611 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
5612 obscuringWindow->setOwnerInfo(111, 111);
5613 obscuringWindow->setFlags(WindowInfo::Flag::NOT_TOUCHABLE);
5614 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5615 sp<FakeWindowHandle> window =
5616 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
5617 window->setInputFeatures(WindowInfo::Feature::DROP_INPUT_IF_OBSCURED);
5618 window->setOwnerInfo(222, 222);
5619 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5620 window->setFocusable(true);
5621 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
5622 setFocusedWindow(window);
5623 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
5624
5625 // With the flag set, window should not get any input
5626 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
5627 mDispatcher->notifyKey(&keyArgs);
5628 window->assertNoEvents();
5629
5630 NotifyMotionArgs motionArgs =
5631 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5632 ADISPLAY_ID_DEFAULT);
5633 mDispatcher->notifyMotion(&motionArgs);
5634 window->assertNoEvents();
5635
5636 // With the flag cleared, the window should get input
5637 window->setInputFeatures({});
5638 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
5639
5640 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
5641 mDispatcher->notifyKey(&keyArgs);
5642 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
5643
5644 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5645 ADISPLAY_ID_DEFAULT);
5646 mDispatcher->notifyMotion(&motionArgs);
5647 window->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
5648 window->assertNoEvents();
5649}
5650
5651TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) {
5652 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
5653 std::make_shared<FakeApplicationHandle>();
5654 sp<FakeWindowHandle> obscuringWindow =
5655 new FakeWindowHandle(obscuringApplication, mDispatcher, "obscuringWindow",
5656 ADISPLAY_ID_DEFAULT);
5657 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
5658 obscuringWindow->setOwnerInfo(111, 111);
5659 obscuringWindow->setFlags(WindowInfo::Flag::NOT_TOUCHABLE);
5660 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5661 sp<FakeWindowHandle> window =
5662 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
5663 window->setInputFeatures(WindowInfo::Feature::DROP_INPUT_IF_OBSCURED);
5664 window->setOwnerInfo(222, 222);
5665 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5666 window->setFocusable(true);
5667 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
5668 setFocusedWindow(window);
5669 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
5670
5671 // With the flag set, window should not get any input
5672 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
5673 mDispatcher->notifyKey(&keyArgs);
5674 window->assertNoEvents();
5675
5676 NotifyMotionArgs motionArgs =
5677 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5678 ADISPLAY_ID_DEFAULT);
5679 mDispatcher->notifyMotion(&motionArgs);
5680 window->assertNoEvents();
5681
5682 // When the window is no longer obscured because it went on top, it should get input
5683 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, obscuringWindow}}});
5684
5685 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
5686 mDispatcher->notifyKey(&keyArgs);
5687 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
5688
5689 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5690 ADISPLAY_ID_DEFAULT);
5691 mDispatcher->notifyMotion(&motionArgs);
5692 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5693 window->assertNoEvents();
5694}
5695
Garfield Tane84e6f92019-08-29 17:28:41 -07005696} // namespace android::inputdispatcher